<template>
  <div>
    <van-nav-bar title="积分录入" left-text="返回" @click-left="$route_back" left-arrow></van-nav-bar>
    <div class="body_com has_header">
      <scroller>

        <van-cell-group>
          <EmployeeSelectorCell required title="录入对象" use_employee_list :employee_list="manage_scope" v-model="employee_list" :multi="true"
                                :max="10"></EmployeeSelectorCell>
        </van-cell-group>
        <van-cell-group>
          <RuleCategorySelectorCell ref="rule_selector" title="选择分类和规则" v-model="rule_item" :multi="true" :max="20">
            <van-cell-group slot="append">
              <van-cell title="使用自定义录入" is-link label="找不到规则?" @click="entry_diy"></van-cell>
            </van-cell-group>
          </RuleCategorySelectorCell>
        </van-cell-group>
        <van-cell-group v-for="(item, index) in rule_item" :key="index" v-if="entry_type==1">

          <van-cell :title="'第'+(index+1)+'条'">
            <template slot="default">
              <van-button size="mini" type="danger" @click="item_del(index)">删除</van-button>
            </template>
          </van-cell>
          <van-cell required>
            <Mtextarea v-model="item.remark" placeholder="请输入申请事由(最多50字)" :text_max="100" :imgs_max="3" images speech
                       :imgs.sync="item.files"></Mtextarea>
          </van-cell>
          <van-field v-model="item.point" label="分值" placeholder="请输入分值" required/>
          <DateCell required title="选择日期" v-model="item.event_time"></DateCell>
          <EmployeeSelectorCell title="审批人" use_employee_list v-model="item.reviewer_list" :multi="false" :max="1"></EmployeeSelectorCell>
        </van-cell-group>


        <van-cell-group v-for="(item, index) in diy_rule_item" :key="index" v-if="entry_type==2">
          <van-cell :title="'第'+(index+1)+'条'">
            <template slot="default">
              <van-button size="mini" type="danger" @click="diy_item_del(index)">删除</van-button>
            </template>
          </van-cell>
          <van-cell required>
            <Mtextarea v-model="item.remark" placeholder="请输入申请事由(最多100字)" :text_max="100" :imgs_max="3" images speech
                       :imgs.sync="item.files"></Mtextarea>
          </van-cell>
          <CategorySelectorCell title="选择分类" v-model="item.rule_cate" :max="1" :multi="false" required></CategorySelectorCell>
          <van-field v-model="item.point" label="分值" placeholder="请输入分值" required/>
          <DateCell required title="选择日期" v-model="item.event_time"></DateCell>
          <EmployeeSelectorCell title="审批人" use_employee_list :employee_list="employee_list" v-model="item.reviewer_list" :multi="false" :max="1"></EmployeeSelectorCell>
        </van-cell-group>

        <van-cell-group v-if="entry_type==2">
          <van-cell title="增加一条" @click="add_diy_item" is-link></van-cell>
        </van-cell-group>

        <div style="padding:0.32rem;">
          <van-button size="large" @click="data_verify" type="info" :loading="send_loading">提交</van-button>
        </div>
      </scroller>
    </div>
  </div>
