<template>
  <div v-loading="loading">
    <div class="form-box">
      <el-form
        label-position="top"
        :model="form"
        ref="form"
        :rules="formRules"
        label-width="100px"
      >
        <el-row :gutter="10">
          <el-col :span="4">
            <el-form-item label="采购单号" prop="purchaseCode">
              <el-input v-model="form.purchaseCode" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="供应商" prop="supplierName">
              <el-input v-model="form.supplierName" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="4">
            <el-form-item label="物流公司" prop="logisticsCompanyName">
              <el-input v-model="form.logisticsCompanyName" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="物流/快递单号" prop="code">
              <el-input v-model="form.code" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <div style="margin-bottom: 20px">
          <labelTitle content="质检明细"></labelTitle>
        </div>

        <el-form-item>
          <el-table :data="form.changeProductList">
            <el-table-column label="物品编码" prop="productCode">
            </el-table-column>
            <el-table-column label="物品名称" prop="productName">
            </el-table-column>
            <el-table-column label="待质检数量" prop="totalQuantity">
            </el-table-column>
            <el-table-column label="质检合格" prop="qualifiedQuantity">
              <template slot-scope="scope">
                <el-form-item
                  :prop="
                    'changeProductList.' + scope.$index + '.qualifiedQuantity'
                  "
                  :rules="formRules.qualifiedQuantity"
                  :inline-message="true"
                  label-width="0"
                >
                  <el-input-number
                    v-model="scope.row.qualifiedQuantity"
                    label="请输入"
                    style="width: 100%"
                    size="mini"
                    :controls="false"
                    :min="0"
                    :max="9999"
                  >
                  </el-input-number>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column label="质检不合格" prop="disqualificationQuantity">
              <template slot-scope="scope">
                <el-form-item
                  :prop="
                    'changeProductList.' +
                    scope.$index +
                    '.disqualificationQuantity'
                  "
                  :rules="formRules.disqualificationQuantity"
                  :inline-message="true"
                  label-width="0"
                >
                  <el-input-number
                    v-model="scope.row.disqualificationQuantity"
                    label="请输入"
                    style="width: 100%"
                    size="mini"
                    :controls="false"
                    :min="0"
                    :max="9999"
                  >
                  </el-input-number>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column label="备注" prop="remark">
              <template slot-scope="scope">
                <el-form-item
                  :prop="'changeProductList.' + scope.$index + '.remark'"
                  :rules="formRules.remark"
                  :inline-message="true"
                  label-width="0"
                >
                  <el-input
                    v-model="scope.row.remark"
                    placeholder="请输入"
                    size="mini"
                  >
                  </el-input>
                </el-form-item>
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
      </el-form>
    </div>
    <div style="text-align: center; margin-top: 15px">
      <el-button size="small" @click="handleCancel">取消 </el-button>
      <el-button type="primary" size="small" @click="handleSubmit">
        确定</el-button
      >
    </div>
  </div>
</template>

<script>
import labelTitle from "@/components/label-title/index.vue";

import { getToken } from "@/util/auth";

export default {
  name: "addInspection",
  components: { labelTitle },
  props: {
    form: {
      type: Object,
      default: () => {},
    },
    warehouseSelectList: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      uploadHeader: {
        Authorization: "Basic c2FiZXI6c2FiZXJfc2VjcmV0",
        "Blade-Auth": "bearer " + getToken(),
      },
      loading: false,
      selectDialog: false,
      formRules: {
        qualifiedQuantity: [
          {
            required: true,
            message: "请输入质检合格数量",
            trigger: "blur",
          },
        ],
        disqualificationQuantity: [
          {
            required: true,
            message: "请输入质检不合格数量",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {},
  methods: {
    handleSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          for (let i = 0; i < this.form.changeProductList.length; i++) {
            if (
              this.form.changeProductList[i].qualifiedQuantity +
                this.form.changeProductList[i].disqualificationQuantity ===
              0
            ) {
              return this.msgInfo("质检合格和质检不合格总合不能为0");
            }
            if (
              Number(this.form.changeProductList[i].qualifiedQuantity) +
                Number(
                  this.form.changeProductList[i].disqualificationQuantity
                ) >
              Number(this.form.changeProductList[i].totalQuantity)
            ) {
              return this.msgInfo("质检合格和质检不合格总合不能大于待质检数量");
            }
          }
          this.loading = true;
          this.$emit("submit");
        }
      });
    },
    handleCancel() {
      this.$emit("cancel");
    },
  },
};
</script>

<style lang="scss" scoped>
.form-box {
  height: calc(100vh - 280px);
  overflow: auto;
  box-sizing: border-box;
  padding: 10px;
}
::v-deep {
  .el-form-item {
    margin-bottom: 3px;
  }
  .el-form--label-top .el-form-item__label {
    padding: 8px 0 0 0;
  }
}
</style>