<template>
  <div class="app-container account-prototype-page">
    <div class="board">
      <el-form
        v-show="showSearch"
        ref="queryForm"
        :model="queryParams"
        size="small"
        :inline="true"
        class="query-form"
        label-width="96px"
      >
        <el-form-item label="所属人类型">
          <el-select v-model="queryParams.ownerType" placeholder="请选择所属人类型" clearable style="width: 180px">
            <el-option label="员工" value="EMPLOYEE" />
            <el-option label="员工关系人" value="RELATION" />
            <el-option label="中介" value="INTERMEDIARY" />
            <el-option label="外部人员" value="EXTERNAL" />
          </el-select>
        </el-form-item>
        <el-form-item label="员工姓名">
          <el-input v-model="queryParams.staffName" placeholder="请输入员工姓名" clearable style="width: 220px" @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="账户范围">
          <el-select v-model="queryParams.bankScope" placeholder="请选择账户范围" clearable style="width: 160px">
            <el-option label="行内" value="INTERNAL" />
            <el-option label="行外" value="EXTERNAL" />
          </el-select>
        </el-form-item>
        <el-form-item label="关系类型">
          <el-select v-model="queryParams.relationType" placeholder="请选择关系类型" clearable style="width: 180px">
            <el-option v-for="item in relationTypeOptions" :key="item" :label="item" :value="item" />
          </el-select>
        </el-form-item>
        <el-form-item label="账户姓名">
          <el-input v-model="queryParams.accountName" placeholder="请输入账户姓名" clearable style="width: 220px" @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="账户类型">
          <el-select v-model="queryParams.accountType" placeholder="请选择账户类型" clearable style="width: 180px">
            <el-option label="银行账户" value="BANK" />
            <el-option label="证券账户" value="SECURITIES" />
            <el-option label="支付账户" value="PAYMENT" />
            <el-option label="其他" value="OTHER" />
          </el-select>
        </el-form-item>
        <el-form-item label="是否实控">
          <el-select v-model="queryParams.isActualControl" placeholder="请选择是否实控" clearable style="width: 180px">
            <el-option label="是" :value="1" />
            <el-option label="否" :value="0" />
          </el-select>
        </el-form-item>
        <el-form-item label="风险等级">
          <el-select v-model="queryParams.riskLevel" placeholder="请选择风险等级" clearable style="width: 180px">
            <el-option label="LOW" value="LOW" />
            <el-option label="MEDIUM" value="MEDIUM" />
            <el-option label="HIGH" value="HIGH" />
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="queryParams.status" placeholder="请选择状态" clearable style="width: 160px">
            <el-option label="正常" :value="1" />
            <el-option label="已销户" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item class="query-actions">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="openCreateDialog">新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="success" plain icon="el-icon-upload2" size="mini">导入</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="warning" plain icon="el-icon-download" size="mini">导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
      </el-row>

      <el-table :data="pageRows" stripe border class="account-table">
        <el-table-column label="员工姓名" prop="staffName" min-width="120" />
        <el-table-column label="所属人类型" prop="ownerType" width="132" align="center">
          <template slot-scope="scope">
            <el-tag :type="ownerTypeTagType(scope.row.ownerType)" size="mini" effect="plain">
              {{ ownerTypeLabel(scope.row.ownerType) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="关系类型" prop="relationType" width="90" align="center">
          <template slot-scope="scope">{{ scope.row.relationType || '-' }}</template>
        </el-table-column>
        <el-table-column label="证件号" prop="ownerId" min-width="170" show-overflow-tooltip>
          <template slot-scope="scope">{{ scope.row.ownerId || scope.row.relationCertNo || '-' }}</template>
        </el-table-column>
        <el-table-column label="账户姓名" prop="accountName" min-width="120">
          <template slot-scope="scope">{{ scope.row.accountName || '-' }}</template>
        </el-table-column>
        <el-table-column label="账户号码" prop="accountNo" min-width="180" />
        <el-table-column label="账户类型" prop="accountType" width="110" align="center">
          <template slot-scope="scope">{{ accountTypeLabel(scope.row.accountType) }}</template>
        </el-table-column>
        <el-table-column label="账户范围" prop="bankScope" width="96" align="center">
          <template slot-scope="scope">
            <el-tag size="mini" :type="bankScopeTagType(scope.row.bankScope)" effect="plain">{{ bankScopeLabel(scope.row.bankScope) }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="开户机构" prop="openBank" min-width="160" show-overflow-tooltip />
        <el-table-column label="是否实控" prop="isActualControl" width="92" align="center">
          <template slot-scope="scope">
            <el-tag size="mini" :type="scope.row.isActualControl ? 'success' : 'danger'">
              {{ scope.row.isActualControl ? '是' : '否' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="月均笔数" prop="avgMonthTxnCount" width="90" align="center" />
        <el-table-column label="月均金额" prop="avgMonthTxnAmount" min-width="120" align="right">
          <template slot-scope="scope">{{ formatAmount(scope.row.avgMonthTxnAmount) }}</template>
        </el-table-column>
        <el-table-column label="频率等级" prop="txnFrequencyLevel" width="96" align="center">
          <template slot-scope="scope">
            <el-tag size="mini" :type="frequencyTagType(scope.row.txnFrequencyLevel)">{{ scope.row.txnFrequencyLevel }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="风险等级" prop="txnRiskLevel" width="96" align="center">
          <template slot-scope="scope">
            <el-tag size="mini" effect="dark" :type="riskTagType(scope.row.txnRiskLevel)">{{ scope.row.txnRiskLevel }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="状态" prop="status" width="80" align="center">
          <template slot-scope="scope">
            <el-tag size="mini" :type="scope.row.status === 1 ? 'success' : 'danger'">{{ scope.row.status === 1 ? '正常' : '已销户' }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180" fixed="right" align="center">
          <template slot-scope="scope">
            <el-button type="text" size="mini" icon="el-icon-view" @click="openDetailDialog(scope.row)">详情</el-button>
            <el-button type="text" size="mini" icon="el-icon-edit" @click="openEditDialog(scope.row)">编辑</el-button>
            <el-button type="text" size="mini" icon="el-icon-delete">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

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

    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="1160px" append-to-body>
      <el-form ref="dialogForm" :model="form" label-width="120px">
        <div class="form-section">
          <div class="section-head"><span>归属信息</span></div>
          <el-row :gutter="16">
            <el-col :span="8">
              <el-form-item label="所属人类型">
                <el-radio-group v-model="form.ownerType" :disabled="dialogMode === 'detail'" @change="handleOwnerTypeChange">
                  <el-radio label="EMPLOYEE">员工</el-radio>
                  <el-radio label="RELATION">员工关系人</el-radio>
                  <el-radio label="INTERMEDIARY">中介</el-radio>
                  <el-radio label="EXTERNAL">外部人员</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item v-if="form.ownerType !== 'EXTERNAL' && form.ownerType !== 'INTERMEDIARY'" label="员工姓名">
                <el-select v-model="form.staffId" placeholder="请选择员工" style="width: 100%" :disabled="dialogMode === 'detail'" @change="handleStaffChange">
                  <el-option v-for="item in staffOptions" :key="item.staffId" :label="item.name" :value="item.staffId" />
                </el-select>
              </el-form-item>
              <el-form-item v-else :label="form.ownerType === 'INTERMEDIARY' ? '中介名称' : '外部人员姓名'">
                <el-input v-model="form.accountName" :placeholder="form.ownerType === 'INTERMEDIARY' ? '请输入中介名称' : '请输入外部人员姓名'" :disabled="dialogMode === 'detail'" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="是否本人账户">
                <el-input :value="form.ownerType === 'EMPLOYEE' ? '是' : '否'" disabled />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="16" v-if="form.ownerType === 'RELATION'">
            <el-col :span="8">
              <el-form-item label="关系人姓名">
                <el-select v-model="form.relationId" placeholder="请选择关系人" style="width: 100%" :disabled="dialogMode === 'detail'" @change="fillRelationMeta">
                  <el-option v-for="item in relationOptions" :key="item.id" :label="item.relationName" :value="item.id" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="关系类型">
                <el-select v-model="form.relationType" style="width: 100%" :disabled="dialogMode !== 'create'">
                  <el-option v-for="item in relationTypeOptions" :key="item" :label="item" :value="item" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="关系人证件号">
                <el-input v-model="form.relationCertNo" disabled />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="16">
            <el-col :span="8">
              <el-form-item label="证件号">
                <el-input v-model="form.ownerId" :placeholder="form.ownerType === 'INTERMEDIARY' ? '请输入中介证件号' : '请输入身份证号'" :disabled="dialogMode === 'detail' || form.ownerType === 'EMPLOYEE'" />
              </el-form-item>
            </el-col>
          </el-row>
        </div>

        <div class="form-section">
          <div class="section-head"><span>账户信息</span></div>
          <el-row :gutter="16">
            <el-col :span="8">
              <el-form-item label="账户号码">
                <el-input v-model="form.accountNo" :disabled="dialogMode === 'detail'" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="账户类型">
                <el-select v-model="form.accountType" style="width: 100%" :disabled="dialogMode === 'detail'">
                  <el-option label="银行账户" value="BANK" />
                  <el-option label="证券账户" value="SECURITIES" />
                  <el-option label="支付账户" value="PAYMENT" />
                  <el-option label="其他" value="OTHER" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="账户范围">
                <el-select v-model="form.bankScope" style="width: 100%" :disabled="dialogMode === 'detail'">
                  <el-option label="行内" value="INTERNAL" />
                  <el-option label="行外" value="EXTERNAL" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="16">
            <el-col :span="8">
              <el-form-item label="开户机构">
                <el-input v-model="form.openBank" :disabled="dialogMode === 'detail'" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="银行代码">
                <el-input v-model="form.bankCode" :disabled="dialogMode === 'detail'" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="币种">
                <el-input v-model="form.currency" :disabled="dialogMode === 'detail'" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="16">
            <el-col :span="8">
              <el-form-item label="状态">
                <el-select v-model="form.status" style="width: 100%" :disabled="dialogMode === 'detail'">
                  <el-option label="正常" :value="1" />
                  <el-option label="已销户" :value="2" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="生效日期">
                <el-date-picker v-model="form.effectiveDate" type="date" value-format="yyyy-MM-dd" style="width: 100%" :disabled="dialogMode === 'detail'" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="失效日期">
                <el-date-picker v-model="form.invalidDate" type="date" value-format="yyyy-MM-dd" style="width: 100%" :disabled="dialogMode === 'detail'" />
              </el-form-item>
            </el-col>
          </el-row>
        </div>

        <div class="form-section accent-section">
          <div class="section-head">
            <span>分析信息</span>
          </div>
          <el-row :gutter="16">
            <el-col :span="8">
              <el-form-item label="是否实控账户">
                <el-select v-model="form.isActualControl" style="width: 100%" :disabled="analysisReadonly">
                  <el-option label="是" :value="1" />
                  <el-option label="否" :value="0" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="月均交易笔数">
                <el-input-number v-model="form.avgMonthTxnCount" :min="0" :disabled="analysisReadonly" style="width: 100%" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="月均交易金额">
                <el-input-number v-model="form.avgMonthTxnAmount" :min="0" :precision="2" :disabled="analysisReadonly" style="width: 100%" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="16">
            <el-col :span="6">
              <el-form-item label="频率等级">
                <el-select v-model="form.txnFrequencyLevel" style="width: 100%" :disabled="analysisReadonly">
                  <el-option label="LOW" value="LOW" />
                  <el-option label="MEDIUM" value="MEDIUM" />
                  <el-option label="HIGH" value="HIGH" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="风险等级">
                <el-select v-model="form.txnRiskLevel" style="width: 100%" :disabled="analysisReadonly">
                  <el-option label="LOW" value="LOW" />
                  <el-option label="MEDIUM" value="MEDIUM" />
                  <el-option label="HIGH" value="HIGH" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="16">
            <el-col :span="6">
              <el-form-item label="借方单笔最高额">
                <el-input-number v-model="form.debitSingleMaxAmount" :min="0" :precision="2" :disabled="analysisReadonly" style="width: 100%" />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="贷方单笔最高额">
                <el-input-number v-model="form.creditSingleMaxAmount" :min="0" :precision="2" :disabled="analysisReadonly" style="width: 100%" />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="借方日累计最高额">
                <el-input-number v-model="form.debitDailyMaxAmount" :min="0" :precision="2" :disabled="analysisReadonly" style="width: 100%" />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="贷方日累计最高额">
                <el-input-number v-model="form.creditDailyMaxAmount" :min="0" :precision="2" :disabled="analysisReadonly" style="width: 100%" />
              </el-form-item>
            </el-col>
          </el-row>
        </div>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">{{ dialogMode === 'detail' ? '关 闭' : '取 消' }}</el-button>
        <el-button v-if="dialogMode !== 'detail'" type="primary" @click="dialogVisible = false">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
const RELATION_TYPES = ['配偶', '父亲', '母亲', '子女', '兄弟姐妹', '朋友', '同事', '其他']

const ROWS = [
  { id: 1, staffId: 100001, staffName: '陈志远', ownerType: 'EMPLOYEE', ownerId: '330106198801126211', relationId: null, relationType: '', relationName: '', accountName: '陈志远', relationCertNo: '', accountNo: '6222024300000187612', accountType: 'BANK', bankScope: 'INTERNAL', openBank: '本行营业部', bankCode: '102331000001', currency: 'CNY', status: 1, isActualControl: 1, avgMonthTxnCount: 82, avgMonthTxnAmount: 356800, txnFrequencyLevel: 'HIGH', debitSingleMaxAmount: 58000, creditSingleMaxAmount: 120000, debitDailyMaxAmount: 132000, creditDailyMaxAmount: 168000, txnRiskLevel: 'MEDIUM', effectiveDate: '2024-01-08', invalidDate: '' },
  { id: 2, staffId: 100001, staffName: '陈志远', ownerType: 'RELATION', ownerId: '330102198903075428', relationId: 90011, relationType: '配偶', relationName: '陆瑶', accountName: '陆瑶', relationCertNo: '330102198903075428', accountNo: '6217002876309918271', accountType: 'BANK', bankScope: 'EXTERNAL', openBank: '建设银行城西支行', bankCode: '105331000017', currency: 'CNY', status: 1, isActualControl: 1, avgMonthTxnCount: 41, avgMonthTxnAmount: 168400, txnFrequencyLevel: 'MEDIUM', debitSingleMaxAmount: 66000, creditSingleMaxAmount: 83000, debitDailyMaxAmount: 91000, creditDailyMaxAmount: 103000, txnRiskLevel: 'LOW', effectiveDate: '2024-06-18', invalidDate: '' },
  { id: 3, staffId: 100002, staffName: '高嘉宁', ownerType: 'RELATION', relationId: 90021, relationType: '父亲', relationName: '高国平', accountName: '高国平', relationCertNo: '330106196603128414', accountNo: '3100038899226611007', accountType: 'SECURITIES', bankScope: 'EXTERNAL', openBank: '国金证券杭州营业部', bankCode: '', currency: 'CNY', status: 1, isActualControl: 0, avgMonthTxnCount: 16, avgMonthTxnAmount: 912000, txnFrequencyLevel: 'LOW', debitSingleMaxAmount: 380000, creditSingleMaxAmount: 420000, debitDailyMaxAmount: 460000, creditDailyMaxAmount: 500000, txnRiskLevel: 'HIGH', effectiveDate: '2025-02-01', invalidDate: '' },
  { id: 4, staffId: 100003, staffName: '宋文洁', ownerType: 'EMPLOYEE', ownerId: '330106199110018922', relationId: null, relationType: '', relationName: '', accountName: '宋文洁', relationCertNo: '', accountNo: '0903001000017623312', accountType: 'BANK', bankScope: 'INTERNAL', openBank: '本行滨江支行', bankCode: '102331000009', currency: 'CNY', status: 1, isActualControl: 1, avgMonthTxnCount: 65, avgMonthTxnAmount: 204600, txnFrequencyLevel: 'MEDIUM', debitSingleMaxAmount: 36000, creditSingleMaxAmount: 92000, debitDailyMaxAmount: 88000, creditDailyMaxAmount: 112000, txnRiskLevel: 'LOW', effectiveDate: '2023-09-20', invalidDate: '' },
  { id: 5, staffId: 100004, staffName: '周明浩', ownerType: 'RELATION', ownerId: '330108201407210024', relationId: 90031, relationType: '子女', relationName: '周晨熙', accountName: '周晨熙', relationCertNo: '330108201407210024', accountNo: '6212260018273009177', accountType: 'BANK', bankScope: 'EXTERNAL', openBank: '农业银行滨江支行', bankCode: '103331000018', currency: 'CNY', status: 1, isActualControl: 1, avgMonthTxnCount: 9, avgMonthTxnAmount: 12800, txnFrequencyLevel: 'LOW', debitSingleMaxAmount: 2500, creditSingleMaxAmount: 10000, debitDailyMaxAmount: 2600, creditDailyMaxAmount: 10000, txnRiskLevel: 'LOW', effectiveDate: '2025-03-15', invalidDate: '' },
  { id: 6, staffId: 100002, staffName: '高嘉宁', ownerType: 'EMPLOYEE', ownerId: '330106199002163517', relationId: null, relationType: '', relationName: '', accountName: '高嘉宁', relationCertNo: '', accountNo: '9558803029941256618', accountType: 'BANK', bankScope: 'EXTERNAL', openBank: '邮储银行滨江支行', bankCode: '403331000003', currency: 'CNY', status: 2, isActualControl: 0, avgMonthTxnCount: 4, avgMonthTxnAmount: 8600, txnFrequencyLevel: 'LOW', debitSingleMaxAmount: 4300, creditSingleMaxAmount: 5000, debitDailyMaxAmount: 4300, creditDailyMaxAmount: 5000, txnRiskLevel: 'MEDIUM', effectiveDate: '2022-11-11', invalidDate: '' },
  { id: 7, staffId: null, staffName: '-', ownerType: 'EXTERNAL', ownerId: '91330108MA2H8X8X2Q', relationId: null, relationType: '其他', relationName: '', accountName: '边界科技有限公司', relationCertNo: '', accountNo: '6214830019287765102', accountType: 'BANK', bankScope: 'EXTERNAL', openBank: '招商银行钱江支行', bankCode: '308331000112', currency: 'CNY', status: 1, isActualControl: 0, avgMonthTxnCount: 28, avgMonthTxnAmount: 68400, txnFrequencyLevel: 'MEDIUM', debitSingleMaxAmount: 32000, creditSingleMaxAmount: 45000, debitDailyMaxAmount: 58000, creditDailyMaxAmount: 76000, txnRiskLevel: 'LOW', effectiveDate: '2025-07-01', invalidDate: '' },
  { id: 8, staffId: null, staffName: '-', ownerType: 'INTERMEDIARY', ownerId: '330105198512124411', relationId: null, relationType: '', relationName: '', accountName: '吴晓峰', relationCertNo: '', accountNo: '13857123456', accountType: 'PAYMENT', bankScope: 'EXTERNAL', openBank: '支付宝', bankCode: '', currency: 'CNY', status: 1, isActualControl: 0, avgMonthTxnCount: 12, avgMonthTxnAmount: 48600, txnFrequencyLevel: 'MEDIUM', debitSingleMaxAmount: 9000, creditSingleMaxAmount: 12000, debitDailyMaxAmount: 15000, creditDailyMaxAmount: 18000, txnRiskLevel: 'LOW', effectiveDate: '2025-04-01', invalidDate: '' }
]

const STAFF_OPTIONS = [
  { staffId: 100001, name: '陈志远', ownerId: '330106198801126211' },
  { staffId: 100002, name: '高嘉宁', ownerId: '330106199002163517' },
  { staffId: 100003, name: '宋文洁', ownerId: '330106199110018922' },
  { staffId: 100004, name: '周明浩', ownerId: '330106198612104136' }
]

const RELATION_OPTIONS = [
  { id: 90011, staffId: 100001, relationName: '陆瑶', relationType: '配偶', relationCertNo: '330102198903075428' },
  { id: 90021, staffId: 100002, relationName: '高国平', relationType: '父亲', relationCertNo: '330106196603128414' },
  { id: 90031, staffId: 100004, relationName: '周晨熙', relationType: '子女', relationCertNo: '330108201407210024' }
]

function createEmptyForm() {
  return {
    id: null,
    staffId: undefined,
    ownerType: 'EMPLOYEE',
    ownerId: '',
    relationId: undefined,
    relationType: '',
    relationName: '',
    accountName: '',
    relationCertNo: '',
    accountNo: '',
    accountType: 'BANK',
    bankScope: 'EXTERNAL',
    openBank: '',
    bankCode: '',
    currency: 'CNY',
    status: 1,
    effectiveDate: '',
    invalidDate: '',
    isActualControl: 1,
    avgMonthTxnCount: 0,
    avgMonthTxnAmount: 0,
    txnFrequencyLevel: 'MEDIUM',
    debitSingleMaxAmount: 0,
    creditSingleMaxAmount: 0,
    debitDailyMaxAmount: 0,
    creditDailyMaxAmount: 0,
    txnRiskLevel: 'LOW'
  }
}

export default {
  name: 'CcdiAccountInfoPrototype',
  data() {
    return {
      showSearch: true,
      dialogVisible: false,
      dialogMode: 'create',
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        staffName: '',
        ownerType: '',
        bankScope: '',
        relationType: '',
        accountName: '',
        accountType: '',
        isActualControl: undefined,
        riskLevel: '',
        status: undefined
      },
      rows: ROWS,
      form: createEmptyForm(),
      staffOptions: STAFF_OPTIONS,
      allRelationOptions: RELATION_OPTIONS,
      relationTypeOptions: RELATION_TYPES
    }
  },
  computed: {
    filteredRows() {
      return this.rows.filter((row) => {
        if (this.queryParams.staffName && !row.staffName.includes(this.queryParams.staffName)) return false
        if (this.queryParams.ownerType && row.ownerType !== this.queryParams.ownerType) return false
        if (this.queryParams.bankScope && row.bankScope !== this.queryParams.bankScope) return false
        if (this.queryParams.relationType && row.relationType !== this.queryParams.relationType) return false
        if (this.queryParams.accountName && !row.accountName.includes(this.queryParams.accountName)) return false
        if (this.queryParams.accountType && row.accountType !== this.queryParams.accountType) return false
        if (this.queryParams.isActualControl !== undefined && row.isActualControl !== this.queryParams.isActualControl) return false
        if (this.queryParams.riskLevel && row.txnRiskLevel !== this.queryParams.riskLevel) return false
        if (this.queryParams.status !== undefined && row.status !== this.queryParams.status) return false
        return true
      })
    },
    pageRows() {
      const start = (this.queryParams.pageNum - 1) * this.queryParams.pageSize
      return this.filteredRows.slice(start, start + this.queryParams.pageSize)
    },
    relationOptions() {
      if (!this.form.staffId) return []
      return this.allRelationOptions.filter(item => item.staffId === this.form.staffId)
    },
    analysisReadonly() {
      return this.dialogMode === 'detail' || this.form.bankScope === 'INTERNAL'
    },
    dialogTitle() {
      if (this.dialogMode === 'detail') return '账户详情'
      if (this.dialogMode === 'edit') return '编辑账户'
      return '新增账户'
    }
  },
  methods: {
    getList() {
      this.queryParams.pageNum = 1
    },
    formatAmount(value) {
      if (value === null || value === undefined || value === '') return '0.00'
      return Number(value).toLocaleString('zh-CN', { minimumFractionDigits: 2, maximumFractionDigits: 2 })
    },
    accountTypeLabel(type) {
      return { BANK: '银行账户', SECURITIES: '证券账户', PAYMENT: '支付账户', OTHER: '其他' }[type] || type
    },
    bankScopeLabel(scope) {
      return { INTERNAL: '行内', EXTERNAL: '行外' }[scope] || scope
    },
    bankScopeTagType(scope) {
      return scope === 'INTERNAL' ? 'success' : 'warning'
    },
    ownerTypeLabel(type) {
      return { EMPLOYEE: '员工', RELATION: '员工关系人', INTERMEDIARY: '中介', EXTERNAL: '外部人员' }[type] || type
    },
    ownerTypeTagType(type) {
      return { EMPLOYEE: 'success', RELATION: 'info', INTERMEDIARY: 'danger', EXTERNAL: 'warning' }[type] || 'info'
    },
    riskTagType(level) {
      return { LOW: 'success', MEDIUM: 'warning', HIGH: 'danger' }[level] || 'info'
    },
    frequencyTagType(level) {
      return { LOW: 'info', MEDIUM: '', HIGH: 'danger' }[level] || 'info'
    },
    handleQuery() {
      this.queryParams.pageNum = 1
    },
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        staffName: '',
        ownerType: '',
        bankScope: '',
        relationType: '',
        accountName: '',
        accountType: '',
        isActualControl: undefined,
        riskLevel: '',
        status: undefined
      }
    },
    handlePagination() {},
    openCreateDialog() {
      this.dialogMode = 'create'
      this.form = createEmptyForm()
      this.dialogVisible = true
    },
    openEditDialog(row) {
      this.dialogMode = 'edit'
      this.form = { ...row }
      this.dialogVisible = true
    },
    openDetailDialog(row) {
      this.dialogMode = 'detail'
      this.form = { ...row }
      this.dialogVisible = true
    },
    handleOwnerTypeChange(value) {
      if (value !== 'RELATION') {
        this.form.relationId = undefined
        this.form.relationType = ''
        this.form.relationCertNo = ''
      }
      if (value === 'EMPLOYEE') {
        this.form.relationName = ''
        const staff = this.staffOptions.find(item => item.staffId === this.form.staffId)
        this.form.accountName = staff ? staff.name : ''
        this.form.ownerId = staff ? (staff.ownerId || '') : this.form.ownerId
      }
      if (value === 'EXTERNAL' || value === 'INTERMEDIARY') this.form.staffId = undefined
    },
    handleStaffChange(staffId) {
      if (this.form.ownerType !== 'EMPLOYEE') return
      const staff = this.staffOptions.find(item => item.staffId === staffId)
      this.form.accountName = staff ? staff.name : ''
    },
    fillRelationMeta(relationId) {
      const relation = this.allRelationOptions.find(item => item.id === relationId)
      if (!relation) return
      this.form.relationName = relation.relationName
      this.form.accountName = relation.relationName
      this.form.relationType = relation.relationType
      this.form.relationCertNo = relation.relationCertNo
    }
  }
}
</script>

<style lang="scss" scoped>
.account-prototype-page {
  min-height: calc(100vh - 84px);
  background: #f5f7fa;
}

.board {
  padding: 20px;
  border-radius: 8px;
  background: #fff;
  border: 1px solid #ebeef5;
  box-shadow: none;
}

.account-table ::v-deep .el-table__header th {
  background: #f8f8f9;
  color: #515a6e;
}

.form-section {
  margin-bottom: 18px;
  padding: 18px 18px 4px;
  border-radius: 8px;
  background: #f9fafb;
  border: 1px solid #ebeef5;
}

.accent-section {
  background: #f9fafb;
}

.section-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 16px;
}

.section-head span {
  color: #303133;
  font-size: 16px;
  font-weight: 700;
}

.section-head small {
  color: #909399;
  font-size: 12px;
}

@media (max-width: 768px) {
  .query-form ::v-deep .el-form-item {
    display: block;
    margin-right: 0;
  }
}
</style>
