<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>

      <van-pull-refresh v-model="refreshing" @refresh="refresh">
        <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="getList"
      >
        <van-swipe-cell v-for="(item,index) in applicationList" :key="index">
          <van-cell :title="item.ywh" 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'">{{ item.houseApplyStatusName}} </span>
            <span v-else-if="item.auditStatus !== '0' && item.auditStatus === '2' " style="color: #F56C6C">{{ item.houseApplyStatusName + ' ● 已驳回' }} </span>
            <span  v-else style="color: #67c23a;">{{item.houseApplyStatusName }} </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.houseApplyStatusName=='申请草稿'" @click="submitApplyProposer(item)" class="delete-button" />
              </van-col>
              <van-col>
                <van-button square text="修改" type="info" v-if="item.houseApplyStatusName=='申请草稿'" :to="{name:'proposerLite', query: {id:item.id, type: 'modify'}}" class="delete-button" />
              </van-col>
              <van-col>
                <van-button square text="删除" type="danger" v-if="item.houseApplyStatusName=='申请草稿'" @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/onlineHome/homestead/application";
import { customSubmit, customSubmitWLHT} from "@/api/onlineHome/homestead/application";
export default {
  name: "applicationList",
  data() {
    return {
      applicationList:[],
      houseApplyStatus:[],
      loading: false,
      finished: false,
      refreshing: false,
      queryParams:{
        pageNum:1,
        pageSize:10,
        orderByColumn:'createTime',
        isAsc:'desc'
      }
    };
  },
  created() {
    this.houseGetDicts("house_apply_status").then((response) => {
      console.log(response)
      this.houseApplyStatus = response.data;
    });
  },
  methods: {
    goAdd(){
      window.location = 'applicationAdd';
    },
    goAddLite(){
      //window.location = 'applicationAdd';
      window.location = 'proposerLite?type=add';
    },
    viewItem(row){
      let type = /*row.houseApplyStatus == '1' // 申请草稿
        ||*/ row.houseApplyStatus == '10' // 申请通过
        || row.houseApplyStatus == '12' // 开工草稿
        || row.houseApplyStatus == '18' // 开工通过
        || row.houseApplyStatus == '20' // 验收草稿
          ? 'modify' : 'view';
      window.location = 'proposerLite?type=' + type + '&id=' + row.id;
    },
    getList(){
      setTimeout(() => {
        getList(this.queryParams).then(response => {
          console.log(response)
          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]);
          }
          if(this.applicationList.length >= response.total){
            this.finished = true;
            return;
          }else{
            this.loading = false;
            this.queryParams.pageNum += 1 ;
          }
        }).finally(() => {
          this.loading = false;
          this.refreshing = false;
        });
      }, 1000);
    },
    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.refreshing = true;
      this.finished = false;
      this.applicationList = []
    },
    submitApplyProposer(item) {
      this.$dialog.confirm({
        message: '您确认提交申请草稿？',
      }).then(() => {
        let func = 1 || item.processKey.endsWith('WLHT') ? customSubmitWLHT : customSubmit; //TODO: 总是走简化流程
        func(item.id).then((resp) => {
          this.$toast.success("提交成功");
          this.refresh();
        }).catch((e) => {
          this.$toast.fail("提交失败!");
        });
      }).catch(() => {});
    },
  },
}
</script>

<style scoped lang="scss">
  .app-container {
    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>
