<template>
    <fullscreen :fullscreen.sync="fullscreen" class="container">
        <div class="container-box">
            <toolList @iconCli='iconCli' :tooltit='tooltit'></toolList>
            <div class="search">
                <mySearch :searchList="searchList" @searchInfo="searchInfo" style="width: 60%"></mySearch>
                <span>
                    <!-- <el-button type="primary" @click="dialogCheck(4)" size="medium">导入</el-button> -->
                    <el-button type="primary" @click="downloadExcel(1)">导出</el-button>
                    <el-button type="primary" @click="downloadExcel(2)">下载模板</el-button>
                    <myUpload style="display:inline-block;" :fileInfo="fileInfo"
                        :fileList="fileInfo.fileList" @uploadBack='uploadBack'></myUpload>
                    <el-button type="primary" @click="dialogCheck(3)" size="medium">添加</el-button>
                    <!-- <el-button type="primary" @click="dialogCheck(3)" size="medium">导入</el-button> -->
                    <!-- <el-button type="primary" @click="dialogCheck(3)" size="medium">导出</el-button> -->
                </span>
            </div>
            <div class="tabbox">
                <el-table height="calc(100% - 40px)" class="com-table" ref="multipleTable" :data="tableData"
                    tooltip-effect="dark" size="small" border style="width: 100%" v-loading="loading">
                    <el-table-column prop="oaNo" label="员工工号">
                    </el-table-column>
                    <el-table-column prop="cityCompName" label="省/地市">
                    </el-table-column>
                    <el-table-column prop="areaCompName" label="区县">
                    </el-table-column>
                    <el-table-column prop="roleName" label="岗位角色">
                    </el-table-column>
                    <el-table-column prop="userName" label="姓名">
                    </el-table-column>
                    <el-table-column prop="phoneNo" label="手机号">
                    </el-table-column>
                    <el-table-column prop="roleDuty" label="岗位职责">
                    </el-table-column>
                    <el-table-column label="操作" width="220px" align="center">
                        <template slot-scope="scope">
                            <el-button size="mini" type="primary" @click="dialogCheck(1,scope.row)">查看</el-button>
                            <el-button v-if="scope.row.sts == 1" size="mini" type="primary" @click="dialogCheck(2,scope.row)">修改</el-button>
                            <el-button v-if="scope.row.sts == 1" size="mini" type="danger" @click="delLine(scope.row)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <el-pagination class="pageBox" @current-change="currchange" layout="prev, pager, next" background
                    :total="total">
                </el-pagination>
            </div>
        </div>

        <el-dialog :title="titname" :visible.sync="dialogStatus" width="50%" :destroy-on-close="true"
            :modal-append-to-body="false" :close-on-click-modal="false" :before-close="closedia">
            <div v-loading="loadinged">
                <el-form :model="infolist" ref="infolist" :rules="rules"
                    style="height: 40vh;overflow-y: scroll;">
                    <div class="info-line">
                        <el-form-item>
                            <span>地市</span>
                            <el-select @change="getareatype" :popper-append-to-body="false" v-model="infolist.cityCompName" placeholder="地市" :disabled="disableStatus">
                                <el-option v-for="item in typeopt" :key="item.id" :label="item.roleName"
                                    :value="item.roleName">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item>
                            <span>区县</span>
                            <el-select  @change="gettype" :popper-append-to-body="false" v-model="infolist.areaCompName" placeholder="区县" :disabled="disableStatus">
                                <el-option v-for="item in kindopt" :key="item.id" :label="item.roleName"
                                    :value="item.roleName">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item prop="roleName">
                            <span>角色</span>
                            <el-select :popper-append-to-body="false" v-model="infolist.roleName" placeholder="角色" :disabled="disableStatus">
                                <el-option v-for="item in uplevelopt" :key="item.id" :label="item.allRoleName"
                                    :value="item.allRoleName">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item prop="userName">
                            <span>姓名</span>
                            <el-input v-model="infolist.userName" placeholder="姓名"
                                :disabled="disableStatus">
                            </el-input>
                        </el-form-item>
                        <el-form-item>
                            <span>工号</span>
                            <el-input v-model="infolist.oaNo" placeholder="工号"
                                :disabled="disableStatus">
                            </el-input>
                        </el-form-item>
                        <el-form-item prop="phoneNo">
                            <span>手机号</span>
                            <el-input v-model="infolist.phoneNo" placeholder="手机号"
                                :disabled="disableStatus">
                            </el-input>
                        </el-form-item>
                    </div>
                    <el-form-item class="info-line online" prop="roleDuty">
                        <span>岗位职责</span>
                        <el-input v-model="infolist.roleDuty" placeholder="岗位职责"
                            :disabled="disableStatus" type="textarea" :rows="4">
                        </el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" style="text-align: right;padding: 20px;">
                    <el-button type="primary" @click="dialogCliadd(2)">确 定</el-button>
                    <el-button @click="dialogCliadd(1)">取 消</el-button>
                </div>
            </div>
        </el-dialog>
        <myMessage :messTit='messTit' @closeMessage="closeMessage" :centerDialogVisible="centerDialogVisible"
            v-if="centerDialogVisible"></myMessage>
    </fullscreen>

