// pages/goodsAdd/goodsAdd.js
const utils = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: 0, //0是新增，1是编辑
    entTaxId: '', //企业税号
    taxRate: [0], //税率
    taxRateIndex: '', // 税率下标
    othersRate: '', // 简易征收税率
    taxRateDisabled: false, // 是否禁用税率
    zeroRateFlag: ['无', '免税', '不征税', '普通零税率'], // 零税率标识
    zeroRateFlagIndex: '', // 零税率标识下标
    preferentialFlag: ['不使用', '使用'], // 是否使用优惠政策
    preferentialFlagIndex: '', // 是否使用优惠政策下标
    preferentialType: [], // 优惠政策类型
    preferentialTypeIndex: '', // 优惠政策类型下标
    params: {
      taxationCateCode: '', //税收分类编码
      taxationCateName: '', //税收分类名称
      productName: '',
      shortCode: '',
      specsModel: '',
      unit: '',
      price: '',
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      entTaxId: wx.getStorageSync('entTaxId'),
      type: Number(options.type)
    })
    if (this.data.type === 1) {
      this.getDetail()
      wx.setNavigationBarTitle({
        title: '编辑商品'
      })
    } else {
      this.getType()
    }
    // this.getCompany()
  },

  //编辑操作，回显信息
  getDetail() {
    let detail = wx.getStorageSync('configPro')
    this.setData({
      ['params.taxationCateCode']: detail.taxationCateCode,
      ['params.taxationCateName']: detail.taxationCateName,
      ['params.productName']: detail.productName,
      ['params.shortCode']: detail.shortCode,
      ['params.specsModel']: detail.specsModel,
      ['params.unit']: detail.unit,
      ['params.price']: detail.price,
      entTaxId: detail.entTaxId,
      preferentialFlagIndex: detail.preferentialFlag,
      productId: detail.productId
    })
    if (detail.zeroRateFlag == 0 || detail.zeroRateFlag) {
      this.setData({
        zeroRateFlagIndex: Number(detail.zeroRateFlag)
      })
    } else {
      this.setData({
        zeroRateFlagIndex: 0
      })
    }
    /* 回显税率 */
    if (detail.taxationCateCode) {
      let params = {
        entTaxId: wx.getStorageSync('entTaxId'),
        taxCateCode: detail.taxationCateCode
      }
      utils.axios({
        method: 'get',
        url: '/sys/entInfo/rates',
        data: params,
        success: res => {
          let rateArray = [0]
          if (res.data) {
            for (let i = 0; i < res.data.length; i++) {
              rateArray.push(res.data[i] * 100)
            }
          }
          this.setData({
            taxRate: rateArray
          })
          for (let i = 0; i < this.data.taxRate.length; i++) {
            if (this.data.taxRate[i] === detail.taxRate * 100) {
              this.setData({
                taxRateIndex: i
              })
              break
            } else {
              this.setData({
                taxRateIndex: ''
              })
            }
          }
        }
      })
    }
    // 回显优惠政策类型
    this.getType().then(() => {
      for (let i = 0; i < this.data.preferentialType.length; i++) {
        if (this.data.preferentialType[i].label === detail.preferentialType) {
          this.setData({
            preferentialTypeIndex: i
          })
        }
      }
      if (detail.preferentialType === '免税') {
        this.setData({
          taxRateDisabled: true,
          taxRateIndex: 0,
          zeroRateFlagIndex: 1
        })
      } else if (detail.preferentialType === '不征税') {
        this.setData({
          taxRateDisabled: true,
          taxRateIndex: 0,
          zeroRateFlagIndex: 2
        })
      } else if (Number(detail.taxRate) === 0.03) {
        this.setData({
          taxRateDisabled: true,
          taxRateIndex: '',
          othersRate: 0.03,
          zeroRateFlagIndex: 0
        })
      } else if (Number(detail.taxRate) === 0.05) {
        this.setData({
          taxRateDisabled: true,
          taxRateIndex: '',
          othersRate: 0.05,
          zeroRateFlagIndex: 0
        })
      }
    })
  },

  //企业税号、税率
  getCompany() {
    let entInfo= utils.getCurrEntInfo;
    let taxRate = [0]
    if (entInfo.availableTaxes) {
      let arr = res.data.availableTaxes.split(',')
      for (let i = 0; i < arr.length; i++) {
        taxRate.push(arr[i] * 100)
      }
    }
    this.setData({
      taxRate,
      entTaxId: entInfo.entTaxId
    })
    if (this.data.type === 1) {
      this.getDetail()
      wx.setNavigationBarTitle({
        title: '编辑商品'
      })
    }
    // utils.getCompany().then(res => {
    //   let taxRate = [0]
    //   if (res.data.availableTaxes) {
    //     let arr = res.data.availableTaxes.split(',')
    //     for (let i = 0; i < arr.length; i++) {
    //       taxRate.push(arr[i] * 100)
    //     }
    //   }
    //   this.setData({
    //     taxRate,
    //     entTaxId: res.data.entTaxId
    //   })
    //   if (this.data.type === 1) {
    //     this.getDetail()
    //     wx.setNavigationBarTitle({
    //       title: '编辑商品'
    //     })
    //   }
    // })
  },

  //提交
  submitData() {
    let paramsData = this.data.params
    let params = {
      mobile: utils.getInfo().mobile,
      reqChannel: 5,
      entTaxId: this.data.entTaxId,
      taxPriceFlag: true,
      taxationCateCode: paramsData.taxationCateCode,
      taxationCateName: paramsData.taxationCateName,
      productName: utils.trimAll(paramsData.productName)
    }
    if (this.data.type === 1) {
      params.productId = this.data.productId
    }
    if (params.taxationCateCode === '') {
      utils.toast('产品分类编码不能为空')
      return
    }
    if (params.productName === '') {
      utils.toast('产品名称不能为空')
      return
    }

    if (utils.trimAll(paramsData.shortCode) !== '') {
      params.shortCode = utils.trimAll(paramsData.shortCode)
    }
    if (utils.trimAll(paramsData.specsModel) !== '') {
      params.specsModel = utils.trimAll(paramsData.specsModel)
    }
    if (utils.trimAll(paramsData.unit) !== '') {
      params.unit = utils.trimAll(paramsData.unit)
    }
    if (utils.trimAll(paramsData.price) !== '') {
      params.price = utils.trimAll(paramsData.price)
    }
    if (this.data.taxRateIndex === '') {
      if (this.data.othersRate === '') {
        utils.toast('税率不能为空')
        return
      } else {
        params.taxRate = this.data.othersRate
      }
    } else {
      params.taxRate = this.data.taxRate[this.data.taxRateIndex] / 100
    }
    if (this.data.zeroRateFlagIndex !== '' && this.data.zeroRateFlagIndex !== 0) {
      params.zeroRateFlag = Number(this.data.zeroRateFlagIndex)
    }
    if (this.data.preferentialFlagIndex !== '') {
      params.preferentialFlag = Number(this.data.preferentialFlagIndex)
    } else {
      params.preferentialFlag = 0
    }
    if (this.data.preferentialTypeIndex !== '') {
      params.preferentialType = this.data.preferentialType[this.data.preferentialTypeIndex].label
    }
    utils.axios({
      method: 'post',
      url: '/sys/product/saveOrUpdate',
      data: params,
      sendBefore() {
        wx.showLoading({
          title: '商品保存中...',
          mask: true
        })
      },
      complete() {
        wx.hideLoading()
      },
      success: res => {
        utils.funPrev(prev => {
          prev.toFresh()
          utils.toast('商品保存成功')
          wx.navigateBack()
        })
      }
    })
  },

  //选择税收分类名称
  search() {
    const name = utils.trimAll(this.data.params.productName)
    if (name) {
      wx.navigateTo({
        url: '/pages/goodsCate/goodsCate?productName=' + name
      })
      return
    }
    wx.navigateTo({
      url: '/pages/goodsCate/goodsCate'
    })
  },

  //产品名称。简码。规格。计量单位。单价
  getName(e) {
    let value = e.detail.value
    this.setData({
      ['params.productName']: value
    })
  },
  getCode(e) {
    let value = e.detail.value
    this.setData({
      ['params.shortCode']: value
    })
  },
  getSpecs(e) {
    let value = e.detail.value
    this.setData({
      ['params.specsModel']: value
    })
  },
  getUnit(e) {
    let value = e.detail.value
    this.setData({
      ['params.unit']: value
    })
  },
  getPrice(e) {
    let value = e.detail.value
    this.setData({
      ['params.price']: value
    })
  },

  //税率
  setTaxRate(e) {
    let value = Number(e.detail.value)
    this.setData({
      taxRateIndex: value
    })
    /* 商品标识使用方法统计-税率 */
    if (value === 0) {
      this.setData({
        zeroRateFlagIndex: 3,
        preferentialFlagIndex: '',
        preferentialTypeIndex: ''
      })
    } else {
      this.setData({
        zeroRateFlagIndex: 0
      })
    }
  },

  //零税率标识
  setZeroRateFlag(e) {
    let value = Number(e.detail.value)
    this.setData({
      othersRate: '',
      zeroRateFlagIndex: value
    })
    /* 商品标识使用方法统计-零税率标识 */
    if (value === 0) {
      this.setData({
        taxRateDisabled: false,
        taxRateIndex: '',
        preferentialFlagIndex: '',
        preferentialTypeIndex: ''
      })
    } else if (value === 3) {
      this.setData({
        taxRateDisabled: true,
        taxRateIndex: 0,
        preferentialFlagIndex: '',
        preferentialTypeIndex: ''
      })
    } else if (value === 1) {
      this.setData({
        taxRateDisabled: true,
        taxRateIndex: 0,
        preferentialFlagIndex: 1
      })
      for (let i = 0; i < this.data.preferentialType.length; i++) {
        if (this.data.preferentialType[i].label === '免税') {
          this.setData({
            preferentialTypeIndex: i
          })
        }
      }
    } else if (value === 2) {
      this.setData({
        taxRateDisabled: true,
        taxRateIndex: 0,
        preferentialFlagIndex: 1
      })
      for (let i = 0; i < this.data.preferentialType.length; i++) {
        if (this.data.preferentialType[i].label === '不征税') {
          this.setData({
            preferentialTypeIndex: i
          })
        }
      }
    }
  },

  //优惠政策标识
  setPreferentialFlag(e) {
    let value = e.detail.value
    this.setData({
      preferentialFlagIndex: Number(value)
    })
  },

  // 获取优惠政策类型
  getType() {
    return new Promise((resolve, reject) => {
      utils.axios({
        method: 'get',
        url: '/auth/param/getPreferentials',
        sendBefore() {
          wx.showLoading({
            title: '加载中...'
          })
        },
        success: res => {
          this.setData({
            preferentialType: res.data
          })
          resolve()
        },
        complete() {
          wx.hideLoading()
        }
      })
    })
  },
  setPreferentialType(e) {
    let value = Number(e.detail.value)
    this.setData({
      preferentialTypeIndex: value
    })
    /* 商品标识使用方法统计-优惠政策类型 */
    const curItem = this.data.preferentialType[value]
    if (curItem.label === '免税') {
      this.setData({
        taxRateDisabled: true,
        taxRateIndex: 0,
        zeroRateFlagIndex: 1
      })
    } else if (curItem.label === '不征税') {
      this.setData({
        taxRateDisabled: true,
        taxRateIndex: 0,
        zeroRateFlagIndex: 2
      })
    } else if (Number(curItem.rate) === 0.03) {
      this.setData({
        taxRateDisabled: true,
        taxRateIndex: '',
        othersRate: 0.03,
        zeroRateFlagIndex: 0
      })
    } else if (Number(curItem.rate) === 0.05) {
      this.setData({
        taxRateDisabled: true,
        taxRateIndex: '',
        othersRate: 0.05,
        zeroRateFlagIndex: 0
      })
    } else {
      this.setData({
        taxRateDisabled: false,
        taxRateIndex: '',
        othersRate: '',
        zeroRateFlagIndex: 0
      })
    }
  },

  //返回
  back() {
    wx.navigateBack()
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

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

  },

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

  },

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

  },

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

  }
})