<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">
        <template #timePeriod="{ item }">
          <div>
            <el-row>
              <el-col :span="11">{{ item.timePeriod }}</el-col>
              <el-col :span="2" style="text-align: center">-</el-col>
              <el-col :span="11">{{ item.timePeriod }}</el-col>
            </el-row>
          </div>
        </template>
        <template #receiptFileList="{ item }">
          <div>
            <el-button type="primary" @click="clickReceiptFile(item)" v-if="item.receiptFileList && item.receiptFileList.length > 0" text>查看</el-button>
          </div>
        </template>
        <template #proofFileList="{ item }">
          <div>
            <el-button type="primary" @click="clickProofFile(item)" v-if="item.proofFileList && item.proofFileList.length > 0" text>查看</el-button>
          </div>
        </template>
      </byTable>
    </el-card>

    <el-dialog :title="title" v-if="open" v-model="open" width="600">
      <div v-if="fileList && fileList.length > 0">
        <div v-for="(item, index) in fileList" :key="index" style="padding: 8px">
          <a style="color: #409eff; cursor: pointer; word-break: break-all" @click="openFile(item.fileUrl)">{{ item.fileName }}</a>
        </div>
      </div>
      <template #footer>
        <el-button @click="open = false" size="large">关 闭</el-button>
      </template>
    </el-dialog>

    <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/check-bill/printSKU.vue";
import PrintBOM from "/src/views/group/finance/check-bill/printBOM.vue";
import PrintOrder from "/src/views/group/finance/check-bill/printOrder.vue";

const { proxy } = getCurrentInstance();
const sourceList = ref({
  data: [],
  pagination: {
    total: 0,
    pageNum: 1,
    pageSize: 10,
    code: "",
    departmentId: proxy.useUserStore().user.deptId === "100" ? "" : proxy.useUserStore().user.deptId,
    beginTime: "",
    endTime: "",
  },
});
const loading = ref(false);
const searchConfig = computed(() => {
  return [
    {
      type: "input",
      prop: "code",
      label: "对账单号",
    },
    {
      type: "date",
      propList: ["beginTime", "endTime"],
      label: "对账日期",
    },
  ];
});
const config = computed(() => {
  return [
    {
      attrs: {
        label: "对账单号",
        prop: "code",
      },
    },
    {
      attrs: {
        label: "创建时间",
        prop: "createTime",
        width: 160,
      },
    },
    {
      attrs: {
        label: "客户",
        prop: "departmentName",
      },
    },
    {
      attrs: {
        label: "对账金额",
        prop: "amount",
        align: "right",
        width: 180,
      },
    },
    {
      attrs: {
        label: "合同数量",
        prop: "orderNum",
        width: 100,
      },
    },
    {
      attrs: {
        label: "对账时间",
        prop: "timePeriod",
        align: "center",
        width: 180,
      },
    },
    {
      attrs: {
        label: "对账单",
        slot: "receiptFileList",
        align: "center",
        width: 120,
      },
    },
    {
      attrs: {
        label: "转账凭证",
        slot: "proofFileList",
        align: "center",
        width: 120,
      },
    },
    {
      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 };
  }
  loading.value = true;
  proxy.post("/statementOfAccount/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 open = ref(false);
const title = ref("");
const fileList = ref([]);
const clickReceiptFile = (row) => {
  title.value = "对账单";
  fileList.value = proxy.deepClone(row.receiptFileList);
  open.value = true;
};
const clickProofFile = (row) => {
  title.value = "转账凭证";
  fileList.value = proxy.deepClone(row.proofFileList);
  open.value = true;
};
const openFile = (path) => {
  window.open(path);
};
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>
