<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%" class="print">
      <el-tabs v-model="tabsCard" type="card">
        <el-tab-pane v-for="(item, index) in cardList" :key="index" :label="item.dictValue" :name="item.dictKey"> </el-tab-pane>
      </el-tabs>
      <div style="padding: 0 10px">
        <el-tabs v-model="activeName">
          <el-tab-pane label="SKU对账单" name="sku">
            <PrintSKU :rowData="rowData" :activeName="activeName" :tabsCard="tabsCard" @clickCancel="openPrint = false"></PrintSKU>
          </el-tab-pane>
          <el-tab-pane label="BOM对账单" name="bom">
            <PrintBOM :rowData="rowData" :activeName="activeName" :tabsCard="tabsCard" @clickCancel="openPrint = false"></PrintBOM>
          </el-tab-pane>
          <el-tab-pane label="订单对账单" name="order">
            <PrintOrder :rowData="rowData" :activeName="activeName" :tabsCard="tabsCard" @clickCancel="openPrint = false"></PrintOrder>
          </el-tab-pane>
        </el-tabs>
      </div>
    </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";

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 cardList = ref([
  {
    dictKey: 1,
    dictValue: "万里牛订单",
  },
  {
    dictKey: 2,
    dictValue: "采购订单",
  },
  {
    dictKey: 3,
    dictValue: "委外订单",
  },
  {
    dictKey: 4,
    dictValue: "售后订单",
  },
  {
    dictKey: 5,
    dictValue: "无理由订单",
  },
]);
const tabsCard = ref(1);
const activeName = ref("sku");
const clickPrint = (row) => {
  tabsCard.value = 1;
  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(`复制成功！`);
        }
      });
    });
  }
};
</script>

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