<script setup lang="ts">
import { reactive, ref, computed } from "vue";
import { ElForm } from "element-plus";

const props = defineProps<{
  isNormal?: boolean;
  isAllEx?: boolean
}>();

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

const showInvoiceInfo = computed(() => formData.return_type === "2");

const formData = reactive({
  return_type: "",
  return_reason: "",
  remark: "",
  status: "",
  invCode: "",
  invNum: "",
  open_date: "",
  checkCode: "",
  subtotal_fee: ""
});

const rules = reactive({
  return_type: [
    {
      required: true,
      trigger: "change",
      message: "请选择退票方式"
    }
  ],
  return_reason: [
    {
      required: true,
      trigger: "change",
      message: "请选择红冲原因"
    }
  ],
  status: [
    {
      required: true,
      trigger: "change",
      message: "请选择审核状态"
    }
  ],
  remark: [
    {
      required: true,
      trigger: "change",
      message: "请输入备注"
    }
  ],
  invCode: [
    {
      required: true,
      trigger: "change",
      message: "请输入发票编码"
    }
  ],
  invNum: [
    {
      required: true,
      trigger: "change",
      message: "请输入发票号码"
    }
  ],
  open_date: [
    {
      required: true,
      trigger: "change",
      message: "请选择开票日期"
    }
  ],
  subtotal_fee: [
    {
      required: true,
      trigger: "change",
      message: "请输入税前金额"
    }
  ],
  checkCode: [
    {
      required: props.isNormal,
      trigger: "change",
      message: "请输入校验码"
    }
  ]
});

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

const handlePaymentReceipt = () => {
  formRef.value.validate(isValid => isValid && emit("changeStatus", formData));
};
</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" placeholder="请选择审批状态">
        <el-option label="通过" value="1" />
        <el-option label="不通过" value="3" />
      </el-select>
    </el-form-item>

    <el-form-item label="退票方式" prop="return_type">
      <el-select
        w-300px
        v-model="formData.return_type"
        placeholder="请选择退票方式"
      >
        <el-option value="1" label="金税" />
        <el-option value="2" label="线下" />
      </el-select>
    </el-form-item>

    <el-form-item
      label="红冲原因"
      prop="return_reason"
      placeholder="请选择红冲原因"
    >
      <el-select w-300px v-model="formData.return_reason">
        <el-option label="销货退回" value="1" />
        <el-option label="开票有误" value="2" />
        <el-option label="服务终止" value="3" />
        <el-option label="销售折让" value="4" />
      </el-select>
    </el-form-item>

    <div w-400px v-if="showInvoiceInfo && !isAllEx">
      <el-form-item
        label="发票代码"
        prop="invCode"
        placeholder="请输入发票代码"
      >
        <el-input v-model="formData.invCode" />
      </el-form-item>

      <el-form-item label="发票号码" prop="invNum" placeholder="请输入发票号码">
        <el-input v-model="formData.invNum" />
      </el-form-item>

      <el-form-item
        v-if="!isAllEx"
        label="校验码"
        prop="checkCode"
        placeholder="请选择输入校验码"
      >
        <el-input v-model="formData.checkCode" />
      </el-form-item>

      <el-form-item
        label="开票日期"
        prop="open_date"
        placeholder="请选择开票日期"
      >
        <el-date-picker
          w-300px
          v-model="formData.open_date"
          value-format="YYYY-MM-DD HH:mm:ss"
        />
      </el-form-item>

      <el-form-item
        label="税前金额"
        prop="subtotal_fee"
        placeholder="请输入税前金额"
      >
        <el-input-number v-model="formData.subtotal_fee" />
      </el-form-item>
    </div>

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