const _Http = getApp().globalData.http,
  getTime = require("../../utils/getTime");
let source = 0,
  sa_quotedpriceid = 0,
  quotedpricenotes = [],
  address = "";
Page({
  data: {
    showAll: false,
    form: [{
      label: "项目",
      error: false,
      errMsg: "",
      type: "route",
      url: "/packageA/select/project/select",
      value: "",
      params: {
        "id": 20221020143503,
        content: {
          nocache: true,
          "type": 1,
          "where": {
            "condition": "",
          }
        },
      },
      interrupt: true,
      query: "&radio=true",
      placeholder: "选择项目",
      valueName: "sa_projectid",
      checking: "base",
      required: true
    }, {
      label: "产品系列",
      error: false,
      errMsg: "",
      type: "option",
      optionNmae: "itemtype",
      optionType: "checkbox", //复选   radio 单选
      value: "",
      placeholder: "请选择产品系列",
      valueName: "itemtype",
      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: "date",
      value: getTime.formatTime(new Date(), '-').split(' ')[0],
      placeholder: "报价日期",
      valueName: "billdate",
      checking: "base",
      required: false
    }, {
      label: "有效期",
      error: false,
      errMsg: "",
      type: "dateRange",
      value: [getTime.formatTime(new Date(), '-').split(' ')[0], ""],
      placeholder: ['生效日期', '截止日期'],
      valueName: "invaliddate",
      checking: "base",
      required: true
    }, {
      label: "备注",
      error: false,
      errMsg: "",
      type: "textarea",
      value: "",
      placeholder: "报价单说明",
      valueName: "remarks",
      required: false, //必填
    }],
    disabled: true
  },
  onLoad(options) {
    let form = this.data.form;
    /* 项目商机进入 */
    if (options.sa_projectid) {
      let data = JSON.parse(options.sa_projectid);
      if (data.sa_projectid) {
        form[0].value = data.sa_projectid;
        form[0].disabled = true;
        source = 1;
        form.splice(1, 0, {
          label: "选择客户",
          error: false,
          errMsg: "",
          type: "route",
          url: "/packageA/select/setclient/select",
          value: "",
          params: {
            id: 20221027143702,
            "content": {
              nocache: true,
              "sa_projectid": data.sa_projectid[1][0],
              "where": {
                "condition": ""
              }
            },
          },
          query: "&radio=true&idname=sys_enterpriseid",
          placeholder: "选择客户",
          valueName: "sys_enterpriseid",
          checking: "base",
          required: true
        });
        //业务员
        form.splice(6, 0, {
          label: "业务员",
          error: false,
          errMsg: "",
          type: "route",
          url: "/packageA/select/contacts/select",
          value: "",
          params: {
            "id": "20230306144402",
            "content": {
              nocache: true,
              "type": 2, //type:1客户2:项目
              "sa_projectid": data.sa_projectid[1][0],
              "where": {
                "condition": ""
              }
            }
          },
          query: "&radio=true&idname=hrid",
          placeholder: "选择业务员",
          valueName: "saler_hrid",
          checking: "base",
          required: true
        });
      };
      this.setData({
        disabled: false,
        form
      });
    };
    if (options.data) {
      let data1 = JSON.parse(options.data);
      sa_quotedpriceid = data1.sa_quotedpriceid;
      quotedpricenotes = data1.quotedpricenotes;
      address = data1.address;
      data1.sa_projectid = [data1.projectname, [data1.sa_projectid]]
      data1.discountrate = data1.discountrate == 1 ? 100 : (data1.discountrate * 100).toFixed(2)
      form = form.map(v => {
        if (v.valueName == "invaliddate") {
          v.value = [data1.begdate, data1.enddate]
        } else {
          v.value = data1[v.valueName] || "";
        }
        return v
      })
      form.splice(1, 0, {
        label: "选择客户",
        error: false,
        errMsg: "",
        type: "route",
        url: "/packageA/select/setclient/select",
        value: [data1.enterprisename, [data1.sys_enterpriseid]],
        params: {
          id: 20221027143702,
          "content": {
            nocache: true,
            "sa_projectid": data1.sa_projectid[1][0],
            "where": {
              "condition": ""
            }
          },
        },
        interrupt: true,
        query: "&radio=true&idname=sys_enterpriseid",
        placeholder: "选择客户",
        valueName: "sys_enterpriseid",
        checking: "base",
        required: true
      });
      form.splice(2, 0, {
        label: "联系人",
        error: false,
        errMsg: "",
        type: "route",
        url: "/packageA/select/contacts/select",
        value: [data1.contactsname, [data1.contactsid]],
        params: {
          "id": "20221022165503",
          "content": {
            nocache: true,
            "sys_enterpriseid": data1.sys_enterpriseid,
            pageNumber: 1,
            pageSize: 10,
            pageTotal: 1,
            "where": {
              "condition": "",
              "workaddress": 0
            }
          }
        },
        query: "&radio=true",
        placeholder: "选择客户联系人",
        valueName: "contactsid",
        checking: "base",
        required: false
      });
      form.splice(6, 0, {
        label: "业务员",
        error: false,
        errMsg: "",
        type: "route",
        url: "/packageA/select/contacts/select",
        value: [data1.name, [data1.saler_hrid]],
        params: {
          "id": "20230306144402",
          "content": {
            nocache: true,
            "type": 2, //type:1客户2:项目
            "sa_projectid": data1.sa_projectid[1][0],
            "where": {
              "condition": ""
            }
          }
        },
        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)
    if (temporary.item.value[0] == data.value[0]) return wx.navigateBack();
    let obj = {};
    temporary.item.value = data.value;
    switch (temporary.item.valueName) {
      case "sa_projectid":
        obj = {
          label: "选择客户",
          error: false,
          errMsg: "",
          type: "route",
          url: "/packageA/select/setclient/select",
          value: "",
          params: {
            id: 20221027143702,
            "content": {
              nocache: true,
              "sa_projectid": data.value[1][0],
              "where": {
                "condition": ""
              }
            },
          },
          interrupt: true,
          query: "&radio=true&idname=sys_enterpriseid",
          placeholder: "选择客户",
          valueName: "sys_enterpriseid",
          checking: "base",
          required: true
        };
        form.splice(1, form[1].label == "选择客户" ? 1 : 0, obj);
        address = data.item.province + data.item.city + data.item.county + data.item.address
        //业务员
        let hr = {
          label: "业务员",
          error: false,
          errMsg: "",
          type: "route",
          url: "/packageA/select/contacts/select",
          value: "",
          params: {
            "id": "20230306144402",
            "content": {
              nocache: true,
              "type": 2, //type:1客户2:项目
              "sa_projectid": data.value[1][0],
              "where": {
                "condition": ""
              }
            }
          },
          query: "&radio=true&idname=hrid",
          placeholder: "选择业务员",
          valueName: "saler_hrid",
          checking: "base",
          required: true
        };
        form.splice(6, form[6].label == "业务员" ? 1 : 0, hr);
        break;
      case "sys_enterpriseid":
        obj = {
          label: "联系人",
          error: false,
          errMsg: "",
          type: "route",
          url: "/packageA/select/contacts/select",
          value: "",
          params: {
            "id": "20221022165503",
            "content": {
              nocache: true,
              "sys_enterpriseid": data.value[1][0],
              pageNumber: 1,
              pageSize: 10,
              pageTotal: 1,
              "where": {
                "condition": "",
                "workaddress": 0
              }
            }
          },
          query: "&radio=true",
          placeholder: "选择客户联系人",
          valueName: "contactsid",
          checking: "base",
          required: false
        };
        form.splice(2, form[2].label == "联系人" ? 1 : 0, obj);
        break;
    }
    form[temporary.index] = temporary.item;
    wx.navigateBack()
    this.setData({
      form
    })
  },
  submit() {
    let content = {
      ...this.selectComponent("#Form").submit(),
      sa_quotedpriceid,
      quotedpricenotes,
      address,
      source
    };
    content.sys_enterpriseid = content.sys_enterpriseid[1] ? content.sys_enterpriseid[1][0] : 0;
    content.projectname = content.sa_projectid[0] || "";
    content.sa_projectid = content.sa_projectid[1] ? content.sa_projectid[1][0] : 0;
    content.contactsid = content.contactsid ? content.contactsid[1][0] : 0;
    content.discountrate = (content.discountrate / 100).toFixed(4) || 1;
    content.saler_hrid = content.saler_hrid[1] ? content.saler_hrid[1][0] : 0;
    content.begdate = content.invaliddate[0];
    content.enddate = content.invaliddate[1];
    delete(content.invaliddate)
    _Http.basic({
      "id": 20221020164803,
      "version": 1,
      content
    }).then(res => {
      console.log("添加项目报价单", res)
      wx.showToast({
        title: res.msg != '成功' ? res.msg : '保存成功',
        icon: "none"
      })
      if (res.msg != '成功') return;
      setTimeout(() => {
        let pages = getCurrentPages(),
          page = pages[pages.length - 2];
        if (page.__route__ == 'packageA/offers/detail') {
          page.getDetail(true);
          wx.navigateBack()
        } else {
          if (page.__route__ == 'packageA/offers/index') {
            page.getList(true);
          } else if (page.__route__ == 'packageA/project/detail') {
            let model = page.selectComponent("#Offers");
            model.getList(model.data.sa_projectid, true);
          }
          wx.redirectTo({
            url: '/packageA/offers/detail?sa_quotedpriceid=' + res.data.sa_quotedpriceid
          })
        }
      }, 300)
    })
  },
  // 是否显示全部
  onChange({
    detail
  }) {
    this.setData({
      showAll: detail
    })
  },
  /* 表单必填项是否完成 */
  onConfirm({
    detail
  }) {
    this.setData({
      disabled: detail
    })
  }
})