<template>
  <div class="wrap">
    <div class="headerWrap">
      <img class="bg" src="@/assets/secondHandCar/bg.png" />
      <img class="mine" src="@/assets/secondHandCar/mine.png" @click="goMine" />
      <el-input
          @input="getList"
          placeholder="请输入"
          v-model.trim="topSearch">
        <img slot="suffix" class="search" src="@/assets/secondHandCar/search.png"/>
      </el-input>
    </div>
    <div class="firstTab">
      <div>
        <img class="big com" v-show="activeTab === 0"  src="@/assets/secondHandCar/cheyuanBig.png" @click="activeTab = 0" />
        <img class="small com" v-show="activeTab === 1" src="@/assets/secondHandCar/cheyuanSmall.png" @click="activeTab = 0"/>
        <img class="big" v-show="activeTab === 1" src="@/assets/secondHandCar/qiugouBig.png" @click="activeTab = 1"/>
        <img class="small" v-show="activeTab === 0" src="@/assets/secondHandCar/qiugouSmall.png" @click="activeTab = 1"/>
      </div>
      <div @click="poupShow = true">
        <img class="suggest" src="@/assets/secondHandCar/suggest.png" />
      </div>
    </div>
    <div class="filterWrap">
      <el-select @change="getList" v-model="licenseType" placeholder="牌照类型" :class="{'customSel':true , 'has-value': licenseType }" clearable>
        <el-option
            v-for="item in licenseTypeOption"
            :key="item.value"
            :label="item.label"
            :value="item.value">
        </el-option>
      </el-select>
      <el-select @change="getList" v-model="vehicleType" placeholder="车辆类型" :class="{'customSel':true , 'has-value': vehicleType }" clearable>
        <el-option
            v-for="item in vehicleTypeOption"
            :key="item.value"
            :label="item.label"
            :value="item.label">
        </el-option>
      </el-select>
      <el-select @change="getList" v-model="price" placeholder="价格区间" :class="{'customSel':true , 'has-value': price }" clearable>
        <el-option
            v-for="item in priceOption"
            :key="item.value"
            :label="item.label"
            :value="item.value">
        </el-option>
      </el-select>
      <el-select @change="getList" v-model="emissionStandard" placeholder="排放标准" :class="{'customSel':true , 'has-value': emissionStandard }" clearable>
        <el-option
            v-for="item in emissionStandardOption"
            :key="item.value"
            :label="item.label"
            :value="item.value">
        </el-option>
      </el-select>
      <el-select @change="getList" v-model="boardType" placeholder="落板方式" :class="{'customSel':true , 'has-value': boardType }" clearable>
        <el-option
            v-for="item in boardTypeOption"
            :key="item.value"
            :label="item.label"
            :value="item.value">
        </el-option>
      </el-select>
    </div>
    <div class="contentWrap" v-show="!showEmpty">
      <van-pull-refresh v-model="isLoading" @refresh="onRefresh"  style="min-height:85vh">
        <div class="contentItem">
          <div class="item" v-for="(item,index) in pageList" :key="index" @click="viewCarSource(item)">
            <div class="left">
              <img class="img" v-if="item.vehicleAnglePhoto"  :src="item.vehicleAnglePhoto" />
              <div v-else class="qiugou">{{item.vehicleType?.substring(0,2)}}</div>
            </div>
            <div class="right">
              <div class="title">{{item.vehicleType}}</div>
              <div v-if="activeTab==0" class="type">{{ item.boardType?.label }} | {{ item.licenseType?.label }} |
                {{ item.brandModel?.match(/^[\u4e00-\u9fa5]+/)?.[0] }} | {{ item.mileage }}km | {{item.emissionStandard?.label}}</div>
              <div v-else class="type">{{ item.boardType?.label }} |
                {{item.vehicleType }} | {{item.emissionStandard?.label}}</div>
              <div class="date" v-show="activeTab==0 && item.registerDate">注册日期： {{item.registerDate?.substring(0,10)}}</div>
              <div v-if="activeTab==0" class="price"><span>￥</span> <span>{{formatNumber(item.maxPrice) }}</span> <span>万</span> </div>
              <div v-else class="priceRange">
                <span class="num">{{formatNumber(item.minPrice) }}</span> <span  class="danwei">万</span>
                <span class="su">~</span>
                <span class="num">{{formatNumber(item.maxPrice) }}</span> <span  class="danwei">万</span>
              </div>
              <div class="line"></div>
            </div>
          </div>
        </div>
      </van-pull-refresh>
    </div>
    <div class="empty" v-show="showEmpty">
      <img src="@/assets/empty.png" />
    </div>
    <img v-show="activeTab==0" @click="releaseHandle(1)" class="fixImg" src="@/assets/secondHandCar/sellCar.png" />
    <img v-show="activeTab==1" @click="releaseHandle(2)" class="fixImg" src="@/assets/secondHandCar/buyCar.png" />
    <van-popup v-model="poupShow" closeable round  :style="{ width: '90%',height:'40%' }"  class="poupWrap">
        <span class="title">反馈建议</span>
        <div class="poupContent">
          <div class="tip">如有任何建议，请及时反馈我们。</div>
          <el-input class="ipt" v-model.trim="suggestContent" type="textarea"  :autosize="{ minRows: 6, maxRows: 10 }"
                    maxlength="500" show-word-limit></el-input>
          <div class="btnWrap">
            <div class="btn" @click="submitSuggest">提交</div>
          </div>
      </div>
    </van-popup>
  </div>
