<template>
  <div>
    <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: () => clickExcelFile(),
          },
        ]"
        @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="clickCancel"></PrintSKU>
        </el-tab-pane>
        <el-tab-pane label="BOM对账单" name="bom">
          <PrintBOM :rowData="rowData" :activeName="activeName" @clickCancel="clickCancel"></PrintBOM>
        </el-tab-pane>
        <el-tab-pane label="订单对账单" name="order">
          <PrintOrder :rowData="rowData" :activeName="activeName" @clickCancel="clickCancel"></PrintOrder>
        </el-tab-pane>
      </el-tabs>
    </el-dialog>
    
    <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>
  </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";
import { copyText } from "vue3-clipboard";
import { ElMessage } from "element-plus";
import ExcelFile from "/src/views/group/finance/check-bill/ExcelFile.vue";

const { proxy } = getCurrentInstance();
const departmentList = ref([{ dictKey: "0", dictValue: "胜德体育" }]);
const sourceList = ref({
  data: [],
  pagination: {
    total: 0,
    pageNum: 1,
    pageSize: 10,
    type: 1,
    departmentId: "",
    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: "日期",
    },
    {
      type: "select",
      prop: "departmentId",
      data: departmentList.value,
      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: 140,
        align: "center",
        fixed: "right",
      },
      renderHTML(row) {
        return [
          {
            attrs: {
              label: "复制单号",
              type: "primary",
              text: true,
            },
            el: "button",
            click() {
              clickCopyWLNCode(row);
            },
          },
          {
            attrs: {
              label: "打印",
              type: "primary",
              text: true,
            },
            el: "button",
            click() {
              clickPrint(row);
            },
          },
        ];
      },
    },
  ];
});
const getDemandData = () => {
  proxy.post("/department/page", { pageNum: 1, pageSize: 999 }).then((res) => {
    if (res.rows && res.rows.length > 0) {
      departmentList.value = 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 };
  }
  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;
};
const clickCopyWLNCode = (row) => {
  if (row.idGroupConcat) {
    ElMessage("复制数据中,请稍后");
    proxy.post("/statementOfAccount/getOrderWlnCodeStr", row.idGroupConcat.split(",")).then((res) => {
      copyText(res, undefined, (error) => {
        if (error) {
          ElMessage.error(`复制失败: ${error} ！`);
        } else {
          ElMessage.success(`复制成功！`);
        }
      });
    });
  }
};
const openFileList = ref(false);
const clickCancel = (status) => {
  openPrint.value = false;
  if (status) {
    openFileList.value = true;
  }
};
const clickExcelFile = () => {
  openFileList.value = true;
};
</script>

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