<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="120px"
    >
      <el-form-item label="代理商名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入代理商名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>

      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button type="primary" plain @click="openForm('create')">
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
      row-key="id"
      v-loading="loading"
      :data="list"
      :stripe="true"
      :show-overflow-tooltip="true"
    >
      <el-table-column label="代理商ID" align="center" prop="id" min-width="120px" />
      <el-table-column label="代理商名称" align="center" prop="name" min-width="150px" />
      <el-table-column label="登录账号" align="center" prop="loginname" min-width="120px" />
      <el-table-column label="联系人" align="center" prop="contact" min-width="100px" />
      <el-table-column label="地址" align="center" prop="address" min-width="150px" />
      <el-table-column label="创建时间" align="center" prop="createTime" min-width="160px">
        <template #default="scope">
          {{ scope.row.createTime || '-' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" min-width="240px" fixed="right">
        <template #default="scope">
          <el-button link type="primary" @click="handleDetail(scope.row.id)">
            详情
          </el-button>
          <el-button link type="primary" @click="openForm('update', scope.row.id)">
            编辑
          </el-button>
          <el-button link type="success" @click="openBalance(scope.row.id, 'add')">
            充值
          </el-button>
          <el-button link type="warning" @click="openBalance(scope.row.id, 'recovery')">
            收回
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <AgentForm ref="formRef" @success="getList" />
  
  <!-- 详情弹窗 -->
  <AgentDetail ref="detailRef" />
  
  <!-- 点卡充值/收回弹窗 -->
  <AgentBalanceDialog ref="balanceRef" @success="getList" />
</template>

<script setup lang="ts">
import { getAgentList, type AgentListParams, type Agent } from '@/api/interphone/agent'
import AgentForm from './AgentForm.vue'
import AgentDetail from './AgentDetail.vue'
import AgentBalanceDialog from './AgentBalanceDialog.vue'

/** 代理商列表 */
defineOptions({ name: 'InterphoneAgent' })

const message = useMessage() // 消息弹窗

const loading = ref(true) // 列表的加载中
const list = ref<Agent[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive<AgentListParams>({
  pageNo: 1,
  pageSize: 10,
  name: undefined
})
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const res = await getAgentList(queryParams)
    console.log(res)
    if (res.code === 20001) {
      list.value = res.data?.data || []
      total.value = res.data?.recordsFiltered || 0
    } else {
      message.error(res.msg || '获取代理商列表失败')
    }
  } catch (error) {
    console.error('获取代理商列表失败:', error)
    message.error('获取代理商列表失败')
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: 'create' | 'update', id?: string) => {
  formRef.value.open(type, id)
}

/** 查看详情 */
const detailRef = ref()
const handleDetail = (id: string) => {
  detailRef.value.open(id)
}

/** 点卡充值/收回 */
const balanceRef = ref()
const openBalance = (id: string, type: 'add' | 'recovery') => {
  balanceRef.value.open(id, type)
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
