<template>
  <div>
    <el-form :model="searchForm" inline label-width="0">
      <el-form-item>
        <el-input v-model="searchForm.schoolName" placeholder="机构名称" clearable />
      </el-form-item>
      <el-form-item>
        <el-select v-model="searchForm.status" clearable filterable placeholder="审核状态">
          <el-option label="待审核" :value="1" />
          <el-option label="已通过" :value="2" />
          <el-option label="未通过" :value="3" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery"> 搜索 </el-button>
      </el-form-item>
    </el-form>

    <el-table :data="tableList" border stripe>
      <el-table-column type="index" width="50" />
      <el-table-column prop="schoolName" label="机构名称" />
      <el-table-column prop="contacts" label="联系人" />
      <el-table-column prop="phone" label="联系方式" />
      <el-table-column prop="createTime" label="申请时间" />
      <el-table-column prop="status" label="审核状态" width="100">
        <template #default="{ row }">
          <el-tag v-if="row.status == 1" type="info">待审核</el-tag>
          <el-tag v-else-if="row.status == 2" type="success">已通过</el-tag>
          <el-tag v-else type="danger">未通过</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="checkUser" label="审核人" />
      <el-table-column prop="checkTime" label="审核时间" />
      <!-- 操作 -->
      <el-table-column label="操作" fixed="right" width="150">
        <template #default="{ row }">
          <el-button
            type="primary"
            link
            v-hasPermi="['JoinUs:Station:audit']"
            @click="handleDetail(row, 'edit')"
          >
            修改
          </el-button>
          <el-button
            v-if="row.status == 1"
            type="primary"
            link
            v-hasPermi="['JoinUs:Station:audit']"
            @click="handleDetail(row, 'audit')"
          >
            审核
          </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: 1000px">
      <el-form :model="form" ref="formRef" :rules="rules" label-position="top">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="机构名称" prop="schoolName">
              <el-input v-model="form.schoolName" placeholder="请输入机构名称" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="联系人" prop="contacts">
              <el-input v-model="form.contacts" placeholder="请输入姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="联系方式" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入电话" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="培训机构宣传文案" prop="introduction">
              <el-input
                v-model="form.introduction"
                type="textarea"
                :autosize="{ minRows: 2 }"
                placeholder="请输入"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="培训机构地址" prop="address">
              <el-input
                v-model="form.address"
                type="textarea"
                :autosize="{ minRows: 2 }"
                placeholder="请输入"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12" v-for="(it, index) in form.modelList" :key="index">
            <el-form-item :label="`培训项目-${it.modelName}`" prop="dxy">
              <!-- <el-checkbox-group> -->
              <el-checkbox
                v-for="item in it.schoolTypeList"
                v-model="item.checked"
                :checked="item.checked"
                :key="item.schoolTypeId"
                :value="item.schoolTypeId"
              >
                {{ item.typeName }}
              </el-checkbox>
              <!-- </el-checkbox-group> -->
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="培训项目-垂直起降" prop="czqj">
              <el-checkbox-group v-model="form.czqj">
                <el-checkbox
                  v-for="item in driverOptions"
                  :key="item.value"
                  :label="item.value"
                  :value="item.value"
                >
                  {{ item.label }}
                </el-checkbox>
              </el-checkbox-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="培训项目-直升机" prop="zsj">
              <el-checkbox-group v-model="form.zsj">
                <el-checkbox
                  v-for="item in driverOptions"
                  :key="item.value"
                  :label="item.value"
                  :value="item.value"
                >
                  {{ item.label }}
                </el-checkbox>
              </el-checkbox-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="培训项目-固定翼" prop="gdy">
              <el-checkbox-group v-model="form.gdy">
                <el-checkbox
                  v-for="item in driverOptions"
                  :key="item.value"
                  :label="item.value"
                  :value="item.value"
                >
                  {{ item.label }}
                </el-checkbox>
              </el-checkbox-group>
            </el-form-item>
          </el-col> -->
          <el-col :span="8">
            <el-form-item label="合格证图片" prop="certs">
              <UploadImgs v-model="form.certs" height="100px" width="100px" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="培训机构Logo" prop="logo">
              <UploadImg v-model="form.logo" height="100px" width="100px" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="主页宣传图" prop="imgUrl">
              <UploadImgs v-model="form.imgUrl" :limit="6" height="100px" width="100px" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="优势标签">
              <el-checkbox
                v-for="item in form.schoolTagList"
                v-model="item.checked"
                :checked="item.checked"
                :key="item.id"
                :value="item.id"
              >
                {{ item.tag }}
              </el-checkbox>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="其他服务" prop="schoolServiceList">
              <el-checkbox
                v-for="item in form.schoolServiceList"
                v-model="item.checked"
                :checked="item.checked"
                :key="item.id"
                :value="item.id"
              >
                {{ item.serviceName }}
              </el-checkbox>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="其他诉求" prop="otherRequirement">
              <el-input
                v-model="form.otherRequirement"
                type="textarea"
                :autosize="{ minRows: 2 }"
                placeholder="请输入"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <!-- <div v-if="dialogType == 'audit'"> -->
        <el-row>
          <el-divider direction="horizontal" content-position="left">审核信息</el-divider>
          <el-col :span="6" :offset="0">
            <el-form-item label="审核结果" prop="status">
              <el-radio-group v-model="form.status">
                <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="18" :offset="0">
            <el-form-item label="审核说明" prop="checkRemark">
              <el-input v-model="form.checkRemark" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
        <!-- </div> -->
      </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="Station">
