<template>
  <el-dialog
    :title="`${id ? '编辑' : '新增'} - 训练场地数据`"
    :visible.sync="dialogVisible"
    custom-class="new-edition-dialog"
    width="50%"
    @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="name">
            <el-input
              v-model="form.name"
              clearable
              placeholder="请输入场地名称"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="场地类型" prop="type">
            <el-select
              v-model="form.type"
              clearable
              placeholder="请选择场地类型"
              style="width: 100%;"
            >
              <el-option
                v-for="item in getDictionaryByCode('xlcdsj-cdlx')"
                :key="item.value"
                :label="item.name"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="责任单位" prop="accountabilityOrg">
            <el-input
              v-model="form.accountabilityOrg"
              clearable
              disabled
              placeholder="请输入责任单位"
            ></el-input>
          </el-form-item>
        </el-col>
        <!--        <el-col :span="6">-->
        <!--          <el-form-item label="场地状态" prop="status">-->
        <!--            <el-select-->
        <!--              v-model="form.status"-->
        <!--              clearable-->
        <!--              placeholder="请选择场地状态"-->
        <!--              style="width: 100%;"-->
        <!--            >-->
        <!--              <el-option-->
        <!--                v-for="item in getDictionaryByCode('xlcdsj-cdzt')"-->
        <!--                :key="item.value"-->
        <!--                :label="item.name"-->
        <!--                :value="item.value"-->
        <!--              >-->
        <!--              </el-option>-->
        <!--            </el-select>-->
        <!--          </el-form-item>-->
        <!--        </el-col>-->
        <el-col :span="12">
          <el-form-item label="承训规模" prop="trainScale">
            <el-input
              v-model="form.trainScale"
              clearable
              placeholder="请输入承训规模"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="配套设施详情" prop="detail">
            <el-input
              v-model="form.detail"
              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: 1, maxRows: 3}"
              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/examSafeguardData/examSpaceData.js'
import {getDictionaryByCode} from '@/utils/dictionary'

export default {
  name: 'AddEditDialog',
  props: {
    value: {
      type: Boolean,
      default: false
    },
    rowInfo: {
      type: Object,
      default: () => ({})
    },
    activeTreeItem: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      rules: {
        name: [{required: true, message: '场地名称不能为空', trigger: 'blur'}],
        accountabilityOrg: [
          {required: true, message: '责任单位不能为空', trigger: 'change'}
        ],
        type: [{required: true, message: '场地类型不能为空', trigger: 'change'}]
      },
      form: {
        accountabilityOrg: '',
        orgId: '',
        type: '1'
      },
      saveLoading: false
    }
  },
  computed: {
    dialogVisible: {
      get() {
        return this.value
      },
      set(val) {
        this.$emit('input', val)
      }
    },
    id() {
      return this.rowInfo?.id || ''
    }
  },
  created() {},
  methods: {
    getDictionaryByCode,
    // 打开弹层
    open() {
      if (this.id) {
        this.saveLoading = true
        this.getDetail(this.id)
      } else {
        const {
          name: accountabilityOrg = '',
          id: orgId = ''
        } = this.activeTreeItem
        this.form = {...this.form, accountabilityOrg, 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 {startTime = '', endTime = '', ...form} = await api.tableDetail(
            id
          ),
          startEndTime = startTime && endTime ? [startTime, endTime] : ''
        this.form = {...form, startEndTime}
      } 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>
