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

import {
    handleRes
} from "../submit";

Page({
    data: {
        loading: false,
        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": 18
                    }
                }
            },
            interrupt: true,
            query: "&radio=true&title=选择客户&idname=sys_enterpriseid",
            placeholder: "选择客户",
            valueName: "sys_enterpriseid",
            checking: "base",
            required: true
        }, {
            label: "业务员",
            error: false,
            errMsg: "",
            type: "route",
            value: "",
            placeholder: "选择业务员",
            disabled: true,
            valueName: "saler_hrid",
            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: "paytype",
            checking: "base",
            required: true
        }, {
            label: "发货日期提醒(天)",
            error: false,
            errMsg: "",
            type: "number",
            value: ['HY', 'YOSTEST1'].includes(wx.getStorageSync('userMsg').siteid) ? 7 : "",
            placeholder: "发货日期提醒(天)",
            valueName: "reminddays", //绑定的字段名称
            required: true //必填
        }, {
            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
    },
    async onLoad(options) {
        if (options.data) {
            let data = JSON.parse(options.data);
            try {
                data.paytype = [data.paytype, [0]]
            } catch (error) {}
            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
            })
            this.setData({
                disabled: false,
                form,
                "content.sa_contractid": data.sa_contractid
            })
        } else {
            _Http.basic({
                "id": "20230110100103",
                "version": 1,
                "content": {
                    "nocache": true,
                    "where": {
                        "condition": ""
                    },
                    "pageNumber": 1,
                    pageSize: "9999"
                },
            }).then(res => {
                console.log("付款条件", res)
                if (res.code == '1') {
                    let data = res.data.find(v => v.isdefault);
                    if (data) {
                        let index = this.data.form.findIndex(v => v.valueName == 'paytype')
                        if (index != -1) this.setData({
                            [`form[${index}].value`]: [data.ruleno + '-' + data.remarks, [data.sa_receivableruleid]]
                        })
                    }
                }
            })
        }
        getApp().globalData.Language.getLanguagePackage(this, '直销客户合作协议');
    },
    /* 打断 */
    interrupt(e) {
        let {
            data,
            form,
            temporary
        } = e.detail;
        form[temporary.index].value = data.value;
        if (temporary.item.label == '客户') {
            _Http.basic({
                "id": 20221012164302,
                "content": {
                    "sa_customersid": data.item.sa_customersid
                }
            }).then(res => {
                console.log("业务员", res)
                try {
                    let leader = res.data.leader[0];
                    form[2].value = [leader.name, [leader.hr.hrid]]
                } catch (error) {
                    form[2].value = ''
                }
                this.setData({
                    form
                })
                wx.navigateBack()
            })
        }
    },
    submit() {
        this.setData({
            loading: true
        })
        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.paytype = content.paytype[1] ? content.paytype[0] : "";
        content.saler_hrid = content.saler_hrid[1] ? content.saler_hrid[1][0] : 0;
        content.discountrate = (content.discountrate / 100).toFixed(4);
        delete(content.invaliddate)
        _Http.basic({
            "id": 20221121185302,
            content
        }).then(res => {
            this.setData({
                loading: false
            })
            console.log("添加直销客户合作协议", res)
            handleRes(res);
        })
    },
    // 是否显示全部
    onChange({
        detail
    }) {
        this.setData({
            showAll: detail
        })
    },
    /* 表单必填项是否完成 */
    onConfirm({
        detail
    }) {
        this.setData({
            disabled: detail
        })
    }
})