<template>
  <div class="detail-query-container">
    <div class="query-page-shell">
      <div class="shell-sidebar">
        <div class="shell-panel-title">筛选条件</div>
        <div class="shell-panel-body">筛选区域待接入</div>
      </div>
      <div class="shell-main">
        <div class="shell-header">
          <span class="shell-title">流水明细查询</span>
          <el-button size="small" type="primary" plain @click="handleExport">
            导出流水
          </el-button>
        </div>
        <div class="shell-panel-body">结果区域待接入</div>
      </div>
    </div>
  </div>
</template>

<script>
import {
  listBankStatement,
  getBankStatementOptions,
  getBankStatementDetail
} from "@/api/ccdiProjectBankStatement";

const createDefaultQueryParams = (projectId) => ({
  projectId,
  pageNum: 1,
  pageSize: 10,
  tabType: "all",
  transactionStartTime: "",
  transactionEndTime: "",
  counterpartyName: "",
  counterpartyNameEmpty: false,
  userMemo: "",
  userMemoEmpty: false,
  ourSubjects: [],
  ourBanks: [],
  ourAccounts: [],
  amountMin: "",
  amountMax: "",
  counterpartyAccount: "",
  counterpartyAccountEmpty: false,
  transactionType: "",
  transactionTypeEmpty: false,
  orderBy: "trxDate",
  orderDirection: "desc",
});

const createEmptyOptionData = () => ({
  ourSubjectOptions: [],
  ourBankOptions: [],
  ourAccountOptions: [],
});

export default {
  name: "DetailQuery",
  props: {
    projectId: {
      type: [String, Number],
      default: null,
    },
    projectInfo: {
      type: Object,
      default: () => ({
        projectName: "",
        updateTime: "",
        projectStatus: "0",
      }),
    },
  },
  data() {
    return {
      loading: false,
      optionsLoading: false,
      activeTab: "all",
      dateRange: [],
      list: [],
      total: 0,
      queryParams: createDefaultQueryParams(this.projectId),
      optionData: createEmptyOptionData(),
    };
  },
  created() {
    this.getList();
    this.getOptions();
  },
  watch: {
    projectId() {
      this.syncProjectId();
      this.getOptions();
      this.getList();
    },
  },
  methods: {
    async getList() {
      this.syncProjectId();
      if (!this.queryParams.projectId) {
        this.list = [];
        this.total = 0;
        return;
      }

      this.loading = true;
      try {
        const res = await listBankStatement(this.queryParams);
        this.list = res.rows || [];
        this.total = res.total || 0;
      } catch (error) {
        this.list = [];
        this.total = 0;
        console.error("加载流水明细失败", error);
      } finally {
        this.loading = false;
      }
    },
    async getOptions() {
      this.syncProjectId();
      if (!this.queryParams.projectId) {
        this.optionData = createEmptyOptionData();
        return;
      }

      this.optionsLoading = true;
      try {
        const res = await getBankStatementOptions(this.queryParams.projectId);
        const data = res.data || {};
        this.optionData = {
          ourSubjectOptions: data.ourSubjectOptions || [],
          ourBankOptions: data.ourBankOptions || [],
          ourAccountOptions: data.ourAccountOptions || [],
        };
      } catch (error) {
        this.optionData = createEmptyOptionData();
        console.error("加载流水筛选项失败", error);
      } finally {
        this.optionsLoading = false;
      }
    },
    syncProjectId() {
      this.queryParams.projectId = this.projectId;
      this.queryParams.tabType = this.activeTab;
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.queryParams.tabType = this.activeTab;
      this.getList();
    },
    resetQuery() {
      this.activeTab = "all";
      this.dateRange = [];
      this.queryParams = createDefaultQueryParams(this.projectId);
      this.syncProjectId();
      this.getOptions();
      this.getList();
    },
    handleExport() {
      void listBankStatement;
      void getBankStatementOptions;
      void getBankStatementDetail;
    },
  },
};
</script>

<style lang="scss" scoped>
.detail-query-container {
  padding: 16px;
  background: #fff;
  min-height: 480px;
}

.query-page-shell {
  display: grid;
  grid-template-columns: 320px minmax(0, 1fr);
  gap: 16px;
}

.shell-sidebar,
.shell-main {
  min-height: 448px;
  border: 1px solid #ebeef5;
  border-radius: 4px;
  background: #fff;
}

.shell-sidebar {
  padding: 20px 16px;
}

.shell-main {
  padding: 20px;
}

.shell-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
}

.shell-title,
.shell-panel-title {
  font-size: 16px;
  font-weight: 600;
  color: #303133;
}

.shell-panel-body {
  color: #909399;
  font-size: 14px;
  line-height: 22px;
}

@media (max-width: 992px) {
  .query-page-shell {
    grid-template-columns: 1fr;
  }
}
</style>
