<template>
    <div v-loading="loading" element-loading-text="拼命加载中" element-loading-spinner="el-icon-loading">
        <div class="mcr-box">
            <div class="mcr-head">
                <!-- 预计出团量 -->
                <!-- <div style="display: flex;margin-bottom: 10px;">
                    <div style="width:24%">
                        预计出团:<span>{{ preDele }}</span>
                    </div>
                    <div style="width:24%">
                        预计出团总量:<span>{{ groupNumber.preDeleAll }}</span>
                    </div>
                    <div style="width:24%">
                        已出团:<span>{{ finlishedDele }}</span>
                    </div>
                    <div style="width:24%">
                        已出团总量:<span>{{ groupNumber.finlishedDeleAll }}</span>
                    </div>
                </div> -->


                <div>
                    <el-select v-model="principalSel" :disabled='isSelect' clearable placeholder="负责人" style="width
                    :18%">
                        <el-option v-for="item in principalList" :key="item.id" :label="item.name" :value="item.id">
                        </el-option>
                    </el-select>
                    <el-select v-model="territorySel" clearable placeholder="省 域" style="width
                    :18%">
                        <el-option v-for="item in territoryList" :key="item.id" :label="item.name" :value="item.id">
                        </el-option>
                    </el-select>
                    <el-select v-model="clientLvSel" clearable placeholder="客户级别" style="width
                    :18%">
                        <el-option v-for="item in clientLvList" :key="item.id" :label="item.name" :value="item.id">
                        </el-option>
                    </el-select>
                    <el-select v-model="clientTypeSel" clearable placeholder="客户类别" style="width
                    :18%">
                        <el-option v-for="item in clientTypeList" :key="item.id" :label="item.name" :value="item.id">
                        </el-option>
                    </el-select>
                    <el-select v-model="businessClassSel" clearable placeholder="业务分类" style="width
                    :18%">
                        <el-option v-for="item in businessClassList" :key="item.id" :label="item.name" :value="item.id">
                        </el-option>
                    </el-select>

                </div>
                <!-- textbox框 -->
                <div style="display: flex;margin-top: 10px;position: relative;">
                    <div style="width: 31%;">
                        <el-input v-model="inputLXR" placeholder="联系人"></el-input>
                    </div>
                    &nbsp;
                    <div style="width: 30%;">
                        <el-input v-model="inputDQ" placeholder="地区"></el-input>
                    </div>
                    &nbsp;
                    <div style="width: 29.59%;">
                        <el-input v-model="inputDW" placeholder="单位"></el-input>
                    </div>
                    &nbsp;
                    <div class="btnOp">
                        <div>
                            <el-button type="primary" @click="QueryData">查 询</el-button>
                        </div>
                        <div style="height: 10px;">
                        </div>
                        <div>
                            <el-button type="primary" @click="Add">新 增</el-button>
                        </div>
                    </div>
                </div>

            </div>
            <div class="mcr-table">
                <el-table :data="PageList" border @select="handleSelect" style="width: 100%">
                    <el-table-column prop="weight" label="权重" width="50">
                        <template slot-scope="scope">
                            <span v-if="scope.row.weight == '393'">A</span>
                            <span v-else-if="scope.row.weight == '392'">B</span>
                            <span v-else-if="scope.row.weight == '391'">C</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="client" label="单位" width="230">
                    </el-table-column>
                    <el-table-column prop="contact" label="联系人" width="90">
                    </el-table-column>
                    <el-table-column prop="job" label="职位" width="110">
                    </el-table-column>
                    <el-table-column prop="telephone" label="手机号" width="120">
                    </el-table-column>
                    <el-table-column prop="phone" label="座机号" width="120">
                    </el-table-column>
                    <el-table-column prop="location" label="所在区域" width="130">
                    </el-table-column>
                    <!-- <el-table-column prop="birthday" label="生日">
                    </el-table-column> -->

                    <el-table-column prop="ascribedDepartment" label="业务归属" width="80">
                        <template slot-scope="scope">
                            <el-tooltip class="item" effect="dark" placement="top">
                                <span style="color:royalblue">查看</span>
                                <div slot="content">
                                    <div v-for="(yw, index) in scope.row.ascribedDepartment" :key="index"
                                        style="text-align: center; width: 100px;padding: 5px;">
                                        {{ yw.name }}
                                    </div>
                                </div>
                            </el-tooltip>
                        </template>
                    </el-table-column>
                    <el-table-column prop="ascribedUser" label="负责人" width="85">
                        <template slot-scope="scope">
                            <!-- <el-tooltip class="item" effect="dark" placement="top">
                                <span style="color:royalblue">查看</span>
                                <div slot="content">
                                    <div v-for="yw in scope.row.ascribedUser"
                                        style="text-align: center; width: 100px;padding: 5px;">
                                        {{ yw.cnName }}
                                    </div>
                                </div>
                            </el-tooltip> -->
                            <div v-for="(yw, index) in scope.row.ascribedUser" :key="index" style="text-align: center;">
                                {{ yw.cnName }}
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="remark" label="备注">
                        <template slot-scope="scope">
                            <div id="remark">
                                <el-tooltip v-show="scope.row.remark != ''" class="item" effect="dark"
                                    :content="scope.row.remark" placement="top">
                                    <span v-if="scope.row.remark.length > 10">{{ scope.row.remark.slice(0, 10) + "..."
                                        }}</span>
                                    <span v-else>{{ scope.row.remark }}</span>
                                    <div slot="content">
                                        <div style="width: 300px;">
                                            {{ scope.row.remark }}
                                        </div>
                                    </div>
                                </el-tooltip>
                            </div>
                        </template>
                    </el-table-column>
                    <!-- <el-table-column prop="preDele" label="预计出团">
                    </el-table-column>
                    <el-table-column prop="finlishedDele" label="已出团">
                    </el-table-column> -->
                    <el-table-column label="操作" width="200">
                        <template slot-scope="scope">
                            <el-button type="primary" size="mini" @click="Edit(scope.row)">编
                                辑</el-button>

                            <el-button type="danger" size="mini" @click="Del(scope.row)">删
                                除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <div class="block">
                    <el-pagination align='center' @size-change="handleSizeChange" @current-change="handleCurrentChange"
                        :current-page.sync="currentPage" :page-sizes="[10, 20]" :page-size="pageSize"
                        layout="total, sizes, prev, pager, next" :total="total">
                    </el-pagination>
                </div>
            </div>
        </div>
    </div>
