<script setup lang="ts">
import { ElForm, ElMessage, UploadProps } from "element-plus";
import { reactive, ref } from "vue";
import { receipt_rules } from "../../config/rules";

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

const formData = reactive({
  return_image: "http://www.baidu.com",
  retunr_time: ""
});

const types = ["image/png", "image/jpg", "image/bmp", "image/jpeg"];
const formRef = ref<InstanceType<typeof ElForm>>(null);

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

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

//付款回执 状态为4
function handlePaymentReceipt() {
  formRef.value.validate(isValid => {
    if (isValid) emit("changeStatus", "5", formData);
  });
}
</script>

<template>
  <h1 text-xl font-bold py-2>付款回执</h1>
  <el-form ref="formRef" :model="formData" :rules="receipt_rules">
    <el-form-item label="回执图片" prop="return_image">
      <div>
        <el-upload class="upload" :before-upload="onBeforeReturnImageUpload">
          <div class="icon" />
        </el-upload>
        <span>大小:小于1M; 尺寸:100*100; 类型:jpg.png.bmp.jpeg</span>
      </div>
    </el-form-item>
    <el-form-item label="回执时间" prop="return_time">
      <el-date-picker type="datetime" v-model="formData.retunr_time" />
    </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);
}

.icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
