<template>
  <div>
    <el-form :model="searchForm" inline @submit.prevent>
      <el-form-item>
        <el-input
          v-model="searchForm.userName"
          placeholder="请输入姓名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="searchForm.phone"
          placeholder="请输入手机号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="searchForm.idcard"
          placeholder="请输入身份证号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-select
          v-model="searchForm.carTypeId"
          placeholder="选择考试类型"
          clearable
          filterable
          @change="handleQuery"
        >
          <el-option
            v-for="item in carTypeOptions"
            :key="item.carTypeId"
            :label="item.carName"
            :value="item.carTypeId"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery" v-hasPermi="['customer:customer:search']">搜索</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['customer:customer:add']"
        >
          新增
        </el-button>
        <el-upload
          style="margin-left: 10px"
          ref="studentFile"
          action="#"
          :limit="1"
          accept=".xls,.xlsx"
          :show-file-list="false"
          :before-upload="fileBeforeUpload"
          :http-request="handleImport"
          v-hasPermi="['customer:customer:import']"
        >
          <el-button type="primary" plain>导入</el-button>
        </el-upload>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="tableList" border stripe>
      <el-table-column type="index" width="50" />
      <el-table-column label="姓名" prop="userName" />
      <el-table-column label="性别" prop="sex">
        <template #default="scope">
          <span v-if="scope.row.sex == 1">男</span>
          <span v-else>女</span>
        </template>
      </el-table-column>
      <el-table-column label="手机号码" prop="phone" width="120" />
      <el-table-column label="身份证号" prop="idcard" min-width="120" />
      <el-table-column label="考试类型" prop="carTypeName" min-width="120" />
      <el-table-column label="操作" width="260" fixed="right">
        <template #default="scope">
          <el-button
            type="primary"
            link
            @click="openForm('update', scope.row)"
            v-hasPermi="['customer:customer:update']"
          >
            修改
          </el-button>
          <el-button
            type="primary"
            link
            @click="handleDelete(scope.row.userId)"
            v-hasPermi="['customer:customer:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      :total="total"
      v-model:page="searchForm.pageNo"
      v-model:limit="searchForm.pageSize"
      @pagination="getList"
    />
    <DialogCustomer ref="formRef" @success="getList" />
  </div>
</template>

<script name="CustomerList" setup>
import DialogCustomer from './Comp/DialogCustomer.vue'
import { removeNullField } from '@/utils'
import * as CustomerApi from '@/api/customer/customer.js'

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

const carTypeOptions = ref([])

const searchForm = ref({
  userName: undefined,
  carTypeId: 1011,
  pageNo: 1,
  pageSize: 20
})

onMounted(() => {
  CustomerApi.getCustomerExamCarType().then((res) => {
    carTypeOptions.value = res
  })
  handleQuery()
})

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

const loading = ref(false)
const tableList = ref([])
const total = ref(0)
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await CustomerApi.getCustomerPage(removeNullField(searchForm.value))
    tableList.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type, info = undefined) => {
  formRef.value.open(type, info)
}

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

const fileBeforeUpload = (file) => {
  let format = '.' + file.name.split('.')[1]
  if (!['.xls', '.xlsx'].includes(format)) {
    message.error(`请上传指定格式".xls,.xlsx"文件`)
    return false
  }
  let isRightSize = file.size / 1024 / 1024 < 20
  if (!isRightSize) {
    message.error('文件大小超过 20MB')
  }
  return isRightSize
}

const uploading = ref(false)
async function handleImport(data) {
  uploading.value = true
  const fd = new FormData()
  fd.append('file', data.file)
  try {
    await CustomerApi.importStudent(fd)
    message.success('导入成功')
    handleQuery()
  } finally {
    uploading.value = false
  }
}
</script>

<style lang="scss" scoped></style>
