<template>
  <div>
    <el-card class="box-card">
      <byTable
        :source="sourceList.data"
        :pagination="sourceList.pagination"
        :config="config"
        :loading="loading"
        :searchConfig="searchConfig"
        highlight-current-row
        @get-list="getList"
        @clickReset="clickReset"
        @changeRadioGroup="changeRadioGroup">
      </byTable>
    </el-card>

    <el-dialog title="打印" v-if="openPrint" v-model="openPrint" width="94%">
      <el-tabs v-model="activeName" class="demo-tabs">
        <el-tab-pane label="SKU对账单" name="sku">
          <PrintSKU :rowData="rowData" :activeName="activeName" @clickCancel="openPrint = false"></PrintSKU>
        </el-tab-pane>
        <el-tab-pane label="BOM对账单" name="bom">
          <PrintBOM :rowData="rowData" :activeName="activeName" @clickCancel="openPrint = false"></PrintBOM>
        </el-tab-pane>
        <el-tab-pane label="订单对账单" name="order">
          <PrintOrder :rowData="rowData" :activeName="activeName" @clickCancel="openPrint = false"></PrintOrder>
        </el-tab-pane>
      </el-tabs>
    </el-dialog>
  </div>
</template>

<script setup>
import byTable from "/src/components/byTable/index";
import PrintSKU from "/src/views/group/finance/summary/printSKU.vue";
import PrintBOM from "/src/views/group/finance/summary/printBOM.vue";
import PrintOrder from "/src/views/group/finance/summary/printOrder.vue";

const { proxy } = getCurrentInstance();
const sourceList = ref({
  data: [],
  pagination: {
    total: 0,
    pageNum: 1,
    pageSize: 10,
    type: 1,
    departmentId: proxy.useUserStore().user.deptId === "100" ? "" : proxy.useUserStore().user.deptId,
    beginTime: "",
    endTime: "",
  },
});
const loading = ref(false);
const searchConfig = computed(() => {
  return [
    {
      type: "radio-group",
      prop: "type",
      label: "维度",
      data: [
        {
          dictKey: 1,
          dictValue: "月度",
        },
        {
          dictKey: 2,
          dictValue: "季度",
        },
        {
          dictKey: 3,
          dictValue: "年度",
        },
      ],
    },
    {
      type: "date",
      propList: ["beginTime", "endTime"],
      label: "日期",
    },
  ];
});
const config = computed(() => {
  return [
    {
      attrs: {
        label: "维度",
        prop: "dimensionality",
      },
    },
    {
      attrs: {
        label: "客户",
        prop: "departmentName",
      },
    },
    {
      attrs: {
        label: "对账金额",
        prop: "reconcilingAmount",
        align: "right",
      },
      render(val) {
        return proxy.moneyFormat(val);
      },
    },
    {
      attrs: {
        label: "对账单数量",
        prop: "quantityOfStatement",
      },
    },
    {
      attrs: {
        label: "合同数量",
        prop: "orderQuantity",
      },
    },
    {
      attrs: {
        label: "操作",
        width: 80,
        align: "center",
        fixed: "right",
      },
      renderHTML(row) {
        return [
          {
            attrs: {
              label: "打印",
              type: "primary",
              text: true,
            },
            el: "button",
            click() {
              clickPrint(row);
            },
          },
        ];
      },
    },
  ];
});
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 };
  }
  if (sourceList.value.pagination.beginTime || sourceList.value.pagination.endTime) {
    sourceList.value.pagination.type = 4;
  }
  loading.value = true;
  proxy.post("/statementOfAccountMerge/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);
  });
};
getList();
const clickReset = () => {
  getList("", true);
};
const changeRadioGroup = () => {
  getList({ beginTime: "", endTime: "" });
};
const openPrint = ref(false);
const rowData = ref({});
const activeName = ref("sku");
const clickPrint = (row) => {
  activeName.value = "sku";
  rowData.value = row;
  openPrint.value = true;
};
</script>

<style lang="scss" scoped>
:deep(.el-dialog) {
  margin-top: 10px !important;
  margin-bottom: 10px !important;
}
</style>
