<template>
  <div>
    <el-form :model="searchForm" ref="searchRef" inline label-width="0">
      <el-form-item>
        <el-input
          v-model="searchForm.schoolName"
          placeholder="请输入驾校名称"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="searchForm.placeName"
          placeholder="请输入场地名称"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="searchForm.className"
          placeholder="请输入班型名称"
          @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.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button plain @click="handleQuery">查询</el-button>
        <el-button plain @click="handleReset">重置</el-button>
        <el-button type="primary" plain @click="handleOpenDialog('create')">新增</el-button>
        <el-button type="danger" @click="handleBatchDelete">批量删除</el-button>
        <el-button type="warning" @click="handleBatchStatus">批量启/停用</el-button>
      </el-form-item>
    </el-form>

    <el-table
      :data="tableList"
      v-loading="loading"
      border
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="50" />
      <el-table-column type="index" label="序号" width="60" />
      <el-table-column
        v-for="col in columns"
        :prop="col.props"
        :key="col.props"
        :label="col.label"
        :width="col.width"
        show-overflow-tooltip
      />
      <el-table-column label="状态" width="80">
        <template #default="{ row }">
          <el-switch
            v-model="row.status"
            :active-value="1"
            :inactive-value="0"
            @change="handleChangeStatus(row)"
          />
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200px">
        <template #default="{ row }">
          <el-button type="primary" text @click="handleOpenDialog('update', row)">修改</el-button>
          <el-button type="danger" text @click="handleRemove(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      v-model:limit="searchForm.pageSize"
      v-model:page="searchForm.pageNum"
      :total="total"
      @pagination="getList"
    />
  </div>
</template>

<script setup name="Class">
const message = useMessage() // 消息弹窗

const loading = ref(false)
const total = ref(0)

const tableList = ref([])
// const schoolDialog = ref()

const searchForm = ref({
  schoolName: undefined,
  placeName: undefined,
  className: undefined,
  cartypeId: undefined,
  pageNum: 1,
  pageSize: 20
})

const cartypeOptions = ref([])

const columns = [
  { props: 'schoolName', label: '驾校', width: '100px' },
  { props: 'placeName', label: '场地' },
  { props: 'className', label: '班型名称' },
  { props: 'cartypeName', label: '驾照类型', width: '100px' },
  { props: '', label: '最新报价', width: '100px' },
  { props: '', label: '最新底价', width: '100px' },
  { props: 'remark', label: '备注', width: '100px' }
]

function handleQuery() {
  searchForm.value.pageNum = 1
  getList()
}

function getList() {
  tableList.value = [
    { schoolName: '测试驾校', placeName: '测试场地', className: '测试', status: 1 }
  ]
}

const ClassDialog = ref()
function handleOpenDialog(type, row = null) {
  ClassDialog.value.open(type, row)
}

function handleReset() {
  searchForm.value = {
    schoolName: undefined,
    placeName: undefined,
    className: undefined,
    cartypeId: undefined,
    pageSize: 20,
    pageNum: 1
  }
  getList()
}

async function handleBatchDelete(arr = []) {
  if (!arr.length || !selectRows.value.length) {
    message.info('请选择表格行！')
  }
  try {
    console.log(arr)
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    // await UserApi.deleteUser(row.id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

const selectRows = ref([])
function handleSelectionChange(selection) {
  selectRows.value = selection.map((item) => item.classId)
}

function handleRemove(row) {
  handleBatchDelete([row])
}

async function handleChangeStatus(row) {
  try {
    console.log(row)
    // 删除的二次确认
    await message.confirm('是否确认修改状态')
    // 发起删除
    // await UserApi.deleteUser(row.id)
    message.success('修改成功')
    // 刷新列表
    await getList()
  } catch {}
}

function handleBatchStatus() {
  if (!selectRows.value.length) {
    message.info('请选择表格行！')
  }
}
</script>

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