<template>
  <div class="page-body">
    <div class="head-box-home">
      <div class="head-box-top">
        <div class="top-call" />
        <div class="top-call"><img src="@/assets/home_images/cisnet_logo.png"></div>
        <div class="top-call"><title-menus ref="titleMenus" menu-index="0" /></div>
        <div class="top-call"><user-login ref="userLogin" /></div>
        <div class="top-call"><language-setting ref="languageSetting" /></div>
      </div>
      <div class="head-title"><img src="@/assets/home_images/home_title.png" alt="home_title"></div>
      <search-panel ref="searchPanel" style="margin-top: 64px;" />
    </div>
    <div class="line-btn" style="padding-top: 42px;">
      <div class="btn-o" style="padding: 10px" @click="goToDetails(null, '/publish/supplyInfo/all')">我要供应</div>
      <div class="btn-o" style="padding: 10px" @click="goToDetails(null, '/publish/buyInfo/all')">我要采购</div>
    </div>
    <div class="line-btn" style="padding-top: 30px">
      <div v-for="(item,index) in dict.product_type" :key="index" @mouseenter="changeProductPush(item)">
        <div v-if="index<4">
          <el-dropdown placement="bottom">
            <div class="btn-b" :class="currentProductTypeObj===item?'active':''">{{ item.label }}</div>
            <el-dropdown-menu slot="dropdown" style="width: 170px;text-align: center;">
              <router-link v-for="(t,i) in subTypeOpts" :key="'st_'+i" :to="{'name':'hardware',params:{'productType':currentProductTypeObj.value,'subProductType':t.value}}">
                <el-dropdown-item style="font-size: 16px;padding: 5px;font-weight: bold;">
                  {{ t.label }}
                </el-dropdown-item>
              </router-link>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
        <div v-else class="btn-b" :class="currentProductTypeObj===item?'active':''">{{ item.label }}</div>
      </div>
    </div>
    <div class="content-box">
      <div class="showcase">
        <!--产品展柜-->
        <div style="padding-top: 20px">
          <div v-for="(item,index) in showcaseProducts" :key="'p_'+index" class="box-card">
            <img v-if="item.isGood" src="@/assets/home_images/recommend.png" class="stamp" title="推荐" style="top: 10px;right: 8px">
            <img v-if="item.isHot" src="@/assets/home_images/hot.png" class="stamp" title="热门" style="top: 0;left: 0">
            <el-image
              class="touch"
              :src="item.imgSrc?imgSrcStart+'/productPic/'+item.imgSrc:defaultImgProduct"
              style="width:284px;height: 284px;margin: 0 32px 4px"
              @click="goToDetails(item, '/product/details')"
            >
              <div slot="placeholder" class="image-slot">加载中<span class="dot">...</span></div>
            </el-image>
            <el-row>
              <el-col :span="21">
                <div class="title touch" :title="item.title" @click="goToDetails(item, '/product/details')">{{ item.title }}</div>
              </el-col>
              <el-col :span="3">
                <img v-if="item.isStore" class="touch" src="@/assets/home_images/to_like_h.png" title="点击取消收藏" @click="storeShowcase(item, 'product')">
                <img v-else class="touch" src="@/assets/home_images/to_like.png" title="点击收藏" @click="storeShowcase(item, 'product')">
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <div class="subTitle touch" :title="item.subTitle" @click="goToDetailsSupplier(item, '/supplier/details')">{{ item.subTitle }}</div>
              </el-col>
            </el-row>
            <el-row style="margin-top: 6px">
              <el-col :span="3" style="padding-left: 20px">
                <img src="@/assets/home_images/locate.png">
              </el-col>
              <el-col :span="13">
                <div class="area">{{ item.area }}</div>
              </el-col>
              <el-col :span="8">
                <div class="date">{{ item.date }}</div>
              </el-col>
            </el-row>
            <div class="line-btn" style="margin-top: 4px">
              <div class="btn-o" style="width: 150px;border-radius: 10px;box-shadow: 0 1px 2px 0 rgba(0,0,0,0.2);font-size:20px;" @click="inquiry(item, 'product')">询 价</div>
            </div>
          </div>
          <div style="padding: 15px 50px;text-align: right"><span class="more touch" title="点击查看更多..." @click="moreTo('hardware')">更多产品>></span></div>
        </div>
        <!--供应商展柜-->
        <div class="line-title"><div class="title-point" /><span class="title-text">供应商</span><div class="title-line" /></div>
        <div style="padding-top: 20px">
          <div v-for="(item,index) in showcaseSuppliers" :key="'s_'+index" class="box-card" style="padding-top: 20px;">
            <img v-if="item.isGood" src="@/assets/home_images/diamond.png" class="stamp" title="推荐" style="top: 10px;right: 8px">
            <img v-if="item.isHot" src="@/assets/home_images/hot.png" class="stamp" title="热门" style="top: 0;left: 0">
            <el-image
              class="touch"
              :src="item.imgSrc?imgSrcStart+item.imgSrc:defaultImgProduct"
              style="width:284px;height: 284px;margin: 0 32px 4px"
              @click="goToDetails(item, '/supplier/details')"
            >
              <div slot="placeholder" class="image-slot">加载中<span class="dot">...</span></div>
            </el-image>
            <el-row>
              <el-col :span="21">
                <div class="title touch" :title="item.title" @click="goToDetails(item, '/supplier/details')">{{ item.title }}</div>
              </el-col>
              <el-col :span="3">
                <img v-if="item.isStore" class="touch" src="@/assets/home_images/to_like_h.png" title="点击取消收藏" @click="storeShowcase(item, 'supplier')">
                <img v-else class="touch" src="@/assets/home_images/to_like.png" title="点击收藏" @click="storeShowcase(item, 'supplier')">
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <div class="subTitle" :title="item.subTitle">TEL：{{ item.subTitle }}</div>
              </el-col>
            </el-row>
            <el-row style="margin-top: 6px">
              <el-col :span="3" style="padding-left: 20px">
                <img src="@/assets/home_images/locate.png">
              </el-col>
              <el-col :span="13">
                <div class="area">{{ item.area }}</div>
              </el-col>
              <el-col :span="8">
                <div class="date">{{ item.date }}</div>
              </el-col>
            </el-row>
          </div>
          <div style="padding: 15px 50px;text-align: right"><span class="more touch" title="点击查看更多..." @click="moreTo('supplier')">更多供应商>></span></div>
        </div>
        <!--热门排行榜-->
        <div class="line-title"><div class="title-point" /><span class="title-text">热门排行榜</span></div>
        <el-row :gutter="20" style="padding-top: 20px">
          <el-col :span="8"><div class="top_area">
            <div
              v-for="(item,index) in topAreas"
              :key="'ta_'+index"
              class="top_area_text touch"
              :style="'top:'+item.top+';left:'+item.left+';font-size:'+item.size+';color:'+color[index]"
              @click="areaTopClick(item.name)"
            >{{ item.name }}</div>
          </div></el-col>
          <el-col :span="8"><div class="box-toplist">
            <div class="toplist-title">供应商TOP8</div>
            <ul class="contact-list">
              <li v-for="(item, index) in topSuppliers" :key="'ts_'+index" class="contact-item">
                <img src="@/assets/home_images/to_like_b.png">
                <span class="touch" @click="goToDetails(item, '/supplier/details')">{{ item.name }}</span>
              </li>
            </ul>
          </div></el-col>
          <el-col :span="8"><div class="box-toplist">
            <div class="toplist-title">产品TOP8</div>
            <ul class="contact-list">
              <li v-for="(item, index) in topProducts" :key="'tp_'+index" class="contact-item">
                <img src="@/assets/home_images/to_like_h.png" style="top:10px">
                <span class="touch" @click="goToDetails(item, '/product/details')">{{ item.name }}</span>
              </li>
            </ul>
          </div></el-col>
        </el-row>
        <div class="line-btn" style="padding-top: 64px;">
          <div class="btn-o" style="padding: 10px" @click="goToDetails(null, '/publish/supplyInfo/all')">我要供应</div>
          <div class="btn-o" style="padding: 10px" @click="goToDetails(null, '/publish/buyInfo/all')">我要采购</div>
        </div>
        <div class="line-btn" style="padding: 30px 0;">
          <div class="btn-l" @click="goToDetails(null, '/publish/otherInfo')">没有我需要的，我要留言</div>
        </div>
        <!--合作伙伴 -->
        <div class="line-title"><div class="title-point" /><span class="title-text">合作伙伴 </span></div>
        <div style="padding: 20px 30px">
          <div v-for="(item,index) in partners" :key="'pt_'+index" class="partners-card" :title="item.name">
            <a :href="item.url||'http://www.minestar.cn'" target="_blank">
              <el-image :src="item.imgSrc?imgSrcStart+item.imgSrc:defaultImgLogo" style="width: 100%;height: 100%" />
            </a>
          </div>
        </div>
      </div>
    </div>
    <home-footer ref="homeFooter" />
    <float-btn ref="floatBtn" />
    <inquiry-view ref="inquiryView" />
  </div>
