let _Http = getApp().globalData.http,
    count = null;

Page({
    data: {
        loading: false,
        repetitionShow: false,
        repetitionList: [],
        isSubmit: false,
        sa_projectid: 0,
        showAll: false,
        disabled: true,
        form: [{
                label: "项目名称",
                error: false,
                errMsg: "",
                type: "textarea",
                value: "",
                placeholder: "项目名称",
                valueName: "projectname",
                checking: "base",
                required: true
            }, {
                label: "项目类型",
                error: false,
                errMsg: "",
                type: "option",
                optionNmae: "projecttype",
                optionType: "radio", //复选   radio 单选
                value: "",
                placeholder: "选择类型",
                valueName: "projecttype",
                checking: "base",
                required: true
            }, {
                label: "项目等级",
                error: false,
                errMsg: "",
                type: "option",
                optionNmae: "projectgrade",
                optionType: "radio", //复选   radio 单选
                value: "",
                placeholder: "选择项目等级",
                valueName: "grade",
                checking: "base",
                required: false
            }, {
                label: "省市县",
                error: false,
                errMsg: "",
                type: "region",
                value: [],
                placeholder: "省/市/县",
                valueName: "region",
                required: true
            }, {
                label: "详细地址",
                error: false,
                errMsg: "",
                type: "textarea",
                value: "",
                placeholder: "详细地址",
                valueName: "address",
                checking: "base",
                required: false
            }, {
                label: "项目规模",
                error: false,
                errMsg: "",
                type: "textarea",
                value: "",
                placeholder: "请填写建筑面积(㎡)/吨位(t)",
                valueName: "scale",
                checking: "base",
                required: false
            }, {
                label: "项目预算(万元)",
                error: false,
                errMsg: "",
                type: "digit",
                value: "",
                placeholder: "请填写金额",
                valueName: "budgetary",
                checking: "base",
                required: false
            }, {
                label: "总投资额(万元)",
                error: false,
                errMsg: "",
                type: "digit",
                value: "",
                placeholder: "请填写金额",
                valueName: "totalinvestment",
                checking: "base",
                required: false
            }, {
                label: "造价(万元)",
                error: false,
                errMsg: "",
                type: "digit",
                value: "",
                placeholder: "请填写金额",
                valueName: "costofconstruction",
                checking: "base",
                required: false
            }, {
                label: "预计开工时间",
                error: false,
                errMsg: "",
                type: "date",
                fields: "month",
                value: "",
                placeholder: "预计开工时间",
                valueName: "begdate_due",
                checking: "base",
                required: false
            }, {
                label: "预计完工时间",
                error: false,
                errMsg: "",
                type: "date",
                fields: "month",
                value: "",
                placeholder: "预计完工时间",
                valueName: "enddate_due",
                checking: "base",
                required: false
            },
            {
                label: "预计签约时间",
                error: false,
                errMsg: "",
                type: "date",
                fields: "month",
                value: "",
                placeholder: "预计签约时间",
                valueName: "signdate_due",
                checking: "base",
                required: false
            }, {
                label: "领域",
                error: false,
                errMsg: "",
                type: "option",
                optionNmae: "tradefield",
                optionType: "radio", //复选   radio 单选
                value: "",
                placeholder: "选择领域",
                valueName: "tradefields",
                checking: "base",
                required: true
            }, {
                label: "品牌",
                error: false,
                errMsg: "",
                type: "route",
                url: "/packageA/select/brand/select",
                params: {
                    "id": 20220924163702,
                    "content": {
                        nochace: true,
                    }
                },
                query: "&radio=true",
                value: "",
                placeholder: "选择品牌",
                valueName: "sa_brandid",
                checking: "base",
                required: false
            }
        ],
        countDown: "", //查重倒计时
    },
    onLoad(options) {
        if (options.data) {
            let data = JSON.parse(options.data);
            let disabled = options.disabled == "true";
            data.sa_brandid = [data.brandname, [data.sa_brandid]]
            this.setData({
                disabled: false,
                sa_projectid: data.sa_projectid,
                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] : []
                    };
                    if (disabled) {
                        if (['projectname', 'region', 'address'].includes(v.valueName)) v.disabled = true
                    }
                    return v
                })
            })
        };
    },
    /* 表单必填项是否完成 */
    onConfirm({
        detail
    }) {
        this.setData({
            disabled: detail
        })
    },
    // 是否显示全部
    onChange({
        detail
    }) {
        this.setData({
            showAll: detail
        })
    },
    /* 查询是否重复 */
    async queryRepetition(e) {
        let {
            projectname,
            address
        } = this.selectComponent("#Form").query();
        if (projectname == '') return wx.showToast({
            title: `您还未填写项目名称`,
            icon: "none"
        });
        let res = await this.handleQueryRepetition({
            sa_projectid: this.data.sa_projectid,
            projectname,
            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
            })
        }
    },
    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
        })
    },
    /* 处理查重 */
    handleQueryRepetition(content) {
        return _Http.basic({
            "id": 20221208184202,
            content
        })
    },
    async submit() {
        let data = this.selectComponent("#Form").submit();
        let query = await this.handleQueryRepetition({
            sa_projectid: this.data.sa_projectid,
            projectname: data.projectname,
            address: data.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) {
        this.setData({
            loading: true
        })
        let data = this.selectComponent("#Form").submit();
        data.province = data.region[0] || "";
        data.city = data.region[1] || "";
        data.county = data.region[2] || "";
        data.tradefields = [data.tradefields];
        data.sa_brandid = data.sa_brandid ? data.sa_brandid[1][0] : 0;
        delete(data.region);
        _Http.basic({
            "id": 20221020144202,
            "content": {
                sa_projectid: this.data.sa_projectid,
                ...data
            }
        }).then(res => {
            this.setData({
                loading: false
            })
            console.log("新建项目", res)
            wx.showToast({
                title: res.msg != '成功' ? res.msg : "保存成功",
                icon: "none",
                mask: true
            })
            if (res.msg != '成功') return;
            //绑定疑似重复标签
            if (tag) _Http.basic({
                "id": 20220929090901,
                "content": {
                    "ownertable": "sa_project",
                    "ownerid": res.data.sa_projectid,
                    "datatag": ["疑似重复"]
                }
            })

            setTimeout(() => {
                let page = getCurrentPages()[getCurrentPages().length - 2];
                if (page.__route__ == 'packageA/project/index') {
                    wx.redirectTo({
                        url: '/packageA/project/detail?id=' + res.data.sa_projectid,
                    })
                } else if (page.__route__ == 'packageA/project/detail') {
                    wx.navigateBack()
                    page.getDetail();
                }
            }, 500)
        })
    }

})