<template>
  <div class="wrap" >
    <div class="headerWrap">
      <img class="bg" src="@/assets/secondHandCar/bg.png" />
      <div class="nav">
        <img class="back" @click="goPage('indexList',indexActiveTab)" src="@/assets/secondHandCar/back.png" />
        <span>我的发布</span>
        <div style="opacity: 0;" class="back"></div>
      </div>
      <div class="tabBar">
        <div v-for="(item,i) in tabList" :key="i" :class="{'active':activeTab == (i+1)}" @click="tabClickHandle(item)">{{item.name}}
          <span v-if="i==0 && noPassNum">{{noPassNum}}</span>
        </div>
      </div>
    </div>
    <div class="contentWrap" v-show="!show">
      <van-pull-refresh v-model="isLoading" @refresh="onRefresh"  style="min-height:85vh">
        <van-list
            v-model="loading"
            :finished="finished"
            finished-text="没有更多了"
            @load="onLoad"
        >
          <div class="contentItem">
            <div class="item" v-for="(item,index) in pageList" :key="index" @click.stop="itemHandle(item)">
              <div class="left">
                <img v-if="item.vehicleAnglePhoto" class="img"   :src="item.vehicleAnglePhoto" />
                <div v-else class="qiugou">{{item.vehicleType?.substring(0,2)}}</div>
                <img v-if="item.type==1" class="smallImg"  src="@/assets/secondHandCar/mine_cheyuan.png" />
                <img v-else class="smallImg"  src="@/assets/secondHandCar/mine_qiugou.png" />
              </div>
              <div class="right">
                <div class="title">
                  <span>{{item.vehicleType}}</span>
                  <img :class="{'titleImg':true,bigImg:[8,9].includes(item.status?.code)}"  :src="getStatus(item.status?.code)" />
                </div>

                                <div v-if="activeTab==0" class="type">{{ item.boardType?.label  ? item.boardType?.label+' | ' : ''}}  {{ item.licenseType?.label }} |
                  {{ item.mileage }}km | {{item.emissionStandard?.label}}</div>

                <div v-else class="type">{{ item.boardType?.label  ? item.boardType?.label+' | ' : ''}}
                  {{item.vehicleType }} | {{item.emissionStandard?.label}}</div>
                <div v-if="item.type==1" class="price">售价（含税）：<span>{{formatNumber(item.minPrice) }}</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="btnWrap">
                  <img v-if="(activeTab==1 && item.status?.code==3) || activeTab==2"  @click.stop="noMultipleClicks(reviseHandle,item)" class="revise"  src="@/assets/secondHandCar/revise.png" />
                  <img v-if="(activeTab==1 && (item.status?.code==1 || item.status?.code==3)) || activeTab==2"  @click.stop="noMultipleClicks(xiajiaHandle,item)" class="xiajia"  src="@/assets/secondHandCar/xiajia.png" />
                  <img v-if="(activeTab==1 && item.status?.code==9)"  @click.stop="noMultipleClicks(reviseXiajiaHandle,item)" class="reshangjia"  src="@/assets/secondHandCar/reviseXiajia.png" />
                  <img v-if="((activeTab==3 && item.status?.code==7) || activeTab==2) && !isToday(item.wapTime)"  @click.stop="noMultipleClicks(polishHandle,item)" class="polish"  src="@/assets/secondHandCar/polish.png" />
                  <img v-if="activeTab==3 && item.type==1 && item.downReason=='不卖了' "  @click.stop="noMultipleClicks(reShangJiaHandle,item)" class="reshangjia"  src="@/assets/secondHandCar/reshangjia.png" />
                  <img v-if="(activeTab==3 &&  [10,11].includes(item.status?.code)) || (activeTab==1 && [8,9].includes(item.status?.code))"  @click.stop="noMultipleClicks(viewXiajiaHandle,item)" class="reshangjia"  src="@/assets/secondHandCar/viewXiajia.png" />
                </div>
                <div class="line"></div>
              </div>
            </div>
          </div>
        </van-list>
      </van-pull-refresh>
    </div>
    <div class="empty" v-show="show">
      <img src="@/assets/empty.png" />
    </div>
    <van-popup v-model="poupShow" closeable round  :style="{ width: '70%',height:xiajiaInfo.proprietary==1 && xiajiaInfo?.type==1 && radio==1 ? '20%' : '18%' }"  class="poupWrap">
      <span class="title">下架原因</span>
      <div class="poupContent">
        <van-radio-group v-model="radio" class="radioWrap">
          <van-radio name="1">{{xiajiaInfo?.type==1 ? '已售出' : '已买到'}}</van-radio>
          <van-radio name="2">{{xiajiaInfo?.type==1 ? '不卖了' : '不买了'}}</van-radio>
        </van-radio-group>
        <div v-show="xiajiaInfo.proprietary==1 && xiajiaInfo?.type==1 && radio==1" class="tip">注：已售出需前往完善售出信息</div>
        <div class="btnWrap">
          <div class="btn" @click="noMultipleClicks(submitHandle)">确认</div>
        </div>
      </div>
    </van-popup>
 </div>
