const _Http = getApp().globalData.http,
    getTime = require("../../../../utils/getTime");

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": "框架"
        },
        disabled: true
    },
    onLoad(options) {
        if (options.data) {
            let data = JSON.parse(options.data);
            data.discountrate = data.discountrate == 1 ? 100 : (data.discountrate * 100).toFixed(2);
            let form = this.data.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.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
            })
        }
    },
    /* 打断 */
    interrupt(e) {
        let {
            data,
            form,
            temporary
        } = e.detail;
        form[temporary.index].value = data.value;
        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.begdate = content.invaliddate[0];
        content.enddate = content.invaliddate[1];
        content.sys_enterpriseid = content.sys_enterpriseid[1] ? content.sys_enterpriseid[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);
        content.paytype = ""
        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
        })
    }
})