/*******************************************************************************
 * Copyright(c) 2021 dongke All rights reserved. / Confidential
 * 类的信息：
 *		1.程序名称：
 *		2.功能描述：单选下拉组件
 * 编辑履历：
 *		作者				日期					版本				修改内容
 *		jyh		  	2022-5-5		1.00		      	新建
 *******************************************************************************/

const api = require('../../../utils/api');
const app = getApp()
const Constants = require('../../../utils/Constants');
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    /**
     * 客户来源:customerFrom,卫生间数:toiletCount,装修类型:fitupType,房屋类型:houseType,
     * 装修风格:fitupStyle,年龄构成:ageComposition,决策者:decisionMaker,结算类型:settlement,
     * 费用项目:feeItem,业务部门:org,性别：gender orther:其他分类
     */
    typeName: {
      type: String,
      default: 'org'
    },
    content: {
      type: String,
      default: 'org'
    },
    warehouseId: {
      type: String,
      default: 'org'
    },
    dataItem: {
      type: Object,
      default: {}
    },
    dynamicList: { //开单商品编辑临时数组
      type: Array,
      default: []
    },
    choooseFlg: {
      type: Boolean,
      default: false
    },
    docName: {
      type: String,
      value: ''
    },
    contentID: {
      type: Number,
      default: 0
    },
    channelId: {
      type: Number,
      default: null
    },
    jobIds: {
      type: Array,
      value: []
    },
    orgId: {
      type: Number,
      value: null
    },
    show: {
      type: Boolean,
      default: false,
      observer: function (newVal) {
        let show = this.properties.show
        if (!show) {
          return
        }
        // 给类型赋值
        this.setDataSource();
        let datalists = this.data.datalists;
        let datalist = datalists[this.data.typeName] || []
        if (!datalists[this.data.typeName] || datalists[this.data.typeName].length == 0) {
          let DataSourceCode = this.data.DataSourceCode
          let typeName = this.data.typeName
          this.getdata(DataSourceCode).then(res => {
            let list = res.data.data
            if (list == null || list.length <= 0) {
              this.setData({
                datalist: []
              })
              return;
            }

            let datalist = []
            // 部门
            if (typeName === 'org') {
              let nmb = 0
              let nmb_tenter = 0
              list.forEach(item2 => {
                let item = {
                  id: item2.id,
                  code: item2.orgCode,
                  name: item2.orgName,
                  checked: item2.id == this.properties.contentID ? true : false,
                }
                nmb_tenter++
                if (item2.id == this.properties.contentID) {
                  nmb = nmb_tenter
                }
                datalist.push(item)
              })
              this.setData({
                radio: this.properties.contentID ? (nmb - 1) : '',
                valueKey: 'id',
                labelKey: 'name'
              })
            }
            // 员工
            if (typeName === 'staff') {
              let nmb = 0
              let nmb_tenter = 0
              list.forEach(item2 => {
                let item = {
                  id: item2.id,
                  code: item2.staffCode,
                  name: item2.staffName,
                  checked: item2.id == this.properties.contentID ? true : false
                }
                nmb_tenter++
                if (item2.id == this.properties.contentID) {
                  nmb = nmb_tenter
                }
                datalist.push(item)
              })
              this.setData({
                radio: this.properties.contentID ? (nmb - 1) : '',
                valueKey: 'id',
                labelKey: 'name'
              })
            }
            //客户来源
            if (typeName == 'customerFrom') {
              let nmb = 0
              let nmb_tenter = 0
              list.forEach(item2 => {
                let item = {
                  id: item2.id,
                  code: item2.dataCode,
                  name: item2.dataValue,
                  checked: item2.id == this.properties.contentID ? true : false
                }
                nmb_tenter++
                if (item2.id == this.properties.contentID) {
                  nmb = nmb_tenter
                }
                datalist.push(item)
              })
              this.setData({
                radio: this.properties.contentID ? (nmb - 1) : '',
                valueKey: 'id',
                labelKey: 'name'
              })
            }
            //销售渠道
            if (typeName == 'saleChannel') {
              let nmb = 0
              let nmb_tenter = 0
              list.forEach(item2 => {
                let item = {
                  id: item2.id,
                  code: item2.channelCode,
                  name: item2.channelName,
                  checked: item2.id == this.properties.contentID ? true : false
                }
                nmb_tenter++
                if (item2.id == this.properties.contentID) {
                  nmb = nmb_tenter
                }
                datalist.push(item)
              })
              this.setData({
                radio: this.properties.contentID ? (nmb - 1) : '',
                valueKey: 'id',
                labelKey: 'name'
              })
            }
            //仓库
            if (typeName == 'warehouse') {
              let nmb = 0
              let nmb_tenter = 0
              list.forEach(item2 => {
                let item = {
                  id: item2.id,
                  code: item2.whCode,
                  name: item2.whName,
                  checked: item2.id == this.properties.contentID ? true : false
                }
                nmb_tenter++
                if (item2.id == this.properties.contentID) {
                  nmb = nmb_tenter
                }
                datalist.push(item)
              })
              this.setData({
                radio: this.properties.contentID ? (nmb - 1) : '',
                valueKey: 'id',
                labelKey: 'name'
              })
            }
            //客户意向 留店时长 邀约结果 量尺状态
            if (typeName == 'customerIntention' || typeName == 'storeRetentionTime' || typeName === 'inviteResult' || typeName === 'scaleStatus' || typeName == 'followType') {
              let nmb = 0
              let nmb_tenter = 0
              list.forEach(item2 => {
                let item = {
                  id: item2.id,
                  code: item2.kindCode,
                  name: item2.kindName,
                  checked: item2.id == this.properties.contentID ? true : false
                }
                nmb_tenter++
                if (item2.id == this.properties.contentID) {
                  nmb = nmb_tenter
                }
                datalist.push(item)
              })
              this.setData({
                radio: this.properties.contentID ? (nmb - 1) : '',
                valueKey: 'id',
                labelKey: 'name'
              })
            }


            let finallist = [...datalist]
            datalists[this.data.typeName] = [...datalist]
            this.setData({
              datalists: datalists,
              finallist: finallist
            })
          })

        } else {
          if (this.properties.contentID) {
            // 重刷选中项
            datalists[this.data.typeName].forEach(item => {
              item.checked = item.id == this.properties.contentID ? true : false;
            })
            this.setData({
              datalists: datalists,
            })
          }
        }
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    DataSourceCode: '',
    // 是否点击清除按钮
    isClickClear: false,
    // datalist: [],
    finallist: [],
    condition: '',
    dataValue: '',
    name: '',
    valueKey: 'id',
    labelKey: 'name',
    radio: null,
    label_title: '可选',
    count: 0,
    datalists: {},
    pageInfo: {
      pageSize: 5,
      currentPage: 1
    }
  },
  lifetimes: {
    attached: function () {

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


  /**
   * 组件的方法列表
   */
  methods: {
    /**
     * @desc   : 给类型赋值
     * @author : 周兴
     * @date   : 2024/1/26 11:46
     */
    setDataSource() {
      let DataSourceCode = ''
      let typeName = this.data.typeName
      let name = ''
      if (typeName === 'customerFrom') {
        DataSourceCode = 'customerFrom'
        name = '客户来源'
      }
      if (typeName === 'saleChannel') {
        DataSourceCode = 'saleChannel'
        name = '销售渠道'
      }

      if (typeName === 'org') {
        DataSourceCode = 'org'
        name = '业务部门'
      }
      //业务员
      if (typeName === 'staff') {
        DataSourceCode = 'staff'
        name = '员工'
      }
      if (typeName === 'customerIntention') {
        DataSourceCode = 'customerIntention'
        name = '客户意向'
      }
      if (typeName === 'storeRetentionTime') {
        DataSourceCode = 'storeRetentionTime'
        name = '留店时长'
      }
      if (typeName === 'inviteResult') {
        DataSourceCode = 'inviteResult'
        name = '邀约结果'
      }
      if (typeName === 'scaleStatus') {
        DataSourceCode = 'scaleStatus'
        name = '量尺状态'
      }
      if (typeName === 'followType') {
        DataSourceCode = 'followType'
        name = '跟进方式'
      }
      if (typeName === 'warehouse') {
        DataSourceCode = 'warehouse'
        name = '仓库'
      }
      this.setData({
        DataSourceCode: DataSourceCode
      })
    },
    /**
     * 查询数据
     * @param {*} e 
     */
    getdata: function (DataSourceCode, e) {
      let param = {
        dataSourceCode: DataSourceCode
      }
      let SELECT_API = 'get_init_data'
      return new Promise((resolve, reject) => {
        api.request(Constants.BASIC_COMMON_API + SELECT_API, 'POST', param).then(res => {
          if (res.data.code === 200) {
            resolve(res)
          } else {
            reject(res)
          }
        })
      })
    },
    /**
     * 关闭弹窗
     */
    onClose: function () {
      // this.reset()
      this.setData({
        show: false
      })
      // 为了关闭外面的下拉
      this.triggerEvent('muticlose')
    },
    /**
     * 焦点离开事件
     */
    blur_input() {
      if (this.data.datalists[this.data.typeName].length <= 0) {
        this.reset()
      }
    },
    /**
     * @desc   : 值改变
     * @author : 周兴
     * @date   : 2024/1/26 11:46
     */
    onChange(e) {
      let datalist = this.data.datalists[this.data.typeName] || []
      if (datalist && datalist.length > e.detail) {
        datalist.forEach(it => {
          it.checked = false;
        })
        datalist[e.detail].checked = true;
        let item = datalist[e.detail];
        this.setData({
          radio: e.detail,
          // datalist: datalist,
          show: false
        })
        console.log('ttt34', item, this.data.valueKey);
        this.triggerEvent('commit', {
          value: item[this.data.valueKey],
          text: item[this.data.labelKey],
          item: item
        })
      }
    },
    /**
     * 过滤数据
     * @param {*} e 
     */

    onChange_input: function (e) {

      let finallist = this.data.finallist
      if (finallist === null || finallist.length === 0) return
      // 复制
      finallist = [...finallist]
      let condition = this.data.condition
      this.setData({
        radio: '',
      })
      let datalist = []
      finallist.forEach(item => {
        if (item.name.indexOf(condition) >= 0) {
          datalist.push(item)
        }
      })
      // 单选框的true false显示 是radio控制  
      let nmb = 0
      let nmb_tenter = 0
      datalist.forEach(item2 => {
        nmb_tenter++
        if (item2.name == this.data.clickRodioButton) {
          nmb = nmb_tenter
          this.setData({
            radio: nmb - 1
          })
        }
      })
      let datalists = this.data.datalists
      datalists[this.data.typeName] = datalist
      this.setData({
        datalists: datalists
      })
    },
    /**
     * 重置
     */
    reset: function () {
      // let datalist = this.data.datalist
      let datalist = this.data.datalists[this.data.typeName]
      let finallist = this.data.finallist
      if (finallist == null || finallist.length === 0) return

      finallist.forEach(item => {
        item.checked = false
      })

      datalist = [...finallist]

      this.setData({
        finallist: finallist,
        // datalist: datalist,
        datalists: datalists,
        condition: '',
        radio: '',

      })
    },
    /**
     * 清除数据
     */
    clear() {
      let finallist = this.data.finallist
      // 清空勾选框
      finallist.forEach(item => {
        item.checked = false
      })

      let datalist = [...finallist]
      this.triggerEvent('commit', {
        ids: '',
        names: ''
      })
      let datalists = this.data.datalists
      datalists[thsi.data.typeName] = datalist

      // 清空控件
      this.setData({
        // datalist: datalist,
        datalists: datalists,
        finallist: finallist,
        condition: '',
        isClickClear: true,
        dataValue: '',
        count: 0,
        radio: null,
        label_title: '可选',
        defaultCheackFlag: true
      })
    },
    /**
     * 提交
     */
    commit: function () {
      // let datalist = this.data.datalist
      let datalist = this.data.datalists[this.data.typeName]
      if (datalist == null || datalist.length === 0) return
      // 获取选中的那条数据
      let filters = datalist.filter(it => it.checked);
      if (filters && filters.length > 0) {
        let item = filters[0];
        this.triggerEvent('commit', {
          value: item[this.data.valueKey],
          text: item[this.data.labelKey],
          item: item
        })
        this.setData({
          show: false
        })
      }
    },
  },

})