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/contract/add/setclient/select",
            value: "",
            params: {
                "id": 20220920083901,
                "content": {
                    nocache: true,
                    "where": {
                        "condition": "",
                        "type": 7,
                        typemx: "工具使用(借用)协议"
                    }
                }
            },
            interrupt: true,
            query: "&radio=true&idname=sys_enterpriseid",
            placeholder: "选择企业",
            valueName: "sys_enterpriseid",
            checking: "base",
            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": "工具借用",
            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
            });
            form.splice(2, form[2].label == '业务员' ? 1 : 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
            })
        }
    },
    /* 打断处理form */
    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.saler_hrid = content.saler_hrid[1] ? content.saler_hrid[1][0] : 0;
        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
        })
    }
})