<template>
  <div class="itemList">
    <!-- 数据表格 -->
    <item-search
      @search="reload"
      ref="refSeavch"
      @handledime="handledime"
    ></item-search>
    <ele-pro-table
      ref="table"
      :initLoad="false"
      :columns="columns"
      :datasource="datasource"
      height="calc(100vh - 355px)"
      full-height="calc(100vh - 116px)"
      tool-class="ele-toolbar-form"
      cache-key="systemOrgUserTable"
      @select="selectChange"
      @select-all="changeSelectAll"
    >
      <!-- 表头工具栏 -->
      <template v-slot:toolbar="{ row }">
        <el-button
          v-if="selectedDime == 3 || selectedDime == 4"
          size="small"
          :disabled="checkRadioData.length == 0"
          icon="el-icon-set-up"
          class="ele-btn-icon"
          @click="allPrinting()"
        >
          打印条码
        </el-button>
        <el-button
          v-if="selectedDime == 2 || selectedDime == 3"
          size="small"
          :disabled="checkRadioData.length == 0"
          icon="el-icon-set-up"
          class="ele-btn-icon"
          @click="showAllot()"
        >
          调拨
        </el-button>
      </template>
      <!-- 批次号 -->
      <template v-slot:batchNo="{ row }">
        <el-popover placement="right-start" width="800" trigger="hover">
          <el-table :data="row.outInBatchDetailsVOList">
            <el-table-column
              width="150"
              property="bizNo"
              label="入库单号"
            ></el-table-column>
            <el-table-column
              width="100"
              property="bizType"
              label="入库场景"
            ></el-table-column>
            <el-table-column
              width="120"
              property="count"
              label="入库数量"
            ></el-table-column>
            <el-table-column
              width="80"
              property="measuringUnit"
              label="计量单位"
            ></el-table-column>
            <el-table-column
              width="100"
              property="packageCount"
              label="包装数量"
            ></el-table-column>
            <el-table-column
              width="80"
              property="packingUnit"
              label="包装单位"
            ></el-table-column>
            <el-table-column
              width="330"
              property="position"
              label="库位"
            ></el-table-column>
            <el-table-column
              width="160"
              property="createTime"
              label="入库时间"
            ></el-table-column>
          </el-table>
          <span slot="reference"> {{ row.batchNo }}</span>
        </el-popover>
      </template>
      <!-- 最小包装单元 -->
      <template v-slot:minPackingCount="{ row }">
        <span v-if="row.minPackingCount">
          {{ row.minPackingCount }}
          {{ row.measuringUnit }}/{{ row.minUnit }}
        </span>
      </template>
      <!-- 库存保质期 -->
      <template v-slot:expirationDate="{ row }">
        <span v-if="row.expirationDate">
          {{ row.expirationDate ? row.expirationDate : '-' }}
          {{
            row.expirationDateUnit == 'year'
              ? '年'
              : row.expirationDateUnit == 'month'
              ? '月'
              : '日'
          }}
        </span>
      </template>
      <!-- 质检状态 -->
      <template v-slot:qualityStatus="{ row }">
        <span v-if="row.qualityResult == 0 || row.qualityResult == 1"
          >已检</span
        >
        <span v-else-if="row.qualityStatus == 1">已检</span>
        <span v-else-if="row.qualityStatus == 0">未检</span>
        <span v-else>-</span>
      </template>
      <!-- 质检结果 -->
      <template v-slot:qualityResult="{ row }">
        <span v-if="row.qualityResult == 0 || row.qualityResult == ''"
          >合格</span
        >
        <span v-else-if="row.qualityResult == 1">不合格</span>
        <span v-else-if="row.qualityResult == 3">让步接收</span>
        <span v-else>-</span>
      </template>
      <!-- 操作列 -->
      <template v-slot:action="{ row }">
        <el-link
          type="primary"
          :underline="false"
          icon="el-icon-edit"
          @click="details(row)"
        >
          详情
        </el-link>
      </template>
    </ele-pro-table>
    <print ref="printRef" :dimension="selectedDime"></print>
    <printSr ref="printSrRef" :dimension="selectedDime"></printSr>
    <printTg ref="printTgRef" :dimension="selectedDime"></printTg>
    <allot ref="allotRef" :dimension="selectedDime"></allot>
  </div>
