// home/pages/dispatch/dispatch.js
const { getDispatchEmp,saveDispatch, getDispatchEmpList, saveDispatchEmp } = require("../../../api/api");
Page({
  data: {
    radio:'',
    name:'',//员工姓名
    staffLists:[],//员工列表
    type:''
  },
  onLoad(options) {
    this.setData({
      entryId:options.projectId
    });
    if(options.type){
      this.setData({
        type:options.type,//31电站整改
      });
    }
    this.getStaffList();
  },
  onShow() {

  },
  /** 搜索输入事件 **/
  keyWordsChange(e) {
    this.setData({
      name: e.detail,
    });
  },
  /** 搜索 **/
  keyWordsSearch(e){
    this.getStaffList();
  },
  /** 员工列表 **/
  getStaffList(){
    if(this.data.type == '31'){//整改派工
      getDispatchEmpList({
        userId:wx.getStorageSync('userId'),
        traderId:wx.getStorageSync('traderId'),
        empName:this.data.name
      }).then(res=>{
        if(res.code==200){
          this.setData({
            staffLists:res.data.map((item)=>{return item.columns})
          });
        }else{
          wx.showToast({
            title: res.msg,
            icon:'none'
          })
        }
      });
      return
    }
    //质检派工
    getDispatchEmp({
      userId:wx.getStorageSync('userId'),
      empName:this.data.name
    }).then(res=>{
      if(res.code==200){
        this.setData({
          staffLists:res.data.map((item)=>{return item.columns})
        });
      }else{
        wx.showToast({
          title: res.msg,
          icon:'none'
        })
      }
    });
  },
  /** 单选 **/
  onChange(event) {
    this.setData({
      radio: event.detail,
    });
    console.log("执行1", event);
  },
  /** 单选点击 **/
  onClick(event) {
    console.log("执行", event);
    const id = event.currentTarget.dataset.id;
    const name = event.currentTarget.dataset.name;
    this.setData({
      empId: id,
      empName: name,
      radio: id,
    });
  },
  /** 返回 **/
  back(e) {
    wx.navigateBack();
  },
  /** 提交 **/
  submit(){
    if(!this.data.radio){
      wx.showToast({
        title: '请先选择一项',
        icon:'none'
      })
      return;
    }
    if(this.data.type == '31'){
      saveDispatchEmp({
        traderId:wx.getStorageSync('traderId'),
        id: this.data.entryId, //电站整改单id
        empId: this.data.empId,
        empName:this.data.empName,
      }).then(res=>{
        if(res.code==200){
          wx.showToast({
            title: '派工成功',
            icon:'none'
          })
          setTimeout(()=>{
            wx.navigateBack();
          },1500);
        }else{
          wx.showToast({
            title: res.errMsg,
            icon:'none'
          })
        }
      });
      return
    }
    saveDispatch({
      userId:wx.getStorageSync('userId'),
      id: this.data.entryId, //质检单id
      empId: this.data.empId,
      empName:this.data.empName,
    }).then(res=>{
      if(res.code==200){
        wx.showToast({
          title: '派工成功',
          icon:'none'
        })
        setTimeout(()=>{
          wx.navigateBack();
        },1500);
      }else{
        wx.showToast({
          title: res.errMsg,
          icon:'none'
        })
      }
    });
  },  
})