<template>
  <div class="form">
    <van-nav-bar
      :title="$t('arrivalQuality.name')"
      :left-text="$t('common.back')"
      left-arrow
      @click-left="onClickLeft"
    >
    </van-nav-bar>
    <testForm
      v-model="formData.data"
      :formOption="formOption"
      :formConfig="formConfig"
      :rules="rules"
      @onSubmit="onSubmit"
      ref="formDom"
    ></testForm>
  </div>
</template>

<script setup>
import { ref, reactive, getCurrentInstance, onMounted } from "vue";
import { showSuccessToast, showFailToast } from "vant";
import { useRoute } from "vue-router";
import testForm from "@/components/testForm/index.vue";
const proxy = getCurrentInstance().proxy;
const route = useRoute();
const formDom = ref(null);
const formData = reactive({
  data: {},
});
const rules = {
  qualifiedCount: [
    {
      required: true,
      message: proxy.t("arrivalQuality.qualityQualifiedCanNotBeEmpty"),
    },
  ],
  noQualifiedCount: [
    {
      required: true,
      message: proxy.t("arrivalQuality.qualityUnQualifiedCanNotBeEmpty"),
    },
  ],
};
const formOption = reactive({
  readonly: false, //用于控制整个表单是否只读
  disabled: false,
  labelAlign: "top",
  scroll: true,
  labelWidth: "62pk",
  submitBtnText: proxy.t("common.submit"),
  btnConfig: {
    isNeed: false,
    listTitle: proxy.t("arrivalQuality.relatedContract"),
    prop: "arrivalQualityContractList",
    plain: true,
    listConfig: [],
  },
});
const formConfig = reactive([
  {
    type: "input",
    label: proxy.t("arrivalQuality.supplyName"),
    prop: "supplyName",
    itemType: "text",
    readonly: true,
  },
  {
    type: "input",
    label: proxy.t("arrivalQuality.arrivalCode"),
    prop: "code",
    itemType: "text",
    readonly: true,
  },
  {
    type: "input",
    label: proxy.t("arrivalQuality.productName"),
    prop: "productName",
    itemType: "text",
    readonly: true,
  },
  {
    type: "input",
    label: proxy.t("arrivalQuality.productSpec"),
    prop: "productSpec",
    itemType: "text",
    readonly: true,
  },
  {
    type: "input",
    label: proxy.t("arrivalQuality.arrivalNumber"),
    prop: "count",
    itemType: "text",
    readonly: true,
  },
  {
    type: "input",
    label: proxy.t("arrivalQuality.alreadyQualityNumber"),
    prop: "sumQualityCount",
    itemType: "text",
    readonly: true,
  },

  {
    type: "input",
    label: proxy.t("arrivalQuality.qualityQualified"),
    prop: "qualifiedCount",
    itemType: "number",
  },
  {
    type: "input",
    label: proxy.t("arrivalQuality.qualityUnQualified"),
    prop: "noQualifiedCount",
    itemType: "number",
  },
]);
const onClickLeft = () => history.back();

onMounted(() => {
  if (route.query && route.query.id) {
    let ids = [route.query.id];
    proxy.post("/arrivalDetail/detail", { ids }).then((res) => {
      formData.data = {
        arrivalId: route.query.arrivalId,
        supplyId: route.query.supplyId,
        supplyName: route.query.supplyName,
        code: route.query.code,
        productSpec: route.query.productSpec,
        productName: route.query.productName,
        count: route.query.count,
        arrivalDetailId: res.data[0].id,
        sumQualityCount: res.data[0].sumQualityCount,
        qualifiedCount: "",
        noQualifiedCount: "",
      };
    });
  }
});

const onSubmit = () => {
  let data = { ...formData.data };
  if (Number(data.qualifiedCount) + Number(data.noQualifiedCount) > 0) {
    if (
      Number(data.qualifiedCount) +
        Number(data.noQualifiedCount) +
        Number(data.sumQualityCount) >
      Number(data.count)
    ) {
      return showFailToast(
        proxy.t(
          "arrivalQuality.qualityInspectionQuantityNotBeGreaterThanArrivalNumber"
        )
      );
    } else {
      let obj = {
        arrivalDetailId: data.arrivalDetailId,
        qualifiedCount: data.qualifiedCount,
        noQualifiedCount: data.noQualifiedCount,
      };
      delete data.arrivalDetailId;
      delete data.qualifiedCount;
      delete data.noQualifiedCount;
      data.qualityDetailList = [obj];
      proxy.post("/quality/add", data).then(
        () => {
          showSuccessToast(proxy.t("common.operationSuccessful"));
          setTimeout(() => {
            onClickLeft();
          }, 500);
        },
        (err) => {
          return showFailToast(err.message);
        }
      );
    }
  } else {
    return showFailToast(
      proxy.t("arrivalQuality.qualityInspectionQuantityCannotBeZero")
    );
  }
};
</script>
<style lang="scss" scoped>
</style>