</template>

<script>
import {myMixins} from "@/utils/myMixins"
import {appPageList,userFeedback } from "@/api/secondHandCar"
export default {
  name: "indexList",
  computed: {
  },
  mixins:[myMixins],
  watch:{
    activeTab(){
      this.getList()
    }
  },
  data(){
    return{
      pageList:[],
      topSearch:'',
      activeTab:0,
      showEmpty:false,
      isLoading:false,
      licenseType:'',
      vehicleType:'',
      minPrice:'',
      maxPrice:'',
      price:'',
      emissionStandard:'',
      boardType:'',
      licenseTypeOption:[
        {value:2,label:'黄牌'},{value:3,label:'绿牌'},{value:1,label:'蓝牌'},
      ],
      vehicleTypeOption:[
        {value:1,label:'拖车'},{value:2,label:'地库车'},{value:3,label:'牵引车'},{value:4,label:'随车吊'},{value:5,label:'其他'},
      ],
      priceOption:[
        {value:1,label:'5~6'},{value:2,label:'7~8'},{value:3,label:'8~9'}
      ],
      emissionStandardOption:[
        {value:1,label:'不限'},{value:4,label:'国四'},{value:5,label:'国五'},{value:6,label:'国六'}
      ],
      boardTypeOption:[
        {value:1,label:'全落地'},{value:2,label:'斜落地'},{value:3,label:'一般斜板'},{value:9,label:'不限'}
      ],
      poupShow:false,
      suggestContent:'',
    }
  },
  mounted() {
    const savedTab = sessionStorage.getItem('indexActiveTab');
    this.activeTab = Number(savedTab); // 转换为数字
    // }
    this.getList();
    document.addEventListener('visibilitychange', async (  ) => {
      let state = document.visibilityState
      if (state == 'hidden') {  // 用户离开了
        // console.log("用户离开了")
      }
      if (state == 'visible') {
        // console.log("用户visible")
      }
    });
  },
  methods:{
    goMine(){//我的发布
      this.$router.push({ name: "mineRelease",})
    },
    viewCarSource(item){
      if(this.activeTab===0){//车源查看
        this.$router.push({ name: "forSale", query: {type:1 , id:item.id,queryType:1 }})
      }else{//求购查看
        this.$router.push({ name: "wantBuySale", query: { type:1 ,id:item.id ,queryType:2}})
      }
      // 保存当前的 activeTab
      sessionStorage.setItem('indexActiveTab',String(this.activeTab) );
    },
    releaseHandle(type){
      if(type===1){//车源发布
         this.$router.push({
          name: "carSource",
          query: { type:1 }
         })
      }else{//求购发布
        this.$router.push({ name: "wantBuy", query: { type:1 }  })
      }
    },
    async submitSuggest(){
      if(!this.suggestContent){
        this.$toast('反馈提交不能为空')
        return
      }
      //继续提交
      await userFeedback ({
        content:this.suggestContent,
      })
      this.poupShow=false
      this.suggestContent=''
      this.$toast('反馈建议提交成功')
    },
    onRefresh() {
      this.getList()
      setTimeout(() => {
        this.$toast('刷新成功');
        this.isLoading = false;
      }, 1000);
    },
    async getList(){
      let res= await appPageList({
        type:this.activeTab==0 ? 1 : 2,
        topSearch:this.topSearch,
        licenseType:this.licenseType,
        vehicleType:this.vehicleType,
        minPrice:this.minPrice,
        maxPrice:this.maxPrice,
        boardType:this.boardType,
        emissionStandard:this.emissionStandard,
        // brandModel:this.brandModel,
      })
      this.pageList=res.data
      if(this.pageList?.length === 0){
        this.showEmpty  = true
      }else {
        this.showEmpty = false
      }
    },
    goApp(){
      let data = {"action":"goBack","params":""}
      var u = navigator.userAgent;
      var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
      if(isiOS){
        window.webkit.messageHandlers.nativeObject.postMessage(data);
      }else {
        window.android.sendMessage("goBack");
      }
    },
  }
}
</script>
<style scoped lang="scss">
@import "@/styles/common.scss";
@import "@/styles/mixin.scss";
.wrap{
  position: relative;
  width: 100%;
  height: 100%;
}
.fixImg{
  width: 210px;
  height: 102px;
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  z-index: 11;
}
.headerWrap{
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-sizing: border-box;
  padding-left: 15px;
  padding-right: 10px;
  padding-top: 10px;
  position: relative;
  .bg{
    position: absolute;
    width: 100%;
    height: 144px;
    top: 0;
    left: 0;
    z-index: -1;
  }
  .mine{
    width: 55px;
    height: 43px;
    margin-right: 15px;
  }
  .search{
    width: 17px;
    height: 17px;
  }
  ::v-deep .el-input__inner{
    background: rgba(255,255,255,0.8);
    border-radius: 20px;
    border: 2px solid #264B94;
  }
  ::v-deep .el-input__suffix{
    top: 12px;
    right: 10px;
  }
}
.firstTab{
  width: 100%;
  padding-top: 18px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  img{
    cursor: pointer;
  }
  .big,.small{
    width: 52px;
    height: 21px;
  }
  .com{
    margin-left: 32px;
    margin-right: 25px;
  }
  .suggest{
    width: 87px;
    height: 38px;
  }
}
.filterWrap {
  width: 100%;
  display: flex;
  justify-content: space-around;
  padding-bottom: 10px;
  .customSel {
    width: 69px;
    height: 25px;
    background: #F5F5F5;
    border-radius: 4px;
    font-size: 12px;
    color: #323233;
    ::v-deep .el-input__inner{
      padding: 0 2px;
      height: 25px;
      font-size: 12px;
      background: #F5F5F5;
      border-radius: 4px;
      border: none;
    }
    ::v-deep .el-input__icon{
      line-height: 25px;
      font-size: 12px;
      width: 18px;
      color: #2A5094;
    }
    ::v-deep .el-input__suffix{
      right: 2px;
    }
  }
  .has-value  ::v-deep .el-input__inner{
    background: #F1F6FF ;
    color: #007BE9;
    font-weight: bold;
  }
  .has-value ::v-deep .el-input__icon{
    color: #007BE9;
  }
}
.contentWrap{
  width: 100%;
  height: calc(100% - 80px);
  overflow-y: auto;
  background-color: #F2F2F2 ;
  box-sizing: border-box;
  padding: 10px 6px 0;
  .contentItem{
    width: 100%;
    background-color: #FFFFFF;
    border-radius: 10px;
    box-sizing: border-box;
    padding-left: 8px;
    padding-top: 18px;
    .item{
      display: flex;
      justify-content: space-between;
    }
    .left{
      .img, .qiugou{
        width: 115px;
        height: 77px;
        border-radius: 7px;
        margin-right: 10px;
      }
      .qiugou{
        line-height: 77px;
        text-align: center;
        color: #FFFFFF;
        font-size: 18px;
        letter-spacing: 5px;
        background-color: #6C81CD;
      }
    }
    .right{
      .title{
        font-weight: 600;
        font-size: 14px;
        color: #212020;
        line-height: 28px;
      }
      .type{
        font-weight: 400;
        font-size: 12px;
        color: #282828;
        line-height: 23px;
        width: 200px;
        white-space: nowrap; /* 强制文本在一行显示 */
        overflow: hidden; /* 隐藏超出部分 */
        text-overflow: ellipsis; /* 超出部分显示省略号 */
      }
      .date{
        font-weight: 400;
        font-size: 11px;
        color: #999999;
        line-height: 20px;
      }
      .price{
        color: #FF5533;
        span:first-child{
          font-weight: 600;
          font-size: 13px;
        }
        span:nth-child(2){
          font-weight: bold;
          font-size: 18px;
          line-height: 25px;
          margin-right: 2px;
        }
        span:nth-child(3){
          font-weight: 600;
          font-size: 11px;
          line-height: 16px;
        }
      }
      .priceRange{
        color: #FF5533;
        display: flex;
        align-items: center;
        .num{
          font-weight: bold;
          font-size: 18px;
          margin-right: 5px;
        }
        .danwei{
          font-weight: 600;
          font-size: 11px;
        }
        .su{
          font-weight: 600;
          font-size: 11px;
          margin: 0 6px;
        }
      }
      .line{
        width: 224px;
        border-bottom: 2px solid #F5F5F5;
        margin: 10px 0;
      }
    }
  }
}
.poupWrap{
  width: 100%;
  padding-top: 20px;
  padding-left: 20px;
  .title{
    font-size: 14px;
    font-weight: bold;
    color: #c8c9cc;
  }
  .poupContent{
    width: 100%;
    box-sizing: border-box;
    padding: 10px 20px 10px 10px;
    .tip,.ipt{
        margin-bottom: 15px;
    }
    .ipt{

    }
    .btnWrap{
      width: 100%;
      text-align: -webkit-right;
      .btn{
        width: 80px;
        height: 30px;
        line-height: 30px;
        background-color: #4C81F5;
        color: #FFFFFF;
        text-align: center;
        border-radius: 8px;
      }
    }
  }
}
.empty{
  @include flexTwoCenter;
  height:calc(100% - 86px);
  background-color: #FAFAFA;
  img{
    width: 100%;
  }
}
</style>
