<template>
    <div class="app-container">
<!--      <van-nav-bar-->
<!--        left-arrow-->
<!--        fixed-->
<!--        placeholder-->
<!--        @click-left="$router.back(-1)"-->
<!--        @click-right="goAddLite()"-->
<!--      >-->
<!--        <template #title>-->
<!--          <p style="font-weight: bold;">宅基地申请</p>-->
<!--        </template>-->
<!--        <template #right>-->
<!--          <van-icon name="add" size="18"/>-->
<!--        </template>-->
<!--      </van-nav-bar>-->

      <div class="header_main">
        宅基地申请
        <div class="return_btn" @click="onClickLeft"></div>
        <div class="add_btn" @click="goAddLite"></div>
      </div>

      <van-pull-refresh v-model="refreshing" @refresh="getList()">
        <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="getList('+1')"
      >
        <van-swipe-cell v-for="(item,index) in applicationList" :key="index">
          <van-cell :title="item.sqhhzxm" center @click="viewItem(item)">
<!--          <van-cell :title="item.ywh" :value="item.houseApplyStatusName" center @click="viewItem(item)">-->
<!--            <template #icon>-->
<!--              <van-icon name="../../../static/images/onlineHome/icon_zjd1.png" size="30" color="#539FFD" style="margin-right: 10px;" />-->
<!--            </template>-->
<!--            <template #label>-->
<!--              <p>{{item.projectName}}</p>-->
<!--            </template>-->
            <template #icon>
              <van-icon name="../../../static/images/onlineHome/icon_zjd1.png" size="30" color="#539FFD" style="margin-right: 10px;" />
            </template>
            <span v-if="item.auditStatus === '0'">{{ formatDict(houseApplyStatus, item.homeApplyStatus) }} </span>
            <span v-else-if="item.auditStatus === '2' " style="color: #F56C6C">{{ formatDict(houseApplyStatus, item.homeApplyStatus) + ' ● 已驳回' }} </span>
            <span  v-else style="color: #67c23a;">{{ formatDict(houseApplyStatus, item.homeApplyStatus) }} </span>
            <template #label>
              <p>{{item.projectName}}</p>
            </template>
          </van-cell>
          <template #right>
            <van-row>
              <van-col>
                <van-button square text="提交" type="primary" v-if="item.homeApplyStatus=='11' || item.homeApplyStatus=='31' || item.homeApplyStatus=='71'" @click="submitApplyProposer(item)" class="delete-button" />
              </van-col>
              <van-col>
                <van-button square text="修改" type="info" v-if="item.homeApplyStatus=='11' || item.homeApplyStatus=='31' || item.homeApplyStatus=='71'" :to="{name:'homesteadProposerLite', query: {id:item.id, type: 'modify'}}" class="delete-button" />
              </van-col>
              <van-col>
                <van-button square text="删除" type="danger" v-if="item.homeApplyStatus=='11'" @click="deleteList(item.id,index)" class="delete-button" />
              </van-col>
            </van-row>
          </template>
        </van-swipe-cell>
      </van-list>
      </van-pull-refresh>
    </div>
</template>

