/*******************************************************************************
 * Copyright(c) 2022 dongke All rights reserved. / Confidential
 * 类的信息：
 *		1.程序名称：
 * 编辑履历：
 *		作者				日期					版本				修改内容
 *		于继渤		 2022-5-28			1.00		 筛选多选标签
 *		作者				日期					版本				修改内容
 *		于继渤		 2022-5-31			1.01		 完善抛出事件
 *******************************************************************************/
const app = getApp()
const Constants = require('../../../utils/Constants.js');
const api = require('../../../utils/api.js');
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    typeName: {
      type: String,
      default: ''
    },

    oneSelectFlag: {
      type: Boolean,
      default: false
    },
    show: {
      type: Boolean,
      default: true,
      observer: function (newVal) {
        let show = this.properties.show
        if (show) {
          let stowList = this.data.stowList;
          let moreList = this.data.moreList;
          if (stowList.length == 0 && moreList.length == 0) {
            let dataSourceCode = this.data.dataSourceCode
            let typeName = this.data.typeName
            // 从数据库中取值
            if (this.data.loadDataFlag) {
              this.getData(dataSourceCode).then(res => {
                // 赋值逻辑
                let list = res.data
                list.forEach(function(item){
                  if(!item.id){
                    item.id = item.code
                  } 
                })
                if (list == null || list.length <= 0) return
                list.forEach(item => {
                  item.infotype = 'default'
                })
                if (list.length > 4) {
                  //收起内容(不包含前4个)
                  stowList = list.slice(5, list.length - 1)
                  moreList = list.slice(0, 4)
                } else {
                  //更多 前4个
                  moreList = list
                }
                this.setData({
                  stowList: stowList,
                  moreList: moreList
                })
              })
            }
          }
        }
      }
    }
  },

  options: {
    multipleSlots: true, //插槽
  },

  /**
   * 组件的初始数据
   */
  data: {
    stowList: [],
    moreList: [],
    dataList: [],
    dataListTemp: [],
    loadDataFlag: true, // 是否从数据库加载数据
  },

  /**
   * 组件生命周期
   */
  lifetimes: {
    attached: function () {
      let dataSourceCode = ''
      let typeName = this.data.typeName
      let name = ''

      if (typeName === 'other') {
        dataSourceCode = 'other'
        name = '其他'
      }
      if (typeName === 'salesChannel') {
        dataSourceCode = 'salesChannel'
        name = '销售渠道'
      }

      if (typeName === 'org') {
        dataSourceCode = 'org'
        name = '业务部门'
      }
       //收付款类型
       if (typeName === 'rpType') {
        dataSourceCode = 'rpType'
        name = '收付款类型'
        let stowList = [{
          name: '收款',
          id: '收付款类型-收款',
          infotype: 'default'
        },         
        {
          name: '付款',
          id: '收付款类型-付款',
          infotype: 'default'
        },
        ];
        this.setData({
          moreList: stowList,
          moreFlag: false,
          loadDataFlag: false
        })
      }
      //业务员
      if (typeName === 'staff') {
        dataSourceCode = 'staff'
        name = '业务员'
      }
       //入库类型
       if (typeName === 'intoType') {
        dataSourceCode = 'intoType'
        name = '入库类型'
        let stowList = [{
          name: '采购入库',
          id: '入库类型-采购入库',
          infotype: 'default'
        },
        {
          name: '销退出库',
          id: '出库类型-销退出库',
          infotype: 'default'
        },
        
        {
          name: '其他入库',
          id: '入库类型-其他入库',
          infotype: 'default'
        },
   
        ];
        this.setData({
          moreList: stowList,
          moreFlag: false,
          loadDataFlag: false
        })
      }
      //出库进度
      if (typeName === 'outStatus') {
        dataSourceCode = 'outStatus'
        name = '出库进度'
        let stowList = [{
          name: '未出库',
          id: 0,
          infotype: 'default'
        },
        {
          name: '出库中',
          id: 1,
          infotype: 'default'
        },
        {
          name: '已出库',
          id: 2,
          infotype: 'default'
        },
        {
          name: '有退货',
          id: 3,
          infotype: 'default'
        }
        ];
        this.setData({
          moreList: stowList,
          moreFlag: false,
          loadDataFlag: false
        })
      }
      //入库类型
      if (typeName === 'receiptType') {
        dataSourceCode = 'entryType'
        name = '入库类型'
      }
      //入库原因
      if (typeName === 'intoReason') {
        dataSourceCode = 'inReason'
        name = '入库原因'
        let stowList = [{
          name: '盘盈入库',
          id: '10112024-0314-0000-0000-000010d79635',
          infotype: 'default'
        },         
      
        ];
        this.setData({
          moreList: stowList,
          moreFlag: false,
          loadDataFlag: false
        })
      }
      //出库类型
      if (typeName === 'outType') {
        dataSourceCode = 'outType'
        name = '出库类型'
        let stowList = [{
          name: '销售出库',
          id: '出库类型-销售出库',
          infotype: 'default'
        },
        
        {
          name: '采退入库',
          id: '入库类型-采退入库',
          infotype: 'default'
        },
        {
          name: '其他出库',
          id: '出库类型-其他出库',
          infotype: 'default'
        },
      
        ];
        this.setData({
          moreList: stowList,
          moreFlag: false,
          loadDataFlag: false
        })
      }

      //出库原因
      if (typeName === 'outReason') {
        dataSourceCode = 'outReason'
        name = '出库原因'
        let stowList = [{
          name: '破损出库',
          id: '10112024-0312-0000-0000-00000f5759fd',
          infotype: 'default'
        },         
      
        ];
        this.setData({
          moreList: stowList,
          moreFlag: false,
          loadDataFlag: false
        })
      }
      //采购入库状态
      if (typeName === 'inInventoryStatus') {
        dataSourceCode = 'inInventoryStatus'
        name = '入库状态'
        let stowList = [{
          name: '待入库',
          id: 0,
          infotype: 'default'
        },
        {
          name: '已入库',
          id: 1,
          infotype: 'default'
        }
        ];
        this.setData({
          moreList: stowList,
          moreFlag: false,
          loadDataFlag: false
        })
      }
      //采购状态
      if (typeName === 'purchaseStatus') {
        dataSourceCode = 'purchaseStatus'
        name = '采购状态'
        let stowList = [{
          name: '暂存',
          id: 0,
          infotype: 'default'
        },
        {
          name: '成交',
          id: 1,
          infotype: 'default'
        },
        {
          name: '出库中',
          id: 2,
          infotype: 'default'
        },
        {
          name: '待入库',
          id: 3,
          infotype: 'default'
        },
        {
          name: '已入库',
          id: 4,
          infotype: 'default'
        }
        ];
        this.setData({
          moreList: stowList,
          moreFlag: false,
          loadDataFlag: false
        })
      }
      //退货状态
      if (typeName === 'outInventoryStatus') {
        dataSourceCode = 'outInventoryStatus'
        name = '退货状态'
        let stowList = [{
          name: '审批中',
          id: 0,
          infotype: 'default'
        },
        {
          name: '未出库',
          id: 1,
          infotype: 'default'
        },
        {
          name: '已出库',
          id: 2,
          infotype: 'default'
        },
        {
          name: '已退货',
          id: 3,
          infotype: 'default'
        }
        ];
        this.setData({
          moreList: stowList,
          moreFlag: false,
          loadDataFlag: false
        })
      }
      
      if (typeName === 'purchaseAccordingStatus') {
        dataSourceCode = 'purchaseAccordingStatus'
        name = '采购状态'
        let stowList = [
        {
          name: '成交',
          id: 0,
          infotype: 'info'
        },
        {
          name: '出库中',
          id: 1,
          infotype: 'default'
        }
        ];
        this.setData({
          moreList: stowList,
          moreFlag: false,
          loadDataFlag: false
        })
      }
      
      //收款进度
      if (typeName === 'receiveStatus') {
        dataSourceCode = 'receiveStatus'
        name = '收款进度'
        let stowList = [{
          name: '未收款',
          id: 0,
          infotype: 'default'
        },
        {
          name: '部分收款',
          id: 1,
          infotype: 'default'
        },
        {
          name: '收款完成',
          id: 2,
          infotype: 'default'
        }
        ];
        this.setData({
          moreList: stowList,
          moreFlag: false,
          loadDataFlag: false
        })
      }
      
      //客户接待
      if (typeName === 'followStatus') {
        dataSourceCode = 'followStatus'
        name = '接待类型'
        let stowList = [{
          name: '留资接待',
          id: 0,
          infotype: 'default'
        },
        {
          name: '陌生接待',
          id: 1,
          infotype: 'default'
        }
        ]
        this.setData({
          moreList: stowList,
          moreFlag: false,
          loadDataFlag: false
        })
      }

      this.setData({
        dataSourceCode: dataSourceCode,
        name: name
      })
    },
    detached: function () {
      // 在组件实例被从页面节点树移除时执行
    },
  },

  /**
   * 组件的方法列表
   */
  methods: {
    /**
     * @desc : 筛选更多触发事件
     * @author : 于继渤
     * @date : 2022/5/28 08:50
     */
    onChangeMore(e) {
      if (this.data.stowList.length == 0) {
        return
      }
      this.setData({
        moreFlag: !this.data.moreFlag
      })
    },
    /**
     * @desc : 点击标签事件
     * @author : 于继渤
     * @date : 2022/5/28 08:50
     */
    onClickTag(e) {
      let key = e.currentTarget.dataset.key
      let typekey = e.currentTarget.dataset.typekey
      let index = e.currentTarget.dataset.index
      let moreList = this.data.moreList
      let stowList = this.data.stowList
      if (key === 'more') {
        if (moreList[index].infotype === 'default') {
          moreList[index].infotype = 'info'
        } else if (moreList[index].infotype === 'info') {
          moreList[index].infotype = 'default'
        }
      }
      if (key === 'stow') {
        if (stowList[index].infotype === 'default') {
          stowList[index].infotype = 'info'
        } else if (stowList[index].infotype === 'info') {
          stowList[index].infotype = 'default'
        }
      }
      this.setData({
        moreList: moreList,
        stowList: stowList
      })
      let dataList = this.data.dataList
      if (moreList.length > 0) {
        moreList.forEach(res1 => {
          if (res1.infotype === 'info') {
            dataList.push(res1)
          }
        })
      }
      if (this.data.oneSelectFlag) {
        moreList.forEach(itt => {
          if (itt.name == moreList[index].name) {
            itt.infotype = 'info'
          } else {
            itt.infotype = 'default'
          }

        })

        this.setData({
          moreList: moreList
        })

      }
      if (stowList.length > 0) {
        stowList.forEach(res2 => {
          if (res2.infotype === 'info') {
            dataList.push(res2)
          }
        })
      }

      let dataListTemp = []

      dataList.forEach(res3 => {
        if (res3.infotype == 'info') {
          dataListTemp.push(res3.id)
        }
      })


      dataList = dataList.filter((item, index) => {

        return dataList.indexOf(item) === index
      })

      this.triggerEvent('onClickTag', {
        dataList: Array.from(new Set(dataListTemp)),
        key: typekey
      })
    },

    /**
     * @desc : 重置事件
     * @author : 于继渤
     * @date : 2022/5/28 08:50
     */
    clearForm() {
      let moreList = this.data.moreList
      let stowList = this.data.stowList
      if (moreList.length > 0) {
        moreList.forEach(res => {
          res.infotype = 'default'
        })
      }
      if (stowList.length > 0) {
        stowList.forEach(res => {
          res.infotype = 'default'
        })
      }
      this.setData({
        moreList: moreList,
        stowList: stowList,
        dataList: []
      })
    },
    /**
     * @desc : 筛选数据源获取
     * @author : 于继渤
     * @date : 2022/5/28 08:50
     */
    getData(dataSourceCode) {
      let param = {
        dataSourceCode: dataSourceCode
      }
      return new Promise((resolve, reject) => {
        api.request(Constants.BASIC_COMMON_API + 'get_wx_init_data', 'POST', param).then(res => {
          if (res.data.code === 200) {
            resolve(res.data)
          } else {
            reject(res.data)
          }
        })
      })
    },

  }
})