<template>
  <el-form ref="form" :rules="rules" label-width="120px" :model="form">
    <el-row>
      <el-col :span="24">
        <el-form-item label="重点工作" prop="importantWork">
          <el-input v-model="form.importantWork"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item label="单位" prop="unit">
          <el-select v-model="form.unit" placeholder="请选择" style="width: 100%;">
            <el-option v-for='item in unitList' :key='item.value' :label="item.label" :value= 'item.value' ></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item label="所属年份" prop="time">
          <el-date-picker
            v-model="form.time"
            type="year"
            placeholder="选择年"
            style="width: 100%;">
    </el-date-picker>
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item label="累计值计算方式" prop="calculateMethod">
          <el-select v-model="form.calculateMethod" placeholder="请选择" style="width: 100%;">
            <el-option v-for='item in calculateMethodList' :key='item.value' :label="item.label" :value= 'item.value' ></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item label="数值目标属性" prop="targetProperty">
          <el-select v-model="form.targetProperty" placeholder="请选择" style="width: 100%;">
            <el-option v-for='item in targetPropertyList' :key='item.value' :label="item.label" :value= 'item.value' ></el-option>
          </el-select>
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
</template>
<script>
import { addImportantWorkConfig, updateImportantWorkConfig, getImportantWorkConfig } from '@/api/basicInfoConfig'
import {getDictDatas } from '@/utils/dict'
import moment from 'moment';
export default {
  name: 'groupKeyAdd',
  data() {
    return {
      form: {
        id: '',
        importantWork: '',
        unit: '',
        time:'',
        calculateMethod:'',
        targetProperty:'',
      },
      isEdit: false, //是否是编辑
      rules: {
        importantWork: [{ required: true, message: '请输入重点工作', trigger: 'blur' }],
        unit: [{ required: true, message: '请选择单位', trigger: 'change' }],
        time: [{ required: true, message: '请选择所属年份', trigger: 'change' }],
        calculateMethod: [{ required: true, message: '请选择累计值计算方式', trigger: 'change' }],
        targetProperty: [{ required: true, message: '请选择数值目标属性', trigger: 'change' }],
      },
      unitList:getDictDatas('lb_dw'),
      calculateMethodList:getDictDatas('important_work_method'),
      targetPropertyList:getDictDatas('target_property')
    }
  },
  methods: {
    init(id) {
      if (id) {
        this.isEdit = true
        this.form.id = id
        getImportantWorkConfig({id}).then((res) => {
          if (res.code === 0) {
            this.form = res.data
          }
        })
      }
    },
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.form.time = moment(this.form.time).endOf('year').endOf('month').endOf('day').unix() * 1000
          if (this.isEdit) {
            //编辑
            updateImportantWorkConfig({ ...this.form }).then((res) => {
              if (res.code === 0) {
                this.$modal.msgSuccess("操作成功");
                this.$emit('successSubmit')
              }
            })
          } else {
            addImportantWorkConfig({ ...this.form }).then((res) => {
              if (res.code === 0) {
                this.$modal.msgSuccess("操作成功");
                this.$emit('successSubmit')
              }
            })
          }
        } else {
          return false
        }
      })
    },
    formClear() {
      this.$refs.form.resetFields()
      this.isEdit = false
    }
  }
}
</script>
