<template>
  <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
          @get-list="getList"
          @clickReset="clickReset">
        </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="[
            {
              text: '导出Excel',
              action: () => clickGiveaway(),
            },
          ]"
          @get-list="getListTwo"
          @clickReset="clickResetTwo">
        </byTable>
      </el-tab-pane>
    </el-tabs>

    <el-dialog title="上传" v-if="openGiveaway" v-model="openGiveaway" width="600">
      <div v-loading="loadingGiveaway">
        <el-upload :show-file-list="false" action="##" :http-request="giveawayServerLog" :before-upload="handleBeforeGiveaway">
          <el-button style="background: #20b2aa; color: #fff; border: 1px solid #20b2aa">上传</el-button>
        </el-upload>
        <div style="text-align: center; margin: 10px">
          <el-button @click="openGiveaway = false" size="large">关 闭</el-button>
        </div>
      </div>
    </el-dialog>
  </el-card>
</template>

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

const { proxy } = getCurrentInstance();
const activeName = ref("first");
const departmentList = ref([{ dictKey: "0", dictValue: "胜德体育" }]);
const sourceList = ref({
  data: [],
  pagination: {
    total: 0,
    pageNum: 1,
    pageSize: 10,
    skuSpecCode: "",
    skuSpecName: "",
  },
});
const sourceListTwo = ref({
  data: [],
  pagination: {
    total: 0,
    pageNum: 1,
    pageSize: 10,
    skuSpecCode: "",
    skuSpecName: "",
    orderCode: "",
    orderWlnCode: "",
    departmentId: "",
  },
});
const loading = ref(false);
const searchConfig = computed(() => {
  return [
    {
      type: "input",
      prop: "skuSpecCode",
      label: "SKU品号",
    },
    {
      type: "input",
      prop: "skuSpecName",
      label: "SKU品名",
    },
  ];
});
const searchConfigTwo = computed(() => {
  return [
    {
      type: "input",
      prop: "orderCode",
      label: "订单号",
    },
    {
      type: "input",
      prop: "orderWlnCode",
      label: "E10单号",
    },
    {
      type: "input",
      prop: "skuSpecCode",
      label: "SKU品号",
    },
    {
      type: "input",
      prop: "skuSpecName",
      label: "SKU品名",
    },
    {
      type: "select",
      prop: "departmentId",
      data: departmentList.value,
      label: "事业部",
    },
  ];
});
const config = computed(() => {
  return [
    {
      attrs: {
        label: "SKU品号",
        prop: "skuSpecCode",
        "min-width": 140,
      },
    },
    {
      attrs: {
        label: "SKU品名",
        prop: "skuSpecName",
        "min-width": 340,
      },
    },
    {
      attrs: {
        label: "数量",
        prop: "quantity",
        "min-width": 120,
      },
    },
  ];
});
const configTwo = computed(() => {
  return [
    {
      attrs: {
        label: "订单号",
        prop: "orderCode",
        width: 200,
      },
    },
    {
      attrs: {
        label: "E10单号",
        prop: "orderWlnCode",
        width: 140,
      },
    },
    {
      attrs: {
        label: "事业部",
        prop: "departmentName",
        width: 120,
      },
    },
    {
      attrs: {
        label: "SKU品号",
        prop: "skuSpecCode",
        width: 160,
      },
    },
    {
      attrs: {
        label: "SKU品名",
        prop: "skuSpecName",
      },
    },
    {
      attrs: {
        label: "数量",
        prop: "quantity",
        width: 100,
      },
    },
    {
      attrs: {
        label: "操作类型",
        prop: "operationType",
        width: 100,
      },
      render(val) {
        if (val == "1") {
          return "入库";
        } else if (val == "2") {
          return "出库";
        }
      },
    },
    {
      attrs: {
        label: "操作时间",
        prop: "createTime",
        width: 160,
        align: "center",
      },
    },
  ];
});
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 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("/inventoryFinished/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("/inventoryFinishedOrder/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 openGiveaway = ref(false);
const loadingGiveaway = ref(false);
const clickGiveaway = () => {
  loadingGiveaway.value = false;
  openGiveaway.value = true;
};
const handleBeforeGiveaway = () => {
  loadingGiveaway.value = true;
};
const giveawayServerLog = (params) => {
  let file = params.file;
  let formFile = new FormData();
  formFile.append("file", file);
  proxy.postUploadAndDownloadFile("/inventoryFinishedOrder/erpExcelExport", formFile).then(
    (res) => {
      if (res.type === "application/json") {
        const fileReader = new FileReader();
        fileReader.onloadend = () => {
          const jsonData = JSON.parse(fileReader.result);
          ElMessage({ message: jsonData.msg, type: "error" });
          loadingGiveaway.value = false;
        };
        fileReader.readAsText(res);
      } else {
        ElMessage({ message: "操作成功", type: "success" });
        proxy.downloadFile(res, "成品库明细-" + moment().format("yyyy-MM-DD") + ".xlsx");
        loadingGiveaway.value = false;
        openGiveaway.value = false;
      }
    },
    (err) => {
      console.log(err);
      loadingGiveaway.value = false;
    }
  );
};
</script>

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