<template>
  <div>
    <a-form-model ref="form" :model="formData" :rules="rules" :label-col="{ span: 6 }" :wrapper-col="{ span: 18 }">
      <a-row :gutter="24">
        <a-col :span="24">
          <a-form-model-item ref="name" label="评审标准名称" prop="name">
            <a-input v-model="formData.name" style="width:350px" />
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row :gutter="24">
        <a-col :span="24">
          <a-form-model-item ref="batchId" label="年度批次" prop="batchId">
            <a-select :default-value="formData.batchId" :key="formData.batchId" :value="formData.batchId" @change="batchIdChangeForm" style="width:200px;">
              <a-select-option :value="item.id" v-for="item in EvaluationYearBatchList" :key="item.id">
                {{ item.year + '年第' + item.batch + '批次' }}
              </a-select-option>
            </a-select>
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row :gutter="24">
        <a-col :span="24">
          <a-form-model-item ref="groupId" label="专家组" prop="groupId">
            <a-select :default-value="formData.groupId" :key="formData.groupId" :value="formData.groupId" @change="groupIdChangeForm" style="width:200px;">
              <a-select-option :value="item.id" v-for="item in ExpertGroupList" :key="item.id">
                {{ item.name }}
              </a-select-option>
            </a-select>
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row :gutter="24">
        <a-col :span="24">
          <a-form-model-item ref="remark" label="备注" prop="remark">
            <a-textarea v-model="formData.remark" style="width:350px;" :rows="3" />
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row :gutter="24">
        <a-col :span="24" style="text-align: center">
          <a-button type="primary" size="small" @click="submitEvaluateStandard">提交</a-button>
        </a-col>
      </a-row>
    </a-form-model>
  </div>
</template>

<script>
import { isEmptyParams } from '@/views/utils/common'

export default {
  name: 'EvaluationStandardEdit',
  props: {
    value: {
      type: String,
      default: () => {
        return null
      },
    },
    editType: {
      type: String,
      default: () => {
        return 'edit'
      }
    }
  },
  data() {
    return {
      formData: {
        id: null,
        name: '',
        batchId: null,
        groupId: null,
        remark: ''
      },
      EvaluationYearBatchList: [],
      ExpertGroupList: [],
      rules: {
        name: { required: true, message: '请填写评审标准名称', trigger: 'blur' },
        batchId: { required: true, message: '请选择年度批次', trigger: 'blur' },
        groupId: { required: true, message: '请选择专家组', trigger: 'blur' },
        remark: { required: false },
      }
    };
  },
  created() {
    this.getEvaluationYearBatchList()
    this.getExpertGroupList()
    if (this.editType === 'add') {
      this.formData = {
        id: null,
        name: '',
        batchId: null,
        groupId: null,
        remark: ''
      }
    } else {
      this.getEvaluationStandardById()
    }
  },
  methods: {
    getEvaluationStandardById() {
      let par = { id: this.value }
      this.$api.systemManage.getEvaluationStandardById(par).then(({ data = {} }) => {
        if (data) {
          this.formData = data
        }
      }).catch(() => {})
    },
    getEvaluationYearBatchList() {
      this.$api.systemManage.getEvaluationYearBatchList().then(({ data = {} }) => {
        if (data) {
          this.EvaluationYearBatchList = data
        }
      }).catch(() => {
      })
    },
    getExpertGroupList() {
      this.$api.expertGroup.getExpertGroupList().then(({ data = {} }) => {
        if (data) {
          this.ExpertGroupList = data
        }
      }).catch(() => {
      })
    },
    batchIdChangeForm(value) {
      this.formData.batchId = value
    },
    groupIdChangeForm(value) {
      this.formData.groupId = value
    },
    submitEvaluateStandard() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.editType === 'add') {
            let pars = isEmptyParams(this.formData)
            let par = { ...pars }
            this.$api.systemManage.insertEvaluationStandard(par).then(({ data = {} }) => {
              if (data) {
                this.$message.info(data)
                this.$emit('close')
              }
            }).catch(() => {})
          } else {
            let pars = isEmptyParams(this.formData)
            let par = { ...pars }
            this.$api.systemManage.updateEvaluationStandard(par).then(({ data = {} }) => {
              if (data) {
                this.$message.info(data)
                this.$emit('close')
              }
            }).catch(() => {})
          }
        }
      })
    }
  }
};
</script>
