<!-- 
 * @Description: 表单
 * @Author: syy 
 * @Date: 2023-12-15 15:45:59
-->
<template>
  <div>
    <form-container v-model="visibleChamber" title="处理" form-size="600px" @close="onClose">
      <el-form
        ref="SysDormitoryFormRef"
        :rules="rules"
        :disabled="warnHandleProps.disabled"
        :model="warnHandleProps.record"
        :hide-required-asterisk="warnHandleProps.disabled"
        label-width="auto"
        label-suffix=" :"
      >
        <div>
          <el-row :gutter="16">
            <el-col :span="22">
              <s-form-item label="处理意见" prop="remark">
                <el-input v-model="warnHandleProps.record.remark" :autosize="{ minRows: 4 }" type="textarea" placeholder="请输入处理意见" />
              </s-form-item>
            </el-col>
          </el-row>
        </div>
      </el-form>
      <template #footer>
        <el-button @click="onClose"> 取消 </el-button>
        <el-button v-show="!warnHandleProps.disabled" type="primary" @click="handleSubmit"> 确定 </el-button>
      </template>
    </form-container>
  </div>
</template>

<script setup lang="ts" name="warnHandleForm">
import { ref } from "vue";
import { ZJRQ, warnZJRQApi } from "@/api";
import { FormOptEnum } from "@/enums";
import { required } from "@/utils/formRules";
import { FormInstance } from "element-plus";

const visibleChamber = ref(false); //是否显示表单
// 表单参数
const warnHandleProps = reactive<FormProps.Base<ZJRQ.WarnInfo>>({
  opt: FormOptEnum.ADD,
  record: {},
  disabled: false
});
// 表单验证规则
const rules = reactive({
  remark: [required("请输入处理意见")]
});

/**
 * 打开表单
 * @param props 表单参数
 */
function onOpen(props: FormProps.Base<ZJRQ.WarnInfo>) {
  Object.assign(warnHandleProps, props); //合并参数
  visibleChamber.value = true; //显示表单
  warnHandleProps.record = props.record;
}

// 提交数据（新增/编辑）
const SysDormitoryFormRef = ref<FormInstance>();
/** 提交表单 */
async function handleSubmit() {
  SysDormitoryFormRef.value?.validate(async valid => {
    if (!valid) return; //表单验证失败
    let params: any = {
      id: warnHandleProps.record.id,
      remark: warnHandleProps.record.remark
    };
    await warnZJRQApi
      .handWarn(params)
      .then((res: any) => {
        ElMessage({
          message: res.msg,
          type: "success"
        });
        warnHandleProps.successful!(); //调用父组件的successful方法
      })
      .finally(() => {
        onClose();
      });
  });
}

/** 关闭表单*/
function onClose() {
  visibleChamber.value = false;
}

// 暴露给父组件的方法
defineExpose({
  onOpen
});
</script>

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