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

        return {
            imageUrl: '',
            data: {
                d: {},
                action: ""
            },
            dkcompany: {
                ctype: '1'
            },
            editTitle: "新增",
            showEdit: false,
            saveLoading: false,
            ownerList:[],
            ownerMap:[],
            ownerLoading:false,
            owner_id: "",
            search: {
                list: [],
                currentPage: 1,
                pageSize: 20,
                total : 0,
                name:'',
            },
            rules: {
                name : [
                    { required: true, message: '请输入渠道名称', trigger: 'blur' },
                    { validator: validMethod, trigger: 'blur' }
                ],
                idcode : [
                    { validator: validMethod, trigger: 'blur' }
                ],
                business_license : [
                    { validator: validMethod, trigger: 'blur' }
                ],
                idcard_front : [
                    { validator: validMethod, trigger: 'blur' }
                ],
                idcard_back : [
                    { validator: validMethod, trigger: 'blur' }
                ],
                contact_man : [
                    { required: true, message: '请输入联系地址', trigger: 'blur' },
                ],
                contact_mobile: [
                    { required: true, message: '请输入联系电话', trigger: 'blur' },
                ],
                contact_email: [
                    { required: true, message: '请输入公章名称', trigger: 'blur' },
                ],
                contact_addr: [
                    { required: true, message: '请输入公章名称', trigger: 'blur' },
                ],
                a : [],
            },
        }
    },
    mounted: function () {
        this.resetSearch(true);
        this.getUploadConfig();
    },
    created: function () {},
    methods: {
        chooseType(value) {
            console.log(value);
            this.dkcompany = {ctype: value};
        },
        getUploadConfig() {
            var that = this;
            this.$root.postReq("/web/uploadCtl/getOssConfig", {
                
            }).then(function (d) {
                console.log(d, "阿里接口");
                if (d.status != -200) {
                  that.data.d["Bucket"] = d.Bucket;
                  that.data.d["OSSAccessKeyId"] = d.OSSAccessKeyId;
                  that.data.d["signature"] = d.Signature;
                  that.data.d["policy"] = d.policy;
                  that.data.d["success_action_status"] = d.success_action_status;
                  that.data.action = d.url;
                }
            });
        },
        handleAvatarSuccess1(res, file,files) {
            var result = res.substring(
                res.indexOf("https://gsb-zc.oss-cn-beijing.aliyuncs.com"),
                res.indexOf("</Location>")
            );
            this.dkcompany['business_license'] = result;
            this.$forceUpdate();
            console.log(result);
        },
        handleAvatarSuccess2(res, file,files) {
            var result = res.substring(
                res.indexOf("https://gsb-zc.oss-cn-beijing.aliyuncs.com"),
                res.indexOf("</Location>")
            );
            this.dkcompany['idcard_front'] = result;
            this.$forceUpdate();
        },
        handleAvatarSuccess3(res, file,files) {
            console.log(file.raw);
            var result = res.substring(
                res.indexOf("https://gsb-zc.oss-cn-beijing.aliyuncs.com"),
                res.indexOf("</Location>")
            );
            this.dkcompany['idcard_back'] = result;
            this.$forceUpdate();
        },
        beforeAvatarUpload(file) {
            this.data.d["key"] = "zc_wxapplet_" + new Date().getTime() + ".xlsx";

            const isJPG = file.type === 'image/jpeg';

            if (!isJPG) {
                this.$message.error('上传头像图片只能是 JPG 格式!');
            };
            return isJPG;
        },
        getEmptySearch() {
            return {
                list: [],
                currentPage: 1,
                pageSize: 20,
                total : 0,
                name:'',
            }
        },
        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/channelCtl/channelPage", 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 || "---";
        },
        getDKcompany(id) {
            var self = this;
            self.$root.postReq("/web/channelCtl/channelInfo", {
                id: id,
            }).then(function (d) {
                if (d.status == 0) {
                    self.dkcompany = d.data;
                    self.dkcompany.ctype = String(d.data.ctype);
                    /**if(d.data) {
                        d.data.isEnabled = d.data.isEnabled ? true : false;
                    }
                    
                    self.dkcompany.owner_id = self.dkcompany.owner_id || "";
                    self.dkcompany.ctype = '1';
                    self.owner_id = (d.data.owner || {}).id || "";
                    if(d.data.owner) {
                        self.ownerList = [d.data.owner];
                        self.ownerMap["id_" + d.data.owner.id] = d.data.owner;
                    } else {
                        self.ownerList = null;
                    }**/
                } else {
                    self.$message.warning(d.msg || `您的网络不稳定, 请稍后重试`);
                }
            });
        },
        toDetail(id) {
        },
        openEdit(id) {
            this.ownerLoading = false;
            this.saveLoading = false;
            this.owner_id = null;
            if(this.$refs.dkcompany) {
                this.$refs.dkcompany.resetFields();
            }
            if(id) {
                this.getDKcompany(id);
                this.editTitle = "修改";
            } else {
                this.ownerList = [];
                this.editTitle = "新增";
                this.dkcompany = {ctype: '1'};
            }
            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 == 'idcode') {
                if(!self.dkcompany.idcode) {
                    let msg = self.dkcompany.ctype == 1 ? "请输入统一社会信用代码": "请输入身份证";
                    return callback(new Error(msg));
                }
            }
            if(rule.field == 'business_license') {
                if(self.dkcompany.ctype == 1 && !self.dkcompany.business_license) {
                    return callback(new Error("请上传营业执照"));
                }
            }
            if(rule.field == 'idcard_front') {
                if(!self.dkcompany.idcard_front) {
                    return callback(new Error("请上传身份证正面"));
                }
            }
            if(rule.field == 'idcard_back') {
                if(!self.dkcompany.idcard_back) {
                    return callback(new Error("请上传身份证反面"));
                }
            }
            return new callback();
        },

        ownerChange(val) {
            console.log(val, );
            this.dkcompany.owner_id = val || 0;
            this.dkcompany.ctype = '1';
            this.owner = this.ownerMap["id_" + val];
            console.log(this.owner);
        },
        getOwnerList(query) {
            var self = this;
            self.ownerLoading = true;
            self.$root.postReq("/web/userCtl/rolesSuggest", {
                name: query,
                roleCodes: ["zczy", "zcgl", "qdzy", "qdgl", "ptzy", "ptgl", "dkhzy", "dkhgl", "ziying", "tzgl", "tzzy"]
            }).then(function (d) {
                self.ownerLoading = false;
                self.ownerList = d.data || [];
                self.ownerMap = [];

                for (var o of self.ownerList) {
                    self.ownerMap["id_" + o.id] = o;
                }
            });
            setTimeout(() => {
                self.ownerLoading = false; 
            }, 15000);
        },
        saveCompany() {
            var self = this;
            self.saveLoading = true;
            self.$refs.dkcompany.validate((valid) => {
                if (valid) {
                    self.$root.postReq("/web/channelCtl/channelSave", self.dkcompany).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;
                }
            });
            setTimeout(() => {
                self.saveLoading = false;
            }, 5000);
        },
    },
    vname: "gsb-dkcompanyadmin"
}