<template>
  <el-dialog
    :title="mode === 'add' ? '新增计划体系' : '编辑计划体系'"
    :visible.sync="innerVisible"
    width="520px"
    append-to-body
    @open="handleOpen"
  >
    <el-form ref="formRef" :model="form" :rules="rules" label-width="88px">
      <el-form-item label="年份" prop="year">
        <el-date-picker
          v-model="form.year"
          type="year"
          value-format="yyyy"
          format="yyyy"
          placeholder="请选择年份"
          style="width: 100%;"
        />
      </el-form-item>
      <el-form-item label="计划体系名称" prop="name">
        <el-input v-model="form.name" placeholder="请输入计划体系名称" />
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input-number v-model="form.sort" :min="1" :max="999" style="width: 100%;" />
      </el-form-item>
    </el-form>
    <span slot="footer">
      <el-button @click="innerVisible = false">取消</el-button>
      <el-button type="primary" @click="handleSubmit">保存</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  name: 'SystemFormDialog',
  props: {
    visible: { type: Boolean, default: false },
    mode: { type: String, default: 'add' },
    form: { type: Object, required: true },
    rules: { type: Object, default: () => ({}) }
  },
  computed: {
    innerVisible: {
      get() { return this.visible },
      set(val) { this.$emit('update:visible', val) }
    }
  },
  methods: {
    handleOpen() {
      this.$nextTick(() => {
        if (this.$refs.formRef) {
          this.$refs.formRef.clearValidate()
        }
      })
    },
    handleSubmit() {
      this.$refs.formRef.validate((valid) => {
        if (!valid) return
        this.$emit('submit')
      })
    }
  }
}
</script>

