<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>
<!--          <div class="tip" slot="title" style="text-align: right;">使用之前的绩效工作？
             <quoteMark @confirm="event_select" :base_point="base_point" title="去引用"></quoteMark>
          </div> -->
          <van-cell required>
            <Mtextarea
              v-model="data.task_name"
              placeholder="输入任务内容"
              name="任务内容"
              v-validate="'required|min:3'"
              :text_max="20"
              :imgs_max="3"
              images
              :imgs.sync="data.file_list"
            ></Mtextarea>
          </van-cell>
        </van-cell-group>
        <van-cell-group>
          <div class="tip" slot="title" v-if="(surplus/base_point*100).toFixed(0) >= 0">
            <div v-if="surplus_mark == 0">权重还剩余{{(surplus/base_point*100).toFixed(0)}}%可以分配</div>
            <div v-if="surplus_mark > 0">权重分配已超出100%，当前额外分配<span style="color:#26A2FF;">{{surplus_mark}}%</span></div>
          </div>
          <van-field label="权重" @input="changeunqualified" v-model="data.weight" name="权重" placeholder="请输入权重百分比" type="Number" right-icon="%" v-validate="'required'" required >
          <template slot="right-icon">
              <smallTip icon="question-o" position="30%">
                <p style="margin: 0; line-height: 0.6rem; text-align: left;">本条任务占月绩效的比重，权重越高越重要</p>
            </smallTip>
          </template>
          </van-field>
          <van-field :disabled="true" label="任务积分" name="任务积分" v-model="data.base_point" type="Number" v-validate="'required|numeric'" placeholder="月平衡分*权重" required />
        </van-cell-group>
        <van-cell-group>
        <DateTimeCell required v-validate="'required'" name="截止时间" title="截止时间" v-model="data.expire_time"></DateTimeCell>
      </van-cell-group>
        <van-cell-group>
          <van-cell>
            <Mtextarea v-model="data.task_remark" placeholder="输入任务备注" :text_max="100"></Mtextarea>
          </van-cell>
        </van-cell-group>
        <van-cell-group>
          <EmployeeSelectorCell required title="审批人" :employee_list="superior_list" use_employee_list  v-model="reviewer_list" v-validate="'required'" name="审批人" :multi="false"></EmployeeSelectorCell>
        </van-cell-group>
        <div style="padding:0.32rem;">
          <van-button size="large" @click="data_verify" type="info">确认修改</van-button>
        </div>
      </scroller>
    </div>
  </div>