</template>
<script>
import default_product from '@/assets/home_images/default_product.png'
import default_p_logo from '@/assets/home_images/p_logo.png'
import floatBtn from './components/floatBtn'
import titleMenus from './components/titleMenus'
import userLogin from './components/userLogin'
import languageSetting from './components/languageSetting'
import searchPanel from './components/searchPanel'
import homeFooter from './components/homeFooter'
import inquiryView from './components/inquiryView'
import { HttpReq } from '@/api/common'

export default {
  components: { floatBtn, titleMenus, userLogin, languageSetting, searchPanel, homeFooter, inquiryView },
  dicts: ['product_type', 'product_type_1', 'product_type_2', 'product_type_3', 'product_type_4', 'product_type_5', 'product_type_6', 'product_type_7', 'area_province'],
  data() {
    return {
      color: ['#C1232B', '#27727B', '#FCCE10', '#E87C25', '#B5C334', '#FE8463', '#9BCA63', '#FAD860', '#F3A43B', '#60C0DD', '#D7504B', '#C6E579', '#F4E001', '#F0805A', '#26C0C0'],
      defaultImgProduct: default_product,
      defaultImgLogo: default_p_logo,
      imgSrcStart: process.env.VUE_APP_BASE_API,
      query: {},
      currentProductTypeObj: null,
      subTypeOpts: [],
      showcaseProducts: [],
      showcaseSuppliers: [],
      topAreas: [],
      topSuppliers: [],
      topProducts: [],
      partners: [],
      throttle: null, // 节流器
      throttleTime: 300
    }
  },
  mounted() {
    this.$nextTick(() => {
      HttpReq.webClientApi.homePageGetRank({
        preview: false
      }).then((res) => {
        this.topSuppliers = res.data.topSuppliers
        this.topProducts = res.data.topProducts
        const random = (min, max) => {
          return Math.floor(Math.random() * (max - min)) + min
        }
        const arr = res.data.topArea[0].topArea
        arr.forEach((item, index) => {
          this.topAreas.push({
            name: item,
            top: `${random(0, 382)}px`,
            left: `${random(0, 324)}px`,
            size: `${Math.max(80 - index * 7, 12)}px`
          })
        })
        this.topAreas[0].top = '180px'
        this.topAreas[0].left = '120px'
      })
      HttpReq.webClientApi.homePageGetPartners({
        preview: false
      }).then((res) => {
        this.partners = res.data.partners
      })
      HttpReq.webClientApi.homePageGetSupplier({
        preview: false
      }).then((res) => {
        this.showcaseSuppliers = res.data.showcaseSuppliers
      })
    })
  },
  methods: {
    onDictReady(dict) {
      this.currentProductTypeObj = dict.product_type[0]
      this.subTypeOpts = this.dict['product_type_' + this.currentProductTypeObj.value]
      HttpReq.webClientApi.homePageGetProduct({
        preview: false,
        productType: this.currentProductTypeObj.value
      }).then((res) => {
        this.showcaseProducts = res.data.showcaseProducts
      })
    },
    // 变更产品推送
    changeProductPush(item) {
      clearTimeout(this.throttle)
      this.throttle = setTimeout(() => {
        if (this.currentProductTypeObj === item) {
          return
        }
        this.currentProductTypeObj = item
        this.subTypeOpts = this.dict['product_type_' + this.currentProductTypeObj.value]
        HttpReq.webClientApi.homePageGetProduct({
          preview: false,
          productType: this.currentProductTypeObj.value
        }).then((res) => {
          this.showcaseProducts = res.data.showcaseProducts
        })
      }, this.throttleTime)
    },
    // 点击询价
    inquiry(item, type) {
      this.$refs.inquiryView.initView(item.id, item.customerId, type)
    },
    // 点击收藏
    storeShowcase(item, type) {
      HttpReq.webClientApi.collectProductOrSupplier({
        id: item.id,
        collect: !item.isStore,
        type: type
      }).then((res) => {
        item.isStore = !item.isStore
        this.$message.warning({
          message: res.msg
        })
      })
    },
    // 跳转到详情
    goToDetails(item, path) {
      if (item) {
        this.$router.push({ path: `${path}/${item.id || 123}` })
      } else {
        this.$router.push({ path: `${path}` })
      }
    },
    goToDetailsSupplier(item, path) {
      if (item) {
        this.$router.push({ path: `${path}/${item.customerId || 123}` })
      } else {
        this.$router.push({ path: `${path}` })
      }
    },
    // 点击地区top
    areaTopClick(text) {
      this.$router.push({
        name: 'supplier',
        params: {
          areaText: text
        }
      })
    },
    // 点击更多
    moreTo(type) {
      if (type === 'supplier') {
        this.$router.push({ path: `/supplier` })
      }
      if (type === 'hardware') {
        switch (this.currentProductTypeObj.value) {
          case '5':
            this.$router.push({ path: `/project` })
            break
          case '6':
            this.$router.push({ path: `/software` })
            break
          case '7':
            this.$router.push({ path: `/course` })
            break
          default:
            this.$router.push({
              name: 'hardware',
              params: {
                productType: this.currentProductTypeObj.value,
                subProductType: ''
              }
            })
        }
      }
    },
    goTop() {
      document.body.scrollTop = 0
      document.documentElement.scrollTop = 0
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.touch {
  cursor:pointer;
}
.clear {
  clear: both
}
.content-box {
  display: flex;
  justify-content: center;
  font-family: Source Han Sans CN;
  font-weight: 400;
  //user-select: none;
  .showcase {
    width: 80%;
  }
  .partners-card {
    width: 240px;
    height: 135px;
    display: inline-block;
    position: relative;
    margin: 15px 25px;
    background: #fafafa;
    border: 1px solid transparent;
    box-shadow: 1px 1px 0 0 rgba(0,0,0,0.12);
  }
  .partners-card:hover {
    border: 1px solid rgba(0,0,0,0.12);
  }
  .box-card {
    display: inline-block;
    position: relative;
    margin: 12px;
    padding: 10px 0;
    width: 350px;
    background: #FFFFFF;
    border: 1px solid rgba(0,0,0,0.12);
    box-shadow: 1px 2px 8px 0 rgba(0,0,0,0.12);
    border-radius: 10px;
    .title {
      width: 305px;
      padding: 2px 20px;
      font-size: 18px;
      color: #333333;
      overflow:hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
      -o-text-overflow:ellipsis;
    }
    .title:hover {
      font-weight: 600;
    }
    .subTitle {
      width: 260px;
      padding: 2px 20px;
      font-size: 15px;
      color: #1961C5;
      overflow:hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
      -o-text-overflow:ellipsis;
    }
    .area {
      padding: 2px 0;
      font-size: 17px;
      color: #666666;
    }
    .date {
      padding: 2px 10px;
      font-size: 17px;
      color: #999999;
    }
    .stamp {
      border-style: none;
      position: absolute;
      z-index: 99;
    }
  }
  .more {
    font-size: 18px;
    font-weight: 600;
    color: #0457C7;
    opacity: 0.7;
  }
  .more:hover {
    opacity: 1;
  }
  .line-title {
    padding: 6px 0;
    position: relative;
    border-bottom: 3px solid #0457C7;
    font-size: 24px;
    color: #333333;
    .title-point {
      width: 8px;
      height: 21px;
      position: absolute;
      top: 9px;
      background: #0457c7;
    }
    .title-text {
      padding-left: 14px;
      font-weight: 600;
    }
    .title-line{
      width: 100%;
      position: absolute;
      bottom: -7px;
      border-bottom: 2px solid #1961C5;
      opacity: 0.35
    }
  }
  .top_area {
    position: relative;
    width: 100%;
    height: 492px;
    background: url("../../assets/home_images/top_area.png") no-repeat;
    background-size:100% 100%;
    .top_area_text {
      position: absolute;
      font-weight: bolder;
      opacity: 0.7;
    }
    .top_area_text:hover {
      opacity: 1;
    }
  }
  .box-toplist{
    background: #FFFFFF;
    border: 1px solid rgba(0,0,0,0.12);
    box-shadow: 1px 2px 8px 0px rgba(0,0,0,0.12);
    border-radius: 10px;
    font-size: 22px;
    letter-spacing: 2px;
    .toplist-title {
      width: 100%;
      padding: 10px 15px;
      background: #0457C7;
      color: #FFFFFF;
      font-weight: 600;
    }
    .contact-list {
      padding: 15px 0;
      margin: 0;
      height: 440px;
      overflow: hidden;
      .contact-item {
        position: relative;
        display: inherit;
        padding: 10px 30px;
        overflow:hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
        -o-text-overflow:ellipsis;
        img {
          position: absolute;
          top: 8px;
        }
        span {
          padding-left: 40px;
        }
        span:hover {
          font-weight: 600;
        }
      }
    }
  }
}
.head-box-home {
  width: 100%;
  height: 750px;
  background: #1961C5;
  .head-box-top {
    display: flex;
    justify-content: space-evenly;
    align-items: center;
    padding: 15px 15px 0;
    .top-call {
      height: 50px;
    }
  }
  .head-title {
    width: 100%;
    margin-top: 92px;
    text-align: center;
    //user-select: none;
  }
}
.line-btn {
  display: flex;
  justify-content: center;
  font-family: Source Han Sans CN;
  font-weight: 400;
  .btn-o {
    margin: 0 25px;
    width: 360px;
    padding: 6px 10px;
    background: #F7601A;
    box-shadow: 0 3px 6px 0 rgba(0,0,0,0.2);
    text-align: center;
    border: 1px solid transparent;
    font-size: 22px;
    color: #FFFFFF;
    cursor:pointer;
    //user-select: none;
  }
  .btn-o:hover {
    border: 1px solid rgba(30, 144, 255, 0.8);
    color: #1482f0;
  }
  .btn-l {
    margin: 0 25px;
    width: 360px;
    padding: 10px;
    background: #1961C5;
    box-shadow: 0 3px 6px 0 rgba(0,0,0,0.2);
    text-align: center;
    border: 1px solid transparent;
    font-size: 22px;
    color: #FFFFFF;
    cursor:pointer;
    //user-select: none;
  }
  .btn-l:hover {
    border: 1px solid rgba(0,0,0,0.3);
    color: #F7601A;
  }
  .btn-b {
    margin: 0 10px;
    width: 168px;
    padding: 6px;
    background: #EFF6FF;
    border: 1px solid transparent;
    box-shadow: 0 1px 2px 0 rgba(0,0,0,0.2);
    text-align: center;
    font-size: 20px;
    color: #1961C5;
    cursor:pointer;
    //user-select: none;
  }
  .btn-b:hover {
    border: 1px solid rgba(30, 144, 255, 0.9);
  }
  .active {
    color: #FFFFFF;
    font-weight: 400;
    background: #1961C5;
  }
}
</style>
