<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="100px">
            <!--            <el-form-item label="库位编码" prop="locationCode">
                <el-input v-model="queryParams.locationCode" placeholder="请输入库位编码" clearable @keyup.enter="handleQuery" />
              </el-form-item>-->
            <el-form-item label="物料编码" prop="productCode">
              <el-input v-model="queryParams.productCode" placeholder="请输入物料编码" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="物料名称" prop="productName">
              <el-input v-model="queryParams.productName" placeholder="请输入物料名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>

            <el-form-item label="选择仓库" prop="warehouseId">
              <el-select v-model="queryParams.warehouseId" placeholder="请选择仓库" clearable>
                <el-option v-for="item in warehouseList" :key="item.warehouseId" :label="item.warehouseName" :value="item.warehouseId" />
              </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-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <el-table
        ref="childTable"
        v-loading="loading"
        border
        :data="inventoryDetailsList"
        @expand-change="selectChildList"
        :row-key="(row) => `${row.materielId}_${row.warehouseId}`"
      >
        <!-- 添加展开列 -->
        <el-table-column type="expand" width="55">
          <template #default="{ row }">
            <el-table border :data="row.data" v-loading="row.loading" style="width: 95%; margin: 0 auto">
              <!--<el-table-column label="库位编码" align="center" prop="locationCode" />-->
              <el-table-column label="批次号" align="center" prop="batchNumber" />
              <el-table-column label="库存数量" align="center" prop="inventoryAmount" />
              <el-table-column label="库存数量" align="center" prop="inventoryAmount" />
              <!--              <el-table-column label="锁定数量" align="center" prop="lockedAmount" />-->

              <el-table-column label="创建时间" align="center" prop="createTime" />
            </el-table>
          </template>
        </el-table-column>

        <el-table-column label="所属仓库" align="center" prop="warehouseName" v-if="columns[3].visible" />
        <el-table-column label="仓库编码" align="center" prop="warehouseCode" v-if="columns[3].visible" />
        <el-table-column label="物料编码" align="center" prop="productCode" v-if="columns[4].visible" />
        <el-table-column label="物料名称" align="center" prop="productName" v-if="columns[4].visible" />
        <el-table-column label="物料描述" align="center" prop="productSpe" v-if="columns[4].visible" />
        <el-table-column label="外部品牌" align="center" prop="externalBrand" v-if="columns[4].visible" />
        <el-table-column label="库存数量" align="center" prop="inventoryAmount" v-if="columns[6].visible" />
      </el-table>

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

<script setup name="InventoryDetails" lang="ts">
import { getWmsInventoryDetailsList, listCountQuery } from '@/api/wms/inventoryDetails';
import { InventoryDetailsForm, InventoryDetailsQuery, InventoryDetailsVO } from '@/api/wms/inventoryDetails/types';
import { getWmsWarehouseInfoList } from '@/api/wms/warehouseInfo';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { use_state } = toRefs<any>(proxy?.useDict('use_state'));

const inventoryDetailsList = ref<InventoryDetailsVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const warehouseList = ref([]);
const childTable = ref([]);
const queryFormRef = ref<ElFormInstance>();
const inventoryDetailsFormRef = ref<ElFormInstance>();

const getWarehouseList = async () => {
  const res = await getWmsWarehouseInfoList();
  warehouseList.value = res.data;
};
const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

// 列显隐信息
const columns = ref<FieldOption[]>([
  { key: 0, label: `库存ID`, visible: false },
  { key: 1, label: `租户编号`, visible: false },
  { key: 2, label: `库位编码`, visible: false },
  { key: 3, label: `仓库ID`, visible: true },
  { key: 4, label: `物料ID`, visible: true },
  { key: 5, label: `批次号`, visible: true },
  { key: 6, label: `库存数量`, visible: true },
  { key: 7, label: `锁定数量`, visible: true },
  { key: 8, label: `备注`, visible: false },
  { key: 9, label: `删除标志`, visible: true },
  { key: 10, label: `创建部门`, visible: true },
  { key: 11, label: `创建人`, visible: true },
  { key: 12, label: `创建时间`, visible: true },
  { key: 13, label: `更新人`, visible: true },
  { key: 14, label: `更新时间`, visible: true }
]);

const initFormData: InventoryDetailsForm = {
  inventoryDetailsId: undefined,
  locationCode: undefined,
  warehouseId: undefined,
  materielId: undefined,
  batchNumber: undefined,
  inventoryAmount: undefined,
  lockedAmount: undefined,
  remark: undefined
};
const data = reactive<PageData<InventoryDetailsForm, InventoryDetailsQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    locationCode: undefined,
    productCode: undefined,
    productName: undefined,
    warehouseId: undefined,
    materielId: undefined,
    batchNumber: undefined,
    inventoryAmount: undefined,
    lockedAmount: undefined,
    params: {}
  },
  rules: {}
});

const { queryParams, form, rules } = toRefs(data);

// 展开行的处理
const selectChildList = async (row, expandedRows) => {
  if (expandedRows.length && !row.data) {
    row.loading = true;
    const res = await getWmsInventoryDetailsList({
      materielId: row.materielId,
      warehouseId: row.warehouseId
    });
    row.data = res.data;
    row.loading = false;
  }
};

/** 查询库存明细列表 */
const getList = async () => {
  loading.value = true;
  const res = await listCountQuery(queryParams.value);
  inventoryDetailsList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
};

/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  inventoryDetailsFormRef.value?.resetFields();
};

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

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

onMounted(() => {
  getList();
  getWarehouseList();
});
</script>
