<template>
  <div>
    <el-card class="box-card">
      <el-tabs v-model="activeName" type="card" @tab-change="changeActiveName">
        <el-tab-pane label="待备料" name="first">
          <byTable
            :hidePagination="true"
            :source="sourceList.data"
            :pagination="sourceList.pagination"
            :config="config"
            :loading="loading"
            :searchConfig="searchConfig"
            highlight-current-row
            :table-events="{
              select: selectRow,
              'select-all': selectRow,
            }"
            :action-list="[
              {
                text: '完成备料',
                action: () => clickAccomplish(),
              },
            ]"
            @get-list="getList"
            @clickReset="clickReset">
            <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 }">
              <div>{{ item.length }} * {{ item.width }} * {{ item.height }}</div>
            </template>
          </byTable>
        </el-tab-pane>
        <el-tab-pane label="已备料" name="second">
          <byTable
            :source="sourceListTwo.data"
            :pagination="sourceListTwo.pagination"
            :config="configTwo"
            :loading="loading"
            :searchConfig="searchConfig"
            highlight-current-row
            @get-list="getListTwo"
            @clickReset="clickResetTwo">
          </byTable>
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script setup>
import byTable from "@/components/byTable/index";
import { ElMessage } from "element-plus";

const { proxy } = getCurrentInstance();
const activeName = ref("first");
const departmentList = ref([{ dictKey: "0", dictValue: "胜德体育" }]);
const sourceList = ref({
  data: [],
  pagination: {
    orderCode: "",
    orderWlnCode: "",
    bomSpecCode: "",
    bomSpecName: "",
    skuSpecCode: "",
    skuSpecName: "",
    departmentId: "",
    warehouseName: "",
    width: "",
  },
});
const sourceListTwo = ref({
  data: [],
  pagination: {
    total: 0,
    pageNum: 1,
    pageSize: 10,
    type: "1",
    orderCode: "",
    orderWlnCode: "",
    bomSpecCode: "",
    bomSpecName: "",
    skuSpecCode: "",
    skuSpecName: "",
    departmentId: "",
    warehouseName: "",
    width: "",
  },
});
const loading = ref(false);
const searchConfig = computed(() => {
  return [
    {
      type: "input",
      prop: "orderCode",
      label: "订单号",
    },
    {
      type: "input",
      prop: "orderWlnCode",
      label: "万里牛单号",
    },
    {
      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 [
    {
      type: "selection",
      attrs: {
        checkAtt: "isCheck",
      },
    },
    {
      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: "万里牛单号",
        prop: "orderWlnCode",
        width: 160,
      },
    },
  ];
});
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: "万里牛单号",
        prop: "orderWlnCode",
        width: 160,
      },
    },
  ];
});
const getList = async (req) => {
  sourceList.value.pagination = { ...sourceList.value.pagination, ...req };
  loading.value = true;
  proxy.post("/stockPreparation/uncompletedList", sourceList.value.pagination).then((res) => {
    if (res && res.length > 0) {
      sourceList.value.data = res.map((item) => {
        return {
          ...item,
          isCheck: true,
        };
      });
    } else {
      sourceList.value.data = res;
    }
    setTimeout(() => {
      loading.value = false;
    }, 200);
  });
};
getList();
const clickReset = () => {
  getList("");
};
const getListTwo = async (req, status) => {
  if (status) {
    sourceListTwo.value.pagination = {
      pageNum: sourceListTwo.value.pagination.pageNum,
      pageSize: sourceListTwo.value.pagination.pageSize,
    };
  } 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 {
    getListTwo();
  }
};
const getDemandData = () => {
  proxy.post("/department/page", { pageNum: 1, pageSize: 999 }).then((res) => {
    if (res.rows && res.rows.length > 0) {
      departmentList.value = departmentList.value.concat(
        res.rows.map((item) => {
          return {
            dictKey: item.id,
            dictValue: item.name,
          };
        })
      );
    }
  });
};
getDemandData();
const selectData = ref([]);
const selectRow = (data) => {
  selectData.value = data;
};
const openFile = (path) => {
  window.open(path);
};
const clickAccomplish = () => {
  if (selectData.value && selectData.value.length > 0) {
    proxy
      .post(
        "/stockPreparation/submit",
        selectData.value.map((item) => item.orderSkuId)
      )
      .then();
    ElMessage({ message: "提交完成", type: "success" });
    getList();
  } else {
    return ElMessage("请选择操作的数据");
  }
};
</script>

<style lang="scss" scoped>
::v-deep(.el-input-number .el-input__inner) {
  text-align: left;
}
</style>
