<template>
  <div class="main">
    <div class="w_1300">
      <el-col :span="24" class="two">
        <div class="Seacher">
          <div class="Left">
            <span>行业</span>
          </div>
          <div v-if="!oneShow" class="Right">
            <div class="label" :class="[item.is_active ? 'show' : '']" v-for="(item, index) in plateList.slice(0, 6)" :key="index" @click="toSelect(item, '1')">
              {{ item.title }}
            </div>
          </div>
          <div v-else class="Right">
            <div class="label" :class="[item.is_active ? 'show' : '']" v-for="(item, index) in plateList" :key="index" @click="toSelect(item, '1')">
              {{ item.title }}
            </div>
          </div>
          <div class="button">
            <span v-if="!oneShow" @click="oneShow = true">
              <el-icon><ArrowDown /></el-icon>
            </span>
            <span v-else @click="oneShow = false">
              <el-icon><ArrowUp /></el-icon>
            </span>
          </div>
        </div>
        <div class="Seacher">
          <div class="Left">
            <span>企业类型</span>
          </div>
          <div class="Right">
            <div class="label" :class="[item.is_active ? 'show' : '']" v-for="(item, index) in patternList" :key="index" @click="toSelect(item, '2')">
              {{ item.label }}
            </div>
          </div>
        </div>
        <div class="Seacher border">
          <div class="Left">
            <span>所在地</span>
          </div>
          <div class="Right">
            <div class="label" :class="[item.is_active ? 'show' : '']" v-for="(item, index) in cityList" :key="index" @click="toSelect(item, '3')">
              {{ item.name }}
            </div>
          </div>
        </div>
        <!-- <div class="two_ipunt">
          <el-input class="input" clearable size="large" v-model="searchForm.name" placeholder="公司名称" />
          <el-input class="input" clearable size="large" v-model="searchForm.tags" placeholder="标签名称" />
          <el-input class="input" clearable size="large" v-model="searchForm.direction" placeholder="研究方向" />
          <el-input class="input" clearable size="large" v-model="searchForm.scale" placeholder="公司规模" />
          <el-button class="button" size="large" type="primary" @click="toSearchInfo">检索</el-button>
        </div> -->
      </el-col>
      <div class="thr">
        <div class="list" :class="['list' + index]" v-for="(item, index) in list" :key="index" @click="toView(item)">
          <div class="name">{{ item.name || '暂无' }}</div>
          <div class="other">
            <div class="other_1">
              <el-image class="image" :src="getUrl(item.logo)" fit="fill">
                <template v-slot:error>
                  <el-image class="image" :src="companyLogo" fit="fill" />
                </template>
              </el-image>
            </div>
            <div class="other_2" v-if="user && user.id">
              <span class="text"><em>企业类型：</em>{{ getDict(item.pattern, 'pattern') || '暂无' }}</span>
              <span class="text"><em>员工人数：</em>{{ item.person || '暂无' }}人</span>
              <span class="text"><em>地址：</em>{{ getArea(item.area) || '暂无' }}</span>
            </div>
          </div>
        </div>
      </div>
      <div 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" />
      </div>
    </div>
  </div>
</template>

<script setup>
// 图片引入
import companyLogo from '/images/companyLogo.jpg'

const $checkRes = inject('$checkRes')
import { get } from 'lodash-es'
// 接口
import { RegionStore } from '@/store/api/system/region'
import { SectorStore } from '@/store/api/platform/sector'
import { DictDataStore } from '@/store/api/system/dictData'
import { EsStore } from '@/store/api/es'
const esStore = EsStore()
const regionStore = RegionStore()
const sectorStore = SectorStore()
const dictDataStore = DictDataStore()
// 用户信息
import { UserStore } from '@/store/user'
const userStore = UserStore()
const user = computed(() => userStore.user)
// 加载中
const loading = ref(false)

