const _Http = getApp().globalData.http,
  currency = require("../../utils/currency"),
  CNY = value => currency(value, {
    symbol: "￥",
    precision: 2
  }).format();

Component({
  properties: {
    ownertable: {
      type: String
    },
    ownerid: {
      type: String
    },
    ownerid1: {
      type: String
    },
    resource: {
      type: String
    },
    disabled: {
      type: Boolean,
      value: true
    },
    isAdmin: {
      type: Boolean
    }
  },
  options: {
    addGlobalClass: true
  },
  data: {
    content: {
      nocache: true,
      pageNumber: 1,
      pageSize: 10,
      pageTotal: 1,
      total: null,
      where: {
        condition: ""
      }
    },
    list: [],
    showSearch: false,
    focus: false,
    condition: ""
  },
  lifetimes: {
    attached: function () {
      this.setData({
        userid: wx.getStorageSync('userMsg').userid,
      })
      getApp().globalData.Language.getLanguagePackage(this)
    }
  },
  methods: {
    toSearch() {
      if (this.data.showSearch && this.data.content.where.condition) {
        this.data.content.where.condition = '';
        this.getList("", true);
      } else if (this.data.condition) {
        this.data.content.where.condition = this.data.condition;
        this.setData({
          condition: this.data.condition
        })
        this.getList("", true);
      }
      this.setData({
        showSearch: !this.data.showSearch
      })
      setTimeout(() => {
        this.setData({
          focus: this.data.showSearch
        })
      }, 300)
    },
    onChange({
      detail
    }) {
      this.data.condition = detail;
    },
    onSearch({
      detail
    }) {
      this.data.content.where.condition = detail;
      this.getList("", true)
    },
    getList(id, init = false) {
      let content = {
        ...this.data.content,
        "ownertable": this.data.ownertable,
        "ownerid": this.data.ownerid,
      };
      if (init) {
        content.pageNumber = 1
        content.pageTotal = 1
      }
      _Http.basic({
        "id": 20220930121501,
        content
      }).then(res => {
        console.log("跟进动态", res)
        if (res.code != '1') return wx.showToast({
          title: res.data,
          icon: "none"
        });
        let list = res.data.map(v => {
          try {
            v.names = v.contacts.map(n => n.name)
          } catch (error) {
            v.names = null
          }

          v.showsalesfeesamount = CNY(v.salesfeesamount || 0)
          return v
        })
        this.setData({
          "content.pageNumber": res.pageNumber + 1,
          "content.pageTotal": res.pageTotal,
          "content.total": res.total,
          list: res.pageNumber == 1 ? list : this.data.list.concat(list)
        })
      })
    },
    editItem(e) {
      let item = null;
      try {
        item = e.currentTarget.dataset.item;
      } catch (error) {
        item = e
      }
      let parems = {
        ownertable: this.data.ownertable,
        ownerid: this.data.ownerid,
        ownerid1: this.data.ownerid1,
        resource: this.data.resource,
        sys_datafollowupid: item.sys_datafollowupid,
        content: item.content,
        target: item.target,
        results: item.results,
        nextplan: item.nextplan,
        contactsid: item.names ? [item.names, item.dataextend.contactsid] : "",
        type: item.type,
        attinfos: item.attinfos
      }
      _Http.item = item;
      wx.navigateTo({
        url: '/pages/trace/insert?parems=' + JSON.stringify(parems),
      })
      _Http.changeItem = this.changeItem.bind(this)
    },
    changeItem(item) {
      let content = {
        ...this.data.content,
        "ownertable": this.data.ownertable,
        "ownerid": this.data.ownerid,
      };
      content.pageSize = content.pageSize * (content.pageNumber - 1);
      content.pageNumber = 1;

      _Http.basic({
        "id": 20220930121501,
        content
      }).then(res => {
        console.log("跟进动态", res)
        if (res.msg != '成功') return wx.showToast({
          title: res.data,
          icon: "none"
        });
        let list = res.data.map(v => {
          try {
            v.names = v.contacts.map(n => n.name)
          } catch (error) {
            v.names = null
          }

          v.showsalesfeesamount = CNY(v.salesfeesamount || 0)
          return v
        })
        this.setData({
          list
        })
      })
    },
    expenseBreakdown(e) {
      const {
        item,
        index
      } = e.currentTarget.dataset;
      wx.navigateTo({
        url: `/packageA/expenseBreakdown/index?ownertable=${item.ownertable}&ownerid=${item.sys_datafollowupid}&isAdmin=${this.data.isAdmin}&item=${
          JSON.stringify(item)
        }`,
      })
      _Http.updateEBList = function (salesfeesamount, showsalesfeesamount) {
        this.setData({
          [`list[${index}].salesfeesamount`]: salesfeesamount,
          [`list[${index}].showsalesfeesamount`]: showsalesfeesamount,
        })
      }.bind(this)
    },
    insetr() {
      let parems = {
        ownertable: this.data.ownertable,
        ownerid: this.data.ownerid,
        ownerid1: this.data.ownerid1,
        resource: this.data.resource,
        sys_datafollowupid: 0,
        content: ""
      }
      wx.navigateTo({
        url: '/pages/trace/insert?parems=' + JSON.stringify(parems),
      })
    },
    toDetail(e) {
      const {
        item
      } = e.currentTarget.dataset;
      wx.navigateTo({
        url: `/pages/trace/detail?data=` + JSON.stringify({
          "sys_datafollowupid": item.sys_datafollowupid,
          "ownertable": this.data.ownertable,
          "ownerid": this.data.ownerid
        }),
      });
      _Http.changeItem = this.changeItem.bind(this);
      _Http.editItem = this.editItem.bind(this);
      _Http.handleDelete = this.handleDelete.bind(this);
    },
    deleteItem(e) {
      this.handleDelete(e.currentTarget.dataset.item)
    },

    handleDelete(item) {
      let that = this;
      return new Promise((resolve) => {
        wx.showModal({
          title: getApp().globalData.Language.getMapText('提示'),
          content: getApp().globalData.Language.getMapText('是否确定删除该跟进动态'),
          complete: ({
            confirm
          }) => {
            if (confirm) {
              _Http.basic({
                "id": 20220930121701,
                "content": {
                  "sys_datafollowupid": item.sys_datafollowupid,
                  "ownertable": that.data.ownertable,
                  "ownerid": that.data.ownerid,
                  deletereason: ""
                }
              }).then(res => {
                console.log("删除", res);
                wx.showToast({
                  title: res.code != 1 ? getApp().globalData.Language.getMapText(res.msg) : getApp().globalData.Language.getMapText("删除成功"),
                  icon: "none"
                });
                resolve(res.code)
                if (res.code != 1) return;
                that.setData({
                  list: that.data.list.filter(v => v.sys_datafollowupid != item.sys_datafollowupid),
                  'content.total': that.data.content.total - 1
                })
              })
            } else {
              resolve(0)
            }
          }
        })
      })
    },
    changeTotal() {
      this.setData({
        "content.total": this.data.content.total - 1
      })
    },
    comment(e) {
      let page = this.selectComponent('#' + e.currentTarget.id);
      page.changeShow()
    },
    updateCommentList({
      detail
    }) {
      detail.pageSize = 99999;
      _Http.basic({
        "id": 20240920092204,
        content: detail
      }).then(res => {
        console.log("更新评论列表", res)
        if (res.code == 1) {
          let idName = detail.ownertable + 'id';
          const index = this.data.list.findIndex(v => v[idName] == detail.ownerid)
          if (index != -1) {
            let item = this.data.list[index];
            item.commentqty = res.total == 0 ? 0 : res.data[0].totalqty;
            item.comment = res.data;
            this.setData({
              [`list[${index}]`]: item
            })
          }
        }
      })
    }
  }
})