<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>
        <el-form-item label="被打分部门" prop="deptId" v-if="!dataForm.id" style="margin-left: -120px; width: 400px;">
          <el-cascader
            v-model="deptIdList"
            :options="options"
            :props="{ checkStrictly: true }"
            filterable
            clearable
          ></el-cascader>
        </el-form-item>
      </el-form-item>
      <el-form-item label="考核起始日" prop="monthString" style="width: 200px">
        <el-date-picker v-model="dataForm.monthString" :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="year">
          <el-input v-model="dataForm.year" placeholder="考核年度"></el-input>
      </el-form-item> -->
          <!-- <el-form-item label="考核打分类型 0-月，1-年" prop="scoreType">
          <el-input v-model="dataForm.scoreType" placeholder="考核打分类型 0-月，1-年"></el-input>
      </el-form-item> -->
          <el-form-item label="得分" prop="manualScore" style="width: 400px">
          <el-select v-model="dataForm.manualScore">
            <el-option
              v-for="item in manualScoreType"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
      </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: '',
        deptId: '',
        deptName: '',
        month: '',
        monthString: '',
        year: '',
        scoreType: 0,
        creatorDeptId: '',
        creatorDeptName: '',
        score: 0,
        manualScore: '',
        parentDeptIds: '',
        parentDeptNames: '',
        allDeptIds: '',
        allDeptNames: '',
        revision: '',
        createdBy: '',
        createdTime: '',
        updatedBy: '',
        updatedTime: '',
        delFlag: ''
      },
      // 所属机构配置
      deptIdList: [],
      options: [],
      manualScoreType: [
        {
          'id': '优',
          'name': '优'
        },
        {
          'id': '良',
          'name': '良'
        },
        {
          'id': '中',
          'name': '中'
        },
        {
          'id': '差',
          'name': '差'
        }
      ]
    }
  },
  computed: {
    dataRule () {
      return {
        deptId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        monthString: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        manualScoreType: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        manualScore: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    // 所属机构创建
    this.$http
      .get(`/sys/user/deptOptions/getByLoginUser`)
      .then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.options = res.data.options
      })
      .catch(() => {})
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        this.dataForm.allDeptNames = ''
        this.dataForm.deptId = ''
        if (this.dataForm.id) {
          this.getInfo()
        }
        this.deptIdList = []
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/kpi/manualscore-zlph/${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/manualscore-zlph/', 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 })
  },
  watch: {
    'deptIdList': function (val) {
      if (val.length === 0) {
        this.dataForm.deptId = ''
      } else if (val.length > 0) {
        this.dataForm.deptId = this.deptIdList[val.length - 1]
      }
    }
  }
}
</script>
<style type="text/css">
  .el-input--suffix .el-input__inner {
    padding-right: 50px
  }
</style>
