const _Http = getApp().globalData.http;
Page({
  data: {
    disabled: true,
    loading: false,
    list: [{
      label: "目的",
      key: "target",
      value: "",
    }, {
      label: "过程",
      key: "content",
      value: "",
    }, {
      label: "结果",
      key: "results",
      value: "",
    }, {
      label: "下次跟进计划",
      key: "nextplan",
      value: "",
    }]
  },
  changeState({
    detail
  }) {
    this.setData({
      loading: detail
    })
  },
  onLoad(options) {
    getApp().globalData.Language.getLanguagePackage(this, '跟进')
    let parems = JSON.parse(options.parems),
      form = [{
        label: "跟进类型",
        error: false,
        errMsg: "",
        type: "option",
        optionNmae: "followuptype",
        optionType: "radio", //复选   radio 单选
        value: "",
        placeholder: "选择跟进方式",
        valueName: "type",
        checking: "base",
        required: true
      }, {
        label: "跟进对象",
        error: false,
        errMsg: "",
        type: "route",
        url: "/packageA/select/linkman/select",
        value: "",
        placeholder: "选择跟进对象",
        valueName: "contactsid",
        checking: "base",
        required: true
      }];
    if (parems.ownertable == "sa_project") {
      form[1].params = {
        "id": "20221111131004",
        "content": {
          "nocache": true,
          "sa_projectid": parems.ownerid,
          "pageNumbe": 1,
          "pageTotal": 1,
          "total": null,
          "where": {
            "conditino": ""
          }
        }
      }
    } else if (parems.ownertable == "sys_phonebook") {
      let item = _Http.item;
      if (item == undefined) item = {
        sat_orderclue: [],
        sa_customers: [],
        sa_project: [],
      }
      let list = [{
        label: "关联线索",
        error: false,
        errMsg: "",
        type: "route",
        url: "/packageA/select/clue/select",
        value: item.sat_orderclue.length ? [item.sat_orderclue.map(v => v.enterprisename), item.dataextend.sat_orderclueids] : [],
        params: {
          "id": "2025042409073602",
          "content": {
            "nocache": true,
            "pageNumber": 1,
            "pageSize": 20,
            "pageTotal": 1,
            "sys_phonebookid": parems.ownerid,
            sys_datafollowupid: parems.sys_datafollowupid || 0,
            "where": {
              "conditino": ""
            }
          },
        },
        query: "&idname=sat_orderclueid&showName=name",
        placeholder: "关联线索",
        valueName: "sat_orderclueids",
        checking: "base",
        required: false
      }, {
        label: "关联客户",
        error: false,
        errMsg: "",
        type: "route",
        url: "/packageA/select/setclient/select",
        value: item.sa_customers.length ? [item.sa_customers.map(v => v.name), item.dataextend.sa_customersids] : [],
        params: {
          "id": "2025042409074102",
          "content": {
            "nocache": true,
            "pageNumber": 1,
            "pageSize": 20,
            "pageTotal": 1,
            "sys_phonebookid": parems.ownerid,
            sys_datafollowupid: parems.sys_datafollowupid || 0,
            "where": {
              "conditino": ""
            }
          },
        },
        query: "&idname=sa_customersid",
        placeholder: "关联客户",
        valueName: "sa_customersids",
        checking: "base",
        required: false
      }, {
        label: "关联项目",
        error: false,
        errMsg: "",
        type: "route",
        url: "/packageA/select/project/select",
        value: item.sa_project.length ? [item.sa_project.map(v => v.projectname), item.dataextend.sa_projectids] : "",
        params: {
          "id": "2025042409074702",
          "content": {
            "nocache": true,
            "pageNumber": 1,
            "pageSize": 20,
            "pageTotal": 1,
            "sys_phonebookid": parems.ownerid,
            sys_datafollowupid: parems.sys_datafollowupid || 0,
            "where": {
              "conditino": ""
            }
          },
        },
        query: "&idname=sa_projectid",
        placeholder: "选择关联项目",
        valueName: "sa_projectids",
        checking: "base",
        required: false,
        interrupt: false
      }]
      form.pop()
      form = list.concat(form)
      parems.dataextend = {
        contactsid: [parems.ownerid1]
      }
      delete _Http.item;
    } else {
      form[1].params = {
        "id": "20240531165504",
        "content": {
          "nacache": true,
          "pageNumber": 1,
          "pageSize": 10,
          "pageTotal": 1,
          "total": null,
          "where": {
            "condition": "",
          },
          "sys_enterpriseid": parems.ownerid1
        }
      }
    }
    if (parems.sys_datafollowupid != 0) {
      form[form.findIndex(v => v.optionNmae == "followuptype")].value = parems.type;
      if (parems.ownertable != "sys_phonebook") form[1].value = parems.contactsid
      if (parems.attinfos.length) this.selectComponent("#Yl_files").handleFiles(parems.attinfos);
      this.setData({
        list: this.data.list.map(v => {
          v.value = parems[v.key] || ""
          return v
        })
      })
    }
    this.setData({
      parems,
      form
    })
    this.selectComponent("#Form").confirm()
  },
  onInput(e) {
    const {
      index
    } = e.target.dataset;
    this.data.list[index].value = e.detail.value;
  },
  onVoiceInput(e) {
    let {
      index
    } = e.target.dataset;
    this.setData({
      [`list[${index}].value`]: e.detail
    })
  },
  /* 绑定媒体 */
  insertImgEdit({
    detail
  }) {
    this.handleFileLink(detail)
  },
  handleFileLink(attachmentids, ownertable = "temporary", ownerid = 1, data) {
    _Http.basic({
      "classname": "system.attachment.Attachment",
      "method": "createFileLink",
      "content": {
        ownertable,
        ownerid,
        usetype: 'default',
        attachmentids
      }
    }).then(res => {
      console.log('跟进记录绑定附件', res)
      if (res.code != '1') return wx.showToast({
        title: res.msg,
        icon: "none"
      })
      if (ownertable == 'temporary') {
        this.selectComponent("#Yl_files").handleFiles(res.data)
      } else {
        if (res.data.length) data.attinfos = res.data;

        _Http.basic({
          "classname": "system.attachment.Attachment",
          "method": "createFileLink",
          "content": {
            ownertable: this.data.parems.ownertable,
            ownerid: this.data.parems.ownerid,
            usetype: 'default',
            attachmentids
          }
        })
        this.changeItem(data)
        setTimeout(() => {
          wx.navigateBack()
        }, 500)
      }
    })
  },
  changeItem(data) {
    this.setData({
      loading: false
    })
    let page = getCurrentPages()[getCurrentPages().length - 2],
      isDetail = page.__route__ == 'pages/trace/detail';
    if (isDetail) {
      page.getDetail();
      page = getCurrentPages()[getCurrentPages().length - 3];
    }
    if (_Http.changeItem) {
      _Http.changeItem(data)
      delete(_Http.changeItem)
    } else {
      if (page) {
        let model = page.selectComponent("#Trace");
        if (model) model.getList(0, true)
      }
    }
  },
  submit() {
    this.setData({
      loading: true
    })
    let content = Object.assign(this.data.parems, this.selectComponent("#Form").submit())
    if (this.data.parems.ownertable != "sys_phonebook") {
      content.dataextend = {
        contactsid: content.contactsid[1]
      }
      delete(content.contactsid)
    } else {
      try {
        content.dataextend.sa_customersids = content.sa_customersids[1];
      } catch (error) {
        content.dataextend.sa_customersids = [];
      }
      try {
        content.dataextend.sa_projectids = content.sa_projectids[1];
      } catch (error) {
        content.dataextend.sa_projectids = [];
      }
      try {
        content.dataextend.sat_orderclueids = content.sat_orderclueids[1];
      } catch (error) {
        content.dataextend.sat_orderclueids = [];
      }
      delete content.sa_customersids
      delete content.sa_projectids
      delete content.sat_orderclueids
    }
    this.data.list.forEach(v => {
      content[v.key] = v.value
    })
    _Http.basic({
      "id": 20220930121601,
      content
    }).then(res => {
      console.log("保存跟进内容", res)
      wx.showToast({
        title: res.code != '1' ? res.msg : getApp().globalData.Language.getMapText(content.sys_datafollowupid == 0 ? '保存成功' : '修改成功'),
        icon: "none",
        mask: res.code == '1'
      });
      if (res.code != '1') return this.setData({
        loading: false
      });
      res.data.names = res.data.contacts.map(n => n.name)
      let attachmentids = this.selectComponent("#Yl_files").getFiles().attachmentids;
      if (attachmentids.length) return this.handleFileLink(attachmentids, 'sys_datafollowup', res.data.sys_datafollowupid, res.data);
      this.changeItem(res.data)
      if (this.data.parems.ownertable == "sys_phonebook") {
        _Http.updateList && _Http.updateList()
      }
      setTimeout(() => {
        wx.navigateBack()
      }, 500)
    })
  },
  onConfirm({
    detail
  }) {
    this.setData({
      disabled: detail
    })
  },
  onUnload() {
    if (this.data.parems.sys_datafollowupid == 0) this.selectComponent("#Yl_files").deleteAll()
    _Http.updateList && delete _Http.updateList
    _Http.item && delete _Http.item
  }
})