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": {
                    "where": {
                        "condition": "",
                        "type": 5,
                        typemx: "居间协议"
                    }
                }
            },
            interrupt: true,
            query: "&radio=true&idname=sys_enterpriseid&title=选择服务商",
            placeholder: "选择居间服务商",
            valueName: "sys_enterpriseid",
            checking: "base",
            required: true
        }, {
            label: "项目合同",
            error: false,
            errMsg: "",
            type: "route",
            url: "/packageA/select/contract/select",
            value: "",
            params: {
                "id": 20221217145402,
                "content": {
                    nocache: true,
                    "where": {
                        "condition": ""
                    }
                }
            },
            query: "&radio=true",
            interrupt: true,
            placeholder: "选择项目合同",
            valueName: "ascription_contractid",
            checking: "base",
            required: true
        }, {
            label: "项目名称",
            error: false,
            errMsg: "",
            type: "route",
            url: "/packageA/select/project/select",
            disabled: true,
            value: "",
            params: {
                "id": 20221201090802,
                "content": {
                    "type": 3, //1直销 2经销 3居间
                    nocache: true,
                    "where": {
                        "condition": "",
                    }
                }
            },
            placeholder: "项目名称",
            valueName: "sa_projectid",
            checking: "base",
            required: true
        }, {
            label: "计算方式",
            error: false,
            errMsg: "",
            hint: "",
            type: "radio",
            value: "1",
            interrupt: true,
            radioList: [{
                id: '1',
                name: '按订单金额比例计算'
            }, {
                id: '2',
                name: '按居间产品折扣计算'
            }],
            valueName: "radios", //绑定的字段名称
            required: false, //必填
            checking: `base`,
        }, {
            label: "订单金额比例",
            error: false,
            errMsg: "",
            hint: "请输入0-100%",
            type: "digit",
            value: "",
            placeholder: "请填写订单金额比例(%)",
            valueName: "orderratio", //绑定的字段名称
            required: true, //必填
            checking: `^(100|\\d{0,2})(\\.\\d{1,2})?$`, //0-100%
            slot: "discountrate"
        }, {
            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": "居间",
            sys_enterpriseid: 0
        },
        disabled: true
    },
    onLoad(options) {
        if (options.data) {
            let data = JSON.parse(options.data);
            let form = this.data.form.map(v => {
                if (v.valueName == "invaliddate") v.start = "";
                v.value = data[v.valueName];
                return v
            });
            if (data.radios == 1) {
                form[5].label = '订单金额比例'
                form[5].placeholder = '请填写订单金额比例'
                form[5].valueName = 'orderratio'
                form[5].value = data.orderratio == 1 ? 100 : (data.orderratio * 100).toFixed(2)
            } else {
                form[5].label = '居间产品折扣'
                form[5].placeholder = '请填写居间产品折扣'
                form[5].valueName = 'productdiscount'
                form[5].value = data.productdiscount == 1 ? 100 : (data.productdiscount * 100).toFixed(2)
            }
            form.splice(2, 0, {
                label: "业务员",
                error: false,
                errMsg: "",
                type: "route",
                url: "/packageA/select/contacts/select",
                value: [data.leader[0].name, [data.saler_hrid]],
                params: {
                    "id": 20230305201202,
                    "content": {
                        nocache: true,
                        "type": "1",
                        "sys_enterpriseid": data.sys_enterpriseid[1][0]
                    },
                },
                query: "&radio=true&idname=hrid",
                placeholder: "选择业务员",
                valueName: "saler_hrid",
                checking: "base",
                required: true
            })
            this.setData({
                disabled: false,
                form,
                "content.sa_contractid": data.sa_contractid
            })
        }
        //项目进入 修改经销商选择范围
        if (options.project) {
            let form = this.data.form,
                project = JSON.parse(options.project);
            form[2].params = {
                "id": 20221217145402,
                "content": {
                    "nocache": true,
                    "pageNumber": 1,
                    "pageSize": 20,
                    "where": {
                        "condition": "",
                        "sa_projectid": project.sa_projectid
                    }
                }
            };
            form[3].value = [project.projectname, [project.sa_projectid]]
            source = 1;
            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 == '计算方式') {
            let index = form.findIndex(item => item.valueName == 'radios')
            let i = index + 1
            if (data.value == 1) {
                form[i].label = '订单金额比例'
                form[i].placeholder = '请填写订单金额比例'
                form[i].valueName = 'orderratio'
            } else if (data.value == 2) {
                form[i].label = '居间产品折扣'
                form[i].placeholder = '请填写居间产品折扣'
                form[i].valueName = 'productdiscount'
            }

        } else if (temporary.item.label == '项目合同') {
            let index = form.findIndex(v => v.label == '项目名称');
            if (index != -1 && source == 0) form[index].value = [data.item.projectname, [data.item.sa_projectid]]
            form[temporary.index].value = data.value;
            wx.navigateBack()

        } else if (temporary.item.label == '居间服务商') {
            form.splice(2, form[2].label == '业务员' ? 1 : 0, {
                label: "业务员",
                error: false,
                errMsg: "",
                type: "route",
                url: "/packageA/select/contacts/select",
                value: "",
                params: {
                    "id": 20230305201202,
                    "content": {
                        nocache: true,
                        "type": "1",
                        "sys_enterpriseid": data.id[0]
                    },
                },
                query: "&radio=true&idname=hrid",
                placeholder: "选择业务员",
                valueName: "saler_hrid",
                checking: "base",
                required: true
            })
            wx.navigateBack()
        }
        this.setData({
            form
        })
    },
    submit() {
        let content = {
            ...this.data.content,
            ...this.selectComponent("#Form").submit()
        };
        content.calculatemodel = content.radios
        if (content.calculatemodel == 1) {
            content.orderratio = (content.orderratio / 100).toFixed(4)
        } else {
            content.productdiscount = (content.productdiscount / 100).toFixed(4)
        }
        content.begdate = content.invaliddate[0];
        content.enddate = content.invaliddate[1];
        content.ascription_contractid = content.ascription_contractid[1] ? content.ascription_contractid[1][0] : 0;
        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.saler_hrid = content.saler_hrid[1] ? content.saler_hrid[1][0] : 0;
        content.paytype = ""
        if (this.data.source) content.source = 1;
        console.log(content)
        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
        })
    }
})