</template>
<script>
    import mySearch from "../../../components/search.vue";
    import myMessage from "../../../components/myMessage.vue"
    import toolList from '../../../components/toolList'
    import myUpload from '../../../components/upload'
    import uploadDown from '../../../components/uploadDown.vue'
    import deptTreeOnly from "../../../components/deptTreeOnly.vue"

    export default {
        components: {
            mySearch,
            myMessage,
            toolList,
            myUpload,
            uploadDown,
            deptTreeOnly
        },
        data() {
            const phoneNo = (rule, value, callback) => {
                if (!this.infolist.phoneNo) {
                    callback(new Error('不能为空'))
                } else {
                    callback()
                }
            }
             const userName = (rule, value, callback) => {
                if (!this.infolist.userName) {
                    callback(new Error('不能为空'))
                } else {
                    callback()
                }
            }
            const roleName = (rule, value, callback) => {
                if (!this.infolist.roleName) {
                    callback(new Error('不能为空'))
                } else {
                    callback()
                }
            }
             const roleDuty = (rule, value, callback) => {
                if (!this.infolist.roleDuty) {
                    callback(new Error('不能为空'))
                } else {
                    callback()
                }
            }
            return {
                rules: {
                    phoneNo: [{
                        required: true,
                        trigger: 'blur',
                        validator: phoneNo
                    }],
                    userName: [{
                        required: true,
                        trigger: 'blur',
                        validator: userName
                    }],
                    roleName: [{
                        required: true,
                        trigger: 'change',
                        validator: roleName
                    }],
                    roleDuty: [{
                        required: true,
                        trigger: 'blur',
                        validator: roleDuty
                    }],
                },
                searchList: [{
                        type: 'input',
                        tit: '地市',
                        value: '',
                        width: '24%'
                    },
                    {
                        type: 'input',
                        tit: '区县',
                        value: '',
                        width: '24%',
                    },
                    {
                        type: 'input',
                        tit: '角色',
                        value: '',
                        width: '24%',
                    },
                    {
                        type: 'input',
                        tit: '姓名',
                        value: '',
                        width: '24%',
                    },
                ],
                tooltit: '人员管理',
                fullscreen: false,
                total: 0,
                pageSize: 1,
                tableData: [{}],
                dialogStatus: false,
                disableStatus: false,
                titname: '',
                infolist: {
                    roleName:''
                },
                userInfo: {},
                params: {},
                centerDialogVisible: false,
                messTit: '',
                delid: '',
                loading: false,
                loadinged: false,
                docType:[],
                contentType:[],
                uplevelopt:[],
                kindopt:[],
                typeopt:[],

                // 导入
                fileInfo: {
                    type: 'btn',
                    typename: '导入',
                    btntype: 'primary',
                    // limit: 1,
                    url: '/market/cMkOrgUser/orgUserImportNew',
                    fileList: [],
                    typexz:"xlsx"
                },
            }
        },
        methods: {
            //搜索数据
            searchInfo(v) {
                this.params = {};
                v[0] ? this.params.cityCompName = v[0] : '';
                v[1] ? this.params.areaCompName = v[1] : '';
                v[2] ? this.params.roleName = v[2] : '';
                v[3] ? this.params.userName = v[3] : '';
                this.getList(this.params, this.pageSize);
            },
            closedia() {
                this.infolist = {
                    roleName:''
                };
                this.dialogStatus = false;
            },
            //获取列表
            getList(v, n) {
                this.pageSize = n;
                let _this = this;
                this.loading = true;
                this.$http({
                    url: "/market/cMkOrgUser/queryOrgUserPage",
                    method: "post",
                    headers: {
                        "Content-Type": "application/json",
                        "page": '{"pageNo":"' + n + '","pageSize":"10"}'
                    },
                    data: v,
                }).then((res) => {
                    this.tableData = res.data.data;
                    this.total = res.data.totalRecord;
                    this.loading = false;
                });
            },
            // 分页
            currchange(v) {
                this.pageSize = v;
                this.getList(this.params, this.pageSize);
            },
            
            //申请
            dialogCheck(v, n) {
                this.dialogStatus = true;
                let infolist = Object.assign({}, n);
                if (v === 1) {
                    this.titname = '查看';
                    this.disableStatus = true;
                } else if (v === 2) {
                    this.titname = '修改';
                    this.disableStatus = false;
                } else if (v === 3) {
                    this.titname = '添加';
                    this.disableStatus = false;
                    return
                }
                this.infolist = infolist;
                this.getareatype(this.infolist.cityCompName,1);
            },
            //添加
            dialogCliadd(v) {
                if (v == 1 || this.titname == '查看') {
                    this.infolist = {
                        roleName:''
                    };
                    this.dialogStatus = false;
                    return
                }
                let _this = this;
                let info = this.infolist;
                info.opTime = _this.$formatDate(new Date(), "YYYY-MM-DD");
                info.opNo = this.userInfo.loginNo;
                info.opName = this.userInfo.loginName;

                for (let i = 0; i < this.typeopt.length; i++) {
                    if(this.infolist.cityCompName == this.typeopt[i].roleName){
                        console.log(this.typeopt[i])
                        this.infolist.cityCompId = this.typeopt[i].id
                    }
                }
                for (let i = 0; i < this.kindopt.length; i++) {
                    if(this.infolist.areaCompName == this.kindopt[i].roleName){
                        this.infolist.areaCompId = this.kindopt[i].id
                    }
                }
                for (let i = 0; i < this.uplevelopt.length; i++) {
                    if(this.infolist.roleName == this.uplevelopt[i].allRoleName){
                        this.infolist.roleId = this.uplevelopt[i].id
                    }
                }

                if (this.titname == '修改') {
                    this.submitInfo("/market/cMkOrgUser/update", info);
                } else {
                    this.submitInfo("/market/cMkOrgUser/add", info);
                }
            },

            submitInfo(u, v) {
                let _this = this;
                this.$refs.infolist.validate(valid => {
                    if (valid) {
                        if(this.infolist.phoneNo.length!=11){
                            this.$message({
                                message: '请输入正确的手机号！',
                                type: 'error'
                            });
                            return
                        }
                        this.$http({
                            url: u,
                            method: "post",
                            headers: {
                                "Content-Type": "application/json",
                            },
                            data: v,
                        }).then((res) => {
                            if (res.data.result === 1) {
                                _this.$message({
                                    message: res.data.desc,
                                    type: 'error'
                                });
                            } else {
                                _this.$message({
                                    message: '成功',
                                    type: 'success'
                                });
                                _this.closedia();
                                _this.infolist = {
                                    roleName:''
                                };
                                _this.getList(this.params, this.pageSize);
                            }

                        });
                    }
                })
            },
            // 导入
            uploadBack(v) {
                console.log(v);
            },
            downloadExcel(v){
               // v:1  导出数据   v:2  导出模板
                if(v === 1){
                    this.$http({
                        url: "/market/cMkOrgUser/excelExport",
                        method: "post",
                        headers: {
                            "Content-Type": "application/json"
                        },
                        responseType: "blob",
                        data: { }
                    }).then(res => {
                        if (window.navigator && window.navigator.msSaveOrOpenBlob) {
                            let blob = new Blob([res.data], {
                                type: "application/vnd.ms-excel"
                            });
                            window.navigator.msSaveOrOpenBlob(
                                blob,
                                // this.wo.bizName + ".xlsx"
                                "导出数据" + ".xlsx"
                            );
                        } else {
                            /* 火狐谷歌的文件下载方式 */
                            var blob = new Blob([res.data]);
                            var downloadElement = document.createElement("a");
                            var href = window.URL.createObjectURL(blob);
                            downloadElement.href = href;
                            downloadElement.download = "导出数据" + ".xlsx";
                            document.body.appendChild(downloadElement);
                            downloadElement.click();
                            document.body.removeChild(downloadElement);
                            window.URL.revokeObjectURL(href);

                        }
                    });
                }else if(v === 2){
                    this.$http({
                        url: "/market/cMkOrgUser/excelExportTemp",
                        method: "post",
                        headers: {
                            "Content-Type": "application/json"
                        },
                        responseType: "blob",
                        data: {}
                    }).then(res => {
                        if (window.navigator && window.navigator.msSaveOrOpenBlob) {
                            let blob = new Blob([res.data], {
                                type: "application/vnd.ms-excel"
                            });
                            window.navigator.msSaveOrOpenBlob(
                                blob,
                                // this.wo.bizName + ".xlsx"
                                "导出模板" + ".xlsx"
                            );
                        } else {
                            /* 火狐谷歌的文件下载方式 */
                            var blob = new Blob([res.data]);
                            var downloadElement = document.createElement("a");
                            var href = window.URL.createObjectURL(blob);
                            downloadElement.href = href;
                            downloadElement.download = "导出模板" + ".xlsx";
                            document.body.appendChild(downloadElement);
                            downloadElement.click();
                            document.body.removeChild(downloadElement);
                            window.URL.revokeObjectURL(href);

                        }
                    });
                }
                
            },
            closeMessage(v) {
                this.centerDialogVisible = false;
                let _this = this;
                if (v === 1) {
                    _this.$http({
                        url: "/market/cMkOrgUser/del",
                        method: "post",
                        headers: {
                            "Content-Type": "application/json",
                        },
                        data: {
                            id: this.delid
                        },
                    }).then((res) => {
                        if (res.data.result === 1) {
                            _this.$message({
                                message: res.data.desc,
                                type: 'error'
                            });
                        } else {
                            _this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            _this.getList(this.params, this.pageSize);
                        }
                    });
                }
            },
            //删除
            delLine(v) {
                this.centerDialogVisible = true;
                this.messTit = '即将删除此条数据, 是否删除?';
                this.delid = v.id;
            },
            //功能栏
            iconCli(v) {
                if (v === 1) {
                    this.getList(this.params, this.pageSize);
                }
                if (v === 2) {
                    this.fullscreen = !this.fullscreen
                }
            },
            getUser() {
                this.userInfo = JSON.parse(window.sessionStorage.userInfo);
                // this.$http({
                //     url: "/market/cfgDataDict/queryList",
                //     method: "post",
                //     headers: {
                //         "Content-Type": "application/json",
                //     },
                //     data: {
                //         dictCode: 'docType'
                //     },
                // }).then((res) => {
                //     this.docType = res.data;
                // });
                this.$http({
                    url: "/market/cMkOrgRole/queryList",
                    method: "post",
                    headers: {
                        "Content-Type": "application/json",
                    },
                    data: {roleTypePks: "省公司,地市公司",roleLevel: "分公司"},
                }).then((res) => {
                    this.typeopt = res.data;
                });
            },
            getareatype(v,n){
                let parentId = '';
                for (let i = 0; i < this.typeopt.length; i++) {
                    if(this.typeopt[i].roleName == v){
                        parentId = this.typeopt[i].id
                    }
                }
                this.$http({
                    url: "/market/cMkOrgRole/queryList",
                    method: "post",
                    headers: {
                        "Content-Type": "application/json",
                    },
                    data: {roleType: "区县公司",roleLevel: "分公司",parentId:parentId},
                }).then((res) => {
                    this.kindopt = res.data;
                    if(n != 1){
                        this.infolist.areaCompName = ''
                        this.infolist.roleName = ''
                    }
                });
                this.gettype(parentId,1);
            },
            gettype(v,n){
                let roleType = '';
                if(n){
                    roleType = '地市公司'
                }else{
                    roleType = '区县公司';
                }
                if(v == '00440051000000000000'){
                    roleType = '省公司';
                }
                console.log(this.infolist)
                if(this.infolist.areaCompName){
                    roleType = '区县公司'
                }
                this.$http({
                    url: "/market/cMkOrgRole/queryOrgRoleListByComp",
                    method: "post",
                    headers: {
                        "Content-Type": "application/json",
                    },
                    data: {roleType:roleType},
                }).then((res) => {
                    this.uplevelopt = res.data;
                    if(n != 1){
                        this.infolist.roleName = ''
                    }
                });
            }
        },
        mounted() {
            this.getList({}, 1);
            this.getUser();
        },
        created() {

        }
    }
