<script setup lang="ts">
import { ElForm, ElMessage, UploadProps } from "element-plus";
import { reactive, ref, watchEffect } from "vue";
import { httpImageUpload, baseUrl } from "/@/api/other";
import { useResponseHandle } from "/@/hooks";
import { useUserStore } from "/@/store/modules/user";

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

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

const types = ["image/png", "image/jpg", "image/bmp", "image/jpeg"];

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

const formRef = ref<InstanceType<typeof ElForm>>(null);
const responseHandle = useResponseHandle();
const userStore = useUserStore();

const onBeforeReturnImageUpload: UploadProps["beforeUpload"] = ({
  type,
  size
}) => {
  if (!types.includes(type)) {
    ElMessage.error("请上传jpg.png.bmp.jpeg类型图片");
    return false;
  }

  if (size / 1024 / 1024 > 1) {
    ElMessage.error("图片大小超过1M");
    return false;
  }
};

const handleRequeset: UploadProps["httpRequest"] = async ({ file }) => {
  const _formData = new FormData();
  _formData.append("img", file);
  _formData.append("token", userStore.token);
  const { message, code, data } = await httpImageUpload(_formData);

  responseHandle({
    message,
    code,
    handler: () => (formData.return_img = baseUrl + "/" + data[0].url)
  });
};

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

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

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

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

    <el-form-item label="退款回执" prop="return_img">
      <div>
        <el-upload
          class="upload"
          :before-upload="onBeforeReturnImageUpload"
          :http-request="handleRequeset"
          :show-file-list="false"
        >
          <img
            class="avatar"
            v-if="formData.return_img"
            :src="formData.return_img"
          />
          <div class="text" v-else>点击上传</div>
        </el-upload>
        <span text-sm>大小:小于1M; 尺寸:100*100; 类型:jpg.png.bmp.jpeg</span>
      </div>
    </el-form-item>

    <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>
