<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="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-rescue";
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: {
    id: null,
    clhtp: undefined,
    clms: undefined,
  },
  rules: {
    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");
      handlerClose();
    }
  });
};

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

<style scoped>

</style>
