const deleteMark = require("../../../../utils/deleteMark"),
    _Http = getApp().globalData.http;
let count = null;
Component({
    properties: {
        endInsert: Function,
        type: String,
        content: {
            type: Object,
            value: {
                "sat_orderclueid": 0,
                "name": "",
                "phonenumber": "",
                "province": "",
                "city": "",
                "county": "",
                "address": "",
                "notes": "",
                "cluesource": ""
            },
        }
    },
    data: {
        region: ['', '', ''], //省市县选择
        disabled: true,
        loading: false
    },
    lifetimes: {
        ready: function () {
            this.isDisabled();
        }
    },
    methods: {
        initData() {
            let content = this.data.content;
            for (let i in content) {
                if (content[i] == '-') content[i] = ""
            };
            this.setData({
                content,
                "region[0]": content.province,
                "region[1]": content.city,
                "region[2]": content.county
            })
        },
        /* 提交 */
        submit() {
            if (this.data.disabled || this.data.loading) return;
            if (!deleteMark.CheckPhoneNumber(this.data.content.phonenumber)) return;
            const that = this;
            if (this.data.type == '添加线索') return this.handleEdit();
            wx.showModal({
                title: "提示",
                content: "是否确认本次修改",
                success: ({
                    confirm
                }) => {
                    if (confirm) that.handleEdit();
                }
            })
        },
        handleEdit() {
            this.setData({
                loading: true
            });
            _Http.basic({
                "classname": "saletool.orderclue.web.orderclue",
                "method": "edit",
                content: this.data.content
            }).then(res => {
                this.setData({
                    loading: false
                });
                if (res.msg != '成功') return wx.showToast({
                    title: res.msg,
                    icon: "none"
                });
                wx.showToast({
                    title: '保存成功!',
                })
                setTimeout(() => {
                    this.triggerEvent("endInsert");
                }, 300);
            })
        },
        /* 输入框输入内容 */
        inputChange(e) {
            let text = e.type == 'input' ? e.detail.value : e.detail;
            text = deleteMark.queryStr(text);
            const {
                label
            } = e.currentTarget.dataset;
            this.setData({
                ["content." + label]: text
            })
            if (['phonenumber', 'province', 'cluesource'].includes(label)) this.isDisabled();
        },
        /* 是否禁用 */
        isDisabled() {
            clearTimeout(count)
            count = setTimeout(() => {
                let {
                    phonenumber,
                    province,
                    cluesource
                } = this.data.content;
                this.setData({
                    disabled: !(phonenumber && province && cluesource)
                })
            }, 500);
        },
        /* 省市县选择器 */
        bindRegionChange: function (e) {
            let region = e.detail.value;
            this.setData({
                region,
                "content.province": region[0],
                "content.city": region[1],
                "content.county": region[2],
            })
            this.isDisabled();
        }
    }
})