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

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

const props = withDefaults(
  defineProps<{
    approveValue: string;
    rejectValue?: string;
    imgProp?: string;
    noTime?: boolean;
    hasRemark?: boolean;
  }>(),
  {
    imgProp: "return_image",
    noTime: false,
    hasRemark: false
  }
);

const formData = reactive({
  [props.imgProp]: "",
  return_time: "",
  status: props.approveValue
});

const receiptRules: FormRules = reactive({
  status: [{ required: true, trigger: "change", message: "请选择审核状态" }],
  remark: [
    { required: props.hasRemark, trigger: "change", message: "请选择审核状态" }
  ],
  return_time: [
    {
      required: !props.noTime,
      trigger: "change",
      message: "请选择上传回执时间"
    }
  ],
  [props.imgProp]: [
    {
      required: !props.noTime,
      trigger: "change",
      message: "请选择上传回执图片"
    }
  ]
});

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

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

const disabledDate = time => {
  return time.getTime() > Date.now();
};

watch(
  () => formData.status,
  status => {
    const isPass = status === props.approveValue;
    receiptRules.remark[0].required = !isPass;
  },
  {
    immediate: true
  }
);
</script>

<template>
  <el-form
    ref="formRef"
    :model="formData"
    :rules="receiptRules"
    label-width="80px"
  >
    <el-form-item
      label="审核状态"
      prop="status"
      v-if="rejectValue"
      style="margin-bottom: 10px"
    >
      <el-select
        placeholder="审核状态"
        v-model="formData.status"
        style="width: 300px"
      >
        <el-option label="通过" :value="approveValue" />
        <el-option label="不通过" :value="rejectValue" />
      </el-select>
    </el-form-item>

    <el-form-item label="备注" prop="remark" v-if="hasRemark">
      <el-input
        style="width: 300px"
        type="textarea"
        v-model="formData.remark"
        placeholder="请输入备注"
      />
    </el-form-item>

    <el-form-item label="回执图片" :prop="imgProp">
      <ImageUpload multiple @change="imgs => (formData[imgProp] = imgs)" />
    </el-form-item>

    <el-form-item label="回执时间" prop="return_time" v-if="!noTime">
      <el-date-picker
        type="datetime"
        :disabled-date="disabledDate"
        value-format="YYYY-MM-DD HH:mm:ss"
        v-model="formData.return_time"
        placeholder="请输入回执时间"
      />
    </el-form-item>

    <el-form-item>
      <div class="w-full flex justify-end">
        <el-button type="primary" @click="handlePaymentReceipt">保存</el-button>
      </div>
    </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>