</template>

<script>
  import allot from './allot.vue';
  import print from './print.vue';
  import printSr from './printSr.vue';
  import printTg from './printTg.vue';
  import ItemSearch from './item-search.vue';
  // import {
  //   getUserPage,
  //   removePersonnel,
  //   unbindLoginName
  // } from '@/api/system/organization';
  import {
    pageeLedgerMain,
    removeItem,
    getWarehouseList
  } from '@/api/classifyManage/itemInformation';

  import ouint from '@/api/warehouseManagement/outin';
  import { getBatchDetails } from '@/api/classifyManage/index';

  export default {
    components: { ItemSearch, print, printSr, printTg, allot },
    props: {
      // 机构id
      organizationId: [Number, String],
      // 全部机构
      organizationList: Array,
      current: {
        type: Object,
        default: () => ({})
      }
    },
    data() {
      return {
        checkRadioData: [],
        searchForm: {
          dimension: '1'
        },
        selectedDime: '1'
      };
    },
    computed: {
      // 表格列配置
      columns() {
        let arr = [];
        console.log(this.clientEnvironmentId);
        // selectedDime 1物品维度 2批次维度 3包装维度 4物料维度
        switch (this.selectedDime) {
          // 物品维度
          case '1':
            arr = [
              {
                columnKey: 'index',
                type: 'index',
                width: 50,
                align: 'center',
                label: '序号',
                showOverflowTooltip: true,
                fixed: 'left'
              },
              {
                prop: 'code',
                label: '物品编码',
                showOverflowTooltip: true
              },
              {
                prop: 'name',
                label: '名称',
                showOverflowTooltip: true
              },
              {
                prop: 'brandNum',
                label: '牌号',
                showOverflowTooltip: true
              },
              {
                prop: 'modelType',
                label: '型号',
                showOverflowTooltip: true
              },
              {
                prop: 'specification',
                label: '规格',
                showOverflowTooltip: true
              },
              {
                prop: 'manualBatchNo',
                label: '批号',
                align: 'center'
              },
              {
                prop: 'level',
                label: '级别',
                showOverflowTooltip: true
              },
              {
                prop: 'availableCountBase',
                label: '计量库存数量',
                sortable: 'custom',
                showOverflowTooltip: true,
                width: 130,
                align: 'center'
              },
              {
                prop: 'measuringUnit',
                label: '计量单位',
                align: 'center'
              },
              {
                prop: 'weight',
                label: '重量',
                showOverflowTooltip: true
              },
              {
                prop: 'weightUnit',
                label: '重量单位',
                showOverflowTooltip: true
              },
              {
                prop: 'expirationDate',
                slot: 'expirationDate',
                label: '库存保质期',
                showOverflowTooltip: true,
                width: 100
              },
              {
                prop: 'expirationTime',
                label: '周期倒计时',
                showOverflowTooltip: true,
                width: 100
              },
              {
                prop: 'qualityResult',
                slot: 'qualityResult',
                label: '质检结果',
                showOverflowTooltip: true,
                width: 100
              },
              {
                prop: 'qualityStatus',
                slot: 'qualityStatus',
                label: '质检状态',
                showOverflowTooltip: true,
                width: 100
              },
              {
                columnKey: 'action',
                label: '操作',
                width: 100,
                align: 'left',
                resizable: false,
                slot: 'action',
                fixed: 'right'
              }
            ];
            // 1本地 2索尔 3碳谷 4601
            if (this.clientEnvironmentId == 4) {
              arr.splice(10, 0, {
                slot: 'purpose',
                prop: 'purpose',
                label: '采购用途',
                showOverflowTooltip: true
              });
              return arr;
            } else {
              return arr;
            }
          // 批次维度
          case '2':
            arr = [
              {
                width: 45,
                type: 'selection',
                columnKey: 'selection',
                align: 'center'
              },
              {
                columnKey: 'index',
                type: 'index',
                width: 50,
                align: 'center',
                label: '序号',
                showOverflowTooltip: true,
                fixed: 'left'
              },
              {
                prop: 'code',
                label: '物品编码',
                showOverflowTooltip: true
              },
              {
                prop: 'name',
                label: '名称',
                showOverflowTooltip: true
              },
              {
                prop: 'brandNum',
                label: '牌号',
                showOverflowTooltip: true
              },
              {
                prop: 'modelType',
                label: '型号',
                showOverflowTooltip: true
              },
              {
                prop: 'specification',
                label: '规格',
                showOverflowTooltip: true
              },
              {
                prop: 'manualBatchNo',
                label: '批号',
                align: 'center'
              },
              {
                prop: 'level',
                label: '级别',
                showOverflowTooltip: true
              },
              {
                prop: 'packingCountBase',
                label: '包装库存数量',
                sortable: 'custom',
                showOverflowTooltip: true,
                width: 130,
                align: 'center'
              },
              {
                prop: 'minUnit',
                label: '包装单位',
                showOverflowTooltip: true
              },
              {
                prop: 'minPackingCount',
                slot: 'minPackingCount',
                label: '最小包装单元',
                showOverflowTooltip: true,
                width: 120
              },
              {
                prop: 'availableCountBase',
                label: '计量库存数量',
                sortable: 'custom',
                showOverflowTooltip: true,
                width: 130,
                align: 'center'
              },
              {
                prop: 'measuringUnit',
                label: '计量单位',
                align: 'center'
              },
              {
                prop: 'weight',
                label: '重量',
                showOverflowTooltip: true
              },
              {
                prop: 'weightUnit',
                label: '重量单位',
                showOverflowTooltip: true
              },
              {
                prop: 'expirationDate',
                slot: 'expirationDate',
                label: '库存保质期',
                showOverflowTooltip: true,
                width: 100
              },
              {
                prop: 'expirationTime',
                label: '周期倒计时',
                showOverflowTooltip: true,
                width: 100
              },
              {
                prop: 'qualityResult',
                slot: 'qualityResult',
                label: '质检结果',
                showOverflowTooltip: true,
                width: 100
              },
              {
                prop: 'qualityStatus',
                slot: 'qualityStatus',
                label: '质检状态',
                showOverflowTooltip: true,
                width: 100
              },
              {
                prop: 'paths',
                label: '仓库',
                width: 230,
                showOverflowTooltip: true
              },
              {
                columnKey: 'action',
                label: '操作',
                width: 100,
                align: 'left',
                resizable: false,
                slot: 'action',
                fixed: 'right'
              }
            ];
            // 1本地 2索尔 3碳谷 4601
            if (this.clientEnvironmentId != 2) {
              arr.splice(1, 0, {
                slot: 'batchNo',
                prop: 'batchNo',
                label: '批次号',
                showOverflowTooltip: true
              });
              return arr;
            } else {
              return arr;
            }
          // 包装维度
          case '3':
            arr = [
              {
                width: 45,
                type: 'selection',
                columnKey: 'selection',
                align: 'center'
              },
              {
                columnKey: 'index',
                type: 'index',
                width: 50,
                align: 'center',
                label: '序号',
                showOverflowTooltip: true,
                fixed: 'left'
              },
              {
                prop: 'packagingCode',
                label: '包装编码',
                showOverflowTooltip: true
              },
              {
                prop: 'categoryCode',
                label: '物品编码',
                showOverflowTooltip: true
              },
              {
                prop: 'name',
                label: '名称',
                showOverflowTooltip: true
              },
              {
                prop: 'brandNum',
                label: '牌号',
                showOverflowTooltip: true
              },
              {
                prop: 'modelType',
                label: '型号',
                showOverflowTooltip: true
              },
              {
                prop: 'specification',
                label: '规格',
                showOverflowTooltip: true
              },
              {
                prop: 'manualBatchNo',
                label: '批号',
                align: 'center'
              },
              {
                prop: 'level',
                label: '级别',
                showOverflowTooltip: true
              },
              {
                prop: 'barcodes',
                label: '发货条码',
                align: 'center'
              },
              {
                prop: 'clientCode',
                label: '客户代号',
                showOverflowTooltip: true
              },
              {
                prop: 'engrave',
                label: '刻码',
                showOverflowTooltip: true
              },
              {
                prop: 'packingCountBase',
                label: '包装库存数量',
                sortable: 'custom',
                showOverflowTooltip: true,
                width: 130,
                align: 'center'
              },
              {
                prop: 'minUnit',
                label: '包装单位',
                showOverflowTooltip: true
              },
              {
                prop: 'minPackingCount',
                slot: 'minPackingCount',
                label: '最小包装单元',
                showOverflowTooltip: true,
                width: 120
              },
              {
                prop: 'availableCountBase',
                label: '计量库存数量',
                sortable: 'custom',
                showOverflowTooltip: true,
                width: 130,
                align: 'center'
              },
              {
                prop: 'measuringUnit',
                label: '计量单位',
                align: 'center'
              },
              {
                prop: 'weight',
                label: '重量',
                showOverflowTooltip: true
              },
              {
                prop: 'weightUnit',
                label: '重量单位',
                showOverflowTooltip: true
              },
              {
                prop: 'expirationDate',
                slot: 'expirationDate',
                label: '库存保质期',
                showOverflowTooltip: true,
                width: 100
              },
              {
                prop: 'expirationTime',
                label: '周期倒计时',
                showOverflowTooltip: true,
                width: 100
              },
              {
                prop: 'qualityResult',
                slot: 'qualityResult',
                label: '质检结果',
                showOverflowTooltip: true,
                width: 100
              },
              {
                prop: 'qualityStatus',
                slot: 'qualityStatus',
                label: '质检状态',
                showOverflowTooltip: true,
                width: 100
              },
              {
                prop: 'paths',
                width: 230,
                label: '仓库',
                showOverflowTooltip: true
              },
              {
                columnKey: 'action',
                label: '操作',
                width: 100,
                align: 'left',
                resizable: false,
                slot: 'action',
                fixed: 'right'
              }
            ];
            // 1本地 2索尔 3碳谷 4601
            if (this.clientEnvironmentId != 2) {
              arr.splice(1, 0, {
                slot: 'batchNo',
                prop: 'batchNo',
                label: '批次号',
                showOverflowTooltip: true
              });
              return arr;
            } else {
              return arr;
            }
          // 物料维度
          default:
            arr = [
              {
                width: 45,
                type: 'selection',
                columnKey: 'selection',
                align: 'center'
              },
              {
                columnKey: 'index',
                type: 'index',
                width: 50,
                align: 'center',
                label: '序号',
                showOverflowTooltip: true,
                fixed: 'left'
              },
              {
                prop: 'no',
                label: '物料编码',
                showOverflowTooltip: true
              },
              {
                prop: 'assetCode',
                label: '物品编码',
                showOverflowTooltip: true
              },
              {
                prop: 'name',
                label: '名称',
                showOverflowTooltip: true
              },
              {
                prop: 'brandNum',
                label: '牌号',
                showOverflowTooltip: true
              },
              {
                prop: 'modelType',
                label: '型号',
                showOverflowTooltip: true
              },
              {
                prop: 'specification',
                label: '规格',
                showOverflowTooltip: true
              },
              {
                prop: 'manualBatchNo',
                label: '批号',
                align: 'center'
              },
              {
                prop: 'level',
                label: '级别',
                showOverflowTooltip: true
              },
              {
                prop: 'barcodes',
                label: '发货条码',
                align: 'center'
              },
              {
                prop: 'materielCode',
                label: '物料代号',
                showOverflowTooltip: true
              },
              {
                prop: 'clientCode',
                label: '客户代号',
                showOverflowTooltip: true
              },
              {
                prop: 'engrave',
                label: '刻码',
                showOverflowTooltip: true
              },
              {
                prop: 'availableCountBase',
                label: '计量库存数量',
                sortable: 'custom',
                showOverflowTooltip: true,
                width: 130,
                align: 'center'
              },
              {
                prop: 'measuringUnit',
                label: '计量单位',
                align: 'center'
              },
              {
                prop: 'weight',
                label: '重量',
                showOverflowTooltip: true
              },
              {
                prop: 'weightUnit',
                label: '重量单位',
                showOverflowTooltip: true
              },
              {
                prop: 'expirationDate',
                slot: 'expirationDate',
                label: '库存保质期',
                showOverflowTooltip: true,
                width: 100
              },
              {
                prop: 'expirationTime',
                label: '周期倒计时',
                showOverflowTooltip: true,
                width: 100
              },
              {
                prop: 'qualityResult',
                slot: 'qualityResult',
                label: '质检结果',
                showOverflowTooltip: true,
                width: 100
              },
              {
                prop: 'qualityStatus',
                slot: 'qualityStatus',
                label: '质检状态',
                showOverflowTooltip: true,
                width: 100
              },
              {
                prop: 'paths',
                width: 230,
                label: '仓库',
                showOverflowTooltip: true
              },
              {
                columnKey: 'action',
                label: '操作',
                width: 100,
                align: 'left',
                resizable: false,
                slot: 'action',
                fixed: 'right'
              }
            ];
            // 1本地 2索尔 3碳谷 4601
            if (this.clientEnvironmentId != 2) {
              arr.splice(1, 0, {
                slot: 'batchNo',
                prop: 'batchNo',
                label: '批次号',
                showOverflowTooltip: true
              });
              return arr;
            } else {
              return arr;
            }
        }
      },
      clientEnvironmentId() {
        return this.$store.state.user.info.clientEnvironmentId;
      }
    },
    methods: {
      // 调拨
      showAllot() {
        this.$refs.allotRef.open(this.checkRadioData);
      },
      // 全选
      changeSelectAll(arr) {
        console.log(arr);
        if (arr.length != 0) {
          this.checkRadioData = arr;
        } else {
          this.checkRadioData = [];
        }
      },
      selectChange(selection, row) {
        if (selection.length != 0) {
          this.checkRadioData = selection;
        } else {
          this.checkRadioData = [];
        }
      },
      allPrinting() {
        if (this.clientEnvironmentId == 2) {
          this.$refs.printSrRef.open(this.checkRadioData);
        } else if (this.clientEnvironmentId == 3) {
          this.$refs.printTgRef.open(this.checkRadioData);
        } else {
          this.$refs.printRef.open(this.checkRadioData);
        }
      },
      handledime(val) {
        this.selectedDime = val;
        this.reload();
      },
      /* 表格数据源 */
      datasource({ page, limit, where, order }) {
        const dimension = this.$refs.refSeavch.dimension;
        const treeId = this.$parent.$parent.$parent.current.id;
        if (this.selectedDime == 1) {
          // 物品维度
          const data = ouint.getRealTimeInventory({
            ...where,
            ...order,
            pageNum: page,
            size: limit,
            dimension: dimension
          });
          const result = data.then((res) => {
            let item = res.list.map((item) => {
              return {
                ...item,
                qualityResult:
                  typeof item.qualityResult != 'number'
                    ? 3
                    : item.qualityResult,
                qualityStatus:
                  typeof item.qualityStatus != 'number' ? 3 : item.qualityStatus
              };
            });
            return { ...res, list: item };
          });
          return result;
        } else if (this.selectedDime == 2) {
          // 批次维度
          const params = {
            categoryLevelId: treeId,
            ...where,
            ...order,
            dimension: dimension
          };
          const data = getBatchDetails({
            ...params,
            pageNum: page,
            size: limit
          });

          const result = data.then((res) => {
            let item = res.list.map((item) => {
              return {
                ...item,
                paths: item.pathName.split(',')[0],
                minUnit: item.packingUnit,
                qualityResult:
                  typeof item.qualityResult != 'number'
                    ? 3
                    : item.qualityResult,
                qualityStatus:
                  typeof item.qualityStatus != 'number' ? 3 : item.qualityStatus
              };
            });
            return { ...res, list: item };
          });
          return result;
        } else if (this.selectedDime == 4) {
          // 物料维度
          const params = {
            categoryLevelId: treeId,
            ...where,
            ...order,
            dimension: dimension
          };
          const data = ouint.getMaterielDetails({
            ...params,
            pageNum: page,
            size: limit
          });
          const result = data.then((res) => {
            const data = res.list.map((item) => {
              return {
                ...item,
                minUnit: item.packingUnit,
                paths: item.pathName,
                availableCountBase:
                  item.availableCountBase < 0 ? 0 : item.availableCountBase,
                packingCountBase:
                  item.packingCountBase < 0 ? 0 : item.packingCountBase,
                materialCode: item.code,
                batchNum: item.batchNo,
                qualityResult:
                  typeof item.qualityResult != 'number'
                    ? 3
                    : item.qualityResult,
                qualityStatus:
                  typeof item.qualityStatus != 'number' ? 3 : item.qualityStatus
              };
            });
            console.log(data);
            return { ...res, list: data };
          });
          console.log(result);
          return result;
        } else {
          // 包装维度
          const params = {
            categoryLevelId: treeId,
            ...where,
            ...order,
            dimension: dimension
          };
          const data = ouint.getInventoryDetails({
            ...params,
            pageNum: page,
            size: limit
          });
          const result = data.then((res) => {
            const data = res.list.map((item) => {
              return {
                ...item,
                minUnit: item.packingUnit,
                paths: item.pathName.split(',')[0],
                availableCountBase:
                  item.availableCountBase < 0 ? 0 : item.availableCountBase,
                packingCountBase:
                  item.packingCountBase < 0 ? 0 : item.packingCountBase,
                packagingCode: item.code,
                qualityResult:
                  typeof item.qualityResult != 'number'
                    ? 3
                    : item.qualityResult,
                qualityStatus:
                  typeof item.qualityStatus != 'number' ? 3 : item.qualityStatus
              };
            });
            return { ...res, list: data };
          });
          return result;
        }
      },
      /* 刷新表格 */
      reload(where) {
        this.$nextTick(() => {
          this.$refs.table.reload({
            pageNum: 1,
            where: {
              ...where,
              categoryLevelId: this.current?.data?.id || this.current?.id
            }
          });
        });
      },

      details(row) {
        const key = Date.now();
        this.$store.commit('stockManagement/CLEAR_BASEINFO');
        this.$store.commit('stockManagement/CHANGE_BASEINFO', {
          key,
          value: row
        });

        const url =
          this.$refs.refSeavch.dimension == 1
            ? '/warehouseManagement/stockLedger/allBatchDetails'
            : '/warehouseManagement/stockLedger/batchDetails';

        this.$router.push({
          path: url,
          query: {
            key,
            dimension: this.$refs.refSeavch.dimension,
            id: this.$refs.refSeavch.dimension != 3 ? row.id : row.categoryId,
            assetId: row.assetId,
            batchNo: row.batchNo
          }
        });
      }
    },

    watch: {
      // 监听机构id变化
      current: {
        handler() {
          this.reload();
        }
      }
    }
  };
</script>
