<!-- 外协结算前审核 -->
<template>
  <div class="page-container column-container">
    <!-- 搜索区 -->
    <el-form class="list-search-container" :model="queryParams" ref="queryRef" :inline="true">
      <el-form-item label="检查日期：" prop="startTime">
        <!-- <el-input v-model="queryParams.startTime" placeholder="" style="width: 144px" clearable
          @keyup.enter="handleQuery" /> -->
        <el-date-picker v-model="queryParams.startTime" type="date" placeholder="选择日期" style="width: 144px" clearable
          @keyup.enter="handleQuery" value-format="YYYY-MM-DD" />
      </el-form-item>
      <el-form-item label="外协商名称：" prop="supplierName">
        <el-input v-model="queryParams.supplierName" placeholder="请输入外协商名称" style="width: 144px" clearable
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="批次号：" prop="lotCode">
        <el-input v-model.trim="queryParams.lotCode" placeholder="请输入批次号" style="width: 144px" clearable
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="发出单号：" prop="outsourcedOrderDetailFormCode">
        <el-input v-model.trim="queryParams.outsourcedOrderDetailFormCode" placeholder="请输入发出单号" style="width: 144px" clearable
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="收回单号：" prop="returnReceiptDetailFormCode">
        <el-input v-model.trim="queryParams.returnReceiptDetailFormCode" placeholder="请输入收回单号" style="width: 144px" clearable
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="状态：" prop="auditStatus">
        <el-select v-model="queryParams.auditStatus" style="width: 144px" clearable placeholder="请选择">
          <el-option v-for="dict in outsource_before_checkout_verify_status" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        <el-button type="primary" icon="Check" :disabled="ids&& ids.length == 0" @click="handlePass">批量通过</el-button>
        <el-button type="danger" icon="Close" :disabled="ids&& ids.length == 0" @click="handleFail">批量不通过</el-button>
      </el-form-item>
    </el-form>

    <!-- 功能按钮区 -->
    <!-- <div class="list-btns-container">
    </div> -->

    <!-- 渲染数据区 -->
    <div class="el-table-container">
      <div class="el-table-inner-container">
        <el-table v-loading="loading" :data="orderList" size="small" border height="100%"
          @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="48" align="center" />
          <el-table-column label="外协单位" align="center" prop="outsourcedOrderDetail.supplierName" width="150" />
          <el-table-column label="批次号" align="center" prop="lotCode" width="100" />
          <el-table-column label="产品描述" align="center" prop="productDescription" width="320" />
          <!-- <el-table-column label="检查单号" align="center" prop="outsourcedInspectionNo" width="120" /> -->
          <el-table-column label="检查状态" align="center" prop="status" width="120">
            <template #default="scope">
              <dict-tag :options="process_inspection_status" :value="scope.row.status" />
            </template>
          </el-table-column>
          <el-table-column label="外协工序" align="center" prop="processNames" width="120" />
          <el-table-column label="外协日期" align="center" prop="outsourcedOrderDetail.formDate" width="120">
            <template #default="scope">
              {{
      parseTime(
        scope.row.outsourcedOrderDetail.formDate,
        "{y}-{m}-{d}"
      )
    }}
            </template>
          </el-table-column>
          <el-table-column label="发出单号" align="center" prop="outsourcedOrderDetail.formCode" width="120" />
          <el-table-column label="发出量" align="center" prop="outsourcedOrderDetail.productNum" width="120" />
          <el-table-column label="收回日期" align="center" prop="returnReceiptDetail.formDate" width="120" />
          <el-table-column label="收回单号" align="center" prop="returnReceiptDetail.formCode" width="120" />
          <el-table-column label="收回量" align="center" prop="returnReceiptDetail.auditNum" width="120" />
          <el-table-column label="状态" align="center" prop="auditStatus" width="120">
            <template #default="scope">
              <dict-tag :options="outsource_before_checkout_verify_status" :value="scope.row.auditStatus" />
            </template>
          </el-table-column>
          <el-table-column label="检查日期" align="center" prop="startTime" width="120">
            <template #default="scope">
              {{ parseTime(scope.row.startTime, "{y}-{m}-{d}") }}
            </template>
          </el-table-column>
          <el-table-column label="备注" align="center" prop="remark" width="120" show-overflow-tooltip />
          <el-table-column fixed="right" label="操作" align="center" class-name="small-padding fixed-width" width="200">
            <template #default="scope">
              <el-button v-if="scope.row.auditStatus == 0 || scope.row.auditStatus == 2" link type="warning"
                icon="Check" @click="handleConfirm(scope.row)" v-hasPermi="['business:beforeCheckoutVerify:edit']">
                通过
              </el-button>
              <el-button v-if="scope.row.auditStatus == 0 || scope.row.auditStatus == 1" link type="primary"
                icon="Close" @click="handleReject(scope.row)" v-hasPermi="['business:beforeCheckoutVerify:edit']">
                不通过
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>

    <!-- 分页 -->
    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />
  </div>
