let _Http = getApp().globalData.http,
    count = null;
Page({
    data: {
        showAll: false,
        repetitionShow: false,
        repetitionList: [],
        isSubmit: false,
        form: [{
            label: "企业名称",
            error: false,
            errMsg: "",
            type: "textarea",
            value: "",
            placeholder: "企业全称",
            valueName: "enterprisename",
            checking: "base",
            slot: "info",
            required: true
        }, {
            label: "品牌名称",
            error: false,
            errMsg: "",
            type: "textarea",
            value: "",
            placeholder: "品牌名称",
            valueName: "brandname",
            checking: "base",
            required: true
        }, {
            label: "优势信息",
            error: false,
            errMsg: "",
            type: "textarea",
            value: "",
            placeholder: "优势信息",
            valueName: "advantage",
            checking: "base",
            required: false
        }, {
            label: "劣势信息",
            error: false,
            errMsg: "",
            type: "textarea",
            value: "",
            placeholder: "劣势信息",
            valueName: "inferiority",
            checking: "base",
            required: false
        }, {
            label: "备注",
            error: false,
            errMsg: "",
            type: "textarea",
            value: "",
            placeholder: "",
            valueName: "remarks",
            checking: "base",
            required: false
        }],
        content: {
            "sa_competitorid": 0,
            "sys_enterpriseid": 0,
        },
        disabled: true
    },
    onLoad(options) {
        if (options.data) {
            let data = JSON.parse(options.data);
            console.log(data)
            this.se
            this.setData({
                disabled: false,
                form: this.data.form.map(v => {
                    if (v.valueName != 'region') {
                        v.value = data[v.valueName];
                    } else {
                        v.value = data.province ? [data.province, data.city, data.county] : []
                    }
                    return v
                }),
                "content.sa_competitorid": data.sa_competitorid,
                "content.sys_enterpriseid": data.sys_enterpriseid,
            })
        }
    },
    introduce({
        detail
    }) {
        let item = detail;
        this.setData({
            [`form[0].value`]: item.companyName
        })
    },
    queryClient() {
        let data = this.selectComponent("#Form").query();
        if (data.enterprisename == '') {
            wx.showToast({
                title: `您还未填写企业名称`,
                icon: "none"
            });
        } else {
            this.setData({
                form: this.selectComponent("#Form").data.form,
            })
            this.selectComponent("#Info").queryClient(data.enterprisename)
        }
    },
    repClose() {
        if (this.data.isSubmit) {
            let that = this;
            wx.showModal({
                title: '提示',
                content: `是否继续创建`,
                complete: (res) => {
                    if (res.confirm) that.handleSubmit(true);
                }
            })
        }
        this.setData({
            repetitionShow: false,
            isSubmit: false
        })
    },
    /* 表单必填项是否完成 */
    onConfirm({
        detail
    }) {
        this.setData({
            disabled: detail
        })
    },
    // 是否显示全部
    onChange({
        detail
    }) {
        this.setData({
            showAll: detail
        })
    },
    /* 查询是否重复 */
    async queryRepetition(e) {
        let {
            enterprisename
        } = this.selectComponent("#Form").query();
        if (enterprisename == '') return wx.showToast({
            title: `您还未填写企业名称`,
            icon: "none"
        });
        let res = await this.handleQueryRepetition({
            sa_customersid: 0,
            enterprisename,
            taxno: "",
            address: ""
        });
        console.log("查询重复", res)
        if (res.msg != '成功') return wx.showToast({
            title: res.msg,
            icon: "none"
        });
        this.setData({
            countDown: 6
        });
        count = setInterval(() => {
            let countDown = this.data.countDown;
            if (countDown == 0) {
                clearInterval(count);
                this.setData({
                    countDown: ""
                })
            } else {
                countDown--;
                this.setData({
                    countDown
                })
            }
        }, 1000)
        if (res.total == 0) {
            wx.showToast({
                title: '未查询到疑似重复的客户信息',
                icon: "none"
            })
        } else {
            wx.showToast({
                title: `查询到${res.total}条疑似重复客户信息`,
                icon: "none"
            })
            this.setData({
                repetitionShow: true,
                repetitionList: res.data
            })
        }
    },
    /* 处理查重 */
    handleQueryRepetition(content) {
        return _Http.basic({
            "id": 20221208172002,
            content
        })
    },
    async submit() {
        let data = this.selectComponent("#Form").submit();
        let query = await this.handleQueryRepetition({
            sa_customersid: 0,
            enterprisename: data.enterprisename,
            taxno: "",
            address: ""
        });

        if (query.total != 0) {
            wx.showToast({
                title: `查询到${query.total}条疑似重复信息`,
                icon: "none"
            })
            this.setData({
                repetitionShow: true,
                repetitionList: query.data,
                isSubmit: true
            })
        } else {
            this.handleSubmit();
        }

    },
    handleSubmit(tag = false) {
        let content = {
            ...this.data.content,
            ...this.selectComponent("#Form").submit()
        };
        _Http.basic({
            "id": 20221018164102,
            content
        }).then(res => {
            console.log("新建对手", res)
            if (res.msg != '成功') return wx.showToast({
                title: res.data,
                icon: "none"
            })
            wx.showToast({
                title: '保存成功',
                icon: "none"
            })
            //绑定疑似重复标签
            if (tag) _Http.basic({
                "id": 20220929090901,
                "content": {
                    "ownertable": "sa_competitor",
                    "ownerid": res.data.sa_competitorid,
                    "datatag": ["疑似重复"]
                }
            })
            setTimeout(() => {
                let pages = getCurrentPages(),
                    page = pages[pages.length - 2];
                if (page.__route__ == 'packageA/opponent/index') {
                    page.getList(true);
                    wx.redirectTo({
                        url: './detail?id=' + res.data.sa_competitorid,
                    })
                } else if (page.__route__ == 'packageA/opponent/detail') {
                    wx.navigateBack();
                    page.getDetail();
                }
            }, tag ? 500 : 300)

        })
    }
})