<template>
  <div>
    <el-card class="box-card">
      <el-tabs v-model="activeName" type="card" @tab-change="changeActiveName">
        <el-tab-pane :label="'待备料（半成品）' + numData.data.semiFinishedCount" name="first">
          <byTable
            :hideTable="true"
            :hidePagination="true"
            :source="sourceList.data"
            :pagination="sourceList.pagination"
            :config="config"
            :searchConfig="searchConfig"
            highlight-current-row
            :action-list="[
              {
                text: '打印备料单',
                action: () => outExcel(0),
              },
              {
                text: '快捷出库',
                action: () => clickQuickDelivery(),
              },
            ]"
            @get-list="getList"
            @clickReset="clickReset">
          </byTable>
          <table class="table-class" border="0" cellpadding="0" cellspacing="0" v-loading="loading">
            <tr>
              <th style="width: 200px">BOM品号</th>
              <th style="min-width: 320px">BOM品名</th>
              <th style="width: 200px">SKU品号</th>
              <th style="width: 120px; text-align: center">数量小计</th>
              <th style="width: 140px; text-align: center">数量总计</th>
            </tr>
            <tbody v-for="(item, index) in sourceList.data" :key="index">
              <tr v-for="(itemSKU, indexSKU) in item.skuInfoList" :key="indexSKU">
                <td v-if="indexSKU === 0" :rowspan="item.skuInfoList.length">{{ item.bomSpecCode }}</td>
                <td v-if="indexSKU === 0" :rowspan="item.skuInfoList.length">{{ item.bomSpecName }}</td>
                <td>{{ itemSKU.skuSpecCode }}</td>
                <td style="text-align: center">{{ itemSKU.quantity }}</td>
                <td v-if="indexSKU === 0" :rowspan="item.skuInfoList.length" style="text-align: center">{{ item.totalQuantity }}</td>
              </tr>
            </tbody>
          </table>
        </el-tab-pane>
        <el-tab-pane :label="'待备料（成品）' + numData.data.finishedCount" name="third">
          <byTable
            :hideTable="true"
            :hidePagination="true"
            :source="sourceListThree.data"
            :pagination="sourceListThree.pagination"
            :config="config"
            :searchConfig="searchConfig"
            highlight-current-row
            :action-list="[
              {
                text: '打印备料单',
                action: () => outExcel(1),
              },
              {
                text: '快捷出库',
                action: () => clickQuickDelivery(),
              },
            ]"
            @get-list="getListThree"
            @clickReset="clickResetThree">
          </byTable>
          <table class="table-class" border="0" cellpadding="0" cellspacing="0" v-loading="loading">
            <tr>
              <th style="width: 200px">SKU品号</th>
              <th style="min-width: 320px">SKU品名</th>
              <th style="width: 120px; text-align: center">数量</th>
            </tr>
            <tbody v-for="(item, index) in sourceListThree.data" :key="index">
              <tr v-for="(itemSKU, indexSKU) in item.skuInfoList" :key="indexSKU">
                <td>{{ itemSKU.skuSpecCode }}</td>
                <td>{{ itemSKU.skuSpecName }}</td>
                <td style="text-align: center">{{ itemSKU.quantity }}</td>
              </tr>
            </tbody>
          </table>
        </el-tab-pane>
        <el-tab-pane label="已备料" name="second">
          <byTable
            :source="sourceListTwo.data"
            :pagination="sourceListTwo.pagination"
            :config="configTwo"
            :loading="loading"
            :searchConfig="searchConfigTwo"
            highlight-current-row
            @get-list="getListTwo"
            @clickReset="clickResetTwo">
            <template #blueprint="{ item }">
              <div>
                <img
                  v-if="item.blueprint"
                  style="margin-top: 3px; width: 40px; height: 40px; cursor: pointer; object-fit: contain; vertical-align: middle"
                  v-lazy="item.blueprint"
                  @click="openFile(item.blueprint)" />
              </div>
            </template>
            <template #size="{ item }">
              <span>{{ `${item.length} * ${item.width} * ${item.height}` }}</span>
            </template>
          </byTable>
        </el-tab-pane>
      </el-tabs>
    </el-card>

    <el-dialog title="打印" v-if="openPrint" v-model="openPrint" width="1200px">
      <div v-loading="loadingPrint">
        <div style="height: calc(100vh - 174px); overflow-y: auto; overflow-x: hidden">
          <div class="printBomList" id="printMe">
            <div class="t">生产备料单</div>
            <div class="time" style="text-align: right">
              {{ printTime }}
            </div>
            <table border="1" cellspacing="0" class="table">
              <thead>
                <tr>
                  <td style="width: 40px; text-align: center">编号</td>
                  <td style="width: 120px">图稿类型</td>
                  <td style="width: 100px">规格</td>
                  <td style="width: 120px">颜色</td>
                  <td style="width: 110px">BOM品号</td>
                  <td>图稿名称</td>
                  <td style="width: 120px">SKU品号</td>
                  <td style="width: 80px; text-align: center">数量小计</td>
                  <td style="width: 80px; text-align: center">数量总计</td>
                </tr>
              </thead>
              <tbody v-if="formData && formData.length > 0">
                <tr v-for="(item, index) in formData" :key="index">
                  <td style="text-align: center">{{ item.index }}</td>
                  <td>{{ dictKeyValue(item.machinedPanel, useUserStore().allDict["charge_item"]) }}</td>
                  <td :rowspan="item.specIndex" v-if="item.specStatus">{{ item.spec }}</td>
                  <td :rowspan="item.colourIndex" v-if="item.colourStatus">{{ item.colour }}</td>
                  <td :rowspan="item.bomIndex" v-if="item.bomStatus">{{ item.bomSpecCode }}</td>
                  <td>{{ item.artworkName }}</td>
                  <td>{{ item.skuSpecCode }}</td>
                  <td style="text-align: center">{{ item.quantity }}</td>
                  <td :rowspan="item.bomIndex" v-if="item.bomStatus" style="text-align: center">{{ item.totalQuantity }}</td>
                </tr>
              </tbody>
            </table>
          </div>
        </div>
        <div style="text-align: center; margin: 10px">
          <el-button @click="openPrint = false" size="large">取消</el-button>
          <el-button type="primary" v-print="printObj" size="large">打印</el-button>
        </div>
      </div>
    </el-dialog>

    <el-dialog title="快捷出库" v-if="openQuick" v-model="openQuick" width="90%">
      <Quick :pagination="activeName === 'first' ? sourceList.pagination : sourceListThree.pagination" @clickCancel="clickCancel"></Quick>
    </el-dialog>
  </div>
