const _Http = getApp().globalData.http;
Component({
  options: {
    addGlobalClass: true
  },
  properties: {
    list: Array,
    changeTotal: Function
  },
  data: {
    radio: 0,
    detail: {
      item: {},
      list: [],
      show: false
    },
    tabbarList: [{
      icon: "icon-bodadianhua",
      label: "呼叫",
      alias: "call"
    }, {
      icon: "icon-a-yingxiaowuliaofuzhi",
      label: "复制",
      alias: "copy"
    }, {
      icon: "icon-bianji",
      label: "编辑",
      alias: "edit"
    }, {
      icon: "icon-shanchu",
      label: "删除",
      alias: "delete"
    }]
  },
  methods: {
    //详情单元格单击复制
    clickItem({
      detail
    }) {
      wx.setClipboardData({
        data: detail.value,
        success: () => {
          wx.hideToast();
          wx.showToast({
            title: `已复制${detail.label}`,
            icon: "none"
          })
        }
      })
    },
    //打开详情
    onClick(e) {
      const {
        item
      } = e.currentTarget.dataset;
      this.setData({
        "detail.show": true,
        "detail.list": [{
          label: "名称",
          value: item.enterprisename
        }, {
          label: "纳税人识别号",
          value: item.taxno
        }, {
          label: "地址",
          value: item.address
        }, {
          label: "开户行",
          value: item.bank
        }, {
          label: "账号",
          value: item.bankcardno
        }, {
          label: "联系人号码",
          value: item.phonenumber
        }],
        "detail.item": item
      })
    },
    //关闭详情弹窗
    onClose() {
      this.setData({
        detail: {
          item: {},
          list: [],
          show: false
        }
      })
    },
    /* 处理快捷小按钮 */
    handleItem(e) {
      const {
        name,
        item
      } = e.target.dataset,
        that = this;
      if (!name) return;
      switch (name) {
        case "call":
          wx.makePhoneCall({
            phoneNumber: item.phonenumber,
          })
          break;
        case "copy":
          wx.setClipboardData({
            data: `名称：${item.enterprisename}\n纳税人识别号：${item.taxno}\n地址：${item.address}\n开户行：${item.bank}\n账号：${item.bankcardno}`,
          })
          break;
        case "delete":
          wx.showModal({
            title: '提示',
            content: '是否确认删除',
            complete: (res) => {
              if (res.confirm) {
                _Http.basic({
                  "id": 20221013160502,
                  "content": {
                    "sys_enterprise_financeids": [item.sys_enterprise_financeid]
                  },
                }).then(res => {
                  if (res.msg != '成功') return wx.showToast({
                    title: res.data,
                    icon: "none"
                  });
                  that.setData({
                    list: that.data.list.filter(v => v.sys_enterprise_financeid != item.sys_enterprise_financeid)
                  });
                  that.triggerEvent("changeTotal");
                  that.onClose();
                  wx.showToast({
                    title: '删除成功',
                    icon: "none"
                  });
                })
              }
            }
          })
          break;
        case "edit":
          wx.navigateTo({
            url: '/packageA/setclient/modules/financing/add/index?data=' + JSON.stringify(item),
          });
          that.onClose();
          break;
      }
    },
    /* 查询默认项目 */
    queryDefault() {
      const item = this.data.list.find(v => v.isdefault == 1);
      this.setData({
        radio: item.sys_enterprise_financeid
      })
    },
    /* 修改默认项 */
    onChange({
      detail
    }) {
      _Http.basic({
        "id": 20221013160702,
        "content": {
          "sys_enterprise_financeid": detail
        },
      }).then(res => {
        if (res.msg != '成功') return wx.showToast({
          title: res.data,
          icon: "none"
        });
        this.setData({
          list: this.data.list.map(v => {
            v.isdefault = v.sys_enterprise_financeid == detail ? 1 : 0
            return v
          }),
          radio: detail
        })
      })
    },
    //详情按钮回调
    tabbarOnClick({
      detail
    }) {
      this.handleItem({
        target: {
          dataset: {
            name: detail.alias,
            item: this.data.detail.item
          }
        }
      })
    }
  }
})