<script setup lang="ts">
import { reactive, ref, watchEffect, computed, unref } from "vue";
import ImageUpload from "/@/components/ImageUpload";
import { ElForm } from "element-plus";

const emit = defineEmits(["approval"]);

const props = defineProps<{
  returnType: string;
}>();

const formData = reactive({
  return_img: "",
  status: "",
  remark: ""
});

const rules = reactive({
  return_img: [
    {
      required: false,
      trigger: "change",
      message: "请上传退款回执"
    }
  ],
  status: [
    {
      required: true,
      trigger: "change",
      message: "选择审核状态"
    }
  ],
  remark: [
    {
      required: true,
      trigger: "change",
      message: "请输入备注"
    }
  ]
});

const showHiddenUploadImage = computed(() => {
  return props.returnType !== "2";
});

const formRef = ref<InstanceType<typeof ElForm>>(null);

const handlePaymentReceipt = () => {
  formRef.value.validate(
    isValid => isValid && emit("approval", unref(formData))
  );
};

watchEffect(() => {
  rules.remark[0].required = String(formData.status) !== "1";
});
</script>

<template>
  <el-form
    ref="formRef"
    label-width="100px"
    size="small"
    :model="formData"
    :rules="rules"
  >
    <el-form-item label="审批状态" prop="status">
      <el-select w-400px v-model="formData.status">
        <el-option label="通过" value="1" />
        <el-option label="不通过" value="2" />
      </el-select>
    </el-form-item>

    <el-form-item label="备注" prop="remark" w-500px>
      <el-input type="textarea" v-model="formData.remark" />
    </el-form-item>

    <el-row>
      <el-form-item
        v-if="showHiddenUploadImage"
        label="退款回执"
        prop="return_img"
      >
        <ImageUpload multiple @change="imgs => (formData.return_img = imgs)" />
      </el-form-item>
    </el-row>

    <el-form-item w-full flex justify-end>
      <el-button type="primary" @click="handlePaymentReceipt">保存</el-button>
    </el-form-item>
  </el-form>
</template>

<style lang="scss">
.upload {
  width: 178px;
  height: 178px;
  display: block;
  border: 1px dashed var(--el-border-color);

  .text {
    font-size: 20px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    text-align: center;
    line-height: 178px;
  }

  &:hover {
    border-color: var(--el-color-primary);
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
}
</style>
