// pages/publishJob/index.js
let app = getApp();
import { customRequest } from '../../utils/request.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fuwufeiType: 0,
    priceType: 0,
    fuwufeiUnit: '元/小时',
    priceUnit: '元/小时',
    priceArray: [
      { id: 0, name: '时薪', unit: '元/小时', checked: true },
      { id: 1, name: '日薪', unit: '元/天', checked: false },
      { id: 2, name: '月薪', unit: '元/月', checked: false },
      // { id: 2, name: '补贴', unit:'', checked: false },
      { id: 3, name: '保底', unit: '保底', checked: false },
      { id: 4, name: '返费', unit: '返费', checked: false },
      // { id: 4, name: '计件', unit:'计件', checked: false },
      // { id: 5, name: '保底', unit:'', checked: false },
      // { id: 6, name: '面议', unit:'', checked: false },
    ],
    fuwufeiArray: [
      { id: 0, name: '按时', unit: '元/小时', checked: true },
      { id: 1, name: '按日', unit: '元/日', checked: false },
      { id: 2, name: '按月', unit: '元/月', checked: false },
      { id: 3, name: '按次', unit: '元/次', checked: false },
      { id: 4, name: '按件', unit: '元/公斤', checked: false },
      { id: 5, name: '按百分比', unit: '%', checked: false },
      { id: 6, name: '总计', unit: '元', checked: false },
    ],
    sexArray: [
      { id: 1, name: '男', checked: false },
      { id: 2, name: '女', checked: false },
    ],
    nanChecked: false,
    nvChecked: false,
    salaryArray: [],
    salaryIndex: [0, 0],
    salaryArrayN: [],
    salaryIndexN: [0, 0],
    jobObj: {},
    isEdit: false,
    loading: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    let that = this
    console.log(options);
    if (options.isedit == 1) {
      var obj = JSON.parse(options.item)
      console.log(JSON.parse(options.item));

      this.data.priceArray.forEach((item) => {
        if (obj.salaryClassify == item.id) {
          item.checked = true
          this.data.priceType = obj.salaryClassify
          this.data.priceUnit = item.unit
        } else {
          item.checked = false
        }

      })
      that.data.fuwufeiArray.forEach((item, index) => {
        if (obj.returnFeeType == item.id) {
          item.checked = true;
          this.data.fuwufeiType = obj.returnFeeType
          this.data.fuwufeiUnit = item.unit
        } else {
          item.checked = false
        }
      })
      if (app.isNotEmptyCheck(obj.minAgeMan)) {
        that.setData({
          nanChecked: true,
          salaryIndex: [obj.minAgeMan - 16, obj.maxAgeMan - 17],
        })
        // that.setData({

        //   salaryIndexN:[that.data.jobDetail.storeJob.minAgeWoman - 16,that.data.jobDetail.storeJob.maxAgeWoman - 17]
        // })
      }
      if (app.isNotEmptyCheck(obj.minAgeWoman)) {
        that.setData({
          nvChecked: true,
          salaryIndexN: [obj.minAgeWoman - 16, obj.maxAgeWoman - 17]
        })
      }
      wx.setNavigationBarTitle({
        title: "编辑职位",
      });
      that.setData({
        fuwufeiArray: that.data.fuwufeiArray,
        priceArray: that.data.priceArray,
        priceUnit: this.data.priceUnit,
        priceType: this.data.priceType,
        fuwufeiUnit: this.data.fuwufeiUnit,
        fuwufeiType: this.data.fuwufeiType,
        jobObj: { ...obj, returnFee: obj.returnFee / 100 },
        isEdit: true
      })
    } else {
      wx.setNavigationBarTitle({
        title: "新增职位",
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow () {
    let that = this
    let salaryStart = [],
      salaryEnd = [],
      salaryArray = [];
    for (let i = 16; i < 70; i++) {
      salaryStart.push(`${i}`);
      salaryEnd.push(`${i + 1}`)
    }
    // console.log(salaryStart);
    // console.log(salaryEnd);
    salaryArray.push(salaryStart);
    salaryArray.push(salaryEnd);
    that.setData({
      salaryArray: salaryArray,
      salaryArrayN: salaryArray
    })
  },
  showPriceType () {
    this.setData({
      priceTypeShow: true
    })
  },
  showPrice () {
    this.setData({
      priceShow: true
    })
  },
  showSex () {
    console.log(123);
    this.setData({
      sexShow: true
    })
  },
  showFuwufei () {
    this.setData({
      fuwufeiShow: true
    })
  },
  showFuwufeiType () {

    this.setData({
      fuwufeiTypeShow: true
    })
  },
  changeNan () {
    this.setData({
      nanChecked: !this.data.nanChecked
    })
  },
  changeNv () {
    this.setData({
      nvChecked: !this.data.nvChecked
    })
  },
  bindMultiPickerChange (e) {
    // let currentColunm = e.detail.column;
    // let currentClounmIndex = e.detail.value;
    // let salaryArray = this.data.salaryArray
    // console.log('修改的列为', currentColunm, '，值为', currentClounmIndex);
    // let data = {
    //   salaryArray: this.data.salaryArray,
    //   salaryIndex: this.data.salaryIndex
    // }
    // data.salaryIndex[currentColunm] = currentClounmIndex
    // if (this.data.salaryArray[1][this.data.salaryIndex[1]] < this.data.salaryArray[0][this.data.salaryIndex[0]]) {
    //   this.data.salaryIndex[1] = this.data.salaryIndex[0]
    // }
    // if (this.data.salaryArray[0][this.data.salaryIndex[0]] > this.data.salaryArray[1][this.data.salaryIndex[1]]) {
    //   this.data.salaryIndex[0] = this.data.salaryIndex[1] + 1
    // }
    // this.setData(data)
    // this.setData({
    //   nanChecked: true
    // })
    this.setData({
      salaryIndex: e.detail.value,
      nanChecked: true
    })
  },
  bindMultiPickerChangeN (e) {
    console.log(e);
    // let currentColunm = e.detail.column;
    // let currentClounmIndex = e.detail.value;
    // let salaryArrayN = this.data.salaryArrayN
    // console.log('修改的列为', currentColunm, '，值为', currentClounmIndex);
    // let data = {
    //   salaryArrayN: this.data.salaryArrayN,
    //   salaryIndexN: this.data.salaryIndexN
    // }
    // data.salaryIndexN[currentColunm] = currentClounmIndex
    // if (this.data.salaryArrayN[1][this.data.salaryIndexN[1]] < this.data.salaryArrayN[0][this.data.salaryIndexN[0]]) {
    //   this.data.salaryIndexN[1] = this.data.salaryIndexN[0]
    // }
    // if (this.data.salaryArrayN[0][this.data.salaryIndexN[0]] > this.data.salaryArrayN[1][this.data.salaryIndexN[1]]) {
    //   this.data.salaryIndexN[0] = this.data.salaryIndexN[1] + 1
    // }


    // // data.salaryIndexN[1] = currentClounmIndex;
    // this.setData(data);
    // this.setData({
    //   nvChecked: true
    //   // salaryIndexN: this.data.salaryIndexN
    //   // salaryArrayN:this.data.salaryArrayN
    // })
    this.setData({
      salaryIndexN: e.detail.value,
      nvChecked: true
    })
  },
  //滑动列时触发该事件
  bindMultiPickerColumnChange (e) {
    let currentColunm = e.detail.column;
    let currentClounmIndex = e.detail.value;
    let salaryArray = this.data.salaryArray
    console.log('修改的列为', currentColunm, '，值为', currentClounmIndex);
    let data = {
      salaryArray: this.data.salaryArray,
      salaryIndex: this.data.salaryIndex
    }
    data.salaryIndex[currentColunm] = currentClounmIndex
    if (this.data.salaryArray[1][this.data.salaryIndex[1]] < this.data.salaryArray[0][this.data.salaryIndex[0]]) {
      this.data.salaryIndex[1] = this.data.salaryIndex[0]
    }
    if (this.data.salaryArray[0][this.data.salaryIndex[0]] > this.data.salaryArray[1][this.data.salaryIndex[1]]) {
      this.data.salaryIndex[0] = this.data.salaryIndex[1] + 1
    }

    // data.salaryIndex[1] = currentClounmIndex;
    this.setData(data)
    this.setData({
      nanChecked: true
    })
  },

  bindMultiPickerColumnChangeN (e) {
    console.log(e);
    let currentColunm = e.detail.column;
    let currentClounmIndex = e.detail.value;
    let salaryArrayN = this.data.salaryArrayN
    console.log('修改的列为', currentColunm, '，值为', currentClounmIndex);
    let data = {
      salaryArrayN: this.data.salaryArrayN,
      salaryIndexN: this.data.salaryIndexN
    }
    data.salaryIndexN[currentColunm] = currentClounmIndex
    if (this.data.salaryArrayN[1][this.data.salaryIndexN[1]] < this.data.salaryArrayN[0][this.data.salaryIndexN[0]]) {
      this.data.salaryIndexN[1] = this.data.salaryIndexN[0]
    }
    if (this.data.salaryArrayN[0][this.data.salaryIndexN[0]] > this.data.salaryArrayN[1][this.data.salaryIndexN[1]]) {
      this.data.salaryIndexN[0] = this.data.salaryIndexN[1] + 1
    }


    // data.salaryIndexN[1] = currentClounmIndex;
    this.setData(data);
    this.setData({
      nvChecked: true
      // salaryIndexN: this.data.salaryIndexN
      // salaryArrayN:this.data.salaryArrayN
    })
  },

  radioChange (e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value);
    var that = this;
    var unit = ""
    var type = ""
    that.data.priceArray.forEach((item, index) => {
      if (e.detail.value == item.id) {
        item.checked = true;
        unit = item.unit
        type = item.id
      } else {
        item.checked = false
      }
    })
    that.setData({
      priceArray: that.data.priceArray,
      priceUnit: unit,
      priceType: type
    })

    setTimeout(() => {
      this.setData({
        priceTypeShow: false
      })
    }, 200)
  },
  fuwufeiChange (e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value);
    var that = this;
    var name = ""
    var unit = ""
    that.data.fuwufeiArray.forEach((item, index) => {
      if (e.detail.value == item.id) {
        item.checked = true;
        name = item.id
        unit = item.unit
      } else {
        item.checked = false
      }
    })
    that.setData({
      fuwufeiArray: that.data.fuwufeiArray,
      fuwufeiUnit: unit,
      fuwufeiType: name
    })

    setTimeout(() => {
      this.setData({
        fuwufeiTypeShow: false
      })
    }, 200)

  },
  // sexRadioChange () {
  //   var that = this;
  //   var name = ""
  //   that.data.priceArray.forEach((item, index) => {
  //     if (e.detail.value == item.id) {
  //       item.checked = true;
  //       name = item.name
  //     } else {
  //       item.checked = false
  //     }
  //   })
  //   that.setData({
  //     priceArray: that.data.priceArray,
  //     priceUnit: name,
  //   })

  //   setTimeout(() => {
  //     this.setData({
  //       priceTypeShow: false
  //     })
  //   }, 200)
  // },
  hidedrawershow (e) {
    console.log(e);
    this.setData({
      [e.detail.type]: false
    })
    console.log(this.data.drawerShow);
  },
  inputUpdate (e) {
    // console.log(e);
    if (e.currentTarget.dataset.type == 'minMonthlyPay' || e.currentTarget.dataset.type == 'maxMonthlyPay') {
      if (e.currentTarget.dataset.type == 'minMonthlyPay') {
        this.data.jobObj.minMonthlyPay = e.detail.value
      } else {
        this.data.jobObj.maxMonthlyPay = e.detail.value
      }
      console.log(this.data.jobObj);
    } else {
      this.data.jobObj[e.currentTarget.dataset.type] = e.detail.value
      // let type = 'jobObj.' + e.currentTarget.dataset.type
      // console.log(type);
      this.setData({
        // [type]: e.detail.value
        jobObj: this.data.jobObj
      })
    }

    console.log(this.data.jobObj);
  },

  subAdd () {
    let that = this
    console.log(this.data.loading);

    let data = {
      ...this.data.jobObj,
      returnFee: this.data.jobObj.returnFee * 100,
      salaryClassifyValue: this.data.jobObj.salaryClassifyValue * 100,
      returnFeeType: this.data.fuwufeiType,
      salaryClassify: this.data.priceType,
      minAgeMan: that.data.nanChecked ? that.data.salaryArray[0][that.data.salaryIndex[0]] || '' : '',
      maxAgeMan: that.data.nanChecked ? that.data.salaryArray[1][that.data.salaryIndex[1]] || '' : '',
      minAgeWoman: that.data.nvChecked ? that.data.salaryArrayN[0][that.data.salaryIndexN[0]] || '' : '',
      maxAgeWoman: that.data.nvChecked ? that.data.salaryArrayN[1][that.data.salaryIndexN[1]] || '' : '',
      minMonthlyPay: (that.data.jobObj.minMonthlyPay < that.data.jobObj.maxMonthlyPay) ? that.data.jobObj.minMonthlyPay : that.data.jobObj.maxMonthlyPay,

      maxMonthlyPay: (that.data.jobObj.maxMonthlyPay > that.data.jobObj.minMonthlyPay) ? that.data.jobObj.maxMonthlyPay : that.data.jobObj.minMonthlyPay
    }

    console.log(data);

    if (!data.jobName) {
      wx.showToast({
        title: '请输入职位名称',
        icon: 'none'
      })
      return
    } else if (!that.data.nanChecked && !that.data.nvChecked) {
      wx.showToast({
        title: '请至少选择一个性别',
        icon: 'none'
      })
      return
    } else if (!data.minAgeMan && !data.maxAgeMan && !data.minAgeWoman && !data.maxAgeWoman) {
      wx.showToast({
        title: '请选择年龄',
        icon: 'none'
      })
      return
    } else if (!this.data.jobObj.salaryClassifyValue) {
      wx.showToast({
        title: '请输入薪资金额',
        icon: 'none'
      })
      return
    } else if (!data.minAgeMan && !data.maxAgeMan && !data.minAgeWoman && !data.maxAgeWoman) {
      wx.showToast({
        title: '请选择年龄',
        icon: 'none'
      })
      return
    } else if (!data.returnFee) {
      wx.showToast({
        title: '请输入服务费金额',
        icon: 'none'
      })
      return
    } else if (!data.minMonthlyPay || !data.maxMonthlyPay) {
      wx.showToast({
        title: '请输入综合月薪',
        icon: 'none'
      })
      return
    }
    // 上传中判断重复提交
    if (this.data.loading == true) {
      return
    }
    this.data.loading = true
    // 编辑状态判断
    if (this.data.isEdit) {
      data.id = this.data.jobObj.id
      delete data.updateTime
      delete data.createTime
      delete data.rejectTime
    }

    console.log(data);
    customRequest((this.data.isEdit ? "/yishoudan/custom/job/update" : "/yishoudan/custom/job/add"), { header: 'headers', method: 'POST', data: data }).then((res) => {
      if (res.data.status == 200) {
        wx.showToast({
          title: '提交成功',
          icon: 'none',
          success () {
            setTimeout(() => {
              var pages = getCurrentPages();
              var prevPage = pages[pages.length - 2]; // 上一个页面
              console.log(prevPage);
              if (prevPage.route != 'pages/mineJob/index') {
                wx.redirectTo({
                  url: '/pages/mineJob/index?currIndex=0'
                })
              } else {
                prevPage.data.searchParam.status = 0
                prevPage.setData({
                  searchParam: prevPage.data.searchParam,
                  currIndex: 0
                })
                wx.navigateBack({
                  detail: 1
                });
              }


            }, 1000);
          }
        })
      } else {
        wx.showToast({
          title: '提交失败',
          icon: 'none',
          success () {
          }
        })
      }
      setTimeout(() => {
        that.data.loading = false
      }, 2000);
    }).catch(() => {
      setTimeout(() => {
        that.data.loading = false
      }, 2000);
    })
    // wx.request({
    //   url: app.globalData.ip + (this.data.isEdit ? "/yishoudan/custom/job/update" : "/yishoudan/custom/job/add"),
    //   data,
    //   header: app.globalData.headers,
    //   method: "POST",
    //   success: function (res) {
    //     if (res.data.status == 200) {
    //       wx.showToast({
    //         title: '提交成功',
    //         icon: 'none',
    //         success () {
    //           setTimeout(() => {
    //             var pages = getCurrentPages();
    //             var prevPage = pages[pages.length - 2]; // 上一个页面
    //             console.log(prevPage);
    //             if (prevPage.route != 'pages/mineJob/index') {
    //               wx.redirectTo({
    //                 url: '/pages/mineJob/index?currIndex=0'
    //               })
    //             } else {
    //               prevPage.data.searchParam.status = 0
    //               prevPage.setData({
    //                 searchParam: prevPage.data.searchParam,
    //                 currIndex: 0
    //               })
    //               wx.navigateBack({
    //                 detail: 1
    //               });
    //             }


    //           }, 1000);
    //         }
    //       })
    //     } else {
    //       wx.showToast({
    //         title: '提交失败',
    //         icon: 'none',
    //         success () {
    //         }
    //       })
    //     }

    //   },
    //   complete: function () {
    //     setTimeout(() => {
    //       that.data.loading = false
    //     }, 2000);
    //   }
    // })

    console.log(this.data.jobObj);
  },

  surePrice () {
    this.setData({
      priceShow: false
    })
  },

  sureSex () {
    this.setData({
      sexShow: false
    })
  },
  sureFuwufei () {
    this.setData({
      fuwufeiShow: false
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage () {

  }
})