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

        return {
            workload: {},
            editTitle: "新增",
            showEdit: false,
            saveLoading: false,
            uploadResult: "",
            uploadSuccess:false,
            search: {
                list: [],
                currentPage: 1,
                pageSize: 20,
                total : 0,
                name:'',
                beginMonth: '',
                endMonth: '',
            },
            rules: {
                ecompanyName: [
                    { required: true, message: '请输入客户名称', trigger: 'blur' },
                ],
                beginDate : [
                    { required: true, message: '请输入开始时间', trigger: 'blur' },
                ],
                endDate : [
                    { required: true, message: '请输入结束时间', trigger: 'blur' },
                ],
                workDay : [
                    { required: true, message: '请输入工作日', trigger: 'blur' },
                ],
                serviceContent : [
                    { required: true, message: '请输入服务内容', trigger: 'blur' },
                ],
                unit : [
                    { required: true, message: '请输入计量单位', trigger: 'blur' },
                ],
                price : [
                    { required: true, message: '请输入单价', trigger: 'blur' },
                ],
                payPrice : [
                    { required: true, message: '请输入应付价格', trigger: 'blur' },
                ],
            },

            nameLoading1: false,
            nameList1: [],
            nameMap1:[],

            nameLoading: false,
            nameList: [],
            nameMap:[],
        }
    },
    mounted: function () {
        this.resetSearch(true);
    },
    created: function () {},
    methods: {
        getEmptySearch() {
            return {
                list: [],
                currentPage: 1,
                pageSize: 20,
                total : 0,
                name:'',
                beginMonth: '',
                endMonth: '',
            }
        },
        resetSearch() {
            this.search = this.getEmptySearch();
            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();
        },
        getList() {
            var self = this;
            var params = this.getParams();
            this.$root.postReq("/web/ecompanyworkCtl/zccompanylist", {
                search: params
            }).then(function (d) {
                if (d.status == 0) {
                    self.search.list = d.data.rows || [];
                    self.search.total = d.data.count || 0;
                } else {
                }
            });
        },
        getParams() {
            var params = {};
            for(var f in this.search) {
                if(f == "list") {
                    continue;
                }
                params[f] = this.search[f];
            }
            return params;
        },
        onColFormater(row, column, cellValue, index) {
            return cellValue || "---";
        },
        getWorkload(id) {
            var self = this;
            self.$root.postReq("/web/ecompanyworkCtl/info", {
                id: id,
            }).then(function (d) {
                if (d.status == 0) {
                    if(d.data) {
                        var month = (d.data.month || "").toString();
                        if(month) {
                            d.data.month = month.substring(0, 4) + "-" + month.substring(4);
                            console.log(d.data.month, "-------------------- d.data.month");
                        }
                    }
                    self.workload = d.data;
                } else {
                    self.$message.warning(d.msg || `您的网络不稳定, 请稍后重试`);
                }
            });
        },
        toDetail(id) {
        },
        openEdit(id) {
            if(id) {
                this.getWorkload(id);
                this.editTitle = "修改";
            } else {
                this.editTitle = "新增";
                this.workload = {isEnabled:true};
            }
            this.showEdit = 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 == 'name') {
                if(!self.workload.name) {
                    return callback(new Error("请输入名称"));
                }
            }
            return callback();
        },

        nameChange1(val) {
            var self = this;
            var item = self.nameMap1[val];
            console.log(item, "----------------------");
            self.search.ecompanyId = (item || {}).id || "";
        },
        getCompanyNames1(query) {
            var self = this;
            self.nameLoading1 = true;
            self.$root.postReq("/web/ecompanyCtl/suggest", {
                name: query
            }).then(function (d) {
                self.nameLoading1 = false;
                self.nameList1 = d.data || [];
                self.nameMap1 = [];
                for(var item of self.nameList1) {
                    self.nameMap1[item.name] = item;
                }
                console.log(self.nameMap1, "----------------------");
            });
            setTimeout(() => {
                self.nameLoading1 = false; 
            }, 15000);
        },

        nameChange(val) {
            this.$refs.workload.validateField('ecompanyName');
            var self = this;
            var item = self.nameMap[val];
            if(item) {
                self.workload.ecompanyId = item.id;
            }
        },

        getCompanyNames(query) {
            var self = this;
            self.nameLoading = true;
            self.$root.postReq("/web/ecompanyCtl/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);
        },

        saveWorkload() {
            var self = this;
            self.saveLoading = true;
            self.$refs.workload.validate((valid) => {
                if (valid) {
                    setTimeout(() => {
                        self.saveLoading = false;
                    }, 5000);
                    self.$root.postReq("/web/ecompanyworkCtl/zcsave", self.workload).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;
                }
            });
        },
        
    },
    vname: "gsb-ecompanywork"
}