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

        return {
            editTitle: "新增意向",
            showDetail: false,
            showEdit: false,
            nameLoading: false,
            submitLoading: false,
            isFirstEdit: true,
            nameList: [],
            nameMap: [],
            ecompanySourceTypeList: [],
            ecompanyLevelList: [],
            eduTypeList: [],
            search: {
                list: [],
                currentPage: 1,
                pageSize: 20,
                total: 0,
                name: '',
                ecompanyLevel: '',
                ecompanySourceType: '',
                createdBegin: '',
                createdEnd: '',
                owner_name: '',
            },
            intention: {
                id: "",
            },
            applyDetail: {
                id: '',
                eaccount: {},
            },
            rules: {
                name: [{
                        required: true,
                        message: '请输入客户名称',
                        trigger: 'blur'
                    },
                    {
                        validator: validMethod,
                        trigger: 'blur'
                    }
                ],
                contractName: [{
                    required: true,
                    message: '请输入联系人',
                    trigger: 'blur'
                }, ],
                a: [],
            },
            showFollow: false,
            followSearch:{
                list: [],
                currentPage: 1,
                pageSize: 10,
                total : 0,
                eintention_id: 0,
            },

            showAssign: false,
            assignLoading: false,
            assignUserLoading: false,
            assignUserId: "",
            assignUser: "",
            assignIntention: null,
            assignUserList:[],
            assignUserMap:[],
            hasAssign: false,

        }
    },
    mounted: function () {
        this.getEcompanySourceTypeList();
        this.getEcompanyLevelList();
        this.resetSearch();
        this.doRole();
        console.log(this.hasAssign, this.currentUser, "----------------------------");
    },
    created: function () {},
    methods: {
        doRole() {
            var currentUser = this.currentUser;
            if(currentUser) {
                if(currentUser.isAdmin) {
                    this.hasAssign = true;
                } else {
                    var roles = currentUser.roles;
                    if(roles) {
                        var codes = [];
                        for(var role of roles) {
                            codes.push(role.code);
                        }

                        if(codes.indexOf("zcgl") > -1 || codes.indexOf("zczy") > -1) {
                            this.hasAssign = true;
                        }
                    }
                }
            }
        },
        getEcompanySourceTypeList() {
            var self = this;
            this.$root.getReq("/web/metaCtl/getDicConfig", {
                "dicKey": "ecompanySourceType"
            }).then(cfg => {
                self.ecompanySourceTypeList = [];
                Object.keys(cfg.data).forEach(key => {
                    self.ecompanySourceTypeList.push({
                        "label": cfg.data[key],
                        "value": key
                    });
                });
            });
        },
        getEcompanyLevelList() {
            var self = this;
            this.$root.getReq("/web/metaCtl/getDicConfig", {
                "dicKey": "ecompanyLevel"
            }).then(cfg => {
                self.ecompanyLevelList = [];
                Object.keys(cfg.data).forEach(key => {
                    self.ecompanyLevelList.push({
                        "label": cfg.data[key],
                        "value": key
                    });
                });
            });
        },

        getEmptySearch() {
            return {
                list: [],
                currentPage: 1,
                pageSize: 20,
                total: 0,
                name: '',
                ecompanyLevel: '',
                ecompanySourceType: '',
                createdBegin: '',
                createdEnd: '',
                owner_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.getList();
        },
        getList() {
            var self = this;
            this.$root.postReq("/web/eintentionCtl/allList", {
                search: this.getParams()
            }).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 || "---";
        },

        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.intention.name) {
                    return callback(new Error("请输入客户名称"));
                }

                self.$root.postReq("/web/eintentionCtl/validName", {
                    name: self.intention.name
                }).then(function (d) {
                    if (d.status == 0) {
                        return callback();
                    } else {
                        return callback(new Error(d.msg));
                    }
                });
            } else {
                return callback();
            }
        },
        changeStatus() {
            this.curStatus = this.search.applyStatus;
            this.search.currentPage = 1;
            this.search.pageSize = 20;
            this.search.total = 0;
            this.getList();
        },
        openDetail(item) {
            var self = this;
            this.intention = {};
            this.getIntention(item.id, function (data) {
                self.showDetail = true;
            })
        },
        openEdit(item) {
            var self = this;
            self.nameList = [];

            if (item) {
                self.editTitle = "修改意向";
                self.getIntention(item.id);
            } else {
                self.editTitle = "新增意向";
            }
            self.showEdit = true;
            if (this.isFirstEdit) {
                this.isFirstEdit = false;
            } else {
                this.$refs.intention.resetFields();
            }
        },
        getIntention(id, cb) {
            var self = this;
            this.$root.postReq("/web/eintentionCtl/findOne", {
                id: id
            }).then(function (d) {
                if (d.status == 0) {
                    self.intention = d.data;
                    if (cb) {
                        cb(d.data);
                    }
                } else {}
            });
        },
        nameChange(val) {
            this.$refs.intention.validateField('name');
            var self = this;
            var item = self.nameMap[val];
            if (item) {
                self.intention.credit_code = item.creditCode;
            }
        },
        getIntentionNames(query) {
            var self = this;
            self.nameLoading = true;
            self.$root.postReq("/web/dataCtl/companySuggest", {
                name: query
            }).then(function (d) {
                self.nameLoading = false;
                self.nameList = d.data || [];
                self.nameMap = [];
                for (var item of self.nameList) {
                    self.nameMap[item.entName] = item;
                }
            });
            setTimeout(() => {
                self.nameLoading = false;
            }, 15000);
        },

        openFollow(row) {
            this.followSearch = {
                list: [],
                currentPage: 1,
                pageSize: 10,
                total: 0,
                eintention_id: row.id,
            };

            this.showFollow = true;
            this.getFollowList();
        },
        getFollowList() {
            var self = this;
            var params = {
                currentPage: self.followSearch.currentPage,
                pageSize: self.followSearch.pageSize,
                eintention_id: self.followSearch.eintention_id
            };
            console.log(params);
            self.$root.postReq("/web/eintentionfollowCtl/followList", {
                search: params
            }).then(function (d) {
                if (d.status == 0) {
                    self.followSearch.list = d.data.rows || [];
                    self.followSearch.total = d.data.count || 0;
                } else {}
            });
        },
        handleSizeChangeFollow(val) {
            this.followSearch.list = [];
            this.followSearch.currentPage = 1;
            this.followSearch.pageSize = val;
            this.followSearch.total = 0;
            this.getFollowList();
        },
        handleCurrentChangeFollow(val) {
            this.followSearch.currentPage = val;
            this.getFollowList();
        },
        userChange(val) {
            var self = this;
            var item = this.assignUserMap["id_" + val];
            this.assignUser = item;
        },
        getAssignUserList(query) {
            var self = this;
            self.assignUserLoading = 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.assignUserLoading = false;
                self.assignUserList = d.data || [];
                self.assignUserMap = [];
                for(var item of self.assignUserList) {
                    self.assignUserMap["id_" + item.id] = item;
                }
            });
            setTimeout(() => {
                self.assignUserLoading = false; 
            }, 15000);
        },
        openAssign(item) {
            this.assignUserLoading = false;
            this.assignLoading = false;
            this.assignUserList = [];
            this.assignUserMap = [];
            this.assignUserId = "";
            this.assignUser = "";
            this.assignIntention = item;
            this.showAssign = true;
        },
        closeAssign() {
            this.assignUserLoading = false;
            this.assignLoading = false;
            this.assignUserList = [];
            this.assignUserMap = [];
            this.assignUserId = "";
            this.assignUser = "";
            this.assignIntention = null;
            this.showAssign = false;
        },
        saveAssign() {
            var self = this;
            console.log(this.assignUser, this.assignIntention, "--------------------------- ");
            if(!self.assignUser) {
                self.$message.warning(`请选择人员`);
                return;
            }
            var confimMsg = "确定将[" + this.assignIntention.name + "]分配给[" + this.assignUser.nickName + "]？";
            
            self.$confirm(confimMsg, {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                self.assignLoading = true;
                self.$root.postReq("/web/eintentionCtl/assignUser", {
                    id: self.assignIntention.id,
                    assignUserId: self.assignUser.id
                }).then(function (d) {
                    self.assignLoading = false;
                    if (d.status == 0) {
                        self.$message.success(`分配成功`);
                        self.getList();
                        self.closeAssign();
                    } else {
                        self.$message.warning(d.msg || `分配失败`);
                    }
                });

                setTimeout(() => {
                    self.assignLoading = false;
                }, 5000);
            }).catch(() => {
            });
        },
    },
    vname: "gsb-eintentionall"
}