<script setup name="sealCodeDialog">
import { ref, getCurrentInstance, watch } from 'vue'
import { sealSave, sealManagerSave } from '@/http/apis/unitSet/sealConfiguration'
import { useRoute } from 'vue-router'
const { proxy } = getCurrentInstance(),
  route = useRoute(),
  props = defineProps({
    visible: {
      type: Boolean,
      default: false,
      required: true
    }
  }),
  emits = defineEmits(['close']),
  formRef = ref(),
  form = ref({}),
  rules = {
    sealCode: [{ required: true, message: '请输入印章sealCode', trigger: 'blur' }]
  },
  submit = async formEl => {
    if (!formEl) {
      return
    }
    await formEl.validate(async valid => {
      if (route.query.id) {
        if (valid) {
          await sealManagerSave(form.value)
          proxy.$message.success('提交成功！')
          emits('close', true)
        }
      } else {
        if (valid) {
          await sealSave(form.value)
          proxy.$message.success('提交成功！')
          emits('close', true)
        }
      }
    })
  }
watch(
  () => props.visible,
  async val => {
    if (val) {
      console.log(val)
    } else {
      formRef.value.resetFields()
    }
  }
)
</script>

<template>
  <el-dialog
    :model-value="visible"
    title="添加印章"
    width="600px"
    destroy-on-close
    @close="emits('close')"
  >
    <el-form
      ref="formRef"
      :model="form"
      :rules="rules"
      label-width="120px"
      label-suffix=":"
    >
      <el-row>
        <el-col :span="24">
          <el-form-item label="印章sealCode" prop="sealCode">
            <el-input
              v-model="form.sealCode"
              maxlength="50"
              placeholder="请输入"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="是否启用">
            <el-switch
              v-model="form.isUse"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button type="primary" @click="submit(formRef)">
        提交
      </el-button>
      <el-button @click="emits('close')">
        关闭
      </el-button>
    </template>
  </el-dialog>
</template>