</template>
<script>
  import request from '@/utils/request'
  import Mtextarea from '@/components/common/Mtextarea'
  import EmployeeSelectorCell from '@/components/common/EmployeeSelectorCell'
  import RuleCategorySelectorCell from '@/components/common/RuleCategorySelectorCell1'
  import CategorySelectorCell from '@/components/common/CategorySelectorCell'
  import DateCell from "@/components/common/DateCell";
  import moment from 'moment'

  export default {
    name: "integral_entry_b",
    components: {
      DateCell,
      Mtextarea, EmployeeSelectorCell, RuleCategorySelectorCell, CategorySelectorCell
    },
    watch: {
      rule_item: {
        handler(val) {
          let date = moment().format('YYYY-MM-DD')
          let pt_id = 0
          for(let i in this.types_list_array){
            if(this.types_list_array[i].code == 'BF'){
              pt_id = this.types_list_array[i].id
            }
          }
          for (let i in val) {
            if (typeof(val[i]['reviewer_id']) == 'undefined') {
              this.rule_item[i]['reviewer_id'] = 0
            }
            if (typeof(val[i]['rule_id']) == 'undefined') {
              this.rule_item[i]['rule_id'] = val[i].rule_id
            }
            if (typeof(val[i]['item_id']) == 'undefined') {
              this.rule_item[i]['item_id'] = val[i].id
            }
            if (typeof(val[i]['event_time']) == 'undefined') {
              this.rule_item[i]['event_time'] = date
            }
            if (typeof(val[i]['point']) == 'undefined') {
              this.rule_item[i]['point'] = val[i].min_point
            }
            if (typeof(val[i]['remark']) == 'undefined') {
              this.rule_item[i]['remark'] = ''
            }
            if (typeof(val[i]['pt_id']) == 'undefined') {
              this.rule_item[i]['pt_id'] = 0
            }
            if (typeof(val[i]['files']) == 'undefined') {
              this.rule_item[i]['files'] = []
            }
            if (typeof(val[i]['remark']) == 'undefined') {
              this.rule_item[i]['remark'] = ''
            }
            if (typeof(val[i]['reviewer_list']) == 'undefined') {
              this.rule_item[i]['reviewer_list'] = []
            }
            if (val[i].reviewer_list && val[i].reviewer_list.length > 0 && val[i]['reviewer_id'] != val[i].reviewer_list[0].id) {
              this.rule_item[i]['reviewer_id'] = val[i].reviewer_list[0].id
            }else if(val[i].reviewer_list.length == 0){
              this.rule_item[i]['reviewer_id'] = 0
            }
          }
          if(this.rule_item.length > 0){
            this.entry_type = 1
          }
          if(this.rule_item_change_count < 1){
            this.rule_item_change_count ++
            this.rule_item = JSON.parse(JSON.stringify(this.rule_item))
          }else if(this.rule_item_change_count == 1){
            this.rule_item_change_count = 0
          }
        },
        deep: true
      },
      diy_rule_item: {
        handler(val, old) {
          for (let i in val) {
            if (val[i].rule_cate && val[i].rule_cate.length > 0 && val[i]['rule_id'] != val[i].rule_cate[0].id) {
              this.diy_rule_item[i]['rule_id'] = val[i].rule_cate[0].id
            }else if(val[i].rule_cate.length == 0){
              this.diy_rule_item[i]['rule_id'] = 0
            }
            if (val[i].reviewer_list && val[i].reviewer_list.length > 0 && val[i]['reviewer_id'] != val[i].reviewer_list[0].id) {
              this.diy_rule_item[i]['reviewer_id'] = val[i].reviewer_list[0].id
            }else if(val[i].reviewer_list.length == 0){
              this.diy_rule_item[i]['reviewer_id'] = 0
            }
          }
        },
        deep: true
      },
      employee_list: function(val){
        if (val.length > 0) {
          this.data.members = []
          for (let i in val) {
            this.data.members.push(val[i].id)
          }
        } else {
          this.data.members = []
        }
      },
      entry_type: function(val){
        if (val == 1) {
          //选择规则录入
        } else {
          //自定义录入
          this.init_diy_item()
        }
      }
    },
    data() {
      return {
        manage_scope: this.$store.getters.user_info.employee_detail.manage_scope,
        entry_type: 1,
        data: {
          members: [],
          items: []
        },
        diy_rule_item: [],
        types_list_array: [],
        types_list: {},
        rule_item: [],
        rule_item_change_count: 0,
        send_loading: false,
        employee_list: []
      }
    },
    mounted(){
      //刷新当前角色下级上级
      request('get', '/api/employee/detail')
        .then(res => {
          let list = res.data.data.user
          this.manage_scope = list.employee_detail.manage_scope
          this.employee_list = list.employee_detail.superior_list
        })
    },
    methods: {
      init_diy_item() {
        let date = moment().format('YYYY-MM-DD')
        let pt_id = 0
        for(let i in this.types_list_array){
          if(this.types_list_array[i].code == 'BF'){
            pt_id = this.types_list_array[i].id
          }
        }
        this.diy_rule_item = [
          {
            checked: true,
            event_time: date,
            files: [],
            item_id: 0,
            point: '',
            pt_id: pt_id,
            remark: '',
            reviewer_id: 0,
            rule_id: 0,
            reviewer_list: [],
            rule_cate: []
          }
        ]
      },
      add_diy_item() {
        let date = moment().format('YYYY-MM-DD')
        let pt_id = 0
        for(let i in this.types_list_array){
          if(this.types_list_array[i].code == 'BF'){
            pt_id = this.types_list_array[i].id
          }
        }
        this.diy_rule_item.push({
          checked: true,
          event_time: date,
          files: [],
          item_id: 0,
          point: '',
          pt_id: pt_id,
          remark: '',
          reviewer_id: 0,
          rule_id: 0,
          reviewer_list: [],
          rule_cate: []
        })
      },
      entry_diy() {
        this.$refs['rule_selector'].show_dept_selector = false
        this.entry_type = 2
        this.rule_item = []
      },
      item_del(index) {
        this.rule_item.splice(index, 1)
      },
      diy_item_del(index) {
        this.diy_rule_item.splice(index, 1)
      },
      get_point_types() {
        let self = this
        request('get','/api/integral/types').then(res => {
          if (res.data.code == 1) {
            for (let i in res.data.data.list) {
              self.types_list[res.data.data.list[i].id] = res.data.data.list[i].name
            }
            self.types_list_array = res.data.data.list
          }
        }).catch(e => {
        })
      },
      data_verify() {
        let self = this
        self.$validator.validateAll().then(result => {
          if (!result) {
            self.$notify({type: 'danger', message: self.$validator.errors.items[0].msg});
          } else {
            self.send()
          }
        })
      },
      send() {
        let self = this
        self.send_loading = true
        self.$toast.loading({
          message: '正在处理'
        })
        let data = null
        if(self.entry_type == 1){
          self.data.items = self.rule_item
        }else{
          self.data.items = self.diy_rule_item
        }
        data = self.data
        request({
          url: '/api/integral/point/entry',
          method: 'post',
          data: data
        }).then(res => {
          self.send_loading = false
          self.$toast.clear()
          if(res.data.code == 1){
            self.data.members = []
            self.data.items = []
            self.rule_item = []
            self.employee_list = []
            self.diy_rule_item = []
            self.$toast.success(res.data.msg)
          }else{
            self.$toast(res.data.msg)
          }
        }).catch(e => {
          self.send_loading = false
          self.$toast.clear()
        })
      }
    },
    created() {
      this.get_point_types()
    }
  }
</script>
<style scoped>
  .body_com {
    height: calc(100% - 0.92rem);
    position: relative;
  }
</style>
