<template>
  <div class="home_wrapper">
    <div
      class="header_main"
      :style="`background-image:url(${require(showBtn?'@/assets/images/sunVillage_info/list_head.png':'@/assets/images/sunVillage_info/list_head_green.png')})`"
    >
      投票
      <div class="return_btn" @click="onClickLeft"></div>
      <div class="add_btn" @click="goAdd" v-show="showBtn"></div>
    </div>
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="getList"
    >
      <van-swipe-cell v-for="(item,index) in applicationList" :key="index" >
        <van-cell
          :border="false"
          :title="item.subjectName"
          :label="'时间：'+item.startTime.substr(0,10)+' - '+item.endTime.substr(0,10)"
          center
          :to="{name: item.isVote || showBtn ? 'sunVillageInfoListVoteDetail':'sunVillageInfoListVoteForm',query:{id:item.id,type:show?'':'code'}}"
        >
          <template #right-icon>
            <p style="color: #1D6FE9">投票</p>
          </template>
          <template #icon>
            <img src="../../assets/images/sunVillage_info/icon_vote.png" style="width: .5rem;margin-right: 2%;">
          </template>
        </van-cell>
        <template #right v-if="showBtn">
          <div style="background-color: #ee0a24;height: 100%" @click="goRemove(item.id)">删除</div>
          <div style="background-color: #07c160" @click="goEdit(item.id)">修改</div>
          <div style="background-color: rgb(255, 166, 62);" v-if="item.status != '3'" @click="goRanking(item.id)">发布</div>
        </template>
      </van-swipe-cell>
    </van-list>
  </div>
</template>

<script>
  import { listPoll , delPoll , publicPoll } from "@/api/sunVillage_info/fixedAssets";
  import Cookies from "js-cookie";
  import request from '@/utils/request'
  export default {
    name: "certificateList",
    data() {
      return {
        applicationList:[],
        applicationListSecond:[],
        assetStatusOptions:[],
        auditStatus:[],
        loading: false,
        finished: false,
        show: false,
        showTab: false,
        fileList:[],
        listLength:'0',
        searchInput:'',
        queryParams:{
          pageNum:1,
          pageSize:10,
          orderByColumn:'createTime',
          isAsc:'desc',
        },
        uploadFiles1:[],
        projectId:'',
        projectIndex:'',
        showBtn:true,
      };
    },
    created() {
      this.queryParams.bookId = Cookies.get('bookId');
      this.queryParams.deptId = Cookies.get('deptId');
      if (Cookies.get('user')){
        this.queryParams.userId = JSON.parse(Cookies.get('user')).id
      }
      if (this.$route.query.type == 'code'){
        this.queryParams.status = '1,3'
        this.showBtn = false;
      }
    },
    methods: {
      getList(){
        var _this = this;
        listPoll(_this.queryParams.deptId,_this.queryParams).then(response => {
          _this.listLength = response.total;
          response.rows.map(res=>{
            _this.applicationList.push(res);
          })

          if(_this.applicationList.length >= response.total){
            _this.finished = true;
            return;
          }else{
            _this.loading = false;
            _this.queryParams.pageNum += 1 ;
          }
        });
      },
      goAdd(){
        this.$router.push('/sunVillage_info/list_vote_add')
      },
      goDetail(id){
        this.$router.push({path:'/sunVillage_info/list_vote_detail',query: {id:id}})
      },
      goEdit(id){
        this.$router.push({path:'/sunVillage_info/list_vote_edit',query: {id:id}})
      },
      goRanking(id){
        this.$dialog.alert({
          title: '提示',
          message: '确认发布？',
          showCancelButton:true,
        })
          .then(() => {
            publicPoll(id).then(response => {
              this.$notify({ type: 'success', message: '发布成功' });
              this.applicationList = [];
              this.getList()
            });
          })
          .catch(() => {
            // on cancel
          });
      },
      goRemove(id){
        this.$dialog.alert({
          title: '提示',
          message: '确认删除？',
          showCancelButton:true,
        })
          .then(() => {
            delPoll(id).then(response => {
              this.$notify({ type: 'success', message: '删除成功' });
              this.applicationList = [];
              this.getList()
            });
          })
          .catch(() => {
            // on cancel
          });

      }
    },
  }
</script>

<style scoped lang="scss">
  .home_wrapper{
    background: #e9e9e9;
    min-height: 100vh;
    width: 100vw;
    .header_main{
      height: 116px;
      background: url('../../assets/images/sunVillage_info/list_head.png') no-repeat;
      background-size: 100% 100%;
      position: fixed;
      top: 0;
      left: 0;
      width: 100%;
      font-size: 36px;
      line-height: 116px;
      text-align: center;
      color: #fff;
      position: relative;
      .return_btn{
        width: 24px;
        height: 43.2px;
        background: url('../../assets/images/sunVillage_info/list_icon_5.png') center center no-repeat;
        background-size: 20px 36px;
        position: absolute;
        left: 38px;
        top: 36px;
      }
      .add_btn{
        width: 56.4px;
        height: 40.8px;
        background: url('../../assets/images/sunVillage_info/list_icon_9.png') center center no-repeat;
        background-size: 47px 34px;
        position: absolute;
        right: 38px;
        top: 36px;
      }
    }
    .van-swipe-cell {
      position: relative;
      overflow: hidden;
      cursor: grab;
      width: 96%;
      margin: 0 auto;
      margin-top: 15PX;
      box-shadow: 5PX 5PX 2PX #ccc;
      border-radius: 10PX;
    }
    /deep/ .van-swipe-cell__right{
      display: flex;
      align-items: center;
      width: 150PX;
      margin-left: 5PX;
      a,div{
        margin: 0;
        display: flex;
        align-items: center;
        justify-content: center;
        color: #ffffff;
        font-size: 14PX;
        height: 100%;
        flex: 1;
      }
    }
  }
</style>
