// pages/c-dispatch/c-dispatch.js
var myRequest = require("../../utils/api.js");
Page({
  data: {
    activeValues: [0],
    inputName: '',
    planList: [],
    planId: '',
    showTextAndTitleWithInput: false,
    collaborator: [],
  },
  onLoad() {
    this.getPlanList()
  },
  onShow() {

  },
  saveDialog() {

    this.data.planId ? this.exitPlanList(this.data.inputName) : this.addPlanList(this.data.inputName)
  },
  closeDialog() {
    this.setData({
      showTextAndTitleWithInput: false,
      collaborator: [],
      planId: '',
      inputName: ''
    })
  },
  handleClick() {
    this.setData({
      showTextAndTitleWithInput: true,
      inputName: ''
    })
  },
  exitData(e) {
    let planId = e.currentTarget.dataset.planid
    let device = e.currentTarget.dataset.device
    let planList = e.currentTarget.dataset.val
    let mapVal = planList.map(item => item.id)
    let data = {
      type: 'exit',
      planId,
      mapVal: JSON.stringify(mapVal),
      ...device
    }
    wx.navigateTo({
      url: '../s-remould-task/s-remould-task?data=' + JSON.stringify(data),
    })
  },
  addDetail(e) {
    let _this = this;
    let {
      planid,
      type
    } = e.currentTarget.dataset
    myRequest.myRequest('renovationPlan/devices', {
      planId: planid
    }, 'get').then(function (res) {
      let planList = res.data.data
      let mapVal = planList.map(item => item.id)
      let data = {
        type: 'add',
        planId: planid,
        createType: type,
        mapVal: JSON.stringify(mapVal)
      }
      wx.navigateTo({
        url: '../s-remould-task/s-remould-task?data=' + JSON.stringify(data),
      })

    }).catch(function (res) {
      console.log(res);
    })

  },
  changeInput(e) {
    this.setData({
      inputName: e.detail.value
    })
  },
  getPlanList() {
    this.setData({
      activeValues: [],
    });
    let that = this;
    myRequest.myRequest('renovationPlan/list', {

    }, 'get').then(function (res) {

      that.setData({
        planList: res.data.data.items
      })
      console.log()
    }).catch(function (res) {
      console.log(res);
    })
  },
  delDetail(e) {

    let that = this;
    wx.showModal({
      title: '提示',
      content: '是否删除当前改造方案',
      success(res) {
        if (res.confirm) {
          let {
            planid,
            type
          } = e.currentTarget.dataset
          if (type == "0") {
            myRequest.myRequest('renovationPlan/delete?planId=' + planid, {}, 'post').then(function (res) {
              that.getPlanList()
              wx.showToast({
                title: '删除成功',
                icon: 'none'
              })
              console.log()
            }).catch(function (res) {
              console.log(res);
            })

          } else {
            wx.showToast({
              title: '当前是协作方案，无权删除',
              icon: 'none'
            })
          }
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })





  },
  exitDetail(e) {
    let {
      planName,
      planId,
      collaborator
    } = e.currentTarget.dataset.item
    this.setData({
      showTextAndTitleWithInput: true,
      inputName: planName,
      planId: planId,
      collaborator: collaborator
    })
  },

  exitPlanList(val) {

    let that = this;
    if (!val) return
    let collaborator = this.data.collaborator.map(item => item.id)
    myRequest.myRequest('renovationPlan/update', {
      planName: val,
      planId: that.data.planId,
      collaborator: collaborator.toString()
    }, 'post').then(function (res) {
      that.getPlanList()
      that.closeDialog()
      console.log()
      that.setData({
        collaborator: []
      })
    }).catch(function (res) {
      console.log(res);
    })
  },
  addPlanList(val) {
    let that = this;
    if (!val) return
    let collaborator = this.data.collaborator.map(item => item.id)
    myRequest.myRequest('renovationPlan/add', {
      planName: val,
      collaborator: collaborator.toString()
    }, 'post').then(function (res) {
      that.getPlanList()
      that.closeDialog()
      console.log()
      that.setData({
        collaborator: []
      })
    }).catch(function (res) {
      console.log(res);
    })
  },

  handleChange(e) {
    console.log(e.detail.value)
    console.log(this.data.planList)
    let _this = this;
    if (e.detail.value.length > this.data.activeValues.length) {
      e.detail.value.forEach(planId => {
        myRequest.myRequest('renovationPlan/devices', {

          planId: planId
        }, 'get').then(function (res) {
          let planList = _this.data.planList
          planList.forEach(item => {

            if (item.planId == planId) {
              item.deviceList = res.data.data

            }
          })
          _this.setData({
            planList
          })
        }).catch(function (res) {
          console.log(res);
        })
      })

    }
    this.setData({
      activeValues: e.detail.value,
    });


  },
  onIconTapDel(e) {
    console.log(e)
    let index = e.target.dataset.index
    let collaborator = this.data.collaborator
    collaborator.splice(index, 1)
    this.setData({
      collaborator
    })
  },
  onIconTapAdd() {
    let collaborator = this.data.collaborator.map(item => item.id)
    wx.navigateTo({
      url: '../search-user/search-user?user=' + collaborator
    })

  }
})