</script>
<style scoped lang="scss">
    .onetab {
        margin-bottom: 20px;
        padding: 0 20px;
    }

    .titbox {
        div {
            float: right;

            i {
                font-size: 22px;
                margin-left: 20px;
                cursor: pointer;
            }
        }
    }

    .tabbox {
        margin-top: 15px;
    }

    .pageBox {
        text-align: right;
        margin-top: 10px;
    }

    .info-line {
        width: 100%;
        display: block;
        padding-left: 20px;

        div {
            width: 50%;
            display: inline-block;
        }

        span {
            width: 80px;
            display: inline-block;
            text-align: left;

            i {
                color: red;
                display: inline-block;
                padding-right: 5px;
            }
        }

        .el-select,
        .el-input {
            width: calc(100% - 100px);
        }
    }

    .online {
        width: 100%;

        .el-select {
            width: calc(100% - 100px);
        }

        span {
            vertical-align: top;
        }

        .el-textarea {
            width: calc(100% - 100px);
        }

        .tree {
            width: calc(50% - 60px);
            display: inline-block;
            margin-right: 20px;
            height: 300px;
            overflow-y: scroll;

            .el-icon-error {
                float: right;
                font-size: 20px;
                margin-top: 9px;
                cursor: pointer;
            }
        }

        .treeUser {
            margin: 0;
            border: 1px solid #ddd;

            p {
                background: #f4f4f4;
                padding: 0 20px;
                margin-bottom: 5px;
            }
        }

        .treeUserb {
            width: calc(100% - 100px);
            border: 1px solid #ddd;
            background: #f4f4f4;
            border-radius: 3px;
            height: auto;
            overflow: hidden;

            p {
                display: inline-block;
                padding: 0 20px;
                margin-bottom: 5px;
            }
        }
    }

    .adv-type {
        margin-top: 20px;
        display: flex;
        justify-content: space-between;
        flex-wrap: wrap;
        // border: 1px solid #ddd;
        border-radius: 5px;
        padding: 20px;
        box-shadow: 0 2px 12px 0 rgba(0, 0, 0, .1);

        div {
            width: 33%;
            text-align: center;
            height: 80px;
            overflow: hidden;
            min-width: 100px;
            cursor: pointer;
            padding-top: 10px;
            margin: 10px 0;
        }

        div:hover {
            background: #CFE8FC;
            border-radius: 5px;
        }

        span {
            width: 100%;
            display: inline-block;
            height: 40px;

            // line-height: 40px;
            i {
                color: #0074D9;
                font-size: 36px;
            }
        }
    }
</style>
