<template>
  <div>
    <el-form :model="searchForm" inline label-width="0">
      <el-form-item>
        <el-input v-model="searchForm.placeName" placeholder="考场名称" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery">搜 索</el-button>
        <el-button v-hasPermi="['ExamVenue:Room:add']" @click="handleDetail(null)">新 增</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="tableList" border stripe>
      <el-table-column type="index" width="50" />
      <el-table-column prop="placeName" label="考点名称" min-width="200px" />
      <el-table-column prop="leader" label="负责人" width="100px" />
      <el-table-column prop="leaderPhone" label="负责人电话" width="100px" />
      <el-table-column prop="contacts" label="联系人" width="100px" />
      <el-table-column prop="contactPhone" label="联系人电话" width="100px" />
      <el-table-column prop="address" label="地址" min-width="200px" />
      <el-table-column label="理论" align="center">
        <el-table-column label="时间" width="100px" align="center">
          <template #default="{ row }">
            {{
              row.theoryExamDate
                .split(',')
                .map((item) => `每月${item}号`)
                .join(',')
            }}
          </template>
        </el-table-column>
        <el-table-column prop="theoryExamAddress" label="地址" min-width="200px" align="center" />
      </el-table-column>
      <el-table-column label="实践" align="center">
        <el-table-column label="时间" width="100px" align="center">
          <template #default="{ row }">
            {{
              row.practiceExamDate
                .split(',')
                .map((item) => `每月${item}号`)
                .join(',')
            }}
          </template>
        </el-table-column>
        <el-table-column prop="practiceExamAddress" label="地址" min-width="200px" align="center" />
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="150">
        <template #default="{ row }">
          <el-button
            v-hasPermi="['ExamVenue:Room:edit']"
            type="primary"
            link
            @click="handleDetail(row)"
          >
            修改
          </el-button>
          <el-button
            v-hasPermi="['ExamVenue:Room:delete']"
            type="danger"
            link
            @click="handleDelete(row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      :total="total"
      v-model:page="searchForm.pageNo"
      v-model:limit="searchForm.pageSize"
      @pagination="getList"
    />

    <Dialog v-model="dialogVisible" title="教员详情" style="width: 800px">
      <el-form :model="form" ref="formRef" :rules="rules" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="24" :offset="0">
            <el-form-item label="考点名称" prop="placeName">
              <el-input v-model="form.placeName" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="12" :offset="0">
            <el-form-item label="负责人" prop="leader">
              <el-input v-model="form.leader" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="12" :offset="0">
            <el-form-item label="负责人电话" prop="leaderPhone">
              <el-input v-model="form.leaderPhone" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="12" :offset="0">
            <el-form-item label="联系人" prop="contacts">
              <el-input v-model="form.contacts" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="12" :offset="0">
            <el-form-item label="联系人电话" prop="contactPhone">
              <el-input v-model="form.contactPhone" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="24" :offset="0">
            <el-form-item label="地址" prop="address">
              <el-input
                v-model="form.address"
                type="textarea"
                :autosize="{ minRows: 2 }"
                placeholder="请输入"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-divider direction="horizontal" content-position="left">理论</el-divider>
        <el-row :gutter="20">
          <el-col :span="24" :offset="0">
            <el-form-item label="理论地址" prop="theoryExamAddress">
              <el-input
                v-model="form.theoryExamAddress"
                type="textarea"
                :autosize="{ minRows: 2 }"
                placeholder="请输入"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24" :offset="0">
            <el-form-item label="理论时间">
              <el-input
                class="mr-2 mb-2"
                v-for="(item, index) in form.theoryExamDate"
                :key="index"
                v-model="form.theoryExamDate[index]"
                style="width: 180px"
              >
                <template #prepend>每月</template>
                <template #append>日</template>
              </el-input>
              <el-button type="primary" @click="form.theoryExamDate.push(undefined)">
                新增考试时间
              </el-button>
            </el-form-item>
          </el-col>
        </el-row>
        <el-divider direction="horizontal" content-position="left">实践</el-divider>
        <el-row :gutter="20">
          <el-col :span="24" :offset="0">
            <el-form-item label="实践地址" prop="theoryExamAddress">
              <el-input
                v-model="form.practiceExamAddress"
                type="textarea"
                :autosize="{ minRows: 2 }"
                placeholder="请输入"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24" :offset="0">
            <el-form-item label="实践时间">
              <el-input
                class="mr-2 mb-2"
                v-for="(item, index) in form.practiceExamDate"
                :key="index"
                v-model="form.practiceExamDate[index]"
                style="width: 180px"
              >
                <template #prepend>每月</template>
                <template #append>日</template>
              </el-input>
              <el-button type="primary" @click="form.practiceExamDate.push(undefined)">
                新增考试时间
              </el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <el-button type="primary" :disabled="formLoading" @click="submitForm">保 存</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </template>
    </Dialog>
  </div>
</template>

<script setup name="ExamVenue">
import * as api from '@/api/uav/testRoom'

const message = useMessage()

const searchForm = ref({
  placeName: '',
  pageNo: 1,
  pageSize: 20
})

const tableList = ref([])
const total = ref(0)

onMounted(() => {
  handleQuery()
})

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

function getList() {
  try {
    api.searchTestroom(searchForm.value).then((res) => {
      tableList.value = res.list
      total.value = res.total
    })
  } catch (error) {
    console.log(error)
  }
}

const form = ref({})
const rules = {
  placeName: { required: true, message: '请输入名称', trigger: 'blur' },
  leader: { required: true, message: '请输入姓名', trigger: 'blur' },
  leaderPhone: { required: true, message: '请输入手机号', trigger: 'blur' }
}
const dialogVisible = ref(false)
const formLoading = ref(false)

const handleDetail = (row) => {
  resetForm()
  dialogVisible.value = true
  if (row) {
    form.value = { ...row }
    form.value.theoryExamDate = row.theoryExamDate.split(',')
    form.value.practiceExamDate = row.practiceExamDate.split(',')
  }
}

const formRef = ref(null)
function resetForm() {
  form.value = {
    id: undefined,
    placeName: '',
    leader: '',
    leaderPhone: '',
    contacts: '',
    contactPhone: '',
    address: '',
    theoryExamAddress: '',
    theoryExamDate: [],
    practiceExamAddress: '',
    practiceExamDate: []
  }
  formRef.value && formRef.value.resetFields()
}

const handleDelete = async (row) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await api.deleteTestroom({ id: row.placeId })
    message.success('删除成功')
    // 刷新列表
    getList()
  } catch {}
}

const submitForm = async () => {
  if (!formRef.value) return
  const valid = await formRef.value.validate()
  if (!valid) return

  try {
    formLoading.value = true
    let params = { ...form.value }
    params.theoryExamDate = params.theoryExamDate.join(',')
    params.practiceExamDate = params.practiceExamDate.join(',')
    if (form.value.placeId) {
      api.updateTestroom(params).then((response) => {
        if (response) {
          message.success('修改成功')
          dialogVisible.value = false
        }
      })
    } else {
      api.addTestroom(params).then((response) => {
        if (response) {
          message.success('新增成功')
          dialogVisible.value = false
        }
      })
    }
  } finally {
    formLoading.value = false
    getList()
  }
}
</script>

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