</template>

<script>
import {myMixins} from "@/utils/myMixins"
import { minePublishPageList,auditFailCount,usedCarPolish,usedCarReShelf,usedCarRemove} from "@/api/secondHandCar"
export default {
  name: "mineRelease",
  mixins:[myMixins],
  data(){
    return{
      activeTab:1,
      tabList:[{value:1,name:'审核中'},{value:2,name:'已发布'},{value:3,name:'已下架'},],
      noClick:true,
      poupShow:false,
      radio:'',
      pageList:[],
      noPassNum:'',
      showEmpty:false,
      isLoading:false,
      xiajiaInfo:'',
      loading: false,
      finished: false,
      pageNum:1,
      pageSize:10,
      total:'',
      indexActiveTab:'',
    }
  },
  async mounted() {
    this.activeTab =Number(sessionStorage.getItem('mineActiveTab')) || 1;
    this.indexActiveTab=Number(sessionStorage.getItem('indexActiveTab')) || 0
    await this.getCount()
    await this.getList()
  },
  methods:{
    async onLoad(){
      this.pageNum++;
      await this.getList()
      this.loading = false;
      if (this.pageList.length >= this.total) {
        this.finished = true;
      }
    },
    onRefresh() {
      this.pageNum=1
      setTimeout(async () => {
        // this.pageList=[]
        await this.getList()
        this.$toast('刷新成功');
        this.isLoading = false;
      }, 1000);
    },
    itemHandle(item){//审核
      this.$router.push({
        name: item.type==1 ? "forSale" : 'wantBuySale',
        query: { id:item.id,queryType:3,isList:false},
      })
      sessionStorage.setItem('mineActiveTab',String(this.activeTab) )
    },
    async getList(){
      await  this.getCount()
      let res= await minePublishPageList({
        pageNum:this.pageNum,
        pageSize:this.pageSize,
        queryType:this.activeTab
      })
      this.total=res.total
      if(this.pageNum == 1){// 第一页直接赋值
        this.pageList=res.data
      }else{// 第二页数据拼接
        let preList = this.pageList;
        let arr = res.data;
        this.pageList = preList.concat(arr)
      }
    },
    async getCount(){//获取审核不通数量
      let res= await auditFailCount()
      this.noPassNum=res.data
    },
    async submitHandle(){//提交下架原因
      if(!this.radio){
        this.$toast('下架原因不能为空')
        return
      }
      if(this.xiajiaInfo.proprietary==1 && this.xiajiaInfo?.type==1 && this.radio==1){
        this.poupShow=false
        this.radio = ''
        this.$router.push({ name: "sellingInfo", query: {id:this.xiajiaInfo.id,type:'revise'}})
      }else{
        await usedCarRemove({
          id:this.xiajiaInfo.id,
          downReason:this.radio==1 ? (this.xiajiaInfo?.type==1 ? '已售出' : '已买到') : (this.xiajiaInfo?.type==1 ? '不卖了' : '不买了')
        })
        this.poupShow=false
        this.radio = ''
        await this.getList()
      }
    },
    reviseHandle(item){//修改
      this.$router.push({
        name: item.type==1 ? "carSource" : 'wantBuy',
        query: { type:1 ,id:item.id,queryType:3,homePage:false},
      })
    },
    async polishHandle(item){//檫亮
      try {
        await usedCarPolish(item.id)
        this.$toast('你的曝光度已提升')
        await this.getList()
      }catch (e){
        console.log('polishHandle',e)
      } finally {
        console.log(1122)
      }
    },
    async reShangJiaHandle(item){//重新上架
      try {
        await usedCarReShelf(item.id)
        await this.getList()
      } catch (e) {
        console.log('reShangJiaHandle',e)
      }
    },
    reviseXiajiaHandle(item){//修改下架
      console.log("修改下架",item)
      this.$router.push({ name: "sellingInfo", query: {id:item.id,type:'revise'}})
    },
    viewXiajiaHandle(item){//查看下架
      console.log("查看下架",item)
      this.$router.push({ name: "sellingInfo", query: {id:item.id,type:'view'}})
    },
    xiajiaHandle(item){//下架
      this.poupShow=true
      this.radio = ''
      this.xiajiaInfo=item
    },
    isToday(time) {
      const now = new Date();
      const targetTime = new Date(time);
      return (
          now.getFullYear() === targetTime.getFullYear() &&
          now.getMonth() === targetTime.getMonth() &&
          now.getDate() === targetTime.getDate()
      );
    },
    async tabClickHandle(i) {
      this.activeTab = i.value
      sessionStorage.setItem('mineActiveTab',String(this.activeTab) )
      this.pageNum = 1
      this.pageList = [];
      this.total=0
      await this.getList()
    },
    getStatus(i){
      switch (i){
        case 1://待审核
          return require('@/assets/secondHandCar/wait_verify.png')
        case 3://审核不通过
          return require('@/assets/secondHandCar/nopass.png')
        case 5://已发布
          return require('@/assets/secondHandCar/fabuing.png')
        case 6://已下架
          return require('@/assets/secondHandCar/alreayXiajia.png')
        case 7://信息过期
          return require('@/assets/secondHandCar/expire.png')
        case 8://下架待审核
          return require('@/assets/secondHandCar/xiajia_warting.png')
        case 9://下架不通过
          return require('@/assets/secondHandCar/xiajia_nopass.png')
        case 10://下架售出
          return require('@/assets/secondHandCar/xiajia_saled.png')
        case 11://下架报废
          return require('@/assets/secondHandCar/xiajia_scrap.png')
        default://未通过
          return require('@/assets/secondHandCar/nopass.png')
      }
    },
  },
  computed:{
    show() {
      return (this.pageList.length < 0 || this.pageList.length == 0); // 判断数组长度是否大于 0
    },
  },
}
</script>