</template>

<script>


export default {
    data() {
        return {
            total: 0, //总行数
            principalSel: '',
            principalList: [],//负责人数据
            territorySel: '',
            territoryList: [],//省域数据
            clientLvSel: '',
            clientLvList: [],//客户级别数据
            clientTypeSel: '',
            clientTypeList: [],//客户类别数据]
            businessClassSel: '',
            businessClassList: [],//业务分类
            PageList: [],    //页面数据
            currentPage: 1, // 当前页码
            pageSize: 10,// 每页的数据条数
            loading: true,
            inputLXR: '', //联系人
            inputDQ: '', //地区
            inputDW: '', //单位
            pageloadData: {
                "portType": 1,
                "pageIndex": 1,
                "pageSize": 10,
                "contact": "",
                "location": "",
                "client": "",
                "userid": "",
                "lvlid": 0,
                "business": "",
                "Range": 0,
                "Category": 0,
                "operationUserId": 0
            },
            groupNumber: {
                preDeleAll: '',
                finlishedDeleAll: ''
            },
            userId: 0,
            token: '',
            isSelect: true
        }
    },
    methods: {
        //每页条数改变时触发 选择一页显示多少行
        handleSizeChange(val) {
            this.currentPage = 1;
            this.pageSize = val;
            this.pageloadData.pageIndex = this.currentPage;
            this.pageloadData.pageSize = this.pageSize;
            this.pageload();
        },
        //当前页改变时触发 跳转其他页
        handleCurrentChange(val) {
            this.loading = true;
            this.pageloadData.pageIndex = val;
            this.pageloadData.pageSize = this.pageSize;
            this.pageload();
        },
        //多选框选中方法
        handleSelect(selection, row) {

        },
        pageload() {
            var that = this;

            if (this.userId == 21) {
                this.isSelect = false;
            }

            that.pageloadData.operationUserId = that.userId
            //初始化界面数据
            console.log(that.pageloadData)
            this.$axios.post('/api/MarketCustomerResources/QueryNewClientData', that.pageloadData, {
                headers: {
                    'Authorization': that.token,
                },
            }).then(resp => {
                if (resp.data.code == 200) {
                    if (resp.data.data == null) {
                        that.PageList = [];
                        that.total = 0;
                        this.$message.error(resp.data.msg);
                    } else {
                        that.principalList = resp.data.data.users; //负责人
                        that.clientLvList = resp.data.data.level;//客户级别
                        that.clientTypeList = resp.data.data.customerClass;//客户类别
                        that.territoryList = resp.data.data.province; //省域数据
                        that.businessClassList = resp.data.data.serviceClass; //业务归属
                        that.PageList = resp.data.data.clientTableData.pageSource; //table数据
                        that.total = resp.data.data.clientTableData.pageCount; //总数
                        that.groupNumber = resp.data.data.groupNumber; //团组出团总数
                        console.log(that.pageloadData.pageIndex, parseInt(that.pageloadData.pageIndex), 'that.pageloadData.pageIndex')
                        that.currentPage = parseInt(that.pageloadData.pageIndex);
                    }
                    that.loading = false;

                } else if (resp.data.code == 400) {
                    that.PageList = [];
                    that.total = 0;
                    that.loading = false;
                    that.$message.error('暂无数据!');
                }

            }).catch(err => {
                this.$message.error('网络异常！');
                this.loading = false;
            })
        },
        //保存权限
        saveData() {
            var that = this;
        },
        QueryData() {
            this.pageloadData.pageIndex = this.currentPage;
            this.pageloadData.pageSize = this.pageSize;
            this.pageloadData.location = this.inputDQ; //地区
            this.pageloadData.client = this.inputDW;  //单位
            this.pageloadData.contact = this.inputLXR; //联系人
            this.pageloadData.userid = this.principalSel.toString(); //负责人
            this.pageloadData.Range = this.territorySel == '' ? 0 : this.territorySel;//省域
            this.pageloadData.lvlid = this.clientLvSel == '' ? 0 : this.clientLvSel; //客户级别
            this.pageloadData.Category = this.clientTypeSel == '' ? 0 : this.clientTypeSel;//客户类别
            this.pageloadData.business = this.businessClassSel.toString();//业务分类
            this.pageloadData.operationUserId = this.userId;//
            this.pageload();
        },
        Edit(row) {
            localStorage.setItem("mcrPageData", JSON.stringify({
                inputLXR: this.inputLXR,
                inputDW: this.inputDW,
                inputDQ: this.inputDQ,
                principalSel: this.principalSel,
                territorySel: this.territorySel,
                clientLvSel: this.clientLvSel,
                clientTypeSel: this.clientTypeSel,
                businessClassSel: this.businessClassSel,
                pageSize: this.pageSize,
                currentPage: this.currentPage,
            }));
            this.$router.push('/home/MCROper?id=' + row.id);
        },
        Del(row) {
            var that = this;
            var delData = {
                "portType": 1,
                "id": row.id,
                "deleteUserId": this.userId
            }
            console.log(delData);

            this.$axios.post('/api/MarketCustomerResources/NewClientDel', delData, {
                headers: {
                    'Authorization': that.token,
                }
            }).then(resp => {
                if (resp.data.code == 200) {
                    that.$message({
                        message: '删除成功！',
                        type: 'success'
                    });
                } else {
                    that.$message.error(resp.data.msg);
                }
                that.pageload();
            });
        },
        Add(row) {
            localStorage.setItem("mcrPageData", JSON.stringify({
                inputLXR: this.inputLXR,
                inputDW: this.inputDW,
                inputDQ: this.inputDQ,
                principalSel: this.principalSel,
                territorySel: this.territorySel,
                clientLvSel: this.clientLvSel,
                clientTypeSel: this.clientTypeSel,
                businessClassSel: this.businessClassSel,
                pageSize: this.pageSize,
                currentPage: this.currentPage,
            }));
            this.$router.push('/home/MCROper?id=' + 0);
        }
    },
    mounted() {
        this.token = JSON.parse(localStorage.getItem('userinif')).token;
        this.userId = JSON.parse(localStorage.getItem('userinif')).userInfo.userId
        var mcrPageData = localStorage.getItem("mcrPageData");
        if (mcrPageData) {
            try {
                console.log(mcrPageData);
                var json = JSON.parse(mcrPageData);
                this.inputLXR = json.inputLXR;
                this.inputDW = json.inputDW;
                this.inputDQ = json.inputDQ;
                this.principalSel = json.principalSel;
                this.territorySel = json.territorySel;
                this.clientLvSel = json.clientLvSel;
                this.clientTypeSel = json.clientTypeSel;
                this.businessClassSel = json.businessClassSel;
                this.pageSize = json.pageSize;
                this.currentPage = json.currentPage;
                localStorage.removeItem("mcrPageData");
                this.QueryData();
            } catch (e) {
                this.pageload();
            }
            return;
        }
        this.pageload();
    },
    created() {
        var mcrPageData = localStorage.getItem("mcrPageData");
        if (mcrPageData) {
            var json = JSON.parse(mcrPageData);
            this.currentPage = json.currentPage;
        }
    },
    computed: {
        preDele() {
            var number = 0;
            this.PageList.forEach(element => {
                number += element.preDele;
            });
            return number;
        },
        finlishedDele() {
            var number = 0;
            this.PageList.forEach(element => {
                number += element.finlishedDele;
            });
            return number;
        }
    }
}
</script>
<style>
body {
    margin: 0;
    padding: 0;
}


.mcr-box {
    background-color: #fff;
    padding: 10px;
    border-radius: 10px;
}

.mcr-table {
    margin-top: 10px;
}

.mcr-table .block {
    margin-top: 10px;
}

.mcr-table .el-table--enable-row-transition .el-table__body td.el-table__cell {
    text-align: center;
}

.mcr-table .el-table--border .el-table__cell:first-child .cell {
    text-align: center;
}

.mcr-input {
    display: flex;
}

.mcr-input .el-select {
    margin-right: 5px;
}

.btnOp {
    position: absolute;
    right: 6px;
    top: -50px;
}
</style>