<template>
  <el-dialog
    :title="`${id ? '编辑' : '新增'} - 课目训练数据`"
    :visible.sync="dialogVisible"
    custom-class="new-edition-dialog"
    width="60%"
    @closed="closed"
    @open="open"
  >
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-position="top"
      label-width="auto"
    >
      <el-row :gutter="24" style="flex-wrap:wrap" type="flex">
        <el-col :span="8">
          <el-form-item label="考核标准" prop="assessmentCriteria">
            <el-input
              v-model="form.assessmentCriteria"
              clearable
              placeholder="请输入考核标准"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="教练员姓名" prop="couchName">
            <el-input
              v-model="form.couchName"
              clearable
              placeholder="请输入教练员姓名"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="考核时间" prop="examTime">
            <el-date-picker
              v-model="form.examTime"
              clearable
              placeholder="请选择考核时间"
              style="width: 100%;"
              type="date"
              value-format="yyyy-MM-dd"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="考核员" prop="examiner">
            <el-input
              v-model="form.examiner"
              clearable
              placeholder="请输入考核员"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="课目名称" prop="subjectName">
            <el-input
              v-model="form.subjectName"
              clearable
              placeholder="请输入课目名称"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="训练条件" prop="trainCondition">
            <el-input
              v-model="form.trainCondition"
              clearable
              placeholder="请输入训练条件"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="训练内容" prop="trainContent">
            <el-input
              v-model="form.trainContent"
              clearable
              placeholder="请输入训练内容"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="应训时间" prop="trainTime">
            <el-date-picker
              v-model="form.trainTime"
              clearable
              placeholder="请选择应训时间"
              style="width: 100%;"
              type="date"
              value-format="yyyy-MM-dd"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="训练场地" prop="trainingAddr">
            <el-input
              v-model="form.trainingAddr"
              clearable
              placeholder="请输入训练场地"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="memo">
            <el-input
              v-model="form.memo"
              :autosize="{minRows: 2, maxRows: 4}"
              clearable
              placeholder="请输入备注"
              type="textarea"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button :loading="saveLoading" type="primary" @click="handleSave">
        确 认
      </el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import * as api from '@/api/examDataMenu/examActivityData/subjectBaseDataMenu'

export default {
  name: 'AddEditDialog',
  props: {
    value: {
      type: Boolean,
      default: false
    },
    rowInfo: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      rules: {
        name: [{required: true, message: '请输入名称', trigger: 'blur'}]
      },
      form: {},
      saveLoading: false
    }
  },
  computed: {
    dialogVisible: {
      get() {
        return this.value
      },
      set(val) {
        this.$emit('input', val)
      }
    },
    id() {
      return this.rowInfo?.id || ''
    }
  },
  created() {},
  methods: {
    // 打开弹层
    open() {
      if (this.id) {
        this.saveLoading = true
        this.getDetail(this.id)
      }
    },

    // 关闭弹层后
    closed() {
      const {rules} = this
      // 重置组件数据
      Object.assign(this.$data, this.$options.data.call(this), {rules})

      this.$nextTick(_ => {
        this.$refs.form.$el.scrollTop = 0
        this.$refs.form.clearValidate()
      })
    },

    // 获取详情
    async getDetail(id) {
      try {
        const {...form} = await api.tableDetail(id)
        this.form = {...form}
      } finally {
        this.saveLoading = false
      }
    },

    // 保存
    handleSave() {
      this.$refs.form.validate(async (valid, object) => {
        if (!valid) {
          this.$message.warning(
            `有${
              Object.keys(object).length
            }个字段未通过校验，请正确填写表单内容。`
          )
        } else {
          this.saveLoading = true
          const myInterFace = this.id ? api.tableEdit : api.tableAdd

          const {...addEditForm} = this.form

          try {
            let {state, message} = await myInterFace({...addEditForm})

            if (state) {
              this.$emit('submit', this.id ? 'edit' : 'add')
              message = this.id ? '编辑' : '新增'
              this.$message.success(`课目训练数据${message}成功`)
              this.dialogVisible = false
            }
          } finally {
            this.saveLoading = false
          }
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped></style>
