<template>
  <div>
    <el-form :model="searchForm" inline @submit.prevent>
      <el-form-item>
        <el-input v-model="searchForm.name" placeholder="输入姓名" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-input v-model="searchForm.phone" placeholder="输入手机号" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="searchForm.city"
          placeholder="输入城市或地址"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery"> 搜索 </el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="tableList"
      highlight-current-row
      max-height="calc(100vh - 320px)"
    >
      <el-table-column type="index" width="55" align="center" />
      <el-table-column label="姓名" prop="name" />
      <el-table-column label="手机号" prop="phone" />
      <el-table-column label="城市" prop="city" />
      <el-table-column label="意向机构" prop="likeOrg" />
      <el-table-column label="咨询类型" prop="cartype" />
      <el-table-column label="咨询日期" prop="createTime" />
      <el-table-column label="诉求" prop="need" />
      <el-table-column label="备注" prop="remark" />
      <el-table-column label="报名状态" prop="status">
        <template #default="{ row }">
          <el-tag v-if="row.status == 1" type="primary">未报名</el-tag>
          <el-tag v-else-if="row.status == 2" type="success">报名成功</el-tag>
          <el-tag v-else-if="row.status == 3" type="info">已失效</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="绑定机构" prop="bindSchoolName" />

      <el-table-column label="操作" align="center" width="140">
        <template #default="scope">
          <el-button
            type="primary"
            link
            @click="handleEdit(scope.row)"
            v-hasPermi="['UAVClue:CluePool:edit']"
          >
            修改
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      style="margin-bottom: 0"
      v-show="total > 0"
      :total="total"
      v-model:page="searchForm.pageNo"
      v-model:limit="searchForm.pageSize"
      @pagination="getList"
    />

    <Dialog v-model="showDialog" title="修改线索" style="width: 800px">
      <el-form ref="dialogFormRef" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="姓名" prop="name">
              <el-input v-model="form.name" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号" prop="phone">
              <el-input v-model="form.phone" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="城市" prop="city">
              <el-input v-model="form.city" placeholder="请输入城市或地址" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="意向机构" prop="likeOrg">
              <el-select
                v-model="form.likeOrg"
                placeholder="选择意向机构"
                clearable
                filterable
                @change="getCartypeOptions"
              >
                <el-option
                  v-for="item in orgOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12" :offset="0">
            <el-form-item label="咨询类型" prop="carTypeId">
              <el-radio-group v-model="form.carTypeId">
                <el-radio label="10001" value="10001">多旋翼</el-radio>
                <el-radio label="10002" value="10002">垂直起降固定翼</el-radio>
                <el-radio label="10003" value="10003">直升机</el-radio>
                <el-radio label="10004" value="10004">固定翼</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12" :offset="0">
            <el-form-item label="驾驶员类型" prop="driveType">
              <el-radio-group v-model="form.driveType">
                <el-radio label="1" value="1">视距内驾驶员</el-radio>
                <el-radio label="2" value="2">超视距驾驶员</el-radio>
                <el-radio label="3" value="3">教员</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24" :offset="0">
            <el-form-item label="诉求" prop="need">
              <el-input type="textarea" v-model="form.need" placeholder="请输入诉求" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12" :offset="0">
            <el-form-item label="报名状态" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio :label="1" :value="1">未报名</el-radio>
                <el-radio :label="2" :value="2">已报名</el-radio>
                <el-radio :label="3" :value="3">已失效</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12" :offset="0" v-if="form.status == 2">
            <el-form-item label="绑定机构" prop="bindOrgId">
              <el-select v-model="form.bindOrgId" placeholder="选择意向机构" clearable filterable>
                <el-option
                  v-for="item in orgOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24" :offset="0">
            <el-form-item label="备注" prop="remark">
              <el-input
                type="textarea"
                v-model="form.remark"
                placeholder="请输入备注"
                :autosize="{ minRows: 5, maxRows: 10 }"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="showDialog = false">取 消</el-button>
      </template>
    </Dialog>
  </div>
</template>

<script setup name="CluePool">
const loading = ref(false)
const total = ref(0)
const tableList = ref([])
const searchForm = ref({
  name: '',
  phone: '',
  city: '',
  pageNo: 1,
  pageSize: 100
})

onMounted(() => {
  handleQuery()
  orgOptions.value = [
    { label: '银湖低空经济发展有限公司', value: '1' },
    { label: '中科未来飞行科技有限公司', value: '2' }
  ]
})

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

function getList() {
  loading.value = true
  tableList.value = [
    {
      name: '张三',
      phone: '12345678901',
      city: '上海',
      status: 1
    },
    {
      name: '张三1',
      phone: '12345678901',
      city: '合肥',
      status: 2
    }
  ]
  total.value = 0
  loading.value = false
}

const cartypeOptions = ref([])
const orgOptions = ref([])
function getCartypeOptions() {
  cartypeOptions.value = []
}

const form = ref({})
const showDialog = ref(false)
const rules = {
  name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
  phone: [{ required: true, message: '请输入手机号', trigger: 'blur' }]
}

const dialogFormRef = ref()

function handleEdit(row) {
  form.value = row
  showDialog.value = true
}

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

  message.success('修改成功')
  getList()
  showDialog.value = false
}
</script>

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