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": 5,
                        typemx: "经销项目协议"
                    }
                }
            },
            interrupt: true,
            query: "&radio=true&title=选择经销商&idname=sys_enterpriseid",
            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);
            console.log(data)
            project = data;

            form = form.map(v => {
                if (v.valueName == "invaliddate") v.start = "";
                v.value = data[v.valueName] || "";
                return v
            });
            this.data.form.splice(2, 0, {
                label: "项目",
                error: false,
                errMsg: "",
                type: "route",
                url: "/packageA/select/project/select",
                value: data.project,
                params: {
                    "id": 20221201090802,
                    "content": {
                        nocache: true,
                        sys_enterpriseid: data.sys_enterpriseid[1][0],
                        type: 2, //1直销 2经销 3居间
                        where: {
                            condition: "",
                        }
                    }
                },
                query: "&radio=true",
                placeholder: "选择项目",
                valueName: "sa_projectid",
                checking: "base",
                required: true
            })


            this.setData({
                disabled: false,
                "content.sa_contractid": data.sa_contractid
            })
        };



        //项目进入 修改经销商选择范围

        if (options.project) {
            source = 1;
            project = JSON.parse(options.project);
            form[1].params = {
                "id": 20220920083901,
                "content": {
                    nocache: true,
                    "pageNumber": 1,
                    "pageSize": 20,
                    "where": {
                        "condition": "",
                        "type": "14",
                        "sa_projectid": project.sa_projectid
                    }
                }
            };
        }
        if (project) {
            form.splice(2, 0, {
                label: "项目",
                error: false,
                errMsg: "",
                type: "route",
                url: "/packageA/select/project/select",
                value: source == 1 ? [project.projectname, [project.sa_projectid]] : project.sa_projectid,
                params: {
                    "id": 20221201090802,
                    "content": {
                        nocache: true,
                        sys_enterpriseid: source == 1 ? 0 : project.sys_enterpriseid[1][0],
                        "type": 2, //1直销 2经销 3居间
                        "where": {
                            "condition": "",
                        }
                    }
                },
                disabled: source == 1,
                query: "&radio=true",
                placeholder: "选择项目",
                valueName: "sa_projectid",
                checking: "base",
                required: true
            });
            form.splice(3, 0, {
                label: "业务员",
                error: false,
                errMsg: "",
                type: "route",
                url: "/packageA/select/contacts/select",
                value: source == 1 ? "" : [project.leader[0].name, [project.saler_hrid]],
                params: {
                    "id": 20230305201202,
                    "content": {
                        nocache: true,
                        "type": "2",
                        "sa_projectid": source == 1 ? project.sa_projectid : project.sa_projectid[1][0]
                    },
                },
                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;
        console.log("处理", data, form, temporary)
        form[temporary.index].value = data.value;
        if (temporary.item.label == '经销商' && source == 0) {
            form.splice(2, form[2].label == '项目' ? 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": 2, //1直销 2经销 3居间
                        "where": {
                            "condition": "",
                        }
                    }
                },
                interrupt: true,
                query: "&radio=true",
                placeholder: "选择项目",
                valueName: "sa_projectid",
                checking: "base",
                required: true
            });
        } else if (temporary.item.label == '项目') {
            form.splice(3, form[3].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
            })
        }
        this.setData({
            form
        });
        wx.navigateBack();
    },
    submit() {
        let content = {
            ...this.data.content,
            ...this.selectComponent("#Form").submit()
        };
        content.begdate = content.invaliddate[0];
        content.enddate = content.invaliddate[1];

        content.sys_enterpriseid = content.sys_enterpriseid[1] ? content.sys_enterpriseid[1][0] : 0;
        content.sa_projectid = content.sa_projectid[1] ? content.sa_projectid[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 = "";
        content.source = source;
        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
        })
    }
})