{
    template: '${tmpl}',
    data: function () {
        var validMethod = (rule, value, callback) => {
            return this.validfileds(rule, value, callback);
        };

        return {
            etemplate: {isEnabled:true},
            etemplatebusi: {isEnabled:true},
            editTitle: "新增",
            showEdit: false,
            showrelevances: false,
            saveLoading: false,
            busiData:[],


            nameLoading: false,
            nameList: [],
            nameMap: [],

            nameLoading2: false,
            nameList2: [],
            nameMap2:[],
            uploadResult: null,
            fileList:[],

            mainList:[],
            search: {
                list: [],
                currentPage: 1,
                pageSize: 20,
                total : 0,
                name:'',
                begin: '',
                end: '',
            },
            rules: {
                ecompanyName : [
                    { required: true, message: '请输入公司名称', trigger: 'blur' },
                    { validator: validMethod, trigger: 'blur' }
                ],
                company_id : [
                    { required: true, message: '请输入公司名称', trigger: 'blur' },
                    { validator: validMethod, trigger: 'blur' }
                ],
                ecompanyMainId: [
                    { required: true, message: '请选择签约主体', trigger: 'blur' },
                ],
                filepath: [
                    { required: true, message: '请上传签约协议', trigger: 'blur' },
                ],
                a : [],
            },
            busi_id: 0,
        }
    },
    mounted: function () {
        this.resetSearch(true, true);
        this.getMainList();
    },
    created: function () {},
    methods: {
        setWeekDay(){
            if (this.search.begin && this.search.end) {
                return;
            }
            var now = new Date();
            var nowDayOfWeek = now.getDay();
            var nowDay = now.getDate();
            var nowMonth = now.getMonth();
            var nowYear = now.getYear();
            nowYear += (nowYear < 2000) ? 1900 : 0;
            var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek + 1);
            this.search.begin = this.formatDate(weekStartDate);
            var weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek) + 1);
            this.search.end = this.formatDate(weekEndDate);
            console.log(this.search);
        },
        formatDate(date) {
            var myyear = date.getFullYear();
            var mymonth = date.getMonth() + 1;
            var myweekday = date.getDate();
            if (mymonth < 10) {
                mymonth = "0" + mymonth;
            }
            if (myweekday < 10) {
                myweekday = "0" + myweekday;
            }
            return (myyear + "-" + mymonth + "-" + myweekday);
        },
        getEmptySearch() {
            return {
                list: [],
                currentPage: 1,
                pageSize: 20,
                total : 0,
                name:'',
            }
        },
        resetSearch(flag, defaultWeek) {
            this.search = this.getEmptySearch();
            if (defaultWeek) {
                this.setWeekDay();
            }
            this.getList();
        },
        tableHeaderColor({ row, column, rowIndex, columnIndex }) {
            if (rowIndex === 0) {
                return 'background-color: #F5F5F5;color: #2F2F2F;font-size: 14px;font-weight:400;'
            }
        },
        opSearch() {
            this.search.currentPage = 1;
            this.search.total = 0;
            this.getList();
        },
        getMainList() {
            var self = this;
            this.$root.postReq("/web/ecompanymainCtl/all", {
            }).then(function (d) {
                if (d.status == 0) {
                    self.mainList = d.data || [];
                } else {
                }
            });
        },
        getList() {
            var self = this;
            var params = this.getParams();
            this.$root.postReq("/web/busistatCtl/departmentList", {
                search: params
            }).then(function (d) {
                if (d.status == 0) {
                    self.search.list = d.data.rows || [];
                    self.search.total = d.data.count || 0;
                } else {
                }
            });
        },
        exportList() {
            window.open("/web/busistatCtl/departmentExport" + this.getExportParams());
        },
        getParams() {
            var params = {};
            for(var f in this.search) {
                if(f == "list") {
                    continue;
                }
                params[f] = this.search[f];
            }
            return params;
        },
        getExportParams() {
            var params = {};
            for(var f in this.search) {
                if(f == "list") {
                    continue;
                }
                params[f] = this.search[f];
            }
            return "?search=" + encodeURIComponent(JSON.stringify(params));
        },

        onColFormater(row, column, cellValue, index) {
            return cellValue || "---";
        },
        getEtemplate(id) {
            var self = this;
            self.$root.postReq("/web/etemplateCtl/info", {
                id: id,
            }).then(function (d) {
                if (d.status == 0) {
                    if(d.data) {
                        d.data.isEnabled = d.data.isEnabled ? true : false;
                    }
                    self.etemplate = d.data;
                    if(self.etemplate.filepath) {
                        self.fileList = [{
                            name: "模板文件",
                            url: self.etemplate.filepath,
                        }];
                    } else {
                        self.uploadResult = null;
                    }
                } else {
                    self.$message.warning(d.msg || `您的网络不稳定, 请稍后重试`);
                }
            });
        },
        clearInput(){
            this.$refs.etemplatebusi.resetFields();
        },
        addCompany() {
            this.companysForm.bpoCompanyList.push({
                companyName: '',
                key: Date.now(),
                rules: {required: true, message: '公司名称不能为空', trigger: 'blur'},
            });
        },

        getEtemplate_relevance(id) {
            var self = this;
            this.busi_id = id;
            this.$root.postReq("/web/etemplatebusiCtl/getlist", {
                id: id,
                type:1,
            }).then(function (d) {
                self.template_id = id;
                self.busiData = d || [];
            });

        },

        toDetail(id) {
        },
        openEdit(id) {
            this.clearUpload();
            if(id) {
                this.editTitle = "修改";
                this.getEtemplate(id);
            } else {
                this.editTitle = "新增";
                this.etemplate = {isEnabled:true};
            }
            this.showEdit = true;
        },

        relevance(id){
            this.getEtemplate_relevance(id);
            this.showrelevances = true;
        },

        handleSizeChange(val) {
            this.search.pageSize = val;
            this.resetSearch();
        },
        handleCurrentChange(val) {
            this.search.currentPage = val;
            this.getList();
        },
        validfileds(rule, value, callback) {
            var self = this;
            if(rule.field == "ecompanyName") {
                if(!self.etemplate.ecompany_id) {
                    return callback(new Error("请重新选择公司名称"));
                }
            }
            return callback();
        },
        nameChange(val) {
            var self = this;
            var item = self.nameMap[val];
            self.search.ecompany_id = (item || {}).id || "";
        },
        nameChange2(val) {
            var self = this;
            var item = self.nameMap2[val];
            self.etemplate.ecompany_id = (item || {}).id || "";
        },
        nameChange3(val) {
            var self = this;
            var item = self.nameMap2[val];
            self.etemplate.ecompany_id = (item || {}).id || "";
        },
        getCompanyNames(query) {
            var self = this;
            self.nameLoading = true;
            self.$root.postReq("/web/busistatCtl/suggest", {
                name: query
            }).then(function (d) {
                self.nameLoading = false;
                self.nameList = d.data || [];
                self.nameMap = [];
                for(var item of self.nameList) {
                    self.nameMap[item.name] = item;
                }
            });
            setTimeout(() => {
                self.nameLoading = false;
            }, 15000);
        },
        getCompanyNames2(query) {
            var self = this;
            self.nameLoading2 = true;
            self.$root.postReq("/web/ecompanyCtl/suggest", {
                name: query
            }).then(function (d) {
                self.nameLoading2 = false;
                self.nameList2 = d.data || [];
                self.nameMap2 = [];
                for(var item of self.nameList2) {
                    self.nameMap2[item.name] = item;
                }
            });
            setTimeout(() => {
                self.nameLoading2 = false;
            }, 15000);
        },
        getCompanyNames3(query) {
            var self = this;
            self.nameLoading2 = true;
            self.$root.postReq("/web/etemplatebusiCtl/suggest", {
                name: query
            }).then(function (d) {
                self.nameLoading2 = false;
                self.nameList2 = d.data || [];
                self.nameMap2 = [];
                for(var item of self.nameList2) {
                    self.nameMap2[item.name] = item;
                }
            });
            setTimeout(() => {
                self.nameLoading2 = false;
            }, 15000);
        },

        clearUpload() {
            var self = this;
            self.uploadResult = null;
            self.etemplate.filepath = "";
            if(self.$refs.upload3 && self.$refs.upload3.$refs && self.$refs.upload3.$refs.upload) {
                self.$refs.upload3.$refs.upload.clearFiles();
            }
        },
        uploadsuccess(v) {
            if(!v || !v[0] || !v[0].url) {
                this.$message.warning("上传失败");
                return;
            }
            this.etemplate.filepath = v[0].url;
        },

        saveTemplate() {
            var self = this;
            self.$refs.etemplate.validate((valid) => {
                if (valid) {
                    self.saveLoading = true;
                    setTimeout(() => {
                        self.saveLoading = false;
                    }, 15000);
                    self.$root.postReq("/web/etemplateCtl/mysave", self.etemplate).then(function (d) {
                        if (d.status == 0) {
                            self.$message.success(`保存成功`);
                            self.showEdit = false;
                            self.getList();
                        } else {
                            self.$alert(d.msg || '保存失败', '提示', {
                                confirmButtonText: '确定',
                                callback: action => {}
                            });
                        }
                        self.saveLoading = false;
                    });
                } else {
                    self.saveLoading = false;
                    return false;
                }
            });
        },
        saveTemplatebusi() {
            var self = this;
            self.$refs.etemplatebusi.validate((valid) => {
                if (valid) {
                    self.saveLoading = true;
                    setTimeout(() => {
                        self.saveLoading = false;
                    }, 15000);
                    self.etemplatebusi.template_id = self.busi_id;
                    self.etemplatebusi.template_type = 1;
                    self.$root.postReq("/web/etemplatebusiCtl/mysave", self.etemplatebusi).then(function (d) {
                        if (d.status == 0) {
                            self.$message.success(`保存成功`);
                            self.showEdit = false;
                            self.getEtemplate_relevance(self.busi_id);
                        } else {
                            self.$alert(d.msg || '保存失败', '提示', {
                                confirmButtonText: '确定',
                                callback: action => {}
                            });
                        }
                        self.saveLoading = false;
                    });
                } else {
                    self.saveLoading = false;
                    return false;
                }
            });
        },
        deleteTetId(id){
            var self = this;
            self.$root.postReq("/web/etemplatebusiCtl/deleteTetId", {
                id: id,
            }).then(function (d) {
                if (d.status == 0) {
                    self.$message.success(`已删除`);
                    self.showrelevances = true;
                    self.getEtemplate_relevance(self.busi_id);
                } else {
                    self.$message.warning(d.msg || `您的网络不稳定, 请稍后重试`);
                }
            });
        }
    },
    vname: "gsb-ecompanyadmin"
}
