<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">
          <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: {
    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;
        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;
}

.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;
  }
}

.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;
  }

  .filter-actions {
    flex-direction: column;
  }
}
</style>
