<template>
  <!-- 盘点查询页面 - rqrq -->
  <div class="mod-config yzz">
    <!-- 查询区域 -->
    <el-form label-position="top" style="margin-top: 1px; margin-left: 0px;">
      <el-form :inline="true" label-position="top" style="margin-top: 0px">
        <el-form-item label="盘点单号" style="margin-right: 10px;">
          <el-input v-model="queryForm.searchCountNo" placeholder="请输入盘点单号" clearable style="width: 150px;" @keyup.enter.native="search"></el-input>
        </el-form-item>
        <el-form-item label="盘点类型" style="margin-right: 10px;">
          <el-select v-model="queryForm.searchCountType" placeholder="请选择" clearable style="width: 120px;">
            <el-option label="循环盘点" value="CYCLE"></el-option>
            <el-option label="手工盘点" value="MANUAL"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" style="margin-right: 10px;">
          <el-select v-model="queryForm.searchStatus" placeholder="请选择" clearable style="width: 120px;">
            <el-option label="草稿" value="DRAFT"></el-option>
            <el-option label="已下达" value="RELEASED"></el-option>
            <el-option label="盘点中" value="CHECKING"></el-option>
            <el-option label="已完成" value="COMPLETED"></el-option>
            <el-option label="已取消" value="CANCELLED"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="申请日期" style="margin-right: 10px;">
          <el-date-picker v-model="queryForm.startDate" type="date" placeholder="开始日期" value-format="yyyy-MM-dd" style="width: 130px;"></el-date-picker>
          <span style="margin: 0 5px;">至</span>
          <el-date-picker v-model="queryForm.endDate" type="date" placeholder="结束日期" value-format="yyyy-MM-dd" style="width: 130px;"></el-date-picker>
        </el-form-item>
        <el-form-item label=" " style="margin-right: 10px;">
          <el-button type="primary" @click="search">查询</el-button>
          <el-button @click="resetQuery">重置</el-button>
          <el-button type="success" @click="showCycleCountDialog">循环盘点</el-button>
          <el-button type="warning" @click="handleCreateManualCount">手工盘点</el-button>
        </el-form-item>
      </el-form>
    </el-form>

    <!-- 主表格 - rqrq -->
    <el-table :data="dataList" @row-click="handleRowClick" :height="height" border highlight-current-row ref="mainTable" v-loading="dataListLoading" style="width: 100%;">
      <el-table-column
        v-for="(item,index) in mainColumnList" :key="index"
        :sortable="item.columnSortable"
        :prop="item.columnProp"
        :header-align="item.headerAlign"
        :show-overflow-tooltip="item.showOverflowTooltip"
        :align="item.align"
        :fixed="item.fixed==''?false:item.fixed"
        :min-width="item.columnWidth"
        :label="item.columnLabel">
        <template slot-scope="scope">
          <span v-if="item.columnProp === 'statusDesc'" :style="{color: getStatusColor(scope.row.status)}">{{ scope.row.statusDesc }}</span>
          <span v-else-if="item.columnProp === 'countPercent'">{{ scope.row.countPercent ? scope.row.countPercent + '%' : '' }}</span>
          <span v-else-if="item.columnProp === 'countProgress'">{{ scope.row.checkedLabelCount }}/{{ scope.row.totalLabelCount }}</span>
          <span v-else-if="item.columnProp === 'applyDate'">{{ formatDateTime(scope.row.applyDate) }}</span>
          <span v-else-if="item.columnProp === 'releaseDate'">{{ formatDateTime(scope.row.releaseDate) }}</span>
          <span v-else>{{ scope.row[item.columnProp] }}</span>
        </template>
      </el-table-column>
      <el-table-column header-align="center" align="center" fixed="right" width="220" label="操作">
        <template slot-scope="scope">
          <a type="text" style="margin-right: 10px;" @click.stop="handleAddMaterial(scope.row)" v-if="scope.row.status === 'DRAFT' && scope.row.countType === 'MANUAL'">添加物料</a>
          <a type="text" style="margin-right: 10px;" @click.stop="handleRelease(scope.row)" v-if="scope.row.status === 'DRAFT'">下达</a>
          <a type="text" style="margin-right: 10px;" @click.stop="handleComplete(scope.row)" v-if="scope.row.status === 'RELEASED' || scope.row.status === 'CHECKING'">完成</a>
          <a type="text" style="margin-right: 10px;" @click.stop="handleCancel(scope.row)" v-if="scope.row.status !== 'COMPLETED' && scope.row.status !== 'CANCELLED'">取消</a>
          <a type="text" style="color: #F56C6C;" @click.stop="handleDelete(scope.row)" v-if="scope.row.status === 'DRAFT'">删除</a>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex" :page-sizes="[20, 50, 100, 1000]" :page-size="pageSize" :total="totalPage" layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>

    <!-- Tab标签页 -->
    <el-tabs style="font-size: 12px;min-height: 200px" class="customer-tab" v-model="activeName" type="border-card" @tab-click="tabClick">
      <el-tab-pane label="标签明细" name="label">
        <!-- 标签明细表格 - rqrq -->
        <el-table :data="labelList" :height="200" border v-loading="labelListLoading" style="width: 100%;">
          <el-table-column
            v-for="(item,index) in labelColumnList" :key="index"
            :sortable="item.columnSortable"
            :prop="item.columnProp"
            :header-align="item.headerAlign"
            :show-overflow-tooltip="item.showOverflowTooltip"
            :align="item.align"
            :fixed="item.fixed==''?false:item.fixed"
            :min-width="item.columnWidth"
            :label="item.columnLabel">
            <template slot-scope="scope">
              <span v-if="item.columnProp === 'countFlagDesc'" :style="{color: scope.row.countFlag === 'Y' ? '#67C23A' : '#F56C6C'}">{{ scope.row.countFlagDesc }}</span>
              <span v-else-if="item.columnProp === 'countDate'">{{ formatDateTime(scope.row.countDate) }}</span>
              <span v-else>{{ scope.row[item.columnProp] }}</span>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="栈板明细" name="pallet">
        <!-- 栈板明细表格 - rqrq -->
        <el-table :data="palletList" :height="200" border v-loading="palletListLoading" style="width: 100%;">
          <el-table-column
            v-for="(item,index) in palletColumnList" :key="index"
            :sortable="item.columnSortable"
            :prop="item.columnProp"
            :header-align="item.headerAlign"
            :show-overflow-tooltip="item.showOverflowTooltip"
            :align="item.align"
            :fixed="item.fixed==''?false:item.fixed"
            :min-width="item.columnWidth"
            :label="item.columnLabel">
            <template slot-scope="scope">
              <span v-if="item.columnProp === 'countFlagDesc'" :style="{color: scope.row.countFlag === 'Y' ? '#67C23A' : '#F56C6C'}">{{ scope.row.countFlagDesc }}</span>
              <span v-else-if="item.columnProp === 'countDate'">{{ formatDateTime(scope.row.countDate) }}</span>
              <span v-else>{{ scope.row[item.columnProp] }}</span>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="盘点结果" name="result">
        <!-- 盘点结果表格 - rqrq -->
        <el-table :data="resultList" :height="200" border v-loading="resultListLoading" style="width: 100%;">
          <el-table-column
            v-for="(item,index) in resultColumnList" :key="index"
            :sortable="item.columnSortable"
            :prop="item.columnProp"
            :header-align="item.headerAlign"
            :show-overflow-tooltip="item.showOverflowTooltip"
            :align="item.align"
            :fixed="item.fixed==''?false:item.fixed"
            :min-width="item.columnWidth"
            :label="item.columnLabel">
            <template slot-scope="scope">
              <span v-if="item.columnProp === 'countResultDesc'" :style="{color: getResultColor(scope.row.countResult)}">{{ scope.row.countResultDesc }}</span>
              <span v-else-if="item.columnProp === 'countDate'">{{ formatDateTime(scope.row.countDate) }}</span>
              <span v-else>{{ scope.row[item.columnProp] }}</span>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="物料汇总" name="summary">
        <!-- 物料汇总表格 - rqrq -->
        <el-table :data="summaryList" :height="200" border v-loading="summaryListLoading" style="width: 100%;">
          <el-table-column
            v-for="(item,index) in summaryColumnList" :key="index"
            :sortable="item.columnSortable"
            :prop="item.columnProp"
            :header-align="item.headerAlign"
            :show-overflow-tooltip="item.showOverflowTooltip"
            :align="item.align"
            :fixed="item.fixed==''?false:item.fixed"
            :min-width="item.columnWidth"
            :label="item.columnLabel">
            <template slot-scope="scope">
              <span>{{ scope.row[item.columnProp] }}</span>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
    </el-tabs>

    <!-- 循环盘点弹窗 -->
    <el-dialog title="创建循环盘点单" :visible.sync="cycleCountDialogVisible" width="400px" :close-on-click-modal="false" v-drag>
      <el-form :model="cycleCountForm" label-width="100px">
        <el-form-item label="盘点比例(%)">
          <el-input v-model="cycleCountForm.countPercent"   style="width: 250px;"></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="cycleCountForm.remark" type="textarea" :rows="3" style="width: 250px;"></el-input>
        </el-form-item>
      </el-form>
      <el-footer style="height:30px;margin-top: 60px;text-align:center">
        <el-button type="primary" @click="createCycleCount" :loading="createLoading" :disabled="createLoading">{{ createLoading ? '创建中...' : '创 建' }}</el-button>
        <el-button @click="cycleCountDialogVisible = false" :disabled="createLoading">取 消</el-button>
      </el-footer>
    </el-dialog>

    <!-- 添加物料弹窗（显示已有标签列表）-->
    <el-dialog :title="'添加物料 - ' + (addMaterialRow ? addMaterialRow.countNo : '')" :visible.sync="addMaterialDialogVisible" width="1000px" :close-on-click-modal="false" v-drag>
      <div style="margin-bottom: 10px;">
        <el-button type="primary" size="small" @click="showMaterialQueryDialog">添加物料</el-button>
      </div>
      <!-- 当前标签列表表格 - rqrq -->
      <el-table :data="currentLabelList" :height="350" border v-loading="currentLabelListLoading" style="width: 100%;">
        <el-table-column
          v-for="(item,index) in currentLabelColumnList" :key="index"
          :sortable="item.columnSortable"
          :prop="item.columnProp"
          :header-align="item.headerAlign"
          :show-overflow-tooltip="item.showOverflowTooltip"
          :align="item.align"
          :fixed="item.fixed==''?false:item.fixed"
          :min-width="item.columnWidth"
          :label="item.columnLabel">
          <template slot-scope="scope">
            <span>{{ scope.row[item.columnProp] }}</span>
          </template>
        </el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addMaterialDialogVisible = false">关 闭</el-button>
      </span>
    </el-dialog>

    <!-- 物料查询弹窗（添加物料时使用）-->
    <el-dialog title="查询物料" :visible.sync="materialQueryDialogVisible" width="900px" :close-on-click-modal="false" v-drag append-to-body>
      <el-form :inline="true" :model="materialQueryForm" label-position="top">
        <el-form-item label="物料编码" style="margin-right: 10px;">
          <el-input v-model="materialQueryForm.searchPartNo" placeholder="请输入物料编码" clearable style="width: 150px;" @keyup.enter.native="queryMaterialForAdd"></el-input>
        </el-form-item>
        <el-form-item label="批号" style="margin-right: 10px;">
          <el-input v-model="materialQueryForm.searchBatchNo" placeholder="请输入批号" clearable style="width: 120px;"></el-input>
        </el-form-item>
        <el-form-item label="WDR号" style="margin-right: 10px;">
          <el-input v-model="materialQueryForm.searchWdr" placeholder="请输入WDR号" clearable style="width: 120px;"></el-input>
        </el-form-item>
        <el-form-item label=" ">
          <el-button type="primary" @click="queryMaterialForAdd" :loading="materialQueryLoading">查询</el-button>
        </el-form-item>
      </el-form>
      <!-- 物料查询表格 - rqrq -->
      <el-table :data="materialQueryList" :height="300" border highlight-current-row ref="materialQueryTable" v-loading="materialQueryLoading" @selection-change="handleMaterialSelectionChange" style="width: 100%;">
        <el-table-column type="selection" width="50"></el-table-column>
        <el-table-column
          v-for="(item,index) in materialQueryColumnList" :key="index"
          :sortable="item.columnSortable"
          :prop="item.columnProp"
          :header-align="item.headerAlign"
          :show-overflow-tooltip="item.showOverflowTooltip"
          :align="item.align"
          :fixed="item.fixed==''?false:item.fixed"
          :min-width="item.columnWidth"
          :label="item.columnLabel">
          <template slot-scope="scope">
            <span>{{ scope.row[item.columnProp] }}</span>
          </template>
        </el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button @click="materialQueryDialogVisible = false" :disabled="addMaterialLoading">取 消</el-button>
        <el-button type="primary" @click="confirmAddMaterial" :loading="addMaterialLoading" :disabled="addMaterialLoading || selectedMaterials.length === 0">{{ addMaterialLoading ? '添加中...' : '确认添加' }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { searchCountHeaderList, createCycleCount, createManualCount, queryMaterialForManualCount, addMaterialToCount, releaseCount, completeCount, cancelCount, deleteCount, searchCountLabelList, searchCountPalletList, searchCountResultList, searchMaterialSummary } from '@/api/check/physicalInventory'

export default {
  name: 'searchPhysicalInventory',
  data() {
    return {
      // 主表相关 - rqrq
      dataList: [],
      dataListLoading: false,
      pageIndex: 1,
      pageSize: 20,
      totalPage: 0,
      height: 300,
      currentRow: null,

      // 查询表单 - rqrq
      queryForm: {
        searchCountNo: '',
        searchCountType: '',
        searchStatus: '',
        startDate: '',
        endDate: ''
      },

      // 主表格列配置 - rqrq
      mainColumnList: [
        { columnProp: "countNo", headerAlign: "center", align: "center", columnLabel: "盘点单号", columnWidth: 160, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "countTypeDesc", headerAlign: "center", align: "center", columnLabel: "盘点类型", columnWidth: 100, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "statusDesc", headerAlign: "center", align: "center", columnLabel: "状态", columnWidth: 80, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "countPercent", headerAlign: "center", align: "center", columnLabel: "盘点比例", columnWidth: 80, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "countProgress", headerAlign: "center", align: "center", columnLabel: "盘点进度", columnWidth: 120, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "totalPalletCount", headerAlign: "center", align: "center", columnLabel: "栈板数", columnWidth: 80, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "applyUser", headerAlign: "center", align: "center", columnLabel: "申请人", columnWidth: 100, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "applyDate", headerAlign: "center", align: "center", columnLabel: "申请日期", columnWidth: 140, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "releaseUser", headerAlign: "center", align: "center", columnLabel: "下达人", columnWidth: 100, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "releaseDate", headerAlign: "center", align: "center", columnLabel: "下达日期", columnWidth: 140, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "remark", headerAlign: "center", align: "left", columnLabel: "备注", columnWidth: 150, columnSortable: false, showOverflowTooltip: true, fixed: "" }
      ],

      // Tab相关 - rqrq
      activeName: 'label',
      labelList: [],
      labelListLoading: false,
      palletList: [],
      palletListLoading: false,
      resultList: [],
      resultListLoading: false,
      summaryList: [],
      summaryListLoading: false,

      // 标签明细列配置 - rqrq
      labelColumnList: [
        { columnProp: "unitId", headerAlign: "center", align: "center", columnLabel: "标签号", columnWidth: 150, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "partNo", headerAlign: "center", align: "center", columnLabel: "物料号", columnWidth: 120, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "partDesc", headerAlign: "center", align: "left", columnLabel: "物料描述", columnWidth: 180, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "qty", headerAlign: "center", align: "center", columnLabel: "数量", columnWidth: 80, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "batchNo", headerAlign: "center", align: "center", columnLabel: "批号", columnWidth: 100, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "palletId", headerAlign: "center", align: "center", columnLabel: "栈板号", columnWidth: 120, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "labelTypeDesc", headerAlign: "center", align: "center", columnLabel: "标签类型", columnWidth: 100, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "countFlagDesc", headerAlign: "center", align: "center", columnLabel: "盘点状态", columnWidth: 80, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "countUser", headerAlign: "center", align: "center", columnLabel: "盘点人", columnWidth: 100, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "countDate", headerAlign: "center", align: "center", columnLabel: "盘点时间", columnWidth: 140, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "warehouseName", headerAlign: "center", align: "center", columnLabel: "仓库", columnWidth: 100, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "locationName", headerAlign: "center", align: "center", columnLabel: "库位", columnWidth: 100, columnSortable: false, showOverflowTooltip: true, fixed: "" }
      ],

      // 栈板明细列配置 - rqrq
      palletColumnList: [
        { columnProp: "seqNo", headerAlign: "center", align: "center", columnLabel: "序号", columnWidth: 60, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "palletId", headerAlign: "center", align: "center", columnLabel: "栈板号", columnWidth: 150, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "labelCount", headerAlign: "center", align: "center", columnLabel: "标签数", columnWidth: 80, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "checkedCount", headerAlign: "center", align: "center", columnLabel: "已盘点", columnWidth: 80, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "progressPercent", headerAlign: "center", align: "center", columnLabel: "进度", columnWidth: 80, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "countFlagDesc", headerAlign: "center", align: "center", columnLabel: "盘点状态", columnWidth: 80, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "stationArea", headerAlign: "center", align: "center", columnLabel: "站点区域", columnWidth: 100, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "stationId", headerAlign: "center", align: "center", columnLabel: "站点ID", columnWidth: 100, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "locationCode", headerAlign: "center", align: "center", columnLabel: "位置编码", columnWidth: 120, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "countUser", headerAlign: "center", align: "center", columnLabel: "盘点人", columnWidth: 100, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "countDate", headerAlign: "center", align: "center", columnLabel: "盘点时间", columnWidth: 140, columnSortable: false, showOverflowTooltip: true, fixed: "" }
      ],

      // 盘点结果列配置 - rqrq
      resultColumnList: [
        { columnProp: "unitId", headerAlign: "center", align: "center", columnLabel: "标签号", columnWidth: 150, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "partNo", headerAlign: "center", align: "center", columnLabel: "物料号", columnWidth: 120, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "partDesc", headerAlign: "center", align: "left", columnLabel: "物料描述", columnWidth: 180, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "qty", headerAlign: "center", align: "center", columnLabel: "数量", columnWidth: 80, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "batchNo", headerAlign: "center", align: "center", columnLabel: "批号", columnWidth: 100, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "palletId", headerAlign: "center", align: "center", columnLabel: "栈板号", columnWidth: 120, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "countResultDesc", headerAlign: "center", align: "center", columnLabel: "盘点结果", columnWidth: 80, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "countUser", headerAlign: "center", align: "center", columnLabel: "盘点人", columnWidth: 100, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "countDate", headerAlign: "center", align: "center", columnLabel: "盘点时间", columnWidth: 140, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "remark", headerAlign: "center", align: "left", columnLabel: "备注", columnWidth: 150, columnSortable: false, showOverflowTooltip: true, fixed: "" }
      ],

      // 物料汇总列配置 - rqrq
      summaryColumnList: [
        { columnProp: "partNo", headerAlign: "center", align: "center", columnLabel: "物料号", columnWidth: 120, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "partDesc", headerAlign: "center", align: "left", columnLabel: "物料描述", columnWidth: 180, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "batchNo", headerAlign: "center", align: "center", columnLabel: "批号", columnWidth: 100, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "wdr", headerAlign: "center", align: "center", columnLabel: "WDR号", columnWidth: 120, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "warehouseName", headerAlign: "center", align: "center", columnLabel: "仓库", columnWidth: 100, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "locationName", headerAlign: "center", align: "center", columnLabel: "库位", columnWidth: 100, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "labelCount", headerAlign: "center", align: "center", columnLabel: "标签数", columnWidth: 80, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "totalQty", headerAlign: "center", align: "center", columnLabel: "总数量", columnWidth: 100, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "palletCount", headerAlign: "center", align: "center", columnLabel: "栈板数", columnWidth: 80, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "checkedLabelCount", headerAlign: "center", align: "center", columnLabel: "已盘点", columnWidth: 80, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "progressPercent", headerAlign: "center", align: "center", columnLabel: "进度", columnWidth: 80, columnSortable: false, showOverflowTooltip: true, fixed: "" }
      ],

      // 循环盘点弹窗 - rqrq
      cycleCountDialogVisible: false,
      cycleCountForm: {
        countPercent: 10,
        remark: ''
      },
      createLoading: false,

      // 添加物料弹窗 - rqrq
      addMaterialDialogVisible: false,
      addMaterialRow: null,
      currentLabelList: [],
      currentLabelListLoading: false,

      // 当前标签列表列配置 - rqrq
      currentLabelColumnList: [
        { columnProp: "unitId", headerAlign: "center", align: "center", columnLabel: "标签号", columnWidth: 150, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "partNo", headerAlign: "center", align: "center", columnLabel: "物料号", columnWidth: 120, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "partDesc", headerAlign: "center", align: "left", columnLabel: "物料描述", columnWidth: 180, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "qty", headerAlign: "center", align: "center", columnLabel: "数量", columnWidth: 80, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "batchNo", headerAlign: "center", align: "center", columnLabel: "批号", columnWidth: 100, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "palletId", headerAlign: "center", align: "center", columnLabel: "栈板号", columnWidth: 120, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "labelTypeDesc", headerAlign: "center", align: "center", columnLabel: "标签类型", columnWidth: 100, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "warehouseName", headerAlign: "center", align: "center", columnLabel: "仓库", columnWidth: 100, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "locationName", headerAlign: "center", align: "center", columnLabel: "库位", columnWidth: 100, columnSortable: false, showOverflowTooltip: true, fixed: "" }
      ],

      // 物料查询弹窗 - rqrq
      materialQueryDialogVisible: false,
      materialQueryForm: {
        searchPartNo: '',
        searchBatchNo: '',
        searchWdr: ''
      },
      materialQueryList: [],
      materialQueryLoading: false,
      selectedMaterials: [],
      addMaterialLoading: false,

      // 物料查询列配置 - rqrq
      materialQueryColumnList: [
        { columnProp: "partNo", headerAlign: "center", align: "center", columnLabel: "物料号", columnWidth: 120, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "partDesc", headerAlign: "center", align: "left", columnLabel: "物料描述", columnWidth: 180, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "batchNo", headerAlign: "center", align: "center", columnLabel: "批号", columnWidth: 100, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "wdr", headerAlign: "center", align: "center", columnLabel: "WDR号", columnWidth: 120, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "warehouseName", headerAlign: "center", align: "center", columnLabel: "仓库", columnWidth: 100, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "locationName", headerAlign: "center", align: "center", columnLabel: "库位", columnWidth: 100, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "labelCount", headerAlign: "center", align: "center", columnLabel: "标签数", columnWidth: 80, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "totalQty", headerAlign: "center", align: "center", columnLabel: "总数量", columnWidth: 100, columnSortable: false, showOverflowTooltip: true, fixed: "" },
        { columnProp: "palletCount", headerAlign: "center", align: "center", columnLabel: "栈板数", columnWidth: 80, columnSortable: false, showOverflowTooltip: true, fixed: "" }
      ]
    }
  },
  mounted() {
    // 计算表格高度 - rqrq
    this.height = (window.innerHeight - 315) / 2
    this.search()
  },
  methods: {
    // 查询 - rqrq
    search() {
      this.pageIndex = 1
      this.getDataList()
    },

    // 重置查询 - rqrq
    resetQuery() {
      this.queryForm = {
        searchCountNo: '',
        searchCountType: '',
        searchStatus: '',
        startDate: '',
        endDate: ''
      }
      this.search()
    },

    // 获取主表数据 - rqrq
    getDataList() {
      this.dataListLoading = true
      const params = {
        ...this.queryForm,
        site: this.$store.state.user.site,
        page: this.pageIndex,
        limit: this.pageSize
      }
      searchCountHeaderList(params).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list
          this.totalPage = data.page.totalCount
        } else {
          this.$message.error(data.msg || '查询失败')
        }
      }).catch(() => {
        this.$message.error('网络错误')
      }).finally(() => {
        this.dataListLoading = false
      })
    },

    // 行点击 - rqrq
    handleRowClick(row) {
      this.currentRow = row
      this.loadTabData()
    },

    // Tab点击 - rqrq
    tabClick() {
      this.loadTabData()
    },

    // 加载Tab数据 - rqrq
    loadTabData() {
      if (!this.currentRow) return
      const params = {
        site: this.$store.state.user.site,
        countNo: this.currentRow.countNo
      }

      if (this.activeName === 'label') {
        this.loadLabelList(params)
      } else if (this.activeName === 'pallet') {
        this.loadPalletList(params)
      } else if (this.activeName === 'result') {
        this.loadResultList(params)
      } else if (this.activeName === 'summary') {
        this.loadSummaryList(params)
      }
    },

    // 加载标签明细 - rqrq
    loadLabelList(params) {
      this.labelListLoading = true
      searchCountLabelList(params).then(({ data }) => {
        if (data && data.code === 0) {
          this.labelList = data.rows
        }
      }).finally(() => {
        this.labelListLoading = false
      })
    },

    // 加载栈板明细 - rqrq
    loadPalletList(params) {
      this.palletListLoading = true
      searchCountPalletList(params).then(({ data }) => {
        if (data && data.code === 0) {
          this.palletList = data.rows
        }
      }).finally(() => {
        this.palletListLoading = false
      })
    },

    // 加载盘点结果 - rqrq
    loadResultList(params) {
      this.resultListLoading = true
      searchCountResultList(params).then(({ data }) => {
        if (data && data.code === 0) {
          this.resultList = data.rows
        }
      }).finally(() => {
        this.resultListLoading = false
      })
    },

    // 加载物料汇总 - rqrq
    loadSummaryList(params) {
      this.summaryListLoading = true
      searchMaterialSummary(params).then(({ data }) => {
        if (data && data.code === 0) {
          this.summaryList = data.rows
        }
      }).finally(() => {
        this.summaryListLoading = false
      })
    },

    // 分页 - rqrq
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },

    // 显示循环盘点弹窗 - rqrq
    showCycleCountDialog() {
      this.cycleCountForm = {
        countPercent: 10,
        remark: ''
      }
      this.cycleCountDialogVisible = true
    },

    // 创建循环盘点 - rqrq
    createCycleCount() {
      if (!this.cycleCountForm.countPercent || this.cycleCountForm.countPercent < 1 || this.cycleCountForm.countPercent > 50) {
        this.$message.warning('盘点比例必须在1-50之间')
        return
      }
      this.createLoading = true
      const params = {
        site: this.$store.state.user.site,
        countPercent: this.cycleCountForm.countPercent,
        remark: this.cycleCountForm.remark,
        username: this.$store.state.user.name
      }
      createCycleCount(params).then(({ data }) => {
        if (data && data.code === 0) {
          this.$message.success('创建成功，盘点单号：' + data.row.countNo)
          this.cycleCountDialogVisible = false
          this.search()
        } else {
          this.$alert(data.msg || '创建失败', '提示', { type: 'error' })
        }
      }).catch(() => {
        this.$message.error('网络错误')
      }).finally(() => {
        this.createLoading = false
      })
    },

    // 创建手工盘点单 - rqrq
    handleCreateManualCount() {
      this.$confirm('是否创建手工盘点单？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'info'
      }).then(() => {
        this.createLoading = true
        const params = {
          site: this.$store.state.user.site,
          username: this.$store.state.user.name
        }
        createManualCount(params).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message.success('创建成功，盘点单号：' + data.row.countNo)
            this.search()
            // 自动打开添加物料弹窗 - rqrq
            this.$nextTick(() => {
              this.handleAddMaterial(data.row)
            })
          } else {
            this.$alert(data.msg || '创建失败', '提示', { type: 'error' })
          }
        }).catch(() => {
          this.$message.error('网络错误')
        }).finally(() => {
          this.createLoading = false
        })
      }).catch(() => {})
    },

    // 打开添加物料弹窗 - rqrq
    handleAddMaterial(row) {
      this.addMaterialRow = row
      this.addMaterialDialogVisible = true
      this.loadCurrentLabelList()
    },

    // 加载当前盘点单的标签列表 - rqrq
    loadCurrentLabelList() {
      if (!this.addMaterialRow) return
      this.currentLabelListLoading = true
      const params = {
        site: this.$store.state.user.site,
        countNo: this.addMaterialRow.countNo
      }
      searchCountLabelList(params).then(({ data }) => {
        if (data && data.code === 0) {
          this.currentLabelList = data.rows
        }
      }).finally(() => {
        this.currentLabelListLoading = false
      })
    },

    // 显示物料查询弹窗 - rqrq
    showMaterialQueryDialog() {
      this.materialQueryForm = {
        searchPartNo: '',
        searchBatchNo: '',
        searchWdr: ''
      }
      this.materialQueryList = []
      this.selectedMaterials = []
      this.materialQueryDialogVisible = true
    },

    // 查询物料 - rqrq
    queryMaterialForAdd() {
      if (!this.materialQueryForm.searchPartNo) {
        this.$message.warning('请输入物料编码')
        return
      }
      this.materialQueryLoading = true
      const params = {
        site: this.$store.state.user.site,
        ...this.materialQueryForm
      }
      queryMaterialForManualCount(params).then(({ data }) => {
        if (data && data.code === 0) {
          this.materialQueryList = data.rows
          if (data.rows.length === 0) {
            this.$message.warning('未找到符合条件的物料')
          }
        } else {
          this.$alert(data.msg || '查询失败', '提示', { type: 'error' })
        }
      }).catch(() => {
        this.$message.error('网络错误')
      }).finally(() => {
        this.materialQueryLoading = false
      })
    },

    // 物料选择变化 - rqrq
    handleMaterialSelectionChange(selection) {
      this.selectedMaterials = selection
    },

    // 确认添加物料 - rqrq
    confirmAddMaterial() {
      if (this.selectedMaterials.length === 0) {
        this.$message.warning('请选择要添加的物料')
        return
      }
      this.addMaterialLoading = true
      const params = {
        site: this.$store.state.user.site,
        countNo: this.addMaterialRow.countNo,
        selectedMaterials: this.selectedMaterials,
        username: this.$store.state.user.name
      }
      addMaterialToCount(params).then(({ data }) => {
        if (data && data.code === 0) {
          this.$message.success('添加成功，新增 ' + data.result + ' 个标签')
          this.materialQueryDialogVisible = false
          this.loadCurrentLabelList()
          this.getDataList()
        } else {
          this.$alert(data.msg || '添加失败', '提示', { type: 'error' })
        }
      }).catch(() => {
        this.$message.error('网络错误')
      }).finally(() => {
        this.addMaterialLoading = false
      })
    },

    // 下达盘点单 - rqrq
    handleRelease(row) {
      this.$confirm('确认下达盘点单 ' + row.countNo + '？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const params = {
          site: this.$store.state.user.site,
          countNo: row.countNo,
          username: this.$store.state.user.name
        }
        releaseCount(params).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message.success('下达成功')
            this.getDataList()
          } else {
            this.$alert(data.msg || '下达失败', '提示', { type: 'error' })
          }
        }).catch(() => {
          this.$message.error('网络错误')
        })
      }).catch(() => {})
    },

    // 完成盘点单 - rqrq
    handleComplete(row) {
      this.$confirm('确认完成盘点单 ' + row.countNo + '？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const params = {
          site: this.$store.state.user.site,
          countNo: row.countNo,
          username: this.$store.state.user.name
        }
        completeCount(params).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message.success('操作成功')
            this.getDataList()
          } else {
            this.$alert(data.msg || '操作失败', '提示', { type: 'error' })
          }
        }).catch(() => {
          this.$message.error('网络错误')
        })
      }).catch(() => {})
    },

    // 取消盘点单 - rqrq
    handleCancel(row) {
      this.$confirm('确认取消盘点单 ' + row.countNo + '？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const params = {
          site: this.$store.state.user.site,
          countNo: row.countNo,
          username: this.$store.state.user.name
        }
        cancelCount(params).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message.success('取消成功')
            this.getDataList()
          } else {
            this.$alert(data.msg || '取消失败', '提示', { type: 'error' })
          }
        }).catch(() => {
          this.$message.error('网络错误')
        })
      }).catch(() => {})
    },

    // 删除盘点单 - rqrq
    handleDelete(row) {
      this.$confirm('确认删除盘点单 ' + row.countNo + '？删除后不可恢复！', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const params = {
          site: this.$store.state.user.site,
          countNo: row.countNo
        }
        deleteCount(params).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message.success('删除成功')
            this.getDataList()
          } else {
            this.$alert(data.msg || '删除失败', '提示', { type: 'error' })
          }
        }).catch(() => {
          this.$message.error('网络错误')
        })
      }).catch(() => {})
    },

    // 格式化日期时间 - rqrq
    formatDateTime(dateTime) {
      if (!dateTime) return ''
      const date = new Date(dateTime)
      const year = date.getFullYear()
      const month = String(date.getMonth() + 1).padStart(2, '0')
      const day = String(date.getDate()).padStart(2, '0')
      const hours = String(date.getHours()).padStart(2, '0')
      const minutes = String(date.getMinutes()).padStart(2, '0')
      return `${year}-${month}-${day} ${hours}:${minutes}`
    },

    // 获取状态颜色 - rqrq
    getStatusColor(status) {
      const colorMap = {
        'DRAFT': '#909399',
        'RELEASED': '#409EFF',
        'CHECKING': '#E6A23C',
        'COMPLETED': '#67C23A',
        'CANCELLED': '#F56C6C'
      }
      return colorMap[status] || '#909399'
    },

    // 获取盘点结果颜色 - rqrq
    getResultColor(result) {
      const colorMap = {
        'OK': '#67C23A',
        'MISSING': '#F56C6C',
        'SURPLUS': '#E6A23C'
      }
      return colorMap[result] || '#909399'
    }
  }
}
</script>

<style scoped>
/deep/ .el-table a {
  color: #409EFF;
  cursor: pointer;
}
/deep/ .el-table a:hover {
  text-decoration: underline;
}
</style>