<style scoped lang="scss">
@import '@/styles/mixin.scss';
.wrap {
  @include wh(100%, 100%);
  position: relative;
}
.headerWrap{
  width: 100%;
  box-sizing: border-box;
  padding: 15px 16px 0;
  position: relative;
  .nav {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 15px;
  }
  .bg{
    position: absolute;
    width: 100%;
    height: 120px;
    line-height: 120px;
    top: 0;
    left: 0;
    opacity: 0.5;
    z-index: -1;
  }
  .back{
    @include wh(15px,15px);
  }
  span{
    font-weight: bold;
    font-size: 16px;
    color: #203152;
  }
  .tabBar{
    display: flex;
    justify-content: space-around;
    font-size: 13px;
    color: #6A727C;
    height: 30px;
    line-height: 30px;
    span{
      background-color: #4C81F5;
      padding: 2px 6px;
      border-radius: 8px;
      font-size: 12px;
      color: #FFFFFF;
    }
  }
  .active{
    font-weight: 600;
    font-size: 15px;
    color: #06336D;
    position: relative;
  }
  .active:after {
    content: '';
    display: block;
    background: linear-gradient( 270deg, #33A3FF 0%, #176AFE 100%);
    border-radius: 2px;
    @include wh(26px,2px);
    position: absolute;
    border-radius: 2px;
    left: 50%;
    transform: translateX(-50%);
  }
}
.contentWrap{
  height: calc(100% - 100px);
  overflow-y: auto;
  background-color: #F2F2F2;
  box-sizing: border-box;
  padding: 10px 6px 0;
  margin-top: 10px;
}
.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{
    position: relative;
    .img,.qiugou{
      width: 115px;
      height: 77px;
      border-radius: 7px;
      margin-right: 10px;
      object-fit: cover;
      object-position: center;
    }
    .smallImg{
      position: absolute;
      top: 0;
      left: 0;
      width: 36px;
      height: 18px;
    }
    .qiugou{
      line-height: 77px;
      text-align: center;
      color: #FFFFFF;
      font-size: 18px;
      letter-spacing: 5px;
      background-color: #6C81CD;
    }
  }
  .right{

    .title{
      box-sizing: border-box;
      padding-right: 18px;
      font-weight: 600;
      font-size: 14px;
      color: #212020;
      line-height: 28px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      .titleImg{
        width: 43px;
        height: 14px;
      }
      .bigImg{
        width: 60px;
        height: 16px;
      }
    }
    .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: bold;
        font-size: 18px;
        line-height: 25px;
        margin-right: 2px;
      }
      span:last-child{
        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;
    }
    .btnWrap{
      width: 100%;
      box-sizing: border-box;
      padding-right: 18px;
      text-align: right;
      img{
        margin-left: 10px;
      }
      img:first-child{
        margin-left: 0;
      }
      .revise,.xiajia{
        @include wh(56px,25px)
      }
      .polish{
        @include wh(69px,25px)
      }
      .reshangjia{
        @include wh(78px,25px)
      }
    }
  }
}
.poupWrap{
  width: 100%;
  padding-top: 20px;
  padding-left: 20px;
  .title{
    font-size: 14px;
    font-weight: bold;
    color: #c8c9cc;
  }
  .radioWrap{
    display: flex;
    justify-content: space-around;
    align-items: center;
    margin-top: 7px;
    margin-bottom: 15px;
  }
  .poupContent{
    width: 100%;
    box-sizing: border-box;
    padding: 10px 20px 4px 10px;
    .btnWrap{
      width: 100%;
      text-align: -webkit-right;
      //margin-top: 20px;
      .btn{
        width: 80px;
        height: 30px;
        line-height: 30px;
        background-color: #4C81F5;
        color: #FFFFFF;
        text-align: center;
        border-radius: 8px;
      }
    }
  }
  .tip{
    color: red;
    margin-left: 25px;
    margin-bottom: 6px;
  }
}
.empty{
  @include flexTwoCenter;
  height:calc(100% - 86px);
  background-color: #FAFAFA;
  img{
    width: 100%;
  }
}
</style>
