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

const props = defineProps<{
  isNormal?: boolean;
  isJs?: boolean;
  isOutMonth?: string;
  invType?: string;
  isNormalOpen?: boolean;
  getOriginInvoice: () => Record<string, string>;
}>();

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

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

const formData = reactive({
  return_type: "2",
  return_reason: "",
  remark: "",
  status: "1",
  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 noPassRule = reactive({
  status: [
    {
      required: true,
      trigger: "change",
      message: "请选择审核状态"
    }
  ],
  remark: [
    {
      required: true,
      trigger: "change",
      message: "请输入备注"
    }
  ]
});

const formRef = ref<InstanceType<typeof ElForm>>(null);
//const outMonthAndPaper = ref(false);
const origialOrRed = shallowRef("0");

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

const outMonthAndPaper = computed(() => {
  const { invType, isOutMonth, isNormalOpen } = props;
  /***
   * 不跨月&不是普票专票&不是金税 -> 填写原票信息
   */
  return (
    isOutMonth &&
    (invType === "normal" || invType === "special") &&
    !isNormalOpen
  );
});

watchEffect(() => {
  const orginInvData = props.getOriginInvoice();
  console.log(orginInvData);
  Object.keys(orginInvData).forEach(key => {
    formData[key] = origialOrRed.value === "0" ? orginInvData[key] : "";
  });
});

watchEffect(() => (formData.return_type = props.isJs ? "1" : "2"));

watchEffect(() => {
  const { invType } = props;

  //类型和跨月
  rules.remark[0].required = formData.status === "3";

  //校验码是否必填
  rules.checkCode[0].required =
    invType === "special_electronic" || invType === "electronic";
});
</script>

<template>
  <el-form
    ref="formRef"
    label-width="100px"
    size="small"
    :model="formData"
    :rules="formData.status === '1' ? rules : noPassRule"
  >
    <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>

    <template v-if="formData.status !== '3' && !outMonthAndPaper">
      <el-form-item label="退票方式" prop="return_type">
        <el-select
          w-300px
          v-model="formData.return_type"
          placeholder="请选择退票方式"
        >
          <el-option :disabled="!isJs" 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>

      <el-form-item label="红冲/原票">
        <el-select style="width: 300px" v-model="origialOrRed">
          <el-option label="原票" value="0" />
          <el-option label="红冲" value="1" />
        </el-select>
      </el-form-item>

      <div w-400px>
        <el-form-item label="发票编码" prop="invCode">
          <el-input
            v-model="formData.invCode"
            placeholder="发票编码"
            :disabled="origialOrRed === '0'"
          />
        </el-form-item>

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

        <el-form-item label="校验码" prop="checkCode">
          <el-input
            v-model="formData.checkCode"
            placeholder="校验码"
            :disabled="origialOrRed === '0'"
          />
        </el-form-item>

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

        <el-form-item label="税前金额" prop="subtotal_fee">
          <el-input-number
            v-model="formData.subtotal_fee"
            :max="origialOrRed === '0' ? undefined : '0'"
            placeholder="税前金额"
            :disabled="origialOrRed === '0'"
          />
        </el-form-item>
      </div>
    </template>
    <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>
