<template>
  <custom-layout class="main">
    <div class="two">
      <div class="w_1300">
        <custom-search :cityList="cityList" :plateList="plateList" :fields="fields" :searchFields="searchFields" @toSearchInfo="toSearchInfo"></custom-search>
      </div>
    </div>
    <el-col :span="24" class="thr">
      <div class="w_1300">
        <el-row :gutter="16">
          <el-col :span="6" v-for="(item, index) in list" :key="index" @click="toView(item)">
            <div class="list">
              <el-image class="image" :src="getUrl(item.file)" fit="cover"> </el-image>
              <el-col :span="24" class="name textOne">
                <el-tooltip effect="dark" :content="item.name" placement="top">
                  {{ item.name || '暂无名称' }}
                </el-tooltip>
              </el-col>
              <el-col :span="24" class="other_1 textOne">
                <span>建设主体：</span>
                {{ item.build || '暂无建设主体' }}
              </el-col>
              <el-col :span="24" class="other_1 textOne">
                <span>运营主体：</span>
                {{ item.operate || '暂无运营主体' }}
              </el-col>
              <el-col :span="24" class="other_1 textOne">
                <span>服务产业领域：</span>
                {{ item.field || '暂无服务产业领域' }}
              </el-col>
              <el-col :span="24" class="other_1 textOne">
                <span>地址：</span>
                {{ item.address || '暂无地址' }}
              </el-col>
              <el-col :span="24" class="other_1 textOne">
                <span>联系人：</span>
                {{ item.contacts || '暂无联系人' }}
              </el-col>
              <el-col :span="24" class="other_1 textOne">
                <span>联系电话：</span>
                {{ item.phone || '暂无联系电话' }}
              </el-col>
            </div>
          </el-col>
        </el-row>
      </div>
    </el-col>
    <el-col :span="24" class="four">
      <el-pagination background layout="prev, pager, next" :total="total" :page-size="limit" v-model:current-page="currentPage" @current-change="changePage" @size-change="sizeChange" />
    </el-col>
  </custom-layout>
</template>

<script setup>
// 接口
import { FootplateStore } from '@/store/api/platform/footplate'
import { RegionStore } from '@/store/api/system/region'
import { SectorStore } from '@/store/api/platform/sector'
const store = FootplateStore()
const regionStore = RegionStore()
const sectorStore = SectorStore()
// 加载中
const loading = ref(false)
// 路由
const router = useRouter()
const cityList = ref([])
const plateList = ref([])
// 列表
const searchForm = ref({})
const list = ref([])
let skip = 0
let limit = inject('limit')
const total = ref(6)

const fields = ref([
  { model: 'name', label: '中试平台/实验室名称' },
  { model: 'build', label: '建设主体' },
  { model: 'operate', label: '运营主体' },
  { model: 'field', label: '服务产业领域' }
])
const searchFields = ref([
  { title: '行业', type: '1', list: plateList },
  { title: '所在地', type: '3', list: cityList }
])
// 请求
onMounted(async () => {
  loading.value = true
  await searchOther()
  await search({ skip, limit })
  loading.value = false
})
const searchOther = async () => {
  let res
  res = await regionStore.list({ level: 'city', parent_code: 22 })
  if (res.errcode == '0') cityList.value = res.data
  cityList.value.unshift({ id: '-1', code: '-1', name: '不限', is_active: true })
  res = await sectorStore.query({ is_use: '0' })
  if (res.errcode == '0') plateList.value = res.data
  plateList.value.unshift({ id: '-1', title: '不限', is_active: true })
}
const search = async (query = { skip, limit }) => {
  skip = query.skip
  limit = query.limit
  const info = { skip: query.skip, limit: query.limit, is_use: '0', status: '1', ...searchForm.value }
  const res = await store.list(info)
  if (res.errcode == '0') {
    list.value = res.data
    total.value = res.total
  }
}
// 搜索
const toSearchInfo = async (data) => {
  searchForm.value = data
  await search({ skip, limit })
}
// 查看
const toView = (item) => {
  router.push({ path: '/platform/detail', query: { id: item.id || item._id } })
}
const currentPage = ref(1)
// 分页
const changePage = (page = currentPage.value) => {
  search({ skip: (page - 1) * limit, limit: limit })
}
const sizeChange = (limits) => {
  limit = limits
  currentPage.value = 1
  search({ skip: 0, limit: limit })
}
const getUrl = (item) => {
  if (item && item.length > 0) return `${import.meta.env.VITE_APP_HOST}${item[0].uri}`
}
</script>
<style scoped lang="scss">
.main {
  .one {
    .image {
      width: 100%;
      height: 350px;
    }
  }

  .two {
    margin: 10px 0;
    background-color: $global-color-fff;
  }
  .thr {
    .list {
      padding: 15px;
      border: 1px solid #f2f4f6;
      border-radius: 8px;
      margin: 0 0 15px 0;
      .image {
        width: 100%;
        height: 190px;
      }
      .name {
        font-size: $global-font-size-20;
        font-weight: bold;
        display: inline-block;
        margin: 8px 0;
      }

      .name:hover {
        color: #2374ff;
        cursor: pointer;
      }

      .other_1 {
        font-size: $global-font-size-18;
        margin: 0 0 5px 0;
        cursor: default;
        span {
          color: #666;
        }
      }
    }
  }
  .four {
    display: flex;
    justify-content: center;
    margin: 20px 0;
  }
}
</style>
