<template>
  <div>
    <el-form :model="searchForm" inline label-width="0">
      <el-form-item>
        <el-input v-model="searchForm.name" 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="name" label="考点名称" min-width="200px" />
      <el-table-column prop="username" label="负责人" width="100px" />
      <el-table-column prop="userPhone" label="负责人电话" width="100px" />
      <el-table-column prop="contactName" 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 prop="lilunTime" label="时间" width="100px" align="center">
          <template #default="{ row }">
            {{ row.lilunTime.map((item) => `每月${item}号`).join(',') }}
          </template>
        </el-table-column>
        <el-table-column prop="lilunAddress" label="地址" min-width="200px" align="center" />
      </el-table-column>
      <el-table-column label="实践" align="center">
        <el-table-column prop="shijianTime" label="时间" width="100px" align="center">
          <template #default="{ row }">
            {{ row.shijianTime.map((item) => `每月${item}号`).join(',') }}
          </template>
        </el-table-column>
        <el-table-column prop="shijianAddress" 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="name">
              <el-input v-model="form.name" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="12" :offset="0">
            <el-form-item label="负责人" prop="username">
              <el-input v-model="form.username" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="12" :offset="0">
            <el-form-item label="负责人电话" prop="userPhone">
              <el-input v-model="form.userPhone" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="12" :offset="0">
            <el-form-item label="联系人" prop="contactName">
              <el-input v-model="form.contactName" 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="lilunAddress">
              <el-input
                v-model="form.lilunAddress"
                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.lilunTime"
                :key="index"
                v-model="form.lilunTime[index]"
                style="width: 180px"
              >
                <template #prepend>每月</template>
                <template #append>日</template>
              </el-input>
              <el-button type="primary" @click="form.lilunTime.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="lilunAddress">
              <el-input
                v-model="form.shijianAddress"
                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.shijianTime"
                :key="index"
                v-model="form.shijianTime[index]"
                style="width: 180px"
              >
                <template #prepend>每月</template>
                <template #append>日</template>
              </el-input>
              <el-button type="primary" @click="form.shijianTime.push(undefined)">
                新增考试时间
              </el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <el-button type="primary" @click="submitForm">保 存</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </template>
    </Dialog>
  </div>
</template>

<script setup name="ExamVenue">
const message = useMessage()

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

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

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

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

function getList() {
  //   getStationList(searchForm.value).then((res) => {
  //     console.log(res)
  //   })
  // 模拟数据
  tableList.value = [
    {
      id: 1,
      name: '上海01',
      address: '上海01地址',
      username: 'z张三',
      userPhone: '12345678901',
      contactName: '李四',
      contactPhone: '12345678901',
      lilunTime: [1, 10],
      shijianTime: [5, 15],
      lilunAddress: '上海01地址',
      shijianAddress: '上海01地址'
    },
    {
      id: 2,
      name: '上海02',
      address: '上海02地址',
      username: 'z张三',
      userPhone: '12345678901',
      contactName: '李四',
      contactPhone: '12345678901',
      lilunTime: [2, 22],
      shijianTime: [5, 15],
      lilunAddress: '上海02地址',
      shijianAddress: '上海02地址'
    }
  ]
  total.value = 2
}

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

const handleDetail = (row) => {
  resetForm()
  dialogVisible.value = true
  if (row) {
    form.value = { ...row }
  }
}

const formRef = ref(null)
function resetForm() {
  form.value = {
    id: undefined,
    name: '',
    username: '',
    userPhone: '',
    contactName: '',
    contactPhone: '',
    address: '',
    lilunAddress: '',
    lilunTime: [],
    shijianAddress: '',
    shijianTime: []
  }
  formRef.value && formRef.value.resetFields()
}

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

const submitForm = async () => {
  try {
  } catch (err) {
    console.log(err)
  }
}
</script>

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