<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '120px'">
      <el-form-item label="考核规则名称" prop="ruleId">
          <el-select clearable v-model="dataForm.ruleId" :disabled="dataForm.id?true:false">
            <el-option
              v-for="item in kpiManualRuleList"
              :key="item.id"
              :label="item.ruleName"
              :value="item.id">
            </el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="被打分部门" v-if="!dataForm.id">
          <el-cascader
            v-model="deptIdList"
            :options="deptIdOptions"
            clearable
          ></el-cascader>
      </el-form-item>
      <el-form-item label="考核周期起始月" prop="scoreStartMonth">
          <el-date-picker v-model="dataForm.scoreStartMonth" :disabled="dataForm.id?true:false"
              type="month" clearable placeholder="选择月"
              value-format="yyyy-MM"  format="yyyy-MM">
          </el-date-picker>
        </el-form-item>
      <el-form-item label="考核起止日期" prop="scoreStartDate" v-if="allDeptNamesFlag">
          <el-date-picker v-model="dataForm.scoreStartDate"
              type="date" clearable :disabled="true"
              value-format="yyyy-MM-dd"  format="yyyy-MM-dd">
          </el-date-picker>
      </el-form-item>
      <el-form-item label="考核终止日期" prop="scoreEndDate" v-if="allDeptNamesFlag">
        <el-date-picker v-model="dataForm.scoreEndDate"
              type="date" clearable :disabled="true"
              value-format="yyyy-MM-dd"  format="yyyy-MM-dd">
          </el-date-picker>
      </el-form-item>
      <el-form-item label="完整部门名称" prop="allDeptNames" v-if="allDeptNamesFlag">
          <el-input v-model="dataForm.allDeptNames" placeholder="完整部门名称" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="得分" prop="score">
          <el-input-number v-model="dataForm.score" :min="0" :max="100"></el-input-number>
      </el-form-item>
      <el-form-item label="所有父级部门id" prop="parentDeptIds" v-if="false">
          <el-input v-model="dataForm.parentDeptIds" placeholder="所有父级部门id"></el-input>
      </el-form-item>
      <el-form-item label="所有父级部门名称" prop="parentDeptNames" v-if="false">
          <el-input v-model="dataForm.parentDeptNames" placeholder="所有父级部门名称"></el-input>
      </el-form-item>
      <el-form-item label="完整部门id" prop="allDeptIds" v-if="false">
          <el-input v-model="dataForm.allDeptIds" placeholder="完整部门id"></el-input>
      </el-form-item>
      </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: '',
        ruleId: '',
        deptId: '',
        score: '',
        scoreStartDate: '',
        scoreEndDate: '',
        parentDeptIds: '',
        parentDeptNames: '',
        allDeptIds: '',
        allDeptNames: '',
        scoreStartMonth: ''
      },
      kpiManualRuleList: [], // 已绑定手动打分的考核规则列表
      allDeptNamesFlag: 'true',
      deptIdOptions: [],
      deptIdList: []
    }
  },
  created () {
    this.$http
      .get(`/sys/user/deptOptions/getByLoginUser`)
      .then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.deptIdOptions = res.data.options
      })
      .catch(() => {})
  },
  watch: {
    'deptIdList': function (val) {
      if (val.length === 0) {
        this.dataForm.deptId = ''
      } else if (val.length > 0) {
        this.dataForm.deptId = this.deptIdList[val.length - 1]
      }
    }
  },
  computed: {
    dataRule () {
      return {
        ruleId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        deptId: [
          { required: false, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        score: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        scoreStartMonth: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        scoreStartDate: [
          { required: false, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        scoreEndDate: [
          { required: false, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        parentDeptIds: [
          { required: false, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        parentDeptNames: [
          { required: false, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        allDeptIds: [
          { required: false, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        allDeptNames: [
          { required: false, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        this.dataForm.allDeptNames = ''
        this.dataForm.deptId = ''
        if (this.dataForm.id) {
          this.getInfo()
          this.allDeptNamesFlag = true
        } else {
          this.allDeptNamesFlag = false
        }
        this.getKpiManualRule()
        this.deptIdList = []
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/kpi/kpimanualscore/${this.dataForm.id}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/kpi/kpimanualscore/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false }),
    getKpiManualRule () {
      this.$http.get(`/kpi/kpirule/getKpiManualRule/`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.kpiManualRuleList = res.data
      }).catch(() => { })
    }
  }
}
</script>
