<template>
  <el-dialog
    :title="`${id ? '编辑' : '新增'} - 能力评估数据`"
    :visible.sync="dialogVisible"
    custom-class="new-edition-dialog"
    width="60%"
    @closed="closed"
    @open="open"
  >
    <el-steps :active="isActive" finish-status="success" simple>
      <el-step title="年度计划选择"> </el-step>
      <el-step title="能力评估"></el-step>
    </el-steps>
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-position="top"
      label-width="auto"
    >
      <el-row
        :gutter="24"
        v-if="isActive == 0"
        style="flex-wrap:wrap"
        type="flex"
      >
        <el-col :span="24" style="margin:10px 0 ">
          <el-button type="primary" @click="getMainPerson">选择计划</el-button>
        </el-col>
        <el-col :span="8">
          <el-form-item label="单位" prop="orgName">
            <el-input
              disabled
              v-model="form.orgName"
              clearable
              placeholder="请输入单位"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="年度" prop="year">
            <el-input
              disabled
              v-model="form.year"
              placeholder="请输入年度"
              style="width: 100%;"
            >
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="训练课目" prop="subjectName">
            <el-input
              disabled
              v-model="form.subjectName"
              placeholder="请输入训练课目"
              style="width: 100%;"
            >
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24" style="flex-wrap:wrap" type="flex" v-else>
        <el-col :span="8">
          <el-form-item label="指控" prop="accuse">
            <el-input
              v-model="form.accuse"
              clearable
              placeholder="请输入指控"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="打击" prop="crackDown">
            <el-input
              v-model="form.crackDown"
              clearable
              placeholder="请输入打击"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="机动" prop="motorized">
            <el-input
              v-model="form.motorized"
              clearable
              placeholder="请输入机动"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="侦察" prop="reconnoitre">
            <el-input
              v-model="form.reconnoitre"
              clearable
              placeholder="请输入侦察"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="保障" prop="safeguard">
            <el-input
              v-model="form.safeguard"
              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 type="primary" @click="nextStep" v-if="isActive == 0">
        下一步
      </el-button>
      <el-button type="primary" @click="lastStep" v-if="isActive == 1">
        上一步
      </el-button>
      <el-button
        :loading="saveLoading"
        type="primary"
        v-if="isActive == 1"
        @click="handleSave"
      >
        确 认
      </el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </span>
    <rainQuestionData ref="rainQuestionData" @getUser="getMaterials" />
  </el-dialog>
</template>

<script>
import * as api from '@/api/examDataMenu/examManagerData/abilityEvaluationData'
import rainQuestionData from '@/components/examDialog/rainQuestionData.vue'

export default {
  name: 'AddEditDialog',
  props: {
    value: {
      type: Boolean,
      default: false
    },
    rowInfo: {
      type: Object,
      default: () => ({})
    },
    activeTreeItem: {
      type: Object,
      default: () => ({})
    }
  },
  components: {
    rainQuestionData
  },
  data() {
    return {
      rules: {
        orgName: [{required: true, message: '请输入单位', trigger: 'blur'}],
        year: [{required: true, message: '请输入年度', trigger: 'blur'}],
        subjectName: [
          {required: true, message: '请输入训练课目', trigger: 'blur'}
        ]
      },
      form: {},
      saveLoading: false,
      examList: [],
      isActive: 0
    }
  },
  computed: {
    dialogVisible: {
      get() {
        return this.value
      },
      set(val) {
        this.$emit('input', val)
      }
    },
    id() {
      return this.rowInfo?.id || ''
    }
  },
  created() {},
  methods: {
    // 上一页
    lastStep() {
      this.isActive -= 1
    },
    // 下一页
    nextStep() {
      // this.isActive += 1
      this.$refs.form.validate(valid => {
        if (valid) {
          this.isActive += 1
        } else {
          this.$notify({
            type: 'warning',
            message: '请填写完整训练信息',
            duration: 2000
          })
        }
      })
    },
    // 打开弹层
    open() {
      this.$set(this.form, 'orgName', this.activeTreeItem.name)
      this.$set(this.form, 'orgId', this.activeTreeItem.id)
      if (this.id) {
        this.saveLoading = true
        this.getDetail(this.id)
      }
    },
    getMaterials(data) {
      this.examList = data
      let [form] = data
      this.$set(this.form, 'year', form.year)
      this.$set(this.form, 'subjectName', form.subjectName)
      this.$set(this.form, 'subjectId', form.subjectId)
      this.$set(this.form, 'planId', form.id)
    },
    getMainPerson() {
      this.$refs.rainQuestionData.add(this.examList, this.form.orgId)
    },
    // 关闭弹层后
    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>
