<template>
  <el-card class="box-card">
    <byTable
      :source="sourceList.data"
      :pagination="sourceList.pagination"
      :config="config"
      :loading="loading"
      :searchConfig="searchConfig"
      highlight-current-row
      :action-list="[
        {
          text: '导出Excel',
          action: () => clickExcel(),
        },
        {
          text: 'Excel文件',
          action: () => clickExcelFile(),
        },
      ]"
      @get-list="getList"
      @clickReset="clickReset"
      @changeRadioGroup="changeRadioGroup">
    </byTable>

    <el-dialog title="Excel文件" v-if="openFileList" v-model="openFileList" width="60%">
      <ExcelFile></ExcelFile>
      <template #footer>
        <el-button @click="openFileList = false" size="large">关 闭</el-button>
      </template>
    </el-dialog>
  </el-card>
</template>

<script setup>
import byTable from "/src/components/byTable/index";
import * as date from "/src/utils/date";
import { ElMessage, ElMessageBox } from "element-plus";
import ExcelFile from "/src/views/group/finance/check-bill/ExcelFile.vue";

const { proxy } = getCurrentInstance();
const sourceList = ref({
  data: [],
  pagination: {
    total: 0,
    pageNum: 1,
    pageSize: 10,
    purchaseCode: "",
    supplierName: "",
    bomSpecCode: "",
    bomSpecName: "",
    beginTime: "",
    endTime: "",
    type: 1,
  },
});
const loading = ref(false);
const searchConfig = computed(() => {
  return [
    {
      type: "input",
      prop: "purchaseCode",
      label: "采购单号",
    },
    {
      type: "input",
      prop: "supplierName",
      label: "供应商",
    },
    {
      type: "input",
      prop: "bomSpecCode",
      label: "BOM品号",
    },
    {
      type: "input",
      prop: "bomSpecName",
      label: "BOM品名",
    },
    {
      type: "radio-group",
      prop: "type",
      label: "维度",
      data: [
        {
          dictKey: 1,
          dictValue: "本年度",
        },
        {
          dictKey: 2,
          dictValue: "近365天",
        },
        {
          dictKey: 3,
          dictValue: "近180天",
        },
        {
          dictKey: 4,
          dictValue: "近90天",
        },
      ],
    },
    {
      type: "date",
      propList: ["beginTime", "endTime"],
      label: "日期",
    },
  ];
});
const config = computed(() => {
  return [
    {
      attrs: {
        label: "入库日期",
        prop: "outboundTime",
        width: 160,
      },
    },
    {
      attrs: {
        label: "采购订单号",
        prop: "purchaseCode",
        width: 160,
      },
    },
    {
      attrs: {
        label: "入库单号",
        prop: "outboundCode",
        width: 160,
      },
    },
    {
      attrs: {
        label: "仓库名称",
        prop: "warehouseName",
        width: 160,
      },
    },
    {
      attrs: {
        label: "供应商名称",
        prop: "supplierName",
        width: 160,
      },
    },
    {
      attrs: {
        label: "品号",
        prop: "bomSpecCode",
        width: 140,
      },
    },
    {
      attrs: {
        label: "品名",
        prop: "bomSpecName",
        "min-width": 240,
      },
    },
    {
      attrs: {
        label: "入库数量",
        prop: "outboundQuantity",
        width: 120,
      },
    },
    {
      attrs: {
        label: "单位",
        prop: "bomSpecUnit",
        width: 120,
      },
    },
    {
      attrs: {
        label: "含税单价",
        prop: "outboundUnitPrice",
        width: 120,
      },
    },
  ];
});
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("/purchaseWarehousing/page", sourceList.value.pagination).then((res) => {
    if (res.rows && res.rows.length > 0) {
      sourceList.value.data = res.rows.map((item) => {
        return {
          ...item,
          isCheck: true,
        };
      });
    } else {
      sourceList.value.data = [];
    }
    sourceList.value.pagination.total = res.total;
    setTimeout(() => {
      loading.value = false;
    }, 200);
  });
};
const clickReset = () => {
  sourceList.value.pagination.type = 1;
  changeRadioGroup();
};
const changeRadioGroup = () => {
  let beginTime = "";
  let endTime = "";
  if (sourceList.value.pagination.type == 1) {
    beginTime = date.getYearFirstDay();
    endTime = date.getYearLastDay();
  } else if (sourceList.value.pagination.type == 2) {
    let days = date.getDaysNoTime(365);
    beginTime = days.startTime;
    endTime = days.endTime;
  } else if (sourceList.value.pagination.type == 3) {
    let days = date.getDaysNoTime(180);
    beginTime = days.startTime;
    endTime = days.endTime;
  } else if (sourceList.value.pagination.type == 4) {
    let days = date.getDaysNoTime(90);
    beginTime = days.startTime;
    endTime = days.endTime;
  }
  getList({ beginTime: beginTime, endTime: endTime });
};
changeRadioGroup();
const openFileList = ref(false);
const clickExcel = () => {
  ElMessageBox.confirm("你是否确认此操作", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      proxy.postFile("/purchaseWarehousing/exportExcel", sourceList.value.pagination).then(() => {
        ElMessage({ message: "导出成功", type: "success" });
        openFileList.value = true;
      });
    })
    .catch(() => {});
};
const clickExcelFile = () => {
  openFileList.value = true;
};
</script>

<style lang="scss" scoped></style>
