const _Http = getApp().globalData.http,
    getTime = require("../../../../utils/getTime");
let source = 0;
import {
    handleRes
} from "../submit";
Page({
    data: {
        showAll: false,
        form: [{
            label: "合同标题",
            error: false,
            errMsg: "",
            type: "text",
            value: "",
            placeholder: "",
            valueName: "title",
            checking: "base",
            required: true
        }, {
            label: "客户",
            error: false,
            errMsg: "",
            type: "route",
            url: "/packageA/select/setclient/select",
            value: "",
            params: {
                "id": 20220920083901,
                "content": {
                    nocache: true,
                    "where": {
                        "condition": "",
                        "type": 7,
                        typemx: "直销项目协议"
                    }
                }
            },
            query: "&radio=true&idname=sys_enterpriseid",
            interrupt: true,
            placeholder: "选择客户",
            valueName: "sys_enterpriseid",
            checking: "base",
            required: true
        }, {
            label: "折扣(%)",
            error: false,
            errMsg: "",
            hint: "请输入0-100%",
            type: "digit",
            value: "",
            placeholder: "请填写折扣(%)",
            valueName: "discountrate", //绑定的字段名称
            required: false, //必填
            checking: `^(100|\\d{0,2})(\\.\\d{1,2})?$`, //0-100%
            slot: "discountrate"
        }, {
            label: "付款条件",
            error: false,
            errMsg: "",
            type: "route",
            url: "/packageA/select/paytype/select",
            params: {
                "id": "20230110100103",
                "version": 1,
                "content": {
                    nocache: true,
                    "where": {
                        "condition": ""
                    }
                }
            },
            query: "&radio=true",
            value: "",
            placeholder: "请选择付款条件",
            valueName: "sa_receivableruleid",
            checking: "base",
            required: true
        }, {
            label: "发货日期提醒(天)",
            error: false,
            errMsg: "",
            type: "number",
            value: "",
            placeholder: "发货日期提醒(天)",
            valueName: "reminddays", //绑定的字段名称
            required: false //必填
        }, {
            label: "有效期",
            error: false,
            errMsg: "",
            type: "dateRange",
            start: getTime.formatTime(new Date(), '-').split(' ')[0],
            value: [getTime.formatTime(new Date(), '-').split(' ')[0], ""],
            placeholder: ['生效日期', '截止日期'],
            valueName: "invaliddate",
            checking: "base",
            required: true
        }, {
            label: "签约时间",
            error: false,
            errMsg: "",
            type: "date",
            value: getTime.formatTime(new Date(), '-').split(' ')[0],
            placeholder: "报价日期",
            valueName: "signdate",
            checking: "base",
            required: true
        }, {
            label: "合同描述",
            error: false,
            errMsg: "",
            type: "textarea",
            value: "",
            placeholder: "请填写",
            valueName: "remarks",
            required: false, //必填
        }],
        "content": {
            "sa_contractid": 0,
            "type": "项目",
            "typemx": '直销',
            sys_enterpriseid: 0
        },
        disabled: true
    },
    onLoad(options) {
        let form = this.data.form,
            project = null;
        if (options.data) {
            let data = JSON.parse(options.data);
            data.discountrate = data.discountrate == 1 ? 100 : (data.discountrate * 100).toFixed(2);
            form = form.map(v => {
                if (v.valueName == "invaliddate") v.start = "";
                v.value = data[v.valueName] || "";
                return v
            });
            form.splice(2, 0, {
                label: "收货人",
                error: false,
                errMsg: "",
                type: "route",
                url: "/packageA/select/contacts/select",
                value: [data.name, [data.rec_contactsid]],
                params: {
                    "id": "20221022165503",
                    "content": {
                        nocache: true,
                        sys_enterpriseid: data.sys_enterpriseid[1][0],
                        "where": {
                            "condition": "",
                            "workaddress": 0
                        }
                    }
                },
                query: "&radio=true",
                interrupt: true,
                placeholder: "选择收货人",
                valueName: "rec_contactsid",
                checking: "base",
                required: true
            })
            form.splice(3, 0, {
                label: "收货人电话",
                error: false,
                errMsg: "",
                type: "textare",
                value: data.phonenumber,
                placeholder: "收货人电话",
                valueName: "phonenumber",
                disabled: true,
                required: false
            })
            project = data;
            this.setData({
                disabled: false,
                "content.sa_contractid": data.sa_contractid
            })
        }
        //项目进入 修改客户选择范围
        if (options.project) {
            project = JSON.parse(options.project);
            source = 1;
        }

        if (project) {
            if (source === 1) form[1].params = {
                "id": 20220920083901,
                "content": {
                    nocache: true,
                    "pageNumber": 1,
                    "pageSize": 20,
                    "where": {
                        "condition": "",
                        "type": "13",
                        "sa_projectid": project.sa_projectid
                    }
                }
            };
            form.splice(4, 0, {
                label: "项目",
                error: false,
                errMsg: "",
                type: "route",
                url: "/packageA/select/project/select",
                value: Array.isArray(project.sa_projectid) ? project.sa_projectid : [project.projectname, [project.sa_projectid]],
                params: {
                    "id": 20221201090802,
                    "content": {
                        nocache: true,
                        sys_enterpriseid: source == 1 ? 0 : project.sys_enterpriseid[1][0],
                        "type": 1, //1直销 2经销 3居间
                        "where": {
                            "condition": "",
                        }
                    }
                },
                query: "&radio=true",
                disabled: source === 1,
                placeholder: "选择项目",
                valueName: "sa_projectid",
                checking: "base",
                required: true
            })
            form.splice(5, 0, {
                label: "项目地址",
                error: false,
                errMsg: "",
                type: "textare",
                value: project.province + project.city + project.county + project.address,
                placeholder: "项目地址",
                valueName: "123",
                disabled: true,
                required: false
            })
            form.splice(6, 0, {
                label: "项目编号",
                error: false,
                errMsg: "",
                type: "textare",
                value: project.projectnum,
                placeholder: "项目编号",
                valueName: "123",
                disabled: true,
                required: false
            })
            form.splice(7, 0, {
                label: "业务员",
                error: false,
                errMsg: "",
                type: "route",
                url: "/packageA/select/contacts/select",
                value: project.leader[0].name ? [project.leader[0].name, [project.saler_hrid]] : "",
                params: {
                    "id": 20230305201202,
                    "content": {
                        nocache: true,
                        "type": "2",
                        "sa_projectid": project.sa_projectid
                    },
                },
                query: "&radio=true&idname=hrid",
                placeholder: "选择业务员",
                valueName: "saler_hrid",
                checking: "base",
                required: true
            })

        }

        this.setData({
            form
        });
    },


    /* 打断处理form */
    interrupt(e) {
        let {
            data,
            form,
            temporary
        } = e.detail;
        if (temporary.item.value[0] == data.value[0]) return wx.navigateBack();
        temporary.item.value = data.value;
        if (temporary.item.valueName == "sys_enterpriseid") {
            form.splice(2, form.some(v => v.label == '收货人') ? 1 : 0, {
                label: "收货人",
                error: false,
                errMsg: "",
                type: "route",
                url: "/packageA/select/contacts/select",
                value: "",
                params: {
                    "id": "20221022165503",
                    "content": {
                        nocache: true,
                        sys_enterpriseid: data.value[1][0],
                        "where": {
                            "condition": "",
                            "workaddress": 0
                        }
                    }
                },
                query: "&radio=true",
                interrupt: true,
                placeholder: "选择收货人",
                valueName: "rec_contactsid",
                checking: "base",
                required: true
            })
            form.splice(3, form.some(v => v.label == '收货人电话') ? 1 : 0, {
                label: "收货人电话",
                error: false,
                errMsg: "",
                type: "textare",
                value: "",
                placeholder: "收货人电话",
                valueName: "phonenumber",
                disabled: true,
                required: false
            })
            if (source == 0) form.splice(4, form.some(v => v.label == '项目') ? form.some(v => v.label == '项目地址') ? 3 : 1 : 0, {
                label: "项目",
                error: false,
                errMsg: "",
                type: "route",
                url: "/packageA/select/project/select",
                value: "",
                params: {
                    "id": 20221201090802,
                    "content": {
                        nocache: true,
                        sys_enterpriseid: data.value[1][0],
                        "type": 1, //1直销 2经销 3居间
                        "where": {
                            "condition": "",
                        }
                    }
                },
                query: "&radio=true",
                interrupt: true,
                placeholder: "选择项目",
                valueName: "sa_projectid",
                checking: "base",
                required: true
            })
        } else if (temporary.item.valueName == "sa_projectid") {
            let item = data.item;
            form.splice(temporary.index - 0 + 1, form.some(v => v.label == '项目地址') ? 1 : 0, {
                label: "项目地址",
                error: false,
                errMsg: "",
                type: "textare",
                value: item.province + item.city + item.county + item.address,
                placeholder: "项目地址",
                valueName: "123",
                disabled: true,
                required: false
            })
            form.splice(temporary.index - 0 + 2, form.some(v => v.label == '项目编号') ? 1 : 0, {
                label: "项目编号",
                error: false,
                errMsg: "",
                type: "textare",
                value: item.projectnum,
                placeholder: "项目编号",
                valueName: "123",
                disabled: true,
                required: false
            })
            form.splice(temporary.index - 0 + 3, form.some(v => v.label == '业务员') ? 1 : 0, {
                label: "业务员",
                error: false,
                errMsg: "",
                type: "route",
                url: "/packageA/select/contacts/select",
                value: "",
                params: {
                    "id": 20230305201202,
                    "content": {
                        nocache: true,
                        "type": "2",
                        "sa_projectid": data.id[0]
                    },
                },
                query: "&radio=true&idname=hrid",
                placeholder: "选择业务员",
                valueName: "saler_hrid",
                checking: "base",
                required: true
            })
        } else if (temporary.item.valueName == "rec_contactsid") {
            form.find(v => v.label == '收货人电话').value = data.item.phonenumber;
        }
        form[temporary.index] = temporary.item;
        wx.navigateBack()
        this.setData({
            form
        })
    },
    submit() {
        let content = {
            ...this.data.content,
            ...this.selectComponent("#Form").submit()
        };
        content.begdate = content.invaliddate[0];
        content.enddate = content.invaliddate[1];
        content.sa_projectid = content.sa_projectid[1] ? content.sa_projectid[1][0] : 0;
        content.sys_enterpriseid = content.sys_enterpriseid[1] ? content.sys_enterpriseid[1][0] : 0;
        content.rec_contactsid = content.rec_contactsid[1] ? content.rec_contactsid[1][0] : 0;
        content.sa_receivableruleid = content.sa_receivableruleid[1] ? content.sa_receivableruleid[1][0] : 0;
        content.saler_hrid = content.saler_hrid[1] ? content.saler_hrid[1][0] : 0;
        content.discountrate = (content.discountrate / 100).toFixed(4) || 1;
        content.paytype = ""
        if (this.data.source) content.source = 1;
        delete(content.invaliddate)
        _Http.basic({
            "id": 20221121185302,
            content
        }).then(res => {
            console.log("添加经销项目合同", res)
            handleRes(res);
        })
    },
    // 是否显示全部
    onChange({
        detail
    }) {
        this.setData({
            showAll: detail
        })
    },
    /* 表单必填项是否完成 */
    onConfirm({
        detail
    }) {
        this.setData({
            disabled: detail
        })
    }
})