import api from '../api/api';
const _Http = getApp().globalData.http;
let deleteUsers = [];
Page({
  data: {
    stopClick: '',
    SignName: true,
    timerid: null,
    act_confirm_value: '是',
    form: {
      "sa_workorderid": 0,
      "sa_workorder_nodeid": 0,
      "remarks": "",
      "textcontent": "",
      "confirm_value": "",
      "amount": 0,
      "isconfirm": 0,
      "team": [],
      "trainers": [],
      "traintitem": [],
    },
    bindNormalData: {
      ownertable: 'sa_workorder_node',
      ownerid: 0,
      usetype: 'default'
    },
    bindContractData: {
      "ownertable": "sa_workorder_node",
      "ownerid": 0,
      "usetype": "contract",
    },
    bindSignNameData: {
      "ownertable": "sa_workorder_node",
      "ownerid": 0,
      "usetype": "signature",
    }
  },
  async nodeDetail() {
    const res = await api._post({
      "id": "20230209091103",
      "version": 1,
      "content": {
        nocache: true,
        "sa_workorderid": this.data.sa_workorderid,
        "sa_workorder_nodeid": this.data.sa_workorder_nodeid
      }
    })
    console.log("详情", res.data)
    this.setData({
      mainData: res.data[0],
      teams: res.data[0].team.map(v => v.userid),
      titems: res.data[0].titems
    })
    this.setDefaultData()
  },
  // 是否确认
  confirmValue(data) {
    this.data.form.confirm_value = data.currentTarget.dataset.item
    this.setData({
      form: this.data.form
    })
    this.updateNode()
  },
  // 文本信息
  textContent(data) {
    this.data.form.textcontent = data.detail.value
    this.setData({
      form: this.data.form
    })
    this.updateNode()
  },
  // 是否有偿
  amountPay(data) {
    this.data.form.amount = data.detail.value
    this.setData({
      form: this.data.form
    })
    this.updateNode()
  },
  // 备 注
  remarkEdit(data) {
    this.data.form.remarks = data.detail.value
    this.setData({
      form: this.data.form
    })
    this.updateNode()
  },
  confirmBill() {
    this.data.form.isconfirm = 1
    this.setData({
      form: this.data.form
    })
    this.updateNode()
  },
  // 更新节点
  async updateNode(e) {
    this.data.form.traintitem = this.data.titems
    this.data.form.sa_workorderid = this.data.sa_workorderid
    this.data.form.sa_workorder_nodeid = this.data.sa_workorder_nodeid
    this.data.form.team = []
    const res = await api._post({
      "id": "20230209094203",
      "content": this.data.form
    });
    if (!this.data.form.team.some(v => v.userid == wx.getStorageSync('userMsg').userid)) {
      this.setData({
        teams: [wx.getStorageSync('userMsg').userid]
      })
      this.addUser(false);
    }
    if (e && e.currentTarget.dataset.info === 'needBack') {
      wx.navigateBack()
    } else {
      this.nodeDetail()
    }
  },
  // 获取输入的数据
  setDefaultData() {
    let that = this
    Object.keys(this.data.mainData).map((key, index) => {
      Object.keys(this.data.form).map((key2, index) => {
        if (key === key2) {
          that.data.form[key2] = that.data.mainData[key2]
        }
      })
    })
    this.setData({
      form: this.data.form
    })
  },
  // 跳转到添加人员
  addTrainers() {
    wx.navigateTo({
      url: '/Eservice/addTrainers/index',
    })
  },
  // 添加人员
  getAddTrainers(data) {
    this.data.form.trainers.push(data)
    this.data.mainData.trainers = this.data.form.trainers
    this.setData({
      // form:this.data.form,
      mainData: this.data.mainData
    })
    this.updateNode()
  },
  // 删除人员
  deleteMenber(data) {
    this.data.mainData.trainers = this.data.mainData.trainers.filter(e => {
      return e.sa_workorder_node_teamid !== data.detail.sa_workorder_node_teamid
    })
    this.data.form.trainers = this.data.mainData.trainers
    this.setData({
      form: this.data.form,
      mainData: this.data.mainData
    })

    this.updateNode()
  },
  // 跳转到物料添加
  addProduct() {
    wx.navigateTo({
      url: '/Eservice/serviceMaterial/index',
    })
  },
  // 添加物料
  getAdd(selection) {
    this.data.form.traintitem = selection.map(e => {
      return {
        "itemname": e.itemname,
        "itemid": e.itemid,
        "model": e.model,
        "spec": e.spec,
        "qty": 1
      }
    })
    this.setData({
      titems: this.data.form.traintitem
    })
    this.updateNode()
  },
  // 修改物料数量
  qtyChange(data) {
    clearTimeout(this.data.timerid)
    this.data.timerid = setTimeout(() => {
      this.data.titems.forEach(e => {
        if (e.itemid == data.detail.itemid) {
          e.qty = data.detail.qty
        }
      })

      this.setData({
        titems: this.data.titems
      })
      this.updateNode()
    }, 1000)
    this.setData({
      timerid: this.data.timerid
    })

  },
  // 删除物料
  deleteMaterial(data) {
    this.setData({
      titems: this.data.titems.filter(e => {
        return e.itemid !== data.detail.itemid
      })
    })

    this.updateNode()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.data.bindContractData.ownerid = options.id
    this.data.bindNormalData.ownerid = options.id
    this.data.bindSignNameData.ownerid = options.id
    this.setData({
      bindContractData: this.data.bindContractData,
      bindNormalData: this.data.bindNormalData,
      bindSignNameData: this.data.bindSignNameData,
      sa_workorder_nodeid: options.id,
      sa_workorderid: options.wid,
      stopClick: options.class
    })
    this.nodeDetail();
    let page = getCurrentPages()[getCurrentPages().length - 2];
    if (page.__route__ == 'Eservice/workOrderDetail/index') this.setData({
      workers: page.data.billData.team
    })
  },
  onShow() {
    this.setData({
      SignName: true
    })
  },
  onHide() {
    this.setData({
      SignName: false
    })
  },
  /**
   * 开始添加成员或者取消添加
   */
  showTeamDialog() {
    this.setData({
      showTeams: !this.data.showTeams
    })
  },
  /**
   * 选择添加成员
   */
  selectTeams(e) {
    const {
      userid
    } = e.currentTarget.dataset.item;
    let teams = this.data.teams;
    if (teams.some(v => v == userid)) {
      teams = teams.filter(v => v != userid)
      deleteUsers.push(userid);
    } else {
      teams.push(userid)
    }
    this.setData({
      teams
    })
  },
  addUser(init = true) {
    api._post({
      "id": 20220930103803,
      "content": {
        ownertable: 'sa_workorder_node',
        ownerid: this.data.mainData.sa_workorder_nodeid,
        "userids": deleteUsers
      }
    }).then(s => {
      console.log("先删除", s)
      deleteUsers = [];
      api._post({
        "id": 20220930103603,
        "content": {
          ownertable: 'sa_workorder_node',
          ownerid: this.data.mainData.sa_workorder_nodeid,
          "userids": this.data.teams
        }
      }).then(res => {
        console.log("添加成员", res)
        wx.showToast({
          title: res.msg == '成功' ? '修改成功' : res.msg,
          icon: "none",
          mask: true
        });
        if (res.msg == '成功') {
          this.nodeDetail();
          if (init) this.showTeamDialog();
        }
      })
    })

  },
})