<template>
  <div class="detail-query-container">
    <div class="query-page-shell">
      <div class="shell-sidebar">
        <div class="shell-panel-title">筛选条件</div>
        <el-form label-position="top" class="filter-form">
          <el-form-item label="交易时间">
            <el-date-picker
              v-model="dateRange"
              class="filter-control"
              type="datetimerange"
              value-format="yyyy-MM-dd HH:mm:ss"
              range-separator="至"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              unlink-panels
            />
          </el-form-item>

          <el-form-item label="对方名称">
            <el-input
              v-model="queryParams.counterpartyName"
              placeholder="请输入对方名称"
              clearable
            />
            <el-checkbox
              v-model="queryParams.counterpartyNameEmpty"
              class="empty-checkbox"
            >
              匹配空值
            </el-checkbox>
          </el-form-item>

          <el-form-item label="摘要">
            <el-input
              v-model="queryParams.userMemo"
              placeholder="请输入摘要关键字"
              clearable
            />
            <el-checkbox v-model="queryParams.userMemoEmpty" class="empty-checkbox">
              匹配空值
            </el-checkbox>
          </el-form-item>

          <el-form-item label="本方主体">
            <el-select
              v-model="queryParams.ourSubjects"
              class="filter-control"
              multiple
              filterable
              collapse-tags
              clearable
              :loading="optionsLoading"
              placeholder="请选择本方主体"
            >
              <el-option
                v-for="item in optionData.ourSubjectOptions"
                :key="`subject-${item.value}`"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="本方银行">
            <el-select
              v-model="queryParams.ourBanks"
              class="filter-control"
              multiple
              filterable
              collapse-tags
              clearable
              :loading="optionsLoading"
              placeholder="请选择本方银行"
            >
              <el-option
                v-for="item in optionData.ourBankOptions"
                :key="`bank-${item.value}`"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="本方账号">
            <el-select
              v-model="queryParams.ourAccounts"
              class="filter-control"
              multiple
              filterable
              collapse-tags
              clearable
              :loading="optionsLoading"
              placeholder="请选择本方账号"
            >
              <el-option
                v-for="item in optionData.ourAccountOptions"
                :key="`account-${item.value}`"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="金额区间">
            <div class="amount-range">
              <el-input
                v-model="queryParams.amountMin"
                placeholder="最小金额"
                clearable
              />
              <span class="amount-separator">-</span>
              <el-input
                v-model="queryParams.amountMax"
                placeholder="最大金额"
                clearable
              />
            </div>
          </el-form-item>

          <el-form-item label="对方账号">
            <el-input
              v-model="queryParams.counterpartyAccount"
              placeholder="请输入对方账号"
              clearable
            />
            <el-checkbox
              v-model="queryParams.counterpartyAccountEmpty"
              class="empty-checkbox"
            >
              匹配空值
            </el-checkbox>
          </el-form-item>

          <el-form-item label="交易类型">
            <el-input
              v-model="queryParams.transactionType"
              placeholder="请输入交易类型"
              clearable
            />
            <el-checkbox
              v-model="queryParams.transactionTypeEmpty"
              class="empty-checkbox"
            >
              匹配空值
            </el-checkbox>
          </el-form-item>

          <div class="filter-actions">
            <el-button type="primary" @click="handleQuery">查询</el-button>
            <el-button plain @click="resetQuery">重置</el-button>
          </div>
        </el-form>
      </div>

      <div class="shell-main">
        <div class="shell-header">
          <div class="shell-title-group">
            <span class="shell-title">流水明细查询</span>
            <span class="shell-subtitle">按项目范围查询交易明细并查看详情</span>
          </div>
          <el-button
            size="small"
            type="primary"
            plain
            :disabled="total === 0"
            @click="handleExport"
          >
            导出流水
          </el-button>
        </div>

        <el-tabs v-model="activeTab" class="result-tabs" @tab-click="handleTabChange">
          <el-tab-pane label="全部" name="all" />
          <el-tab-pane label="流入" name="in" />
          <el-tab-pane label="流出" name="out" />
        </el-tabs>

        <div class="result-card">
          <el-alert
            v-if="listError"
            :closable="false"
            class="result-alert"
            show-icon
            title="流水明细加载失败，请稍后重试"
            type="error"
          />

          <el-table
            v-loading="loading"
            :data="list"
            border
            stripe
            class="result-table"
            @sort-change="handleSortChange"
          >
            <template slot="empty">
              <el-empty
                :image-size="96"
                description="当前筛选条件下暂无流水明细"
              />
            </template>
            <el-table-column
              label="交易时间"
              prop="trxDate"
              min-width="180"
              sortable="custom"
            />
            <el-table-column label="本方账户" min-width="220">
              <template slot-scope="scope">
                <div class="multi-line-cell">
                  <div class="primary-text">{{ formatField(scope.row.leAccountNo) }}</div>
                  <div class="secondary-text">{{ formatField(scope.row.leAccountName) }}</div>
                </div>
              </template>
            </el-table-column>
            <el-table-column label="对方账户" min-width="220">
              <template slot-scope="scope">
                <div class="multi-line-cell">
                  <div class="primary-text">
                    {{ formatField(scope.row.customerAccountName) }}
                  </div>
                  <div class="secondary-text">
                    {{ formatField(scope.row.customerAccountNo) }}
                  </div>
                </div>
              </template>
            </el-table-column>
            <el-table-column label="摘要 / 交易类型" min-width="240">
              <template slot-scope="scope">
                <div class="multi-line-cell">
                  <div class="primary-text">{{ formatField(scope.row.userMemo) }}</div>
                  <div class="secondary-text">{{ formatField(scope.row.cashType) }}</div>
                </div>
              </template>
            </el-table-column>
            <el-table-column
              label="交易金额"
              prop="displayAmount"
              min-width="140"
              align="right"
              sortable="custom"
            >
              <template slot-scope="scope">
                <span
                  class="amount-text"
                  :class="scope.row.displayAmount >= 0 ? 'amount-in' : 'amount-out'"
                >
                  {{ formatAmount(scope.row.displayAmount) }}
                </span>
              </template>
            </el-table-column>
            <el-table-column label="详情" width="100" fixed="right" align="center">
              <template slot-scope="scope">
                <el-button type="text" size="small" @click="handleViewDetail(scope.row)">
                  详情
                </el-button>
              </template>
            </el-table-column>
          </el-table>

          <pagination
            v-show="total > 0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="handlePageChange"
          />
        </div>
      </div>
    </div>

    <el-drawer
      :visible.sync="detailVisible"
      append-to-body
      custom-class="detail-drawer"
      size="520px"
      title="流水详情"
      @close="closeDetailDialog"
    >
      <div v-loading="detailLoading" class="detail-drawer-body">
        <div class="detail-section">
          <div class="detail-section-title">基础信息</div>
          <div class="detail-grid">
            <div class="detail-item">
              <span class="detail-label">流水ID</span>
              <span class="detail-value">{{ formatField(detailData.bankStatementId) }}</span>
            </div>
            <div class="detail-item">
              <span class="detail-label">交易时间</span>
              <span class="detail-value">{{ formatField(detailData.trxDate) }}</span>
            </div>
            <div class="detail-item">
              <span class="detail-label">币种</span>
              <span class="detail-value">{{ formatField(detailData.currency) }}</span>
            </div>
            <div class="detail-item">
              <span class="detail-label">交易类型</span>
              <span class="detail-value">{{ formatField(detailData.cashType) }}</span>
            </div>
            <div class="detail-item">
              <span class="detail-label">显示金额</span>
              <span class="detail-value">{{ formatAmount(detailData.displayAmount) }}</span>
            </div>
            <div class="detail-item">
              <span class="detail-label">余额</span>
              <span class="detail-value">{{ formatAmount(detailData.amountBalance) }}</span>
            </div>
          </div>
        </div>

        <div class="detail-section">
          <div class="detail-section-title">账户信息</div>
          <div class="detail-grid">
            <div class="detail-item detail-item-full">
              <span class="detail-label">本方账户</span>
              <span class="detail-value">
                {{ formatField(detailData.leAccountName) }} / {{ formatField(detailData.leAccountNo) }}
              </span>
            </div>
            <div class="detail-item detail-item-full">
              <span class="detail-label">对方账户</span>
              <span class="detail-value">
                {{ formatField(detailData.customerAccountName) }} /
                {{ formatField(detailData.customerAccountNo) }}
              </span>
            </div>
            <div class="detail-item">
              <span class="detail-label">本方银行</span>
              <span class="detail-value">{{ formatField(detailData.bank) }}</span>
            </div>
            <div class="detail-item">
              <span class="detail-label">对方银行</span>
              <span class="detail-value">{{ formatField(detailData.customerBank) }}</span>
            </div>
          </div>
        </div>

        <div class="detail-section">
          <div class="detail-section-title">补充信息</div>
          <div class="detail-grid">
            <div class="detail-item detail-item-full">
              <span class="detail-label">摘要</span>
              <span class="detail-value">{{ formatField(detailData.userMemo) }}</span>
            </div>
            <div class="detail-item detail-item-full">
              <span class="detail-label">银行摘要</span>
              <span class="detail-value">{{ formatField(detailData.bankComments) }}</span>
            </div>
            <div class="detail-item">
              <span class="detail-label">银行交易号</span>
              <span class="detail-value">{{ formatField(detailData.bankTrxNumber) }}</span>
            </div>
            <div class="detail-item">
              <span class="detail-label">交易方式</span>
              <span class="detail-value">{{ formatField(detailData.paymentMethod) }}</span>
            </div>
            <div class="detail-item">
              <span class="detail-label">异常类型</span>
              <span class="detail-value">{{ formatField(detailData.exceptionType) }}</span>
            </div>
            <div class="detail-item">
              <span class="detail-label">创建时间</span>
              <span class="detail-value">{{ formatDate(detailData.createDate) }}</span>
            </div>
          </div>
        </div>
      </div>
    </el-drawer>
  </div>
