<template>
  <div class="page-container column-container">
    <!-- 搜索区 -->
    <el-form class="list-search-container" :model="queryParams" ref="queryRef" :inline="true">
      <el-form-item label="产品描述：" prop="productDescription">
        <el-input v-model="queryParams.productDescription" placeholder="请输入产品描述" style="width: 144px" clearable
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="批次号：" prop="lotCode">
        <el-input v-model="queryParams.lotCode" placeholder="请输入批次号" style="width: 144px" clearable
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="工段：">
        <el-autocomplete v-model.trim="deptName" clearable :fetch-suggestions="queryDeptSearchAsync" placeholder="请选择工段"
          @select="handleDeptSelect" style="width: 140px" @clear="handleClearDept">
          <template #default="{ item }">
            <div style="
                display: flex;
                flex-direction: row;
                justify-content: space-between;
              ">
              <div class="name" style="font-size: 12px">
                {{ item.deptName }}
              </div>
            </div>
          </template>
        </el-autocomplete>
      </el-form-item>
      <el-form-item class="section-title" label="车间：">
        <el-select-v2 v-model="queryParams.workshopId" clearable :options="workshopList" placeholder="请选择车间"
          style="width: 100%" />
      </el-form-item>
      <el-form-item label="箱号：" prop="carrierCode">
        <el-input v-model="queryParams.carrierCode" placeholder="请输入箱号" style="width: 144px" clearable
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="炉号：">
        <el-input placeholder="请输入炉号" v-model="queryParams.furnaceNumber" @keydown.enter.prevent clearable
          style="width: 160px" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
      </el-form-item>
    </el-form>
    <!-- 渲染数据区 -->
    <div class="el-table-container">
      <div class="el-table-inner-container">
        <el-table v-loading="loading" :data="dataList" size="small" border height="100%">
          <el-table-column label="客户简称" align="center" prop="companyAlias" width="120" />
          <el-table-column label="生产计划单号" align="center" prop="productionPlanNo" width="120" />
          <el-table-column label="序号" align="center" prop="lineNumber" width="70" />
          <el-table-column label="批次号" align="center" prop="lotCode" width="120">
            <template #default="scope">
              <el-button link type="primary" @click="handleColumnClick(scope.row.lotCode)"><span>{{ scope.row.lotCode
                  }}</span></el-button>
            </template>
          </el-table-column>
          <el-table-column label="产品描述" align="center" prop="productDescription" />
          <el-table-column label="图纸版本" align="center" prop="technologyVersion" width="120" />
          <el-table-column label="炉号" prop="furnaceNumber" width="120" align="center" />
          <el-table-column label="投产量" align="center" prop="productionQuantity" width="120" />
          <el-table-column label="当前车间" align="center" prop="workshopName" width="120" />
          <el-table-column label="当前工段" align="center" prop="deptName" width="120" />
          <el-table-column label="当前工序" align="center" prop="processAlias" width="120" />
          <el-table-column label="车间周转区" align="center" prop="turnoverArea" width="150" />
          <el-table-column label="工段周转区" align="center" prop="place" width="150" />
          <el-table-column label="箱号" align="centerName" prop="carrierName" width="120" />
        </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="SearchPosition">
import { listDept } from "@/api/system/dept";
import { getDeptList } from "@/api/system/dept.js";
import { getLotPosition } from "@/api/business/daywork";
import { listWorkshop } from "@/api/business/workshop";
import queryStore from "@/store/modules/query";
import router from "@/router";
const { proxy } = getCurrentInstance();

const dataList = ref([]);
const loading = ref(false);
const workshopList = ref([]);
const deptName = ref("");
const deptList = ref([]);
const multiple = ref(true);
const total = ref(0);
/** 查询对象 */
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  productDescription: null,
  lotCode: null,
  workshopId: null,
  carrierCode: null,
});

/***********************  方法区  ****************************/
function getDept() {
  // resetQuery()
  //获取工段信息
  getDeptList({ type: 0, isWorkSection: 1 }).then((res) => {
    if (res.code == 200) {
      deptList.value = res.data;
    }
    if (queryStore().searchPositionParams != null) {
      deptName.value = queryStore().searchPositionParams.deptName;
    }
  });
  getWorkshop();
  getList();
}
//获取车间
function getWorkshop() {
  listWorkshop().then((res) => {
    workshopList.value = res.rows.map((item) => {
      return {
        label: item.name,
        value: item.id,
      };
    });
  });
}
/** 查询外协单主
带箱方式，是整单的。如果换新箱子，明细中，都需要更换箱子列表 */
function getList() {
  loading.value = true;
  const listQueryParams = queryStore().searchPositionParams;
  if (listQueryParams != null) {
    queryParams.value = listQueryParams;
  }
  console.log("listQueryParams", listQueryParams);
  getLotPosition(queryParams.value).then((response) => {
    dataList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  const query = queryParams.value;
  if (query.productDescription) {
    queryParams.value.productDescription = query.productDescription.trim();
  }

  if (query.lotCode) {
    queryParams.value.lotCode = query.lotCode.trim();
  }

  if (query.carrierCode) {
    queryParams.value.carrierCode = query.carrierCode.trim();
  }

  queryParams.value.pageNum = 1;
  const listQueryParams = JSON.parse(JSON.stringify(queryParams.value));
  queryStore().setSearchPositionParams(listQueryParams);
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
/** 打开批次详情页 */
function handleColumnClick(lotCode) {
  router.push({ path: "/reviseBath/lotFormParticulars/" + lotCode });
}
const queryDeptSearchAsync = (arg, cb) => {
  const params =
    arg != null
      ? { pageSize: 200, deptName: arg, isWorkSection: 1 }
      : { pageSize: 200 };
  listDept(params).then((res) => {
    const depts = res.rows;
    cb(depts);
  });
};

const handleDeptSelect = (item) => {
  console.log(item);
  deptName.value = item.deptName.trim();
  queryParams.value.deptName = deptName.value;
  queryParams.value.deptId = item.deptId;
};
const handleClearDept = () => {
  deptName.value = "";
  queryParams.value.deptName = "";
  queryParams.value.deptId = null;
};

getDept();
</script>
