const _Http = getApp().globalData.http,
  getTime = require("../../utils/getTime");
let sa_quotedpriceid = 0,
  quotedpricenotes = [],
  address = "";

Page({
  data: {
    showAll: false,
    form: [{
      label: "客户",
      error: false,
      errMsg: "",
      type: "route",
      url: "/packageA/select/setclient/select",
      value: "",
      params: {
        "id": 20220920083901,
        "content": {
          "pageNumber": 1,
          "pageSize": 20,
          "where": {
            "condition": "",
            "type": 7,
            "sa_projectid": ""
          }
        },
      },
      interrupt: true,
      query: "&radio=true&idname=sys_enterpriseid",
      placeholder: "选择客户",
      valueName: "sys_enterpriseid",
      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) {
    if (options.data) {
      let data = JSON.parse(options.data);
      let form = this.data.form;

      sa_quotedpriceid = data.sa_quotedpriceid || 0;
      quotedpricenotes = data.quotedpricenotes || [];
      address = data.address || "";

      data.discountrate = data.discountrate == 1 ? 100 : (data.discountrate * 100).toFixed(2);
      data.sys_enterpriseid = [data.enterprisename, [data.sys_enterpriseid]];
      form = form.map(v => {
        if (v.valueName == "invaliddate") {
          v.value = [data.begdate, data.enddate]
        } else {
          v.value = data[v.valueName] || "";
        }
        return v
      })
      form.splice(1, 0, {
        label: "联系人",
        error: false,
        errMsg: "",
        type: "route",
        url: "/packageA/select/contacts/select",
        value: [data.contactsname, [data.contactsid]],
        params: {
          "id": "20221022165503",
          "version": 1,
          "content": {
            nocache: true,
            "sys_enterpriseid": data.sys_enterpriseid[1][0],
            "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: [data.name, [data.saler_hrid]],
        params: {
          "id": "20230306144402",
          "content": {
            nocache: true,
            "type": 1, //type:1客户2:项目
            "sys_enterpriseid": data.sys_enterpriseid[1][0],
            "where": {
              "condition": ""
            }
          }
        },
        query: "&radio=true&idname=hrid",
        placeholder: "选择业务员",
        valueName: "saler_hrid",
        checking: "base",
        required: true
      });
      this.setData({
        form
      });
      this.selectComponent("#Form").confirm();
    }
  },
  /* 打断处理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();
    temporary.item.value = data.value;

    if (temporary.item.valueName == "sys_enterpriseid") {
      //联系人
      let contacts = {
        label: "联系人",
        error: false,
        errMsg: "",
        type: "route",
        url: "/packageA/select/contacts/select",
        value: "",
        params: {
          "id": "20221022165503",
          "version": 1,
          "content": {
            nocache: true,
            "sys_enterpriseid": data.value[1][0],
            "where": {
              "condition": "",
              "workaddress": 0
            }
          }
        },
        query: "&radio=true",
        placeholder: "选择客户联系人",
        valueName: "contactsid",
        checking: "base",
        required: false
      };
      form.splice(1, form[1].label == "联系人" ? 1 : 0, contacts);

      //业务员
      let hr = {
        label: "业务员",
        error: false,
        errMsg: "",
        type: "route",
        url: "/packageA/select/contacts/select",
        value: "",
        params: {
          "id": "20230306144402",
          "content": {
            nocache: true,
            "type": 1, //type:1客户2:项目
            "sys_enterpriseid": 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);
    }
    form[temporary.index] = temporary.item;
    wx.navigateBack()
    this.setData({
      form
    })
  },
  submit() {
    let content = {
      ...this.selectComponent("#Form").submit(),
      "sa_projectid": 0, //项目id
      projectname: "",
      sa_quotedpriceid,
      quotedpricenotes,
      address
    };
    content.sys_enterpriseid = content.sys_enterpriseid[1] ? content.sys_enterpriseid[1][0] : 0;
    content.contactsid = content.contactsid[1] ? content.contactsid[1][0] : 0;
    content.saler_hrid = content.saler_hrid[1] ? content.saler_hrid[1][0] : 0;
    content.discountrate = (content.discountrate / 100).toFixed(4) || 1;
    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 (['packageA/setclient/detail', 'packageA/publicCustomer/detail'].includes(page.__route__)) {
            let model = page.selectComponent("#Offers");
            model.getList(model.data.sys_enterpriseid, 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
    })
  }
})