<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false" customClass="customWidth">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
      <el-form-item label="部门名称" prop="deptId" label-width="120px">
          <el-cascader
            ref="name"
            v-model="dataForm.joinDeptIdsArr"
            :options="options"
            :props="{ multiple: false, emitPath: false, checkStrictly: true }"
            @visible-change="changeHandle"
            style="width:90%;"
            :disabled="dataForm.id?true:false"
          >
          </el-cascader>
      </el-form-item>
      <el-form-item label="考核年月" prop="monthYear" label-width="120px">
        <el-date-picker v-model="dataForm.monthYear" :disabled="dataForm.id?true:false"
              type="month" clearable placeholder="选择月"
              value-format="yyyy-MM"  format="yyyy-MM" style="width:40%;">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="边界分数" prop="stationEstablishmentBorder" label-width="120px">
          <el-input-number v-model="dataForm.stationEstablishmentBorder" :min="0" :max="1" label="边界分数" :precision="1" style="width:40%;"></el-input-number>
      </el-form-item>
      <el-form-item label="布局分数" prop="stationEstablishmentLayout" label-width="120px">
          <el-input-number v-model="dataForm.stationEstablishmentLayout" :min="0" :max="2" label="布局分数" :precision="1" style="width:40%;"></el-input-number>
      </el-form-item>
      <el-form-item label="标准分数" prop="stationEstablishmentStandard" label-width="120px">
          <el-input-number v-model="dataForm.stationEstablishmentStandard" :min="0" :max="1" label="标准分数" :precision="1" style="width:40%;"></el-input-number>
      </el-form-item>
      <el-form-item label="工作人员分数" prop="staffingPlacementOfficer" label-width="120px">
          <el-input-number v-model="dataForm.staffingPlacementOfficer" :min="0" :max="1" label="工作人员分数" :precision="1" style="width:40%;"></el-input-number>
      </el-form-item>
      <el-form-item label="信息采集分数" prop="staffingPlacementInformation" label-width="120px">
          <el-input-number v-model="dataForm.staffingPlacementInformation" :min="0" :max="1" label="信息采集分数" :precision="1" style="width:40%;"></el-input-number>
      </el-form-item>
      <el-form-item label="人才储备分数" prop="staffingPlacementPersonnel" label-width="120px">
          <el-input-number v-model="dataForm.staffingPlacementPersonnel" :min="0" :max="1" label="人才储备分数" :precision="1" style="width:40%;"></el-input-number>
      </el-form-item>
      <el-form-item label="工作计划分数" prop="gridOperationWorkplan" label-width="120px">
          <el-input-number v-model="dataForm.gridOperationWorkplan" :min="0" :max="1" label="工作计划分数" :precision="1" style="width:40%;"></el-input-number>
      </el-form-item>
      <el-form-item label="运行机制分数" prop="gridOperationOperational" label-width="120px">
          <el-input-number v-model="dataForm.gridOperationOperational" :min="0" :max="2" label="运行机制分数" :precision="1" style="width:40%;"></el-input-number>
      </el-form-item>
      </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" :disabled="isAble" @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: '',
        deptTypeKey: '',
        monthYear: '',
        stationEstablishment: '',
        stationEstablishmentBorder: '',
        stationEstablishmentLayout: '',
        stationEstablishmentStandard: '',
        staffingPlacement: '',
        staffingPlacementOfficer: '',
        staffingPlacementInformation: '',
        staffingPlacementPersonnel: '',
        gridOperation: '',
        gridOperationWorkplan: '',
        gridOperationOperational: '',
        parentDeptIds: '',
        parentDeptNames: '',
        allDeptIds: '',
        allDeptNames: '',
        revision: '',
        createdBy: '',
        createdTime: '',
        updatedBy: '',
        updatedTime: '',
        delFlag: '',
        joinDeptIdsArr: ''
      },
      options: [],
      isAble: false
    }
  },
  computed: {
    dataRule () {
      return {
        deptId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        deptName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        deptTypeKey: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        monthYear: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        stationEstablishmentBorder: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        stationEstablishmentLayout: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        stationEstablishmentStandard: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        staffingPlacementOfficer: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        staffingPlacementInformation: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        staffingPlacementPersonnel: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        gridOperationWorkplan: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        gridOperationOperational: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        joinDeptIdsArr: [
          { required: false, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  created: function () {
    this.getOptions()
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        } else {
          this.dataForm.joinDeptIdsArr = ''
          this.dataForm.deptId = ''
        }
      })
    },
    getOptions () {
      this.$http
        .get(`/sys/user/deptOptions/getAllDeptPartyByUser`)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.options = res.data.options
        })
        .catch(() => {})
    },
    changeHandle (value, selectedData) {
      let parentDeptIds = ''
      let parentDeptNames = ''
      let allDeptIds = ''
      let allDeptNames = ''
      let deptId = ''
      let deptName = ''
      if (this.$refs['name'].getCheckedNodes() !== null && this.$refs['name'].getCheckedNodes().length > 0) {
        var path = this.$refs['name'].getCheckedNodes()[0].path
        var pathLabels = this.$refs['name'].getCheckedNodes()[0].pathLabels
        for (var i = 0; i < path.length; i++) {
          if (i < path.length - 1) {
            parentDeptIds += path[i] + ','
          } else {
            deptId = path[i]
          }
          allDeptIds += path[i] + ','
        }
        for (var j = 0; j < pathLabels.length; j++) {
          if (j < pathLabels.length - 1) {
            parentDeptNames += pathLabels[j] + '-'
          } else {
            deptName = pathLabels[j]
          }
          allDeptNames += pathLabels[j] + '-'
        }
      }
      this.dataForm.parentDeptIds = parentDeptIds.length > 0 ? parentDeptIds.substring(0, parentDeptIds.length - 1) : parentDeptIds
      this.dataForm.parentDeptNames = parentDeptNames.length > 0 ? parentDeptNames.substring(0, parentDeptNames.length - 1) : parentDeptNames
      this.dataForm.allDeptIds = allDeptIds.length > 0 ? allDeptIds.substring(0, allDeptIds.length - 1) : allDeptIds
      this.dataForm.allDeptNames = allDeptNames.length > 0 ? allDeptNames.substring(0, allDeptNames.length - 1) : allDeptNames
      this.dataForm.deptId = deptId
      this.dataForm.deptName = deptName
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/kpi/kpigridentity/${this.dataForm.id}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
        this.dataForm.joinDeptIdsArr = this.dataForm.deptId
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.isAble = true
        this.$http[!this.dataForm.id ? 'post' : 'put']('/kpi/kpigridentity/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            this.isAble = false
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.isAble = false
              this.dataForm.joinDeptIdsArr = ''
              this.dataForm.deptId = ''
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
<style lang="scss">
  .customWidth {
  width:35%
  }
</style>
