<template>
  <div class="tenant">
    <byTable
      :source="sourceList.data"
      :pagination="sourceList.pagination"
      :config="config"
      :loading="loading"
      :selectConfig="selectConfig"
      highlight-current-row
      @get-list="getList">
      <template #amount="{ item }">
        <div>
          <span style="padding-right: 4px">{{ item.currency }}</span>
          <span>{{ moneyFormat(item.amount, 2) }}</span>
        </div>
      </template>
      <template #contractCodes="{ item }">
        <div>
          <a style="color: #409eff; cursor: pointer; word-break: break-all" @click="clickPrint(item)">{{ item.contractCodes }}</a>
        </div>
      </template>
    </byTable>

    <el-dialog title="打印" v-if="openPrint" v-model="openPrint" width="840px">
      <FundsPDF v-if="rowData.type != '20'" :rowData="rowData"></FundsPDF>
      <PaymentPDF v-else :rowData="rowData"></PaymentPDF>
      <template #footer>
        <el-button @click="openPrint = false" size="large">取消</el-button>
        <el-button type="primary" @click="clickDownload()" size="large">下载PDF</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import byTable from "@/components/byTable/index";
import FundsPDF from "@/components/PDF/fundsPDF.vue";
import PaymentPDF from "@/components/PDF/paymentPDF.vue";

const { proxy } = getCurrentInstance();
const corporationList = ref([]);
const fundsType = ref([]);
const sourceList = ref({
  data: [],
  pagination: {
    total: 0,
    pageNum: 1,
    pageSize: 10,
    keyword: "",
    corporationId: "",
    type: "",
  },
});
const loading = ref(false);
const selectConfig = computed(() => {
  return [
    {
      label: "归属公司",
      prop: "corporationId",
      data: corporationList.value,
    },
    {
      label: "数据来源",
      prop: "type",
      data: fundsType.value,
    },
  ];
});
const config = computed(() => {
  return [
    {
      attrs: {
        label: "归属公司",
        prop: "corporationName",
        "min-width": 220,
      },
    },
    {
      attrs: {
        label: "数据来源",
        prop: "type",
        "min-width": 130,
      },
      render(type) {
        if (type != 20) {
          return "请款: " + proxy.dictValueLabel(type, fundsType.value);
        } else {
          return "采购付款";
        }
      },
    },
    {
      attrs: {
        label: "关联单号",
        slot: "contractCodes",
        "min-width": 160,
      },
    },
    {
      attrs: {
        label: "申请时间",
        prop: "applicationTime",
        "min-width": 160,
      },
    },
    {
      attrs: {
        label: "申请金额",
        slot: "amount",
        align: "right",
        "min-width": 120,
      },
    },
    {
      attrs: {
        label: "打款时间",
        prop: "accountPaymentDate",
        "min-width": 160,
      },
    },
    {
      attrs: {
        label: "冲销人",
        prop: "writeOffUserName",
        "min-width": 160,
      },
    },
    {
      attrs: {
        label: "冲销时间",
        prop: "createTime",
        "min-width": 160,
      },
    },
  ];
});
const getDict = () => {
  proxy.getDictOne(["founds_type"]).then((res) => {
    fundsType.value = res["founds_type"]
      .map((x) => ({
        label: x.dictValue,
        value: x.dictKey,
      }))
      .concat({
        label: "采购付款",
        value: "20",
      });
  });
  proxy.post("/corporation/page", { pageNum: 1, pageSize: 999 }).then((res) => {
    if (res.rows && res.rows.length > 0) {
      corporationList.value = res.rows.map((item) => {
        return {
          label: item.name,
          value: item.id,
        };
      });
    }
  });
};
const getList = async (req) => {
  sourceList.value.pagination = { ...sourceList.value.pagination, ...req };
  loading.value = true;
  proxy.post("/writeOffRecords/page", sourceList.value.pagination).then((res) => {
    sourceList.value.data = res.rows;
    sourceList.value.pagination.total = res.total;
    setTimeout(() => {
      loading.value = false;
    }, 200);
  });
};
getDict();
getList();
const openPrint = ref(false);
const rowData = ref({});
const clickPrint = (row) => {
  rowData.value = {
    id: row.businessId,
    type: row.type,
  };
  openPrint.value = true;
};
const clickDownload = () => {
  if (rowData.value.type != "20") {
    proxy.getPdf("请款PDF文件");
  } else {
    proxy.getPdf("采购付款PDF文件");
  }
};
</script>

<style lang="scss" scoped>
.tenant {
  padding: 20px;
}
</style>
