<template>
  <div>
    <el-card class="box-card">
      <el-tabs v-model="activeName" type="card" @tab-change="changeActiveName">
        <el-tab-pane label="出库单" name="first">
          <byTable
            :source="sourceList.data"
            :pagination="sourceList.pagination"
            :config="config"
            :loading="loading"
            :searchConfig="searchConfig"
            highlight-current-row
            :action-list="[
              status
                ? {
                    text: '出库登记',
                    action: () => clickModal(),
                  }
                : {},
              {
                text: '包材出库',
                action: () => clickPackingMaterials(),
              },
            ]"
            @get-list="getList"
            @clickReset="clickReset">
            <template #code="{ item }">
              <div>
                <a style="color: #409eff; cursor: pointer; word-break: break-all" @click="clickCode(item)">{{ item.code }}</a>
              </div>
            </template>
          </byTable>
        </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
            :action-list="[
              status
                ? {
                    text: '出库登记',
                    action: () => clickModal(),
                  }
                : {},
              {
                text: '导出Excel',
                action: () => deriveExcel(),
              },
            ]"
            @get-list="getListTwo"
            @clickReset="clickResetTwo">
          </byTable>
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

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

const { proxy } = getCurrentInstance();
const activeName = ref("first");
const departmentList = ref([{ dictKey: "0", dictValue: "胜德体育" }]);
const warehouseList = ref([]);
const sourceList = ref({
  data: [],
  pagination: {
    total: 0,
    pageNum: 1,
    pageSize: 10,
    type: "0",
    code: "",
    departmentId: "",
    warehouseName: "",
    detailType: "",
    beginTime: "",
    endTime: "",
  },
});
const status = ref(proxy.useUserStore().user.userId === "1");
const sourceListTwo = ref({
  data: [],
  pagination: {
    total: 0,
    pageNum: 1,
    pageSize: 10,
    type: "0",
    code: "",
    purchaseCode: "",
    bomSpecCode: "",
    bomSpecName: "",
    departmentId: "",
    warehouseName: "",
    detailType: "",
    beginTime: "",
    endTime: "",
  },
});
const loading = ref(false);
const searchConfig = computed(() => {
  return [
    {
      type: "input",
      prop: "code",
      label: "出库单号",
    },
    {
      type: "select",
      prop: "departmentId",
      data: departmentList.value,
      label: "事业部",
    },
    {
      type: "select",
      prop: "warehouseId",
      data: warehouseList.value,
      label: "仓库",
    },
    {
      type: "select",
      prop: "detailType",
      dictKey: "come_stock_type",
      label: "出库类型",
    },
    {
      type: "date",
      propList: ["beginTime", "endTime"],
      label: "日期",
    },
  ];
});
const searchConfigTwo = computed(() => {
  return [
    {
      type: "input",
      prop: "purchaseCode",
      label: "采购单号",
    },
    {
      type: "input",
      prop: "code",
      label: "出库单号",
    },
    {
      type: "input",
      prop: "bomSpecCode",
      label: "品号",
    },
    {
      type: "input",
      prop: "bomSpecName",
      label: "品名",
    },
    {
      type: "select",
      prop: "departmentId",
      data: departmentList.value,
      label: "事业部",
    },
    {
      type: "select",
      prop: "warehouseId",
      data: warehouseList.value,
      label: "仓库",
    },
    {
      type: "select",
      prop: "detailType",
      dictKey: "come_stock_type",
      label: "出库类型",
    },
    {
      type: "date",
      propList: ["beginTime", "endTime"],
      label: "日期",
    },
  ];
});
const config = computed(() => {
  return [
    {
      attrs: {
        label: "出库单号",
        slot: "code",
        "min-width": 160,
      },
    },
    {
      attrs: {
        label: "采购单号",
        prop: "purchaseCode",
        "min-width": 160,
      },
    },
    {
      attrs: {
        label: "归属事业部",
        prop: "departmentName",
        "min-width": 160,
      },
    },
    {
      attrs: {
        label: "仓库类型",
        prop: "warehouseType",
        width: 140,
      },
      render(val) {
        return proxy.dictKeyValue(val, proxy.useUserStore().allDict["warehouse_type"]);
      },
    },
    {
      attrs: {
        label: "仓库名称",
        prop: "warehouseName",
        "min-width": 160,
      },
    },
    {
      attrs: {
        label: "出库类型",
        prop: "detailType",
        width: 140,
      },
      render(val) {
        return proxy.dictKeyValue(val, proxy.useUserStore().allDict["come_stock_type"]);
      },
    },
    {
      attrs: {
        label: "出库时间",
        prop: "inOutStorageTime",
        width: 160,
        align: "center",
      },
    },
    {
      attrs: {
        label: "出库申请人",
        prop: "applicant",
        width: 160,
        align: "center",
      },
    },
  ];
});
const configTwo = computed(() => {
  return [
    {
      attrs: {
        label: "出库单号",
        prop: "code",
        width: 160,
      },
    },
    {
      attrs: {
        label: "采购单号",
        prop: "purchaseCode",
        width: 160,
      },
    },
    {
      attrs: {
        label: "归属事业部",
        prop: "departmentName",
        width: 120,
      },
    },
    {
      attrs: {
        label: "仓库类型",
        prop: "warehouseType",
        width: 100,
      },
      render(val) {
        return proxy.dictKeyValue(val, proxy.useUserStore().allDict["warehouse_type"]);
      },
    },
    {
      attrs: {
        label: "仓库名称",
        prop: "warehouseName",
        width: 120,
      },
    },
    {
      attrs: {
        label: "出库类型",
        prop: "detailType",
        width: 100,
      },
      render(val) {
        return proxy.dictKeyValue(val, proxy.useUserStore().allDict["come_stock_type"]);
      },
    },
    {
      attrs: {
        label: "品号",
        prop: "bomSpecCode",
        width: 150,
      },
    },
    {
      attrs: {
        label: "品名",
        prop: "bomSpecName",
        "min-width": 320,
      },
    },
    {
      attrs: {
        label: "数量",
        prop: "quantity",
        width: 80,
      },
    },
    {
      attrs: {
        label: "备注",
        prop: "remark",
        width: 180,
      },
    },
    {
      attrs: {
        label: "出库时间",
        prop: "inOutStorageTime",
        width: 160,
        align: "center",
      },
    },
    {
      attrs: {
        label: "出库申请人",
        prop: "applicant",
        width: 150,
        align: "center",
      },
    },
  ];
});
const getList = async (req, status) => {
  if (status) {
    sourceList.value.pagination = {
      pageNum: sourceList.value.pagination.pageNum,
      pageSize: sourceList.value.pagination.pageSize,
    };
  } else {
    sourceList.value.pagination = { ...sourceList.value.pagination, ...req };
  }
  loading.value = true;
  proxy.post("/inOutStorage/page", sourceList.value.pagination).then((res) => {
    sourceList.value.data = res.rows;
    sourceList.value.pagination.total = res.total;
    setTimeout(() => {
      loading.value = false;
    }, 200);
  });
};
getList();
const clickReset = () => {
  getList("", true);
};
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("/inOutStorageBom/page", 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,
          };
        })
      );
    }
  });
  proxy.post("/warehouse/page", { pageNum: 1, pageSize: 999 }).then((res) => {
    if (res.rows && res.rows.length > 0) {
      warehouseList.value = res.rows.map((item) => {
        return {
          dictKey: item.id,
          dictValue: item.name,
        };
      });
    }
  });
};
getDemandData();
const clickCode = (row) => {
  sourceListTwo.value.pagination.code = row.code;
  activeName.value = "second";
};
const clickModal = () => {
  proxy.$router.replace({
    path: "/outbound/add",
    query: {
      random: proxy.random(),
    },
  });
};
const clickPackingMaterials = () => {
  proxy.$router.replace({
    path: "/outbound/packing-materials/add",
    query: {
      random: proxy.random(),
    },
  });
};
const deriveExcel = () => {
  ElMessageBox.confirm("你是否确认此操作", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      proxy.postFile("/inOutStorageBom/exportExcel", sourceListTwo.value.pagination).then((res) => {
        proxy.downloadFile(res, "出库明细.xlsx");
      });
    })
    .catch(() => {});
};
</script>

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