<template>
  <div>
    <van-cell-group>
      <EmployeeSelectorCell
        required
        bar_title="选择录入对象"
        title="录入对象"
        name="录入对象"
        v-validate="'required'"
        v-model="employee_id_arr"
        :multi="false"
		    :is_filtration_creator="true"
      ></EmployeeSelectorCell>

      <van-cell center title="指定规则">
        <template #right-icon>
          <div>
            <van-switch v-model="show_rule" inactive-color="#9a9a9a" size="24" />
          </div>
        </template>
      </van-cell>

      <div v-show="show_rule">
        <RuleCategorySelectorCell @confirm="rule_item_confirm" title="选择分类和规则" v-model="rule_item"  :multi="false" :max="1"></RuleCategorySelectorCell>
        <van-cell v-for="(d, i) in rule_item" :key="i" size="large" :label="d.remark">
          <template slot="title">
            {{get_point_name(d.pt_id)}}
            <span v-show="d.range_type == 1">{{d.min_point}}</span>
            <span v-show="d.range_type == 2">{{d.min_point}}—{{d.max_point}}</span>
          </template>
        </van-cell>
      </div>

      <van-cell required>
        <Mtextarea v-model="item.remark" name="申请事由" v-validate="'required|max:100'" placeholder="请输入申请事由(最多100字)" :text_max="100" :imgs_max="3" images speech :imgs.sync="item.files"></Mtextarea>
      </van-cell>
      <DateCell required title="选择日期" v-model="item.event_time"></DateCell>
      <div>
        <EmployeeSelectorCell v-show="superior_list.length != 0" required title="审批人" name="审批人" v-validate="'required'" :employee_list="superior_list" use_employee_list v-model="employee_list" :multi="false" ></EmployeeSelectorCell>
        <van-cell v-show="superior_list.length == 0" required title="审批人" is-link @click="$toast('您没有审批人，请联系管理员')"></van-cell>
      </div>
    </van-cell-group>
  </div>
</template>
<script>
import Mtextarea from '@/components/common/Mtextarea'
import EmployeeSelectorCell from '@/components/common/EmployeeSelectorCell'
import RuleCategorySelectorCell from '@/components/common/RuleCategorySelectorCell1'
import DateCell from '@/components/common/DateCell'

import moment from 'moment'
import Vue from 'vue'
import { Switch } from 'vant'
Vue.use(Switch)

export default {
  // 数据
  model: {
    prop: 'list',
    event: 'value'
  },
  props: {
    num: {
      type: Number,
      default: 0
    }
  },
  name: 'integralApplyItem',
  data () {
    let numberIndex = this.num
    return {
      item: {
        employee_id: '',
        event_time: moment().format('YYYY-MM-DD'),
        reviewer_id: '',
        rule_id: 0,
        item_id: 0,
        remark: '',
        files: [],
        num: numberIndex
      },
      employee_list: [],
      rule_item: [],
      show_rule: true,
      employee_id_arr: [],
      superior_list: [],
      point_types: JSON.parse(localStorage.getItem('SET_POINT_TYPES')) || JSON.parse(plus.storage.getItem('SET_POINT_TYPES')),
      user_info: JSON.parse(localStorage.getItem('user_info')) || JSON.parse(plus.storage.getItem('user_info'))
    }
  },
  watch: {
    employee_id_arr (val) {
      if (val.length == 0) {
        this.item.employee_id = ''
      } else {
        this.item.employee_id = val[0].id
        var obj = this.employee_list_fun(val[0].id)
        this.employee_list = obj ? [obj] : []
      }
    },
    employee_list (val) {
      if (val) {
        val.length == 0 ? this.item.reviewer_id = '' : this.item.reviewer_id = val[0].id
      }
    },
    'item': {
      handler (val, oldVal) {
        this.$emit('value', this.item)
      },
      deep: true
    },
    show_rule (val) {
      if (!val) {
        this.item.rule_id && this.item.item_id ? this.item.remark = '' : ''
        this.rule_item = []
        this.item.rule_id = 0
        this.item.item_id = 0
      }
    }
  },
  // 组件
  components: {Mtextarea, EmployeeSelectorCell, RuleCategorySelectorCell, DateCell},
  // 方法
  methods: {
    rule_item_confirm (index, data) {
      this.rule_item = data
      this.item.rule_id = data[0].rule_id
      this.item.item_id = data[0].id
      this.item.remark = JSON.parse(JSON.stringify(data[0].remark))
    },
    get_point_name (id) {
      let name = ''
      this.point_types.forEach(element => {
        if (element.id == id) {
          name = element.name
        }
      })
      return name
    },
    // 审批人
    employee_list_fun (id) { // id: 录入对象ID
      const employee_list = window.plus ? JSON.parse(plus.storage.getItem('SET_EMPLOYEE_MAP')) : JSON.parse(localStorage.getItem('SET_EMPLOYEE_MAP'))
      for (const i in employee_list) {
        if (employee_list[i].id == id) { // 拿到选中的录入对象的信息
          this.superior_list = employee_list[i].superior_list
          if (employee_list[i].superior_id != 0) { // 判断有无设置直属上级
            for (let a in employee_list[i].superior_list) { // 遍历上级
              if (employee_list[i].superior_id == employee_list[i].superior_list[a].id) {
                employee_list[i].superior_list[a].identification = true
                return employee_list[i].superior_list[a]
              }
            }
          } else {
            if (employee_list[i].department !== null && employee_list[i].department.length > 0) {
              return this.manageDeptEmployee(employee_list[i].superior_list, employee_list[i].department[0].dept_id) || employee_list[i].superior_list[0]
            } else {
              return employee_list[i].superior_list[0]
            }
          }
        }
      }
    },
    // 管理人员是否同部门
    manageDeptEmployee (arr, id) {
      const employee_list = window.plus ? JSON.parse(plus.storage.getItem('SET_EMPLOYEE_MAP')) : JSON.parse(localStorage.getItem('SET_EMPLOYEE_MAP'))
      let obj = null
      arr.forEach(element => {
        for (const i in employee_list) {
          if (employee_list[i].id == element.id) {
            if (employee_list[i].department.length !== 0) {
              employee_list[i].department.forEach(item => {
                if (item.dept_id == id) {
                  obj = {id: employee_list[i].id, name: employee_list[i].name}
                }
              })
            }
          }
        }
      })
      return obj
    }
  },
  // 组件挂载完成
  mounted () {
    this.employee_id_arr = [{id: this.user_info.data.id, name: this.user_info.data.name, img_url: this.user_info.data.img_url}]
  }
}
</script>
<style scoped>
  .body_com{height: calc(100% - 1rem); position: relative;}
</style>
