<template>
  <el-dialog
    :title="title"
    v-model="visible"
    width="25%"
    class="x-dialog"
    :destroy-on-close="true"
  >
    <el-form
      class="dialog-data"
      :model="form"
      :rules="rules"
      ref="formRef"
      label-width="100px"
    >
      <el-form-item label="参与人数:" prop="pqrs">
        <el-input v-model="form.pqrs" placeholder="请输入" clearable>
          <template #suffix> 人 </template>
        </el-input>
      </el-form-item>
      <el-form-item label="参与货运车:" prop="hyc">
        <el-input v-model="form.hyc" placeholder="请输入" clearable>
          <template #suffix> 辆 </template>
        </el-input>
      </el-form-item>
      <el-form-item label="参与吊车:" prop="dc">
        <el-input v-model="form.dc" placeholder="请输入" clearable>
          <template #suffix> 辆 </template>
        </el-input>
      </el-form-item>
      <el-form-item label="参与登高车:" prop="dgc">
        <el-input v-model="form.dgc" placeholder="请输入" clearable>
          <template #suffix> 辆 </template>
        </el-input>
      </el-form-item>
      <el-form-item label="处理描述:" prop="clms">
        <el-input
          v-model="form.clms"
          :rows="2"
          type="textarea"
          placeholder="请填写"
        />
      </el-form-item>
      <el-form-item label="处理后照片:" prop="clhtp">
        <ImageUpload v-model="form.clhtp"></ImageUpload>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handlerClose()" icon="CloseBold">取消</el-button>
        <el-button type="primary" icon="Select" @click="confirm()"
          >确定</el-button
        >
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
const { proxy } = getCurrentInstance();
import { feedBackyj } from "@/api/emergency";
const props = defineProps({
  title: {
    type: String,
    default: "工单处理反馈",
  },

  id: {
    type: Number,
  },
  modelValue: {
    type: Boolean,
    default: false,
  },
});
const emit = defineEmits(["update:modelValue", "confirm"]);

const data = reactive({
  form: {
    clhtp: undefined,//处理后图片
    clms: undefined,//处理描述
    dc: undefined,//吊车
    dgc: undefined,//登高车
    hyc: undefined,//货运车
    id: null,
    pqrs: undefined,//派遣人数
  },
  rules: {
    pqrs: [{ required: true, message: "请填写参与人数", trigger: "blur" }],
    hyc: [{ required: true, message: "请填写货运车数", trigger: "blur" }],
    dgc: [{ required: true, message: "请填写登高车数", trigger: "blur" }],
    dc: [{ required: true, message: "请填写吊车数", trigger: "blur" }],
    clhtp: [{ required: true, message: "请上传处理后图片", trigger: "blur" }],
    clms: [{ required: true, message: "请填写处理描述", trigger: "blur" }],
  },
});
const { form, rules } = toRefs(data);
const visible = ref(props.modelValue);

const handlerClose = () => {
  visible.value = false;
  proxy.resetForm("formRef");
};

const confirm = () => {
  proxy.$refs["formRef"].validate(async (valid) => {
    if (valid) {
      form.value.id = props.id;
      await feedBackyj(form.value);
      proxy.$modal.msgSuccess(`工单处理反馈成功`);
      emit("confirm",true);
      handlerClose();
    }
  });
};

// 监听外部 modelValue 变化
watch(
  () => props.modelValue,
  (val) => {
    visible.value = val;
  }
);
watch(visible, (val) => {
  emit("update:modelValue", val);
});
</script>

<style scoped></style>