const searchValue = inject('searchValue')
// 路由
const router = useRouter()
const list = ref([])
let skip = 0
let limit = 15
const total = ref(0)
// 是否展开
const oneShow = ref(false)
// 字典表
const fieldList = ref([])
const statusList = ref([])
const patternList = ref([])
const scaleList = ref([])
const cityList = ref([])
const plateList = ref([])
// 搜索
const searchForm = ref({})
// 查询
const industry = ref([])
const pattern = ref([])
const city = ref([])
// 请求
onMounted(async () => {
  loading.value = true
  await searchOther()
  await search({ skip, limit })
  loading.value = false
})
const searchOther = async () => {
  let result
  // // 技术领域
  // result = await dictDataStore.query({ code: 'field', is_use: '0' })
  // if ($checkRes(result)) fieldList.value = result.data
  // // 企业状态
  // result = await dictDataStore.query({ code: 'companyStatus', is_use: '0' })
  // if ($checkRes(result)) statusList.value = result.data
  // 企业类型
  result = await dictDataStore.query({ code: 'companyType', is_use: '0' })
  if ($checkRes(result)) patternList.value = result.data
  patternList.value.unshift({ id: '-1', value: '-1', label: '不限', is_active: true })
  // 企业规模
  result = await dictDataStore.query({ code: 'companyScale', is_use: '0' })
  if ($checkRes(result)) scaleList.value = result.data
  // // 企业所属行业
  // result = await dictDataStore.query({ code: 'companyIndustry', is_use: '0' })
  // if ($checkRes(result)) IndustryList.value = result.data
  result = await regionStore.list({ level: 'city', parent_code: 22 })
  if ($checkRes(result)) cityList.value = result.data
  cityList.value.unshift({ id: '-1', code: '-1', name: '不限', is_active: true })
  result = await sectorStore.query({ is_use: '0' })
  if ($checkRes(result)) plateList.value = result.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, status: '1', ...searchForm.value }
  if (searchValue.value) info.keyword = searchValue.value
  const res = await esStore.Scompany(info)
  if (res.errcode == '0') {
    list.value = res.data
    total.value = res.total
  }
}
// 地区
const getArea = (data) => {
  if (data) return data.join('-')
  else return '暂无地区'
}
// 字典数据转换
const getDict = (data, model) => {
  let res
  if (model == 'field') res = fieldList.value.find((f) => f.value == data)
  else if (model == 'status') res = statusList.value.find((f) => f.value == data)
  else if (model == 'pattern') res = patternList.value.find((f) => f.value == data)
  else if (model == 'scale') res = scaleList.value.find((f) => f.value == data)
  return get(res, 'label')
}
// 搜索
const toSearchInfo = async () => {
  await search({ skip, limit })
}
// 查看
const toView = (item) => {
  if (user.value.id) {
    router.push({ path: `/company/detail`, query: { id: item.id || item._id } })
  } else ElMessage({ message: '未登录！', type: 'error' })
}
const getUrl = (item) => {
  if (item && item.length > 0) return `${import.meta.env.VITE_APP_HOST}${item[0].uri}`
}
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 toSearchFind = async () => {
  if (industry.value && industry.value.length > 0) {
    searchForm.value.industry = industry.value.map((i) => {
      return i.title
    })
  } else delete searchForm.value.industry
  if (pattern.value && pattern.value.length > 0) {
    searchForm.value.pattern = pattern.value.map((i) => {
      return i.value
    })
  } else delete searchForm.value.pattern
  if (city.value && city.value.length > 0) {
    searchForm.value.area = city.value.map((i) => {
      return i.name
    })
  } else delete searchForm.value.area

  await search({ skip, limit })
}
const toSelect = async (data, type) => {
  if (data.is_active) {
    toDel(data, type)
  } else {
    if (data.id != '-1') {
      if (type == '1') {
        for (const val of plateList.value) {
          if (data.id == val.id) val.is_active = true
          if (val.id == '-1') val.is_active = false
        }
        const res = industry.value.find((i) => i.id == data.id)
        if (!res) industry.value.push(data)
      } else if (type == '2') {
        for (const val of patternList.value) {
          if (data.id == val.id) val.is_active = true
          if (val.id == '-1') val.is_active = false
        }
        const res = pattern.value.find((i) => i.id == data.id)
        if (!res) pattern.value.push(data)
      } else {
        for (const val of cityList.value) {
          if (data.id == val.id) val.is_active = true
          if (val.id == '-1') val.is_active = false
        }
        const res = city.value.find((i) => i.id == data.id)
        if (!res) city.value.push(data)
      }
    } else {
      if (type == '1') {
        for (const val of plateList.value) {
          if (val.id == '-1') val.is_active = true
          else val.is_active = false
        }
        industry.value = []
      } else if (type == '2') {
        for (const val of patternList.value) {
          if (val.id == '-1') val.is_active = true
          else val.is_active = false
        }
        pattern.value = []
      } else {
        for (const val of cityList.value) {
          if (val.id == '-1') val.is_active = true
          else val.is_active = false
        }
        city.value = []
      }
    }
  }
  await toSearchFind()
}
const toDel = async (data, type) => {
  if (type == '1') {
    for (const val of plateList.value) {
      if (data.id == val.id) val.is_active = false
    }
    industry.value = industry.value.filter((f) => f.id != data.id)
    if (industry.value.length == 0) {
      for (const val of plateList.value) {
        if (val.id == '-1') val.is_active = true
      }
    }
  } else if (type == '2') {
    for (const val of patternList.value) {
      if (data.id == val.id) val.is_active = false
    }
    pattern.value = pattern.value.filter((f) => f.id != data.id)
    if (pattern.value.length == 0) {
      for (const val of patternList.value) {
        if (val.id == '-1') val.is_active = true
      }
    }
  } else {
    for (const val of cityList.value) {
      if (data.id == val.id) val.is_active = false
    }
    city.value = city.value.filter((f) => f.id != data.id)
    if (city.value.length == 0) {
      for (const val of cityList.value) {
        if (val.id == '-1') val.is_active = true
      }
    }
  }
  await toSearchFind()
}
defineExpose({
  search
})
</script>
<style scoped lang="scss">
.main {
  padding: 10px 0;
  .one {
    .image {
      width: 100%;
      height: 350px;
    }
  }
  .active {
    .active_1 {
      display: inline-flex;
      background: #f5f7f9;
      border-radius: 2px;
      min-height: 28px;
      line-height: 28px;
      margin: 0 10px 0 0;
      position: relative;
      background-color: #fff;
      padding: 10px;
      .active_left {
        flex: 0 0 auto;
        font-family: PingFangSC-Regular;
        color: #525a68;
        line-height: 36px;
      }
      .active_right {
        font-family: PingFangSC-Regular;
        color: rgba(0, 0, 0, 0.85);
        line-height: 28px;
        padding-right: 4px;
        display: flex;
        flex-wrap: wrap;
        overflow: hidden;
        .active_label {
          overflow: hidden;
          display: inline-block;
          margin-right: 10px;
          margin-top: 3px;
          margin-bottom: 3px;
          padding: 10px;
          display: flex;
          align-items: center;
          flex: none;
          box-sizing: border-box;
          max-width: 100%;
          height: 30px;
          background: #f5f5f5;
          border: 1px solid #f0f0f0;
          border-radius: 2px;
          cursor: default;
        }
      }
    }
  }
  .two {
    margin: 0 0 20px 0;
    .Seacher {
      display: flex;
      justify-content: center;
      align-items: stretch;
      position: relative;
      border: solid 1px #e5e5e5;
      border-bottom: 0;
      font-size: $global-font-size-18;
      color: #666;
      min-height: 60px;
      overflow: hidden;
      .Left {
        display: flex;
        justify-content: center;
        align-items: center;
        flex-shrink: 0;
        width: 110px;
        text-align: center;
        color: #000;
        font-weight: bold;
        background-color: #fafafa;
      }
      .Right {
        display: flex;
        flex-wrap: wrap;
        align-items: center;
        padding: 12px;
        flex: 1;
        border-left: solid 1px #e5e5e5;
        background-color: #fff;
        .label {
          margin-right: 3px;
          color: #313131;
          margin-bottom: 10px;
          padding: 8px 10px;
          border-radius: 3px;
          background-color: #fff;
          border: solid 1px transparent;
          cursor: pointer;
        }
        .show {
          color: #0a58c2;
          border: solid 1px #006dd2;
        }
        .label:hover {
          color: $global-color-107;
        }
      }
      .button {
        display: flex;
        align-items: center;
        margin: 0 5px 0 0;
      }
    }
    .border {
      border: solid 1px #e5e5e5;
      border-bottom: 1;
    }
    .two_ipunt {
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin: 10px 0;
      .input {
        margin: 0 5px 0 0;
      }
      .button {
        margin: 0 0 0 5px;
      }
    }
  }
  .thr {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    .list {
      float: left;
      width: 420px;
      padding: 15px;
      margin: 0 20px 25px 0;
      background: #f0f8ff;
      color: #000;
      .name {
        height: 57px;
        line-height: 57px;
        overflow: hidden;
        font-size: $global-font-size-18;
        font-weight: bold;
      }
      .other {
        display: flex;
        .other_1 {
          .image {
            width: 95px;
            height: 95px;
            display: inline-block;
          }
        }
        .other_2 {
          display: flex;
          flex-direction: column;
          margin: 0 0 0 10px;
          .text {
            height: 25px;
            display: block;
            line-height: 24px;
            overflow: hidden;
            color: #666;
            margin-bottom: 10px;
            em {
              border: 1px #ccc solid;
              background: #fff;
              border: 1px #ab94f8 solid;
              padding: 1px 5px;
              color: #4131bb;
              margin-right: 6px;
            }
          }
        }
      }
    }
    .list:nth-child(3n) {
      margin: 0 0 25px 0;
    }
  }
  .four {
    display: flex;
    justify-content: center;
    margin: 20px 0;
  }
}
</style>