</template>

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

const TAB_MAP = {
  all: "all",
  in: "in",
  out: "out",
};

const SORT_MAP = {
  trxDate: "trxDate",
  displayAmount: "amount",
};

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: [],
});

const createEmptyDetailData = () => ({
  bankStatementId: "",
  trxDate: "",
  currency: "",
  leAccountNo: "",
  leAccountName: "",
  customerAccountName: "",
  customerAccountNo: "",
  customerBank: "",
  userMemo: "",
  bankComments: "",
  bankTrxNumber: "",
  bank: "",
  cashType: "",
  amountBalance: "",
  displayAmount: "",
  paymentMethod: "",
  exceptionType: "",
  createDate: "",
});

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,
      detailLoading: false,
      detailVisible: false,
      activeTab: "all",
      dateRange: [],
      list: [],
      total: 0,
      listError: "",
      detailData: createEmptyDetailData(),
      queryParams: createDefaultQueryParams(this.projectId),
      optionData: createEmptyOptionData(),
    };
  },
  created() {
    this.getList();
    this.getOptions();
  },
  watch: {
    dateRange(value) {
      this.queryParams.transactionStartTime = value && value[0] ? value[0] : "";
      this.queryParams.transactionEndTime = value && value[1] ? value[1] : "";
    },
    projectId() {
      this.syncProjectId();
      this.getOptions();
      this.getList();
    },
  },
  methods: {
    async getList() {
      this.syncProjectId();
      if (!this.queryParams.projectId) {
        this.list = [];
        this.total = 0;
        this.listError = "";
        return;
      }

      this.loading = true;
      try {
        const res = await listBankStatement(this.queryParams);
        this.list = res.rows || [];
        this.total = res.total || 0;
        this.listError = "";
      } catch (error) {
        this.list = [];
        this.total = 0;
        this.listError = "加载流水明细失败";
        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 = TAB_MAP[this.activeTab] || "all";
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.syncProjectId();
      this.getList();
    },
    resetQuery() {
      this.activeTab = "all";
      this.dateRange = [];
      this.queryParams = createDefaultQueryParams(this.projectId);
      this.syncProjectId();
      this.getOptions();
      this.getList();
    },
    handleTabChange(tab) {
      const tabName = tab && tab.name ? tab.name : this.activeTab;
      this.activeTab = tabName;
      this.queryParams.pageNum = 1;
      this.queryParams.tabType = TAB_MAP[tabName] || "all";
      this.getList();
    },
    handleSortChange({ prop, order }) {
      this.queryParams.orderBy = SORT_MAP[prop] || "trxDate";
      this.queryParams.orderDirection = order === "ascending" ? "asc" : "desc";
      if (!order) {
        this.queryParams.orderBy = "trxDate";
        this.queryParams.orderDirection = "desc";
      }
      this.getList();
    },
    handlePageChange(pageInfo) {
      if (typeof pageInfo === "number") {
        this.queryParams.pageNum = pageInfo;
      } else {
        this.queryParams.pageNum = pageInfo.page;
        this.queryParams.pageSize = pageInfo.limit;
      }
      this.getList();
    },
    async handleViewDetail(row) {
      if (!row || !row.bankStatementId) {
        return;
      }

      this.detailVisible = true;
      this.detailLoading = true;
      try {
        const res = await getBankStatementDetail(row.bankStatementId);
        this.detailData = {
          ...createEmptyDetailData(),
          ...(res.data || {}),
        };
      } catch (error) {
        this.detailData = createEmptyDetailData();
        this.$message.error("加载流水详情失败");
        console.error("加载流水详情失败", error);
      } finally {
        this.detailLoading = false;
      }
    },
    closeDetailDialog() {
      this.detailVisible = false;
      this.detailData = createEmptyDetailData();
    },
    handleExport() {
      if (this.total === 0) {
        return;
      }

      this.download(
        "ccdi/project/bank-statement/export",
        { ...this.queryParams },
        `${this.projectInfo.projectName || "项目"}_流水明细_${Date.now()}.xlsx`
      );
    },
    formatField(value) {
      return value || "-";
    },
    formatDate(value) {
      return value ? parseTime(value, "{y}-{m}-{d} {h}:{i}:{s}") : "-";
    },
    formatAmount(value) {
      if (value === null || value === undefined || value === "") {
        return "-";
      }
      return Number(value).toLocaleString("zh-CN", {
        minimumFractionDigits: 2,
        maximumFractionDigits: 2,
      });
    },
  },
};
</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 {
  border: 1px solid #ebeef5;
  border-radius: 4px;
  background: #fff;
}

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