import {
  searchStation,
  // auditStation,
  updateStation,
  stationDetail,
  getCarTypeList
} from '@/api/uav/regiest'

const message = useMessage()

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

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

const form = ref({})
const rules = {
  schoolName: { required: true, message: '请输入名称', trigger: 'blur' },
  contacts: { required: true, message: '请输入创建人', trigger: 'blur' },
  phone: { required: true, message: '请输入手机号', trigger: 'blur' }
}
const dialogVisible = ref(false)
const dialogType = ref('audit')
const driverOptions = ref([])

const defaultTips = [
  { tag: '服务好', isDefault: true, checked: false },
  { tag: '交通便利', isDefault: true, checked: false },
  { tag: '提供住宿', isDefault: true, checked: false },
  { tag: '免费接送', isDefault: true, checked: false }
]
const defaultService = [
  { serviceName: '希望平台主动推荐学员', isDefault: true, checked: false },
  { serviceName: '接受团购学员报名', isDefault: true, checked: false },
  { serviceName: '希望平台推荐教员资源', isDefault: true, checked: false },
  { serviceName: '希望平台推荐无人机相关的设备', isDefault: true, checked: false }
]

onMounted(() => {
  handleQuery()
  getCarTypeList().then((response) => {
    driverOptions.value = response
  })
})

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

function getList() {
  searchStation(searchForm.value).then((response) => {
    tableList.value = response.list
    total.value = response.total
  })
}

function handleDetail(row, type) {
  dialogType.value = type
  stationDetail(row.schoolId).then((response) => {
    dialogVisible.value = true
    let arr = []
    response.modelGroupVoList?.map((model) => {
      model.schoolModelVoList?.map((item) => {
        arr.push(item.schoolTypeId)
      })
    })
    const tags = [
      ...(response.schoolTagList?.map((it) => ({ ...it, checked: true })) || []),
      ...defaultTips
    ]
    const services = [
      ...(response.schoolServiceList?.map((it) => ({ ...it, checked: true })) || []),
      ...defaultService
    ]
    form.value = {
      ...response,
      imgUrl: response.imgUrl || [],
      certs: response.certs || [],
      modelList: driverOptions.value.map((item) => {
        return {
          modelId: item.modelId,
          modelName: item.modelName,
          schoolTypeList: item.schoolDriverTypeVoList.map((type) => {
            return {
              ...type,
              checked: arr.includes(type.schoolTypeId)
            }
          })
        }
      }),
      schoolTagList: uniqueByKey(tags, 'tag'),
      schoolServiceList: uniqueByKey(services, 'serviceName')
    }
  })
}

function uniqueByKey(arr, key) {
  const map = new Map()
  return arr.filter((item) => {
    // 如果 map 中没有该 key，则保留并加入 map
    if (!map.has(item[key])) {
      map.set(item[key], true)
      return true
    }
    return false
  })
}

const formRef = ref(null)
const formLoading = ref(false)
async function submitForm() {
  if (!formRef.value) return
  const valid = await formRef.value.validate()
  if (!valid) return

  try {
    formLoading.value = true

    let params = { ...form.value }
    params.modelGroupVoList = params.modelList
      .map((item) => {
        return {
          modelId: item.modelId,
          modelName: item.modelName,
          schoolModelVoList: item.schoolTypeList.filter((type) => type.checked)
        }
      })
      .filter((item) => item.schoolModelVoList.length > 0)
    params.schoolServiceList = params.schoolServiceList.filter((item) => item.checked)
    params.schoolTagList = params.schoolTagList.filter((item) => item.checked)
    // if (dialogType.value == 'audit') {
    //   auditStation(params).then((response) => {
    //     if (response) {
    //       dialogVisible.value = false
    //       message.success('操作成功')
    //     }
    //   })
    // } else {
    updateStation(params).then((response) => {
      if (response) {
        dialogVisible.value = false
        message.success('操作成功')
      }
    })
    // }
  } finally {
    formLoading.value = false
    getList()
  }
}
</script>

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