<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"></mySearch>
            </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="woName" label="名称">
                    </el-table-column>
                    <el-table-column prop="supplierCode" label="统计周期">
                        <template slot-scope="scope">
                            <span>{{ scope.row.startTime }} - {{ scope.row.endTime }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="contractName" label="填报范围">
                        <template slot-scope="scope">
                            <span @click="jumpfw(scope.row)" style="color: #007AFF;cursor: pointer;">{{
                                scope.row.fillName
                            }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="frameSum" label="分发范围">
                        <template slot-scope="scope">
                            <span @click="jumpjd(scope.row)" style="color: #007AFF;cursor:pointer;">{{
                                scope.row.groupName
                            }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="dutyName" label="分发负责人">
                    </el-table-column>
                    <el-table-column prop="initiatorName" label="发起人">
                    </el-table-column>
                    <el-table-column label="操作" width="160px" align="center">
                        <template slot-scope="scope">
                            <el-button size="mini" type="primary" @click="dialogCheck(1, scope.row)"
                                v-if="scope.row.stsDesc == '终止'">查看</el-button>
                            <el-button size="mini" type="primary" @click="dialogCheck(2, scope.row)"
                                v-if="scope.row.stsDesc != '终止'">设置</el-button>
                            <el-button size="mini" type="danger" @click="delLine(scope.row)"
                                v-if="companyFlag == 0 && scope.row.stsDesc != '终止'">中止</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" :fullscreen="true">
            <el-form :model="infolist" ref="infolist" :rules="rules"
                style="height: calc(100vh - 180px);overflow-y: scroll;">
                <el-form-item prop="woName" class="info-line online">
                    <span>统计名称</span>
                    <el-input v-model="infolist.woName" placeholder="统计名称" :disabled="disableStatus"></el-input>
                </el-form-item>

                <el-form-item prop="time" class="info-line online">
                    <span>统计周期</span>
                    <el-date-picker v-model="infolist.time" type="daterange" :disabled="disableStatus"></el-date-picker>
                </el-form-item>

                <el-form-item prop="woName" class="info-line online" v-if="disableStatus">
                    <span>填报人</span>
                    <el-input v-model="infolist.fillName" type="textarea" :autosize="{ minRows: 1, maxRows: 10 }"
                        disabled></el-input>
                </el-form-item>
                <el-form-item prop="woName" class="info-line online" v-if="disableStatus">
                    <span>负责人</span>
                    <el-input v-model="infolist.dutyName" type="textarea" :autosize="{ minRows: 1, maxRows: 10 }"
                        disabled></el-input>
                </el-form-item>

                <el-form-item class="info-line online" v-if="!disableStatus">
                    <span>填报人</span>

                    <deptTree style="width: 30%;margin-right: 1%;" :closeList="closeList" class="tree" @treeCheck="treeCheck" :defaultList="defaultList"
                        :fillLists="fillLists">
                    </deptTree>
                    <!-- <div class="tree treeUser" style="width: 30%;margin-right: 1%;">
                        <p v-for="(item,index) in treeList" :key="index">{{item.receiveName}}
                            <i @click="deletes(item,index)" class="el-icon-error"></i>
                        </p>
                    </div> -->
                    <div class="tree treeUser" style="width: 30%;margin-right: 1%;">
                        <p style="text-align: center; font-size: 18px;margin:5px">已选填报人</p>
                        <p v-for="(item, index) in treeListOld" :key="index" class="p">
                            {{ item.receiveName }}
                            <i @click="deletess(item, index)" class="el-icon-error"></i>
                        </p>
                    </div>
                    <div class="tree treeUser" style="width: 30%;margin-right: 1%;">
                        <p style="text-align: center; font-size: 18px;margin:5px">选择填报人</p>
                        <p v-for="(item, index) in treeList" :key="index" class="p">
                            {{ item.receiveName }}
                            <i @click="deletes(item, index)" class="el-icon-error"></i>
                        </p>
                    </div>

                </el-form-item>
                <div v-if="!disableStatus">
                    <el-form-item class="info-line online" v-if="this.companyFlag != 2">
                        <span>负责人</span>
                        <div class="tree treeUser" style="width: 30%;margin-right: 1%;">
                            <p v-for="(item, index) in deptList" :key="index" @click="choseDept(item)">{{ item.ou }}</p>
                        </div>
                        <div class="tree treeUser" style="width: 30%;margin-right: 1%;">
                            <p v-for="(item, index) in treeListp" :key="index">
                                <span>{{ item.ou }}</span>
                                <span class="p-btn" @click="fzperson(item)">负责人</span>
                                <i @click="deletest(item, index)" class="el-icon-error"></i>
                            </p>
                        </div>
                        <div class="tree treeUser" style="width: 30%;">
                            <p v-for="(item, index) in treeListp" :key="index">{{ item.loginNameStr }}</p>
                        </div>
                    </el-form-item>
                </div>
            </el-form>
            <div style="text-align: right;margin-top: 20px">
                <el-button @click="dialogCli(1)" type="primary">确 定</el-button>
                <el-button @click="dialogCli(2)">取 消</el-button>
            </div>
        </el-dialog>

        <el-dialog title="选择负责人" :visible.sync="dialogStatusp" width="50%" :destroy-on-close="true"
            :modal-append-to-body="false" :close-on-click-modal="false" :before-close="closeDutydia">
            <el-input placeholder="输入关键字进行过滤" v-model="filterText" @input="dutyNameSearch"></el-input>
            <el-form :model="infolist" ref="infolist" style="height: 50vh;overflow-y: scroll;">

                <div class="treeUserp">
                    <p v-for="(item, index) in treeListponly" :key="index" @click="chofz(item)"
                        v-if="item.loginNameStr">
                        {{ item.groupName }} -
                        {{ item.loginNameStr }}
                    </p>
                </div>
            </el-form>
        </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 deptTree from "../../../components/deptTreeUserNew.vue"
import deptTreep from "../../../components/deptTreeP.vue"

export default {
    components: {
        mySearch,
        myMessage,
        toolList,
        deptTree,
        deptTreep
    },
    data() {
        const supplierName = (rule, value, callback) => {
            if (!this.infolist.supplierName) {
                callback(new Error('不能为空'))
            } else {
                callback()
            }
        }
        return {
            rules: {
                supplierName: [{
                    required: true,
                    trigger: 'blur',
                    validator: supplierName
                }],
            },
            searchList: [{
                type: 'input',
                tit: '名称',
                value: '',
                width: '98%',
            }],
            tooltit: '任务进度跟踪',
            fullscreen: false,
            total: 0,
            pageSize: 1,
            tableData: [{}],
            dialogStatus: false,
            disableStatus: false,
            titname: '',
            infolist: {},
            userInfo: {},
            params: {},
            centerDialogVisible: false,
            messTit: '',
            delid: '',
            loading: false,
            loadinged: false,
            treeList: [],
            treeListOld: [],
            treeListp: [],
            treeListponly: [],
            closeList: [],
            defaultList: [],
            depttype: 0,
            fadept: {},
            deptList: [],
            companyFlag: '',
            dialogStatusp: false,
            choone: {},
            fillLists: [],
            filterText: '',
            filterData: []
        }
    },
    methods: {
        dialogCli(v) {
            let _this = this;
            if (this.titname == '查看') {
                v = 2;
            }
            if (v === 1) {
                let param = {};
                param = this.infolist;
                param.time ? param.startTime = this.$formatDate(param.time[0], "YYYY-MM-DD") : '';
                param.time ? param.endTime = this.$formatDate(param.time[1], "YYYY-MM-DD") : '';
                param.dutyList = [];
                for (let i = 0; i < this.treeListp.length; i++) {

                    if (this.treeListp[i].loginNoStr) {
                        param.dutyList.push({
                            dutyNo: this.treeListp[i].loginNoStr,
                            dutyName: this.treeListp[i].loginNameStr,
                            groupId: this.treeListp[i].o,
                            groupName: this.treeListp[i].ou,
                        })
                    }
                }
                param.fillList = [];
                for (let i = 0; i < this.treeListOld.length; i++) {
                    param.fillList.push({
                        fillNo: this.treeListOld[i].receiveNo,
                        fillName: this.treeListOld[i].label,
                    })
                }
                for (let i = 0; i < this.treeList.length; i++) {
                    param.fillList.push({
                        fillNo: this.treeList[i].receiveNo,
                        fillName: this.treeList[i].label,
                    })
                }
                var obj = {};
                param.fillList = param.fillList.reduce(function (item, next) {
                    obj[next.fillNo] ? '' : obj[next.fillNo] = true && item.push(next);
                    return item;
                }, []);
                JSON.parse(JSON.stringify(param.fillList))
                // for (let i = 0; i < this.treeList.length; i++) {
                //     for (let ii = 0; ii < this.treeListp.length; ii++) {
                //         if (this.treeListp[i].loginNoStr == this.treeList[ii].receiveNo) {
                //             this.$message({
                //                 message: '负责人和填报人重复！',
                //                 type: 'error'
                //             });
                //             return
                //         }
                //     }
                // }
                var end_flag = false;
                this.treeList.forEach(item => {
                    this.treeListp.forEach(n => {
                        if (n.loginNoStr == item.receiveNo) {
                            this.$message({
                                message: '负责人和填报人重复！',
                                type: 'error'
                            });
                            end_flag = true;
                        }
                    })
                })
                if (end_flag) return;
                let url = '';
                if (this.companyFlag == 0) {
                    url = '/market/cMemberWo/saveMkMemberWoByProv';
                } else if (this.companyFlag == 1) {
                    url = '/market/cMemberWo/saveMkMemberWoByRegion';
                    // param.initiatorName = this.choone.receiverName;
                    // param.initiatorNo = this.choone.receiverNo;
                } else if (this.companyFlag == 2) {
                    url = '/market/cMemberWo/saveMkMemberWoByArea';
                    // param.initiatorName = this.choone.receiverName;
                    // param.initiatorNo = this.choone.receiverNo;
                }
                this.$http({
                    url: url,
                    method: "post",
                    headers: {
                        "Content-Type": "application/json",
                    },
                    data: param,
                }).then((res) => {
                    if (res.data.result === 1) {
                        _this.$message({
                            message: res.data.desc,
                            type: 'error'
                        });
                    } else {
                        _this.$message({
                            message: '成功',
                            type: 'success'
                        });
                        _this.infolist = {};
                        // _this.treeList = [];
                        // _this.treeListp = [];
                        _this.dialogStatus = false;
                        _this.getList({}, 1);
                    }
                });
            } else if (v === 2) {
                this.infolist = {};
                // this.treeList = [];
                // this.treeListp = [];
                this.dialogStatus = false;
            }
        },
        chofz(v) {
            this.dialogStatusp = false;
            this.filterText = "";
            for (let i = 0; i < this.treeListp.length; i++) {
                if (this.treeListp[i].o == this.fadept.o) {
                    this.treeListp[i].loginNameStr = v.loginNameStr;
                    this.treeListp[i].loginNoStr = v.loginNoStr;
                }
            }
        },
        fzperson(v) {
            this.fadept = v;
            this.dialogStatusp = true;
            this.$http({
                url: "/sysmgr/sysuserinfo/queryList",
                method: "post",
                headers: {
                    "Content-Type": "application/json",
                },
                data: {
                    groupId: v.o,
                },
            }).then((res) => {
                this.treeListponly = res.data;
            });
            this.$http({
                url: "/sysmgr/csysdept/queryRegionDeptList",
                method: "post",
                headers: {
                    "Content-Type": "application/json",
                },
                data: {
                    parentorgid: v.o,
                },
            }).then((res) => {
                res.data.forEach(item => {
                    this.$http({
                        url: "/sysmgr/sysuserinfo/queryList",
                        method: "post",
                        headers: {
                            "Content-Type": "application/json",
                        },
                        data: {
                            groupId: item.o,
                        },
                    }).then((ress) => {
                        this.treeListponly.push({ bumen: item.ou })
                        this.treeListponly = this.treeListponly.concat(ress.data);

                        this.filterData = this.treeListponly;
                    });
                })
            });
        },
        deletes(val, index) {
         
            this.treeList.splice(index, 1);
            this.closeList = this.treeList;
        },
        deletess(val, index) {
        
            this.treeListOld.splice(index, 1);
            // this.closeList = this.treeListOld;
        },
        deletest(val, index) {
            this.treeListp.splice(index, 1);
        },
        treeCheck(v) {
     
            this.treeList = v;
        },
        choseDept(v) {
            let x = 'no';
            for (let i = 0; i < this.treeListp.length; i++) {
                if (this.treeListp[i].o == v.o) {
                    x = i;
                }
            }
            if (x != 'no') {
                this.treeListp.splice(x, 1)
            } else {
                this.treeListp.push(v);
            }
        },
        jumpfw(v) {
            this.$router.push({
                path: "/reportProgress",
                query: {
                    woNo: v.woNo
                }
            });
            this.setabList("填报进度跟踪", "/reportProgress?woNo=" + v.woNo);
        },
        jumpjd(v) {
            // console.log(v.receiverGroupName)
            this.$router.push({
                path: "/speedOfProgress",
                query: {
                    receiverGroupName: v.receiverGroupName
                }
            });
            this.setabList("进度跟踪", "/speedOfProgress?groupName=" + v.receiverGroupName);
        },
        setabList(n, p) {
            let params = {
                children: "",
                name: n,
                rountPath: p,
                target: "_self",
            };
            for (let i = 0; i < this.$store.state.tabList.length; i++) {
                if (this.$store.state.tabList[i].name === params.name) {
                    this.$store.state.tabList[i] = params;
                }
            }
            let set = new Set([...this.$store.state.tabList, params]);
            set.add(params);
            this.$store.commit("setDefaultActive", params.rountPath);
            this.$store.commit("setTabList", Array.from(set));
        },
        closedia() {
            this.infolist = {};
            this.dialogStatus = false;

        },
        closeDutydia() {
            this.dialogStatusp = false;
            this.filterText = "";
        },
        //搜索数据
        searchInfo(v) {
            this.params = {};
            v[0] ? this.params.woName = v[0] : '';
            this.getList(this.params, this.pageSize);
        },
        //获取列表
        getList(v, n) {
            this.pageSize = n;
            let _this = this;
            this.loading = true;
            this.tableData = [];
            let url = '';
            if (this.companyFlag == 0) {
                url = '/market/cMemberWo/queryProvTracePage';
            } else if (this.companyFlag == 1) {
                url = '/market/cMemberWo/queryRegionTracePage';
            } else if (this.companyFlag == 2) {
                url = '/market/cMemberWo/queryAreaTracePage';
            }
            v.sts = 0;
            this.$http({
                url: url,
                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.choone = n;
            if (v === 1) {
                this.titname = '查看';
                this.disableStatus = true;
            } else if (v === 2) {
                this.titname = '修改';
                this.disableStatus = false;
            }
            this.dialogStatus = true;

            this.$http({
                url: "/market/cMemberWo/queryMemeberWoInfo",
                method: "post",
                headers: {
                    "Content-Type": "application/json",
                },
                data: {
                    woNo: n.woNo
                },
            }).then((res) => {
                this.treeList = []
                this.treeListOld = []
                this.treeListp = []
                this.infolist = res.data;
                this.infolist.time = [res.data.startTime, res.data.endTime];
                this.infolist.opNo = this.userInfo.loginNo;
                this.infolist.opName = this.userInfo.loginName;
                // this.defaultList = []
                // this.defaultList.push({
                //     deptCode: "00440089005200000000",
                //     deptName: "中国移动通信集团黑龙江有限公司\\哈尔滨分公司\\综合部",
                //     id: "694817329862230016",
                //     label: "郭有为",
                //     receiveName: "郭有为",
                //     receiveNo: "guoyouwei",
                //     type: 1,
                // })

                // this.treeList = this.defaultList
                this.fillLists = []
                this.infolist.fillList.forEach(item => {
                    this.fillLists.push({
                        fillName: item.fillName,
                        fillNo: item.fillNo,
                        receiveNo: item.fillNo,
                        receiveName: item.fillName,
                        label: item.fillName
                    })

                })
                this.treeListOld = this.fillLists
                //地市范围
                if (this.companyFlag == '0') {
                    this.$http({
                        url: "/sysmgr/csysdept/queryRegionDeptList",
                        async: false,
                        method: "post",
                        headers: {
                            "Content-Type": "application/json",
                        },
                        data: {},
                    }).then((res) => {
                        this.deptList = res.data[0].children;
                        this.deptList.forEach(item => {
                            this.infolist.dutyList.forEach(m => {
                                if (m.groupId == item.o) {

                                    item = Object.assign(item, { loginNoStr: m.dutyNo, loginNameStr: m.dutyName })
                                    this.treeListp.push(item);
                                }
                            })
                        })
                    });
                } else {
                    this.$http({
                        url: "/sysmgr/csysdept/queryListByParent",
                        method: "post",
                        async: false,
                        headers: {
                            "Content-Type": "application/json",
                        },
                        data: {
                            parentorgid: n.receiverGroupId
                        },
                    }).then((res) => {
                        this.deptList = res.data;
                        this.deptList.forEach(item => {
                            this.infolist.dutyList.forEach(m => {
                                if (m.groupId == item.o) {
                                    item = Object.assign(item, { loginNoStr: m.dutyNo, loginNameStr: m.dutyName })
                                    this.treeListp.push(item);
                                }
                            })
                        })


                    });
                }

                //区县进入查人
                // if(this.companyFlag == '2'){
                //     this.$http({
                //         url: "/sysmgr/csysuserinfo/queryList",
                //         method: "post",
                //         headers: {
                //             "Content-Type": "application/json",
                //         },
                //         data: {
                //             groupId: n.receiverGroupId
                //         },
                //     }).then((res) => {
                //         this.deptList = res.data;
                //     });
                // }
            });
        },


        closeMessage(v) {
            this.centerDialogVisible = false;
            let _this = this;
            if (v === 1) {
                _this.$http({
                    url: "/market/cMemberWo/endFillWo",
                    method: "post",
                    headers: {
                        "Content-Type": "application/json",
                    },
                    data: {
                        provWoNo: 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.woNo;
        },
        //文件返回值
        uploadBack(v) {
            // console.log(v)
        },
        //功能栏
        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: "/sysmgr/csysuserinfo/queryUserInfoByLoginNo",
                method: "post",
                headers: {
                    "Content-Type": "application/json",
                },
                data: {},
            }).then((res) => {
                this.companyFlag = res.data.companyFlag;
                this.getList({}, 1);
            });
        },
        dutyNameSearch() {

            if (this.filterText == "" || this.filterText == null) {
                this.treeListponly = this.filterData;
                return;
            }

            this.treeListponly.forEach((item) => {
                if (item.groupName !== undefined) {
                    if (item.groupName.indexOf(this.filterText) !== -1) {
                        this.treeListponly = this.treeListponly.filter(m => {
                            if (m.groupName)
                                return m.groupName.indexOf(this.filterText) != -1;
                            else {
                                return false;
                            }
                        })
                    }
                }
                if (item.loginNameStr !== undefined) {

                    if (item.loginNameStr.indexOf(this.filterText) !== -1) {
                        this.treeListponly = this.treeListponly.filter(n => {
                            if (n.loginNameStr)
                                return n.loginNameStr.indexOf(this.filterText) != -1;
                            else {
                                return false;
                            }
                        })
                    }
                }

            })

        }
    },
    mounted() {
        this.getUser();
        // this.defaultList.push({
        //             deptCode: "",
        //             deptName: "",
        //             id: "",
        //             label: "",
        //             receiveName: "",
        //             receiveNo: "",
        //             type: 1,
        //             checked:true
        //         })
    },
    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);
    }

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

        p {
            overflow: hidden;
            display: flex;
            justify-content: space-between;
        }

        .p-btn {
            width: 50px !important;
            cursor: pointer;
            color: #0074DA;
        }

        span {
            display: inline-block;
            width: calc(100% - 100px);
        }

        .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;
        }
    }
}

.treeUserp {
    p {
        padding: 5px;
    }

    p:hover {
        background: #01BEF0;
        color: #fff;
        cursor: pointer;
    }
}

.online {
    width: 100%;

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

    span {
        vertical-align: top;
    }

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

    .el-date-editor {
        width: calc(100% - 100px);
        display: inline-flex;
    }
}
</style>
