<template>
  <ContentWrap>
    <SearchBarEx
      :fields="allFields"
      :queryParams="queryParams"
      @query="handleQuery"
      @reset="resetQuery"
      @create="openForm('create')"
      @export="handleExport"
    />
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="客户名称" align="center" prop="name" />
      <el-table-column label="机构代码" align="center" prop="code" />
      <el-table-column label="联系人" align="center" prop="contacts" />
      <el-table-column label="电话" align="center" prop="phone" />
      <el-table-column label="地址" align="center" prop="address" />
      <el-table-column label="累计合同签订额" align="center" prop="total_contract_amount" />
      <el-table-column label="累计三年及以上应收款" align="center" prop="total_accounts_receivable" />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['pm:customer:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['pm:customer:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <CustomerForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import download from '@/utils/download'
import { CustomerApi, CustomerVO } from '@/api/pm/customer'
import CustomerForm from './CustomerForm.vue'
import { ref, onMounted, reactive } from 'vue'
import SearchBarEx from "@/components/SearchBar/SearchBarEx.vue";

/** 客户信息 列表 */
defineOptions({ name: 'Customer' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<CustomerVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: undefined,
  code: undefined,
  contacts: undefined,
  phone: undefined,
  address: undefined,
  address2: undefined,
  date: undefined,
  dateRange: undefined,
  treeSelect: undefined,
  // 可以继续添加其他字段
})
const formRef = ref()

// 属下拉调用示例
// const treeData = [
//   { id: 1, label: '一级 1', children: [{ id: 4, label: '二级 1-1' }] },
//   { id: 2, label: '一级 2', children: [{ id: 5, label: '二级 2-1' }] },
//   { id: 3, label: '一级 3', children: [{ id: 6, label: '二级 3-1' }] }
// ]

// 定义所有搜索字段
const allFields = [
  { label: '名称', prop: 'name' },
  { label: '机构代码', prop: 'code' },
  { label: '联系人', prop: 'contacts' },
  { label: '电话', prop: 'phone' },
  { label: '地址', prop: 'address' },
  // 调用示例
  // { label: '下拉菜单', prop: 'address2', component: 'el-select', options: [{ label: '选项1', value: '1' }, { label: '选项2', value: '2' }] },
  // { label: '日期选择', prop: 'date', component: 'el-date-picker', props: { type: 'date', format: 'YYYY-MM-DD', valueFormat: 'YYYY-MM-DD' } },
  // { label: '日期范围选择', prop: 'dateRange', component: 'el-date-picker', props: { type: 'daterange', rangeSeparator: '-', startPlaceholder: '开始日期', endPlaceholder: '结束日期', format: 'YYYY-MM-DD', valueFormat: 'YYYY-MM-DD' } },
  // { label: '树形选择', prop: 'treeSelect', component: 'el-tree-select', treeData: treeData, treeProps: { label: 'label', children: 'children' }, nodeKey: 'id' }
]



/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await CustomerApi.getCustomerPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = (params: any) => {
  Object.assign(queryParams, params)
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  for (const key in queryParams) {
    if (key !== 'pageNo' && key !== 'pageSize') {
      queryParams[key] = undefined
    }
  }
  queryParams.pageNo = 1
  getList()
}

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

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await CustomerApi.deleteCustomer(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    const data = await CustomerApi.exportCustomer(queryParams)
    download.excel(data, '客户信息.xls')
  } catch {}
}

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

<style scoped>
/* 自定义样式可以根据需要添加 */
</style>