.shell-main {
  padding: 20px;
}

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

.shell-title-group {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.shell-subtitle {
  font-size: 13px;
  color: #909399;
}

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

.filter-form {
  margin-top: 20px;

  :deep(.el-form-item) {
    margin-bottom: 18px;
  }
}

.filter-control {
  width: 100%;
}

.empty-checkbox {
  margin-top: 8px;
}

.amount-range {
  display: flex;
  align-items: center;
  gap: 8px;
}

.amount-separator {
  color: #909399;
  font-size: 13px;
}

.filter-actions {
  display: flex;
  gap: 12px;

  .el-button {
    flex: 1;
  }
}

.result-tabs {
  margin-bottom: 16px;
}

.result-card {
  border: 1px solid #ebeef5;
  border-radius: 4px;
  overflow: hidden;
}

.result-alert {
  margin: 16px 16px 0;
}

.result-table {
  width: 100%;
}

.multi-line-cell {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.primary-text {
  color: #303133;
  line-height: 20px;
}

.secondary-text {
  color: #909399;
  font-size: 12px;
  line-height: 18px;
}

.amount-text {
  font-weight: 600;
}

.amount-in {
  color: #67c23a;
}

.amount-out {
  color: #f56c6c;
}

.detail-drawer-body {
  padding: 0 4px 24px;
}

.detail-section {
  margin-bottom: 20px;
  padding: 16px;
  border: 1px solid #ebeef5;
  border-radius: 4px;
}

.detail-section-title {
  margin-bottom: 14px;
  font-size: 15px;
  font-weight: 600;
  color: #303133;
}

.detail-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px 16px;
}

.detail-item {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.detail-item-full {
  grid-column: 1 / -1;
}

.detail-label {
  font-size: 12px;
  color: #909399;
}

.detail-value {
  color: #303133;
  line-height: 20px;
  word-break: break-all;
}

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

  .filter-actions {
    flex-direction: column;
  }
}

@media (max-width: 768px) {
  .detail-query-container {
    padding: 8px;
  }

  .shell-sidebar,
  .shell-main {
    padding: 16px;
  }

  .shell-header {
    flex-direction: column;
  }

  .result-alert {
    margin: 12px 12px 0;
  }

  .detail-grid {
    grid-template-columns: 1fr;
  }
}
</style>