</template>

<script setup name="Order">
import {
  listCheckoutInspection,
  rejectInspection,
  confirmInspection,
  checkFurnace,
  checkMultiplyFurnace
} from "@/api/business/beforeCheckoutVerify";
import { nextTick } from "vue";
const { proxy } = getCurrentInstance();
/** 字典数组区 */
const { outsource_before_checkout_verify_status } = proxy.useDict(
  "outsource_before_checkout_verify_status"
);

const { process_inspection_status } = proxy.useDict(
  "process_inspection_status"
);

const orderList = ref([]);
const loading = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const selections = ref([])
const printCarriers = ref([]);
const webHost = import.meta.env.VITE_APP_PRODUCTION_API;
/** 查询对象 */
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  formCode: null,
  formDate: null,
  startTime: null,
  endTime: null,
  supplierName: null,
  deliveryMethod: null,
  packagingMethod: null,
  freightPrice: null,
  freightAmount: null,
  isFirstProcess: 0,
});

/***********************  方法区  ****************************/

/** 查询外协单主 带箱方式，是整单的。如果换新箱子，明细中，都需要更换箱子列表 */
function getList() {
  loading.value = true;
  listCheckoutInspection(queryParams.value).then((response) => {
    orderList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  console.log(queryParams.value);
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
//批量通过
function handlePass(){
  checkMultiplyFurnace(selections.value).then((res) => {
    if (res.code === 200) {
      if (res.data.length >=1) {
        proxy
          .$confirm("是否确认通过？", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
          .then(() => {
            confirmInspection(res.data).then((res) => {
              getList();
            });
          });
      } else {
        proxy
          .$confirm("该炉已全部质检，是否全部确认通过？", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
          .then(() => {
            confirmInspection(res.data).then((res) => {
              getList();
            });
          });
      }
    } else {
      proxy.$msgError(res.msg);
    }
  });
}
//批量不通过
function handleFail(){
  checkMultiplyFurnace(selections.value).then((res) => {
    if (res.code === 200) {
      proxy
        .$confirm("是否确认不通过？", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
        .then(() => {
          rejectInspection(res.data).then((res) => {
            getList();
          });
        });
    }
  });
}
// 多选框选中数据
function handleSelectionChange(selection) {
  selections.value = selection;
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

function handleConfirm(row) {
  checkFurnace(row).then((res) => {
    if (res.code === 200) {
      if (res.data.length === 1) {
        proxy
          .$confirm("是否确认通过？", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
          .then(() => {
            confirmInspection(res.data).then((res) => {
              getList();
            });
          });
      } else {
        proxy
          .$confirm("该炉已全部质检，是否全部确认通过？", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
          .then(() => {
            confirmInspection(res.data).then((res) => {
              getList();
            });
          });
      }
    } else {
      proxy.$msgError(res.msg);
    }
  });
}

function handleReject(row) {
  checkFurnace(row).then((res) => {
    if (res.code === 200) {
      proxy
        .$confirm("是否确认不通过？", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
        .then(() => {
          rejectInspection(res.data).then((res) => {
            getList();
          });
        });
    }
  });
}

getList();
</script>