</template>

<script setup>
import byTable from "/src/components/byTable/index";
import moment from "moment";
import Quick from "/src/views/production/operation/batching/quick";

const { proxy } = getCurrentInstance();
const activeName = ref("first");
const departmentList = ref([]);
const sourceList = ref({
  data: [],
  pagination: {
    bomSpecCode: "",
    bomSpecName: "",
    skuSpecCode: "",
    skuSpecName: "",
    departmentId: "1689164627162529793",
    orderStockType: "0",
  },
});
const sourceListThree = ref({
  data: [],
  pagination: {
    bomSpecCode: "",
    bomSpecName: "",
    skuSpecCode: "",
    skuSpecName: "",
    departmentId: "1689164627162529793",
    orderStockType: "1",
  },
});
const sourceListTwo = ref({
  data: [],
  pagination: {
    total: 0,
    pageNum: 1,
    pageSize: 10,
    type: "1",
    orderCode: "",
    orderWlnCode: "",
    bomSpecCode: "",
    bomSpecName: "",
    skuSpecCode: "",
    skuSpecName: "",
    departmentId: "",
    width: "",
  },
});
const loading = ref(false);
const searchConfig = computed(() => {
  return [
    {
      type: "input",
      prop: "bomSpecCode",
      label: "BOM品号",
    },
    {
      type: "input",
      prop: "bomSpecName",
      label: "BOM品名",
    },
    {
      type: "input",
      prop: "skuSpecCode",
      label: "SKU品号",
    },
    {
      type: "input",
      prop: "skuSpecName",
      label: "SKU品名",
    },
    {
      type: "select",
      prop: "departmentId",
      data: departmentList.value,
      label: "事业部",
      clearable: true,
    },
  ];
});
const searchConfigTwo = computed(() => {
  return [
    {
      type: "input",
      prop: "orderCode",
      label: "订单号",
    },
    {
      type: "input",
      prop: "orderWlnCode",
      label: "E10单号",
    },
    {
      type: "input",
      prop: "bomSpecCode",
      label: "BOM品号",
    },
    {
      type: "input",
      prop: "bomSpecName",
      label: "BOM品名",
    },
    {
      type: "input",
      prop: "skuSpecCode",
      label: "SKU品号",
    },
    {
      type: "input",
      prop: "skuSpecName",
      label: "SKU品名",
    },
    {
      type: "select",
      prop: "departmentId",
      data: departmentList.value,
      label: "事业部",
    },
    {
      type: "input",
      prop: "width",
      label: "幅宽（cm）",
    },
  ];
});
const config = computed(() => {
  return [
    {
      attrs: {
        label: "BOM品号",
        prop: "bomSpecCode",
        width: 200,
      },
    },
    {
      attrs: {
        label: "BOM品名",
        prop: "bomSpecName",
        "min-width": 320,
      },
    },
    {
      attrs: {
        label: "SKU品号",
        prop: "skuSpecCode",
        width: 180,
      },
    },
    {
      attrs: {
        label: "数量",
        prop: "quantity",
        width: 120,
      },
    },
  ];
});
const configTwo = computed(() => {
  return [
    {
      attrs: {
        label: "设计图",
        slot: "blueprint",
        width: 80,
      },
    },
    {
      attrs: {
        label: "事业部",
        prop: "departmentName",
        width: 160,
      },
    },
    {
      attrs: {
        label: "BOM品号",
        prop: "bomSpecCode",
        width: 160,
      },
    },
    {
      attrs: {
        label: "BOM品名",
        prop: "bomSpecName",
        "min-width": 240,
      },
    },
    {
      attrs: {
        label: "SKU品号",
        prop: "skuSpecCode",
        width: 160,
      },
    },
    {
      attrs: {
        label: "SKU品名",
        prop: "skuSpecName",
        "min-width": 200,
      },
    },
    {
      attrs: {
        label: "尺寸（长宽高，cm）",
        slot: "size",
        width: 160,
      },
    },
    {
      attrs: {
        label: "数量",
        prop: "quantity",
        width: 100,
      },
    },
    {
      attrs: {
        label: "投产时间",
        prop: "createTime",
        width: 160,
        align: "center",
      },
    },
    {
      attrs: {
        label: "订单号",
        prop: "orderCode",
        width: 160,
      },
    },
    {
      attrs: {
        label: "E10单号",
        prop: "orderWlnCode",
        width: 160,
      },
    },
  ];
});
const getList = async (req, status) => {
  if (status) {
    sourceList.value.pagination = {
      departmentId: "1689164627162529793",
      orderStockType: "0",
    };
  } else {
    sourceList.value.pagination = { ...sourceList.value.pagination, ...req };
  }
  loading.value = true;
  proxy.post("/stockPreparation/uncompletedList", sourceList.value.pagination).then((res) => {
    sourceList.value.data = res;
    setTimeout(() => {
      loading.value = false;
    }, 200);
  });
};
getList();
const clickReset = () => {
  getList("", true);
};
const getListThree = async (req, status) => {
  if (status) {
    sourceListThree.value.pagination = {
      departmentId: "1689164627162529793",
      orderStockType: "0",
    };
  } else {
    sourceListThree.value.pagination = { ...sourceListThree.value.pagination, ...req };
  }
  loading.value = true;
  proxy.post("/stockPreparation/uncompletedList", sourceListThree.value.pagination).then((res) => {
    sourceListThree.value.data = res;
    setTimeout(() => {
      loading.value = false;
    }, 200);
  });
};
const clickResetThree = () => {
  getListThree("", true);
};
const getListTwo = async (req, status) => {
  if (status) {
    sourceListTwo.value.pagination = {
      pageNum: sourceListTwo.value.pagination.pageNum,
      pageSize: sourceListTwo.value.pagination.pageSize,
      type: "1",
    };
  } else {
    sourceListTwo.value.pagination = { ...sourceListTwo.value.pagination, ...req };
  }
  loading.value = true;
  proxy.post("/stockPreparation/completedPage", sourceListTwo.value.pagination).then((res) => {
    sourceListTwo.value.data = res.rows;
    sourceListTwo.value.pagination.total = res.total;
    setTimeout(() => {
      loading.value = false;
    }, 200);
  });
};
const clickResetTwo = () => {
  getListTwo("", true);
};
const changeActiveName = (val) => {
  if (val === "first") {
    getList();
  } else if (val === "second") {
    getListTwo();
  } else if (val === "third") {
    getListThree();
  } else {
    getList();
  }
};
const numData = reactive({
  data: {
    finishedCount: 0,
    semiFinishedCount: 0,
  },
});
const getNum = () => {
  proxy.post("/stockPreparation/uncompletedListStatistics", { orderStockType: 0 }).then((res) => {
    numData.data = res;
  });
};
const getDemandData = () => {
  proxy.post("/department/page", { pageNum: 1, pageSize: 999 }).then((res) => {
    if (res.rows && res.rows.length > 0) {
      departmentList.value = res.rows.map((item) => {
        return {
          dictKey: item.id,
          dictValue: item.name,
        };
      });
    }
  });
  getNum();
};
getDemandData();
const openFile = (path) => {
  window.open(path);
};
const openPrint = ref(false);
const printTime = ref("");
const formData = ref([]);
const loadingPrint = ref(false);
const outExcel = (orderStockType) => {
  formData.value = [];
  printTime.value = moment().format("yyyy-MM-DD HH:mm:ss");
  loadingPrint.value = true;
  openPrint.value = true;
  let bodyParameter = proxy.deepClone(sourceList.value.pagination);
  if (orderStockType === 1) {
    bodyParameter = proxy.deepClone(sourceListThree.value.pagination);
  }
  proxy.post("/stockPreparation/printUncompletedList", bodyParameter).then((res) => {
    if (res && res.length > 0) {
      for (let i = 0; i < res.length; i++) {
        if (res[i].colourGroupingInfoList && res[i].colourGroupingInfoList.length > 0) {
          res[i].specIndex = 0;
          for (let j = 0; j < res[i].colourGroupingInfoList.length; j++) {
            res[i].colourGroupingInfoList[j].colourIndex = 0;
            let colourGroupingInfoList = res[i].colourGroupingInfoList[j];
            if (colourGroupingInfoList.uncompletedVoList && colourGroupingInfoList.uncompletedVoList.length > 0) {
              for (let x = 0; x < colourGroupingInfoList.uncompletedVoList.length; x++) {
                res[i].colourGroupingInfoList[j].uncompletedVoList[x].bomIndex = 0;
                let uncompletedVoList = colourGroupingInfoList.uncompletedVoList[x];
                if (uncompletedVoList.skuInfoList && uncompletedVoList.skuInfoList.length > 0) {
                  for (let y = 0; y < uncompletedVoList.skuInfoList.length; y++) {
                    res[i].specIndex++;
                    res[i].colourGroupingInfoList[j].colourIndex++;
                    res[i].colourGroupingInfoList[j].uncompletedVoList[x].bomIndex++;
                  }
                }
              }
            }
          }
        }
      }
    }
    let list = [];
    if (res && res.length > 0) {
      let index = 0;
      for (let i = 0; i < res.length; i++) {
        if (res[i].colourGroupingInfoList && res[i].colourGroupingInfoList.length > 0) {
          let specStatus = true;
          for (let j = 0; j < res[i].colourGroupingInfoList.length; j++) {
            let colourStatus = true;
            let colourGroupingInfoList = res[i].colourGroupingInfoList[j];
            if (colourGroupingInfoList.uncompletedVoList && colourGroupingInfoList.uncompletedVoList.length > 0) {
              for (let x = 0; x < colourGroupingInfoList.uncompletedVoList.length; x++) {
                let bomStatus = true;
                let uncompletedVoList = colourGroupingInfoList.uncompletedVoList[x];
                if (uncompletedVoList.skuInfoList && uncompletedVoList.skuInfoList.length > 0) {
                  for (let y = 0; y < uncompletedVoList.skuInfoList.length; y++) {
                    let skuInfoList = uncompletedVoList.skuInfoList[y];
                    index++;
                    list.push({
                      index: index,
                      spec: res[i].spec,
                      colour: colourGroupingInfoList.colour,
                      bomSpecCode: uncompletedVoList.bomSpecCode,
                      artworkName: skuInfoList.artworkName,
                      skuSpecCode: skuInfoList.skuSpecCode,
                      machinedPanel: skuInfoList.machinedPanel,
                      quantity: skuInfoList.quantity,
                      totalQuantity: uncompletedVoList.totalQuantity,
                      specStatus: specStatus,
                      specIndex: res[i].specIndex,
                      colourStatus: colourStatus,
                      colourIndex: res[i].colourGroupingInfoList[j].colourIndex,
                      bomStatus: bomStatus,
                      bomIndex: res[i].colourGroupingInfoList[j].uncompletedVoList[x].bomIndex,
                    });
                    specStatus = false;
                    colourStatus = false;
                    bomStatus = false;
                  }
                }
              }
            }
          }
        }
      }
    }
    formData.value = Object.freeze(list);
    loadingPrint.value = false;
  });
};
const printObj = ref({
  id: "printMe",
  popTitle: "",
  extraCss: "https://cdn.bootcdn.net/ajax/libs/animate.css/4.1.1/animate.compat.css, https://cdn.bootcdn.net/ajax/libs/hover.css/2.3.1/css/hover-min.css",
  extraHead: '<meta http-equiv="Content-Language"content="zh-cn"/>',
});
const openQuick = ref(false);
const clickQuickDelivery = () => {
  openQuick.value = true;
};
const clickCancel = (status) => {
  openQuick.value = false;
  if (status) {
    getList();
    getNum();
  }
};
</script>

<style lang="scss" scoped>
::v-deep(.el-input-number .el-input__inner) {
  text-align: left;
}
.printBomList {
  width: 100%;
  font-size: 12px !important;
  .t {
    text-align: center;
    font-size: 26px;
    font-weight: 700;
    margin: 10px 0px;
  }
  .time {
    margin-bottom: 10px;
  }
  .table {
    width: 100%;
    td {
      padding: 4px;
    }
  }
}
:deep(.el-dialog) {
  margin-top: 10px !important;
  margin-bottom: 10px !important;
}
.table-class {
  width: 100%;
  font-size: 14px;
  border: 1px solid #eeeeee;
}
.table-class th {
  height: 35px;
  line-height: 35px;
  padding: 0 12px;
  text-align: left;
  background-color: #eeeeee !important;
  border-right: 1px solid #eeeeee;
}
.table-class td {
  height: 35px;
  line-height: 35px;
  padding: 8px 12px;
  text-align: left;
  border-bottom: 1px solid #eeeeee;
  border-right: 1px solid #eeeeee;
}
</style>
