<template>
  <div class="page-container column-container">
    <!-- 搜索区 -->
    <el-form
      class="list-search-container"
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      style="margin-right: 0px"
    >
      <el-form-item label="单据号：" prop="formCode">
        <el-input
          v-model="queryParams.formCode"
          placeholder="请输入单据号"
          style="width: 144px"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="表单日期：" prop="startTime">
        <el-date-picker
          v-model="queryParams.startTime"
          type="date"
          style="width: 144px"
          value-format="YYYY-MM-DD"
          placeholder="选择起始日期"
          clearable
        />
        <span style="margin: 0 4px">~</span>
        <el-date-picker
          v-model="queryParams.endTime"
          type="date"
          style="width: 144px"
          value-format="YYYY-MM-DD"
          placeholder="选择结束日期"
          clearable
        />
      </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-form-item>
    </el-form>

    <!-- 功能按钮区 -->
    <div class="list-btns-container">
      <el-button
        type="primary"
        icon="Plus"
        v-hasPermi="['business:returnReceipt:add']"
        @click="handleShowFormDialog(null)"
      >
        新增
      </el-button>
      <el-button
        type="danger"
        icon="Delete"
        :disabled="ids.length == 0"
        v-hasPermi="['business:returnReceipt:remove']"
        @click="handleDelete"
      >
        删除
      </el-button>
    </div>
    <!-- 渲染数据区 -->
    <div class="el-table-container">
      <div class="el-table-inner-container">
        <el-table
          v-loading="loading"
          :data="dataList"
          size="small"
          border
          height="100%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="48" align="center" />
          <el-table-column
            label="收回单号"
            align="center"
            prop="formCode"
            width="120"
          />
          <el-table-column
            label="外协商"
            align="center"
            prop="supplierName"
            width="240"
          />
          <el-table-column
            label="收回日期"
            align="center"
            prop="formDate"
            width="120"
          >
            <template #default="scope">
              {{ parseTime(scope.row.formDate, "{y}-{m}-{d}") }}
            </template>
          </el-table-column>
          <el-table-column label="状态" width="80" prop="status" align="center">
            <template #default="scope">
              <dict-tag :options="is_audit_num" :value="scope.row.status" />
            </template>
          </el-table-column>
          <el-table-column label="备注" align="center" prop="remark" />
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
            width="64"
          >
            <template #default="scope">
              <el-button
                link
                type="warning"
                icon="Edit"
                v-hasPermi="['business:returnReceipt:edit']"
                @click="handleShowFormDialog(scope.row)"
              >
                编辑
              </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"
    />

    <!-- 表单 -->
    <receipt-form ref="receiptRef" @handleSaveSuccess="getList()" />
  </div>
</template>

<script setup name="Receipt">
import { listReceipt, delReceipt } from "@/api/business/returnReceipt";
import receiptForm from "./form";
const { proxy } = getCurrentInstance();
/** 字典数组区 */
const { is_audit_num } = proxy.useDict("is_audit_num");
const dataList = ref([]);
const loading = ref(true);
const selected = ref([]);
const ids = ref([]);
const total = ref(0);
/** 查询对象 */
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  formCode: null,
  formDate: null,
});

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

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

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

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  queryParams.value = {
    pageNum: 1,
    pageSize: 10,
    formCode: null,
    formDate: null,
  };
  handleQuery();
}
/**复选框选中数据 */
function handleSelectionChange(selection) {
  selected.value = selection
  ids.value = selection.map((item) => item.id);
}

/** 修改按钮操作 */
function handleShowFormDialog(row) {
  if (row) {
    row.editStatus = false;
  }
  proxy.$refs.receiptRef.open(row ? row : null);
}
/** 删除按钮操作 */
function handleDelete() {
  const _ids = ids.value;
  var flag = true;
  console.log(selected.value)
  selected.value.forEach((item) => {
    item.outsourcedOrderDetails.forEach((label) => {
      if (label.productionDeptId != 0) {
        flag = false; 
        return;
      }
    });
    if(item.processInspecionList.length>0) {
      flag = false; 
        return;
    }
});
//如果收回单明细里的批次有已经周转的批次，或者有批次进行外协序检了，则该外协收回单不能删除
  if(flag){
  proxy.$modal
    .confirm("是否确认删除选中的数据项？")
    .then(function () {
      return delReceipt(_ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功！");
    })
    .catch(() => {});
  }else{
    proxy.$modal.msgError("收回单中的收回明细存在已序检或已周转的批次，不能删除收回单！");
  }
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "business/returnReceipt/export",
    {
      ...queryParams.value,
    },
    `order_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>