</template>
<script>
import Vue from 'vue'
import request from '@/utils/request'
import Mtextarea from '@/components/common/Mtextarea'
import EmployeeSelectorCell from '@/components/common/EmployeeSelectorCell'
import DateTimeCell from '@/components/common/DateTimeCell'
import smallTip from '@/components/common/smallTip'
import quoteMark from '@/components/common/quoteMark'
import {Overlay, calendar} from 'vant'
Vue.use(Overlay).use(calendar)
export default {
  name: 'compile_efficiency',
  components: {
    DateTimeCell,
    quoteMark,
    smallTip,
    Mtextarea,
    EmployeeSelectorCell
  },
  data () {
    return {
      data: {
        targets: [], // 工作执行者列表,以逗号分割的字符串或直接传入数组
        pt_id: 0, // 积分种类id
        task_name: '', // 任务名
        task_remark: '', // 任务详细描述
        base_point: null, // 任务参考积分
        expire_time: '', // 截至时间 格式:2020-02-02 12：00
        package_id: 0, // 绩效任务包id 绩效任务包发布工作时必须大于0，其余工作发布为0
        weight: null, //	绩效权重 绩效任务包发布工作时必须大于0，其余工作发布为0
        reviewer_id: 0, // 审批者id
        timeout_deduction_point: 0, // 逾期扣分/月
        file_list: []// 附件地址列表
      },
      reviewer_list: [],
      superior_list: [],
      targets_list: [],
      base_point: 0,
      surplus: 0
    }
  },
  watch: {
    'data.weight': function (val, old) {
      this.data.base_point = (val / 100 * this.base_point).toFixed(0)
    },
    reviewer_list (val, old) {
      if (val.length > 0) {
        this.data.reviewer_id = val[0].id
      }
    }
  },
  computed: {
    usedWeight () {
      return (this.base_point - this.surplus) / (this.base_point || 1) * 100
    },
    surplus_mark () {
      if ((this.surplus / this.base_point * 100).toFixed(0) - this.data.weight < 0) {
        return Math.abs((this.surplus / this.base_point * 100).toFixed(0) - this.data.weight)
      }
      return 0
    }
  },
  methods: {
    intersection (arr) {
      const arrSet = []
      const intersectionSet = new Set()
      arr.forEach(items => {
        arrSet.push(new Set(items.map(item => item.id)))
        items.forEach(item => intersectionSet.add(item.id))
      })

      return [...intersectionSet].filter(item => arrSet.every(set => set.has(item)))
    },
    changeunqualified (val) {
      if (val == 1 && this.data.base_point == 0) {
        this.data.base_point = 1
      }
      let str = '' + this.data.weight
      let str2 = ''
      if (str.indexOf('.') != -1) {
        let arr = str.split('')
        arr.splice(arr.length - 1)
        let str2 = arr.join('')
        this.data.weight = +str2
      }
    },
    // 关联事件回填
    event_select (item) {
      let point = ((item.base_point) / this.base_point * 100).toFixed(0)
      this.data.task_name = item.task_name
      this.data.task_remark = item.task_remark
      this.data.weight = point
    },
    data_verify () {
      let self = this
      self.$validator.validateAll().then(result => {
        if (!result) {
          self.$notify({ type: 'danger', message: self.$validator.errors.items[0].msg })
        } else {
          if (self.data.weight == '0') {
            self.$notify({ type: 'danger', message: '权重不能为0' })
          } else {
            self.send()
          }
        }
      })
    },
    send () {
      let self = this
      self.send_loading = true
      self.$toast.loading({
        message: '正在处理'
      })
      let data = JSON.parse(JSON.stringify(self.data))
      if (data.task_remark == '') {
        delete data.task_remark
      }
      request('post', '/api/integral/work/update', data).then(res => {
        self.send_loading = false
        self.$toast.clear()
        if (res.data.code == 1) {
          // 成功了
          self.$toast.success(res.data.msg)
          self.$keep_alive_update('update_task_detail', {})
          self.$keep_alive_update('efficiency_detail_update', {})
          setTimeout(() => {
            self.$route_back()
          }, 500)
        } else {
          self.$toast(res.data.msg)
        }
      }).catch(e => {
        self.send_loading = false
        self.$toast.clear()
      })
    }
  },
  created () {
    if (this.$route.query.base_point) {
      this.base_point = this.$route.query.base_point * 1
    }
    if (this.$route.query.surplus) {
      this.surplus = this.$route.query.surplus * 1
    }
  },
  // 组件挂载完成
  mounted () {
    var info = JSON.parse(this.$route.query.info)
    this.reviewer_list = [{name: info.reviewer_name, id: info.reviewer_id}]
    this.superior_list = this.$store.getters.employee_map[info.employee_id].superior_list
    var data = {
      id: info.id,
      targets: info.employee_id, // 工作执行者列表,以逗号分割的字符串或直接传入数组
      pt_id: info.pt_id, // 积分种类id
      task_name: info.task_name, // 任务名
      task_remark: info.task_remark, // 任务详细描述
      base_point: info.point_config.base_point, // 任务参考积分
      expire_time: info.expire_time, // 截至时间 格式:2020-02-02 12：00
      package_id: info.performance_data.package_id, // 绩效任务包id 绩效任务包发布工作时必须大于0，其余工作发布为0
      weight: info.performance_data.weight, // 绩效权重 绩效任务包发布工作时必须大于0，其余工作发布为0
      reviewer_id: info.reviewer_id, // 审批者id
      timeout_deduction_point: info.point_config.timeout_deduction_point, // 逾期扣分/月
      file_list: info.task_file_list// 附件地址列表
    }
    this.data = data
  }
}
</script>
<style scoped>
  .body_com{height: calc(100% - 0.92rem); position: relative;}
</style>