<script>
import { getList , removeList } from "@/api/sunVillage_info/homestead/application";
import { customSubmitWLHT, submitStartWLHT, submitEndWLHT,} from "@/api/sunVillage_info/homestead/application";
import {updateOpretion} from "@/api/sunVillage_info/homestead/application";
import Cookies from "js-cookie";
export default {
  name: "applicationList",
  data() {
    return {
      applicationList:[],
      houseApplyStatus:[],
      loading: false,
      finished: false,
      refreshing: false,
      deptId:null,
      idcard:null,
      total: 0,
      queryParams:{
        pageNum:1,
        pageSize:10,
        deptId:null,
        orderByColumn:'createTime',
        isAsc:'desc'
      }
    };
  },
  created() {
    this.houseGetDicts("home_stage_status").then((response) => {
      this.houseApplyStatus = response.data;
    });
    this.deptId = this.$store.state.user.loginDeptId;
    this.$set(this.queryParams, "deptId", this.deptId);
    this.getList();
  },
  methods: {
    goAdd(){
      window.location = 'applicationAdd';
    },
    goAddLite(){
      //window.location = 'applicationAdd'
      this.$router.push({name:'homesteadProposerLite',query:{type:"add"}})
    },
    viewItem(row){
      let type = row.houseApplyStatus == '11' // 申请草稿
        || row.houseApplyStatus == '1F' // 申请通过
        || row.houseApplyStatus == '31' // 开工草稿
        || row.houseApplyStatus == '3F' // 开工通过
        || row.houseApplyStatus == '71' // 验收草稿
          ? 'modify' : 'view';
      this.$router.push({name:'homesteadProposerLite',query:{type:type,id:row.id}})
    },
    getList(target){
      let type = typeof (target);
      console.log(type, target);
      if(target && this.finished)
        return;
      if (target === 0) {
        this.refreshing = true;
        this.finished = true;
        this.total = 0;
        this.queryParams.pageNum = 1;
        this.applicationList = [];
      }
      else if (type === 'number')
        this.queryParams.pageNum = target;
      else if (type === 'string') {
        this.queryParams.pageNum = eval(this.queryParams.pageNum + target)
      }
      else
      {
        this.refreshing = true;
        this.finished = true;
        this.total = 0;
        this.queryParams.pageNum = 1;
        this.applicationList = []
      }
      getList(this.queryParams).then(response => {
        console.log(response)
        if (response.rows.length === 0) {
          this.finished = true;
          return;
        }
        for (var i = 0; i < response.rows.length; i++) {
          /*var houseApplyStatusName = this.selectDictLabel(this.houseApplyStatus, response.rows[i].houseApplyStatus);
          // if(response.rows[i].auditStatus !== '0' && response.rows[i].auditStatus === '2'){
          //   houseApplyStatusName = houseApplyStatusName+ ' ● 已驳回';
          // }
          response.rows[i].houseApplyStatusName = houseApplyStatusName;*/
          this.applicationList.push(response.rows[i]);
        }
        this.total += response.rows.length;
        this.finished = this.total >= response.total;
      }).finally(() => {
        this.loading = false;
        this.refreshing = false;
      });
    },
    formatDict(dict, value) {
      return this.selectDictLabel(dict, value);
    },
    deleteList(id,index){
      this.$dialog.confirm({
        message: '您确认删除申请草稿？',
      })
      .then(() => {
        // on confirm
        this.applicationList.splice(index,1)
        removeList(id).then(res => {
          if(res.code = 200){
            this.$toast.success('删除成功');
          }
        });
      })
      .catch(() => {
        // on cancel
      });
    },
    refresh() {
      this.getList(); return;
      this.applicationList = [];
      this.queryParams.pageNum = 1;
      this.refreshing = true;
      this.finished = false;
    },
    submitApplyProposer(item) {
        this.$router.push({name:'homesteadProposerLite',query:{type:"edit",id:item.id}})
        // if(item.homeApplyStatus === '11'){
        //   customSubmitWLHT(item.id).then((resp) => {
        //     this.$toast.success("提交成功");
        //     this.refresh();
        //   }).catch((e) => {
        //     this.$toast.fail("提交失败!");
        //   });
        // }else if(item.homeApplyStatus === '31'){
        //   submitStartWLHT(item.id).then((resp) => {
        //     this.$toast.success("提交成功");
        //     this.refresh();
        //   }).catch((e) => {
        //     this.$toast.fail("提交失败!");
        //   });
        // }else if(item.homeApplyStatus === '71'){
        //   submitEndWLHT(item.id).then((resp) => {
        //     this.$toast.success("提交成功");
        //     this.refresh();
        //   }).catch((e) => {
        //     this.$toast.fail("提交失败!");
        //   });
        // }
    },
  },
}
</script>

<style scoped lang="scss">
  .app-container {
    .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;
      }
    }
  }
  /deep/.van-list{
    padding: 0.2rem 3%;
  }
  /deep/.van-cell__title{
    flex: 0.7;
  }
  /deep/.van-cell__title span{
    font-family: Arial;
    font-size: 0.4rem;
    font-weight: normal;
  }
  /deep/.van-cell__value{
    flex: 0.3;
    color: #1D6FE9;
    font-weight: bold;
  }
  /deep/.van-swipe-cell{
    margin-bottom: 0.2rem;
    border-radius: 0.2rem;
    overflow: hidden;
    box-shadow: 0px 3px 6px 0px rgba(0,0,0,0.16);
  }
  /deep/van-ellipsis{
    font-weight: bold;
  }
  .van-row{
    height: 100%;
  }
  .van-col{
    height: 100%;
  }
  .delete-button {
    height: 100%;
  }
</style>
