<template>
    <div class="app-container">
      <van-nav-bar
        left-arrow
        fixed
        placeholder
        @click-left="$router.back(-1)"
        @click-right="goAdd"
      >
        <template #title>
          <p style="font-weight: bold;">使用权抵押</p>
        </template>
        <template #right>
          <van-icon name="add" size="18" />
        </template>
      </van-nav-bar>
      <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.zjddm" :value="item.auditStatus" center :to="{name:'mortgageDetail', query: {id:item.id}}" >
            <span :style="{color: item.auditStatus == '2' ? '#F56C6C' : (item.auditStatus == '0' ? '#67c23a' : ''),}">{{ item.auditStatus == '2' ? '已驳回' : formatDict(auditStatus, item.auditStatus) }} </span>
            <template #icon>
              <van-icon name="../../../static/images/onlineHome/icon_zjd4.png" size="30" color="#539FFD" style="margin-right: 10px;" />
            </template>
            <template #label>
              <p><b style="color: #3AC2DB;">{{item.dyfs}}</b><i style="margin-right: 0.5rem;"></i><b style="color: #333333;">{{item.dyr}}</b><i style="margin-right: 0.5rem;"></i>{{item.dybdclx}}</p>
            </template>
          </van-cell>
          <template #right>
            <van-row>
              <van-col>
                <van-button square text="修改" v-if="item.auditStatus=='0' || item.auditStatus=='2'" :to="{name:'mortgageModify', query: {id:item.id}}" type="info" class="delete-button" />
              </van-col>
              <van-col>
                <van-button square text="提交" type="primary" v-if="item.auditStatus=='0' || item.auditStatus=='2'" class="delete-button" @click="goSubmit(item)" />
              </van-col>
              <van-col>
                <van-button square text="删除" v-if="item.auditStatus=='0' || item.auditStatus=='2'" @click="deleteList(item.id,index)" type="danger" class="delete-button" />
              </van-col>
            </van-row>
          </template>
        </van-swipe-cell>
      </van-list>
    </div>
</template>

<script>
import { getList , removeList, dyApply } from "@/api/onlineHome/homestead/mortgage";
import {Notify} from "vant";
export default {
    name: "mortgageList",
  data() {
    return {
      applicationList:[],
      houseApplyStatus:[],
      dybdclxStatus:[],
      auditStatus:[],
      dyfsStatus:[],
      loading: false,
      finished: false,
      queryParams:{
        pageNum:1,
        pageSize:10,
        orderByColumn:'createTime',
        isAsc:'desc'
      }
    };
  },
  created() {
    this.houseGetDicts("dybdclx").then((res) => {
      this.dybdclxStatus = res.data;
    });
    this.houseGetDicts("dyfs").then((res) => {
      this.dyfsStatus = res.data;
    });
    this.houseGetDicts("audit_status").then((res) => {
      this.auditStatus = res.data;
    });
  },
  methods: {
    goAdd(){
      window.location = 'mortgageAdd';
    },
    getList(){
      setTimeout(() => {
        getList(this.queryParams).then(response => {
          console.log(response)
          for (var i = 0; i < response.rows.length; i++) {
            response.rows[i].dybdclx = this.selectDictLabel(this.dybdclxStatus, response.rows[i].dybdclx)
            response.rows[i].dyfs = this.selectDictLabel(this.dyfsStatus, response.rows[i].dyfs)
            this.applicationList.push(response.rows[i]);
          }
          if(this.applicationList.length >= response.total){
            this.finished = true;
            return;
          }else{
            this.loading = false;
            this.queryParams.pageNum += 1 ;
          }
        });
      }, 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
      });
    },
    formatDict(dict, value) {
      return this.selectDictLabel(dict, value);
    },
    goSubmit(item){
      this.$dialog.confirm({
        message: '确认提交该草稿？',
      })
        .then(() => {
          // on confirm
          dyApply(item.id).then(res => {
            if(res.code = 200){
              this.$toast.success('提交成功');
              this.refresh();
            }
          });
        })
        .catch(() => {
          // on cancel
        });
    },
    refresh() {
      this.finished = false;
      this.queryParams.pageNum = 1;
      this.applicationList = [];
      this.getList();
    },
  },
}
</script>

<style scoped lang="scss">
  .app-container {
    padding: 0.2rem 3%;
  }
  /deep/.van-cell__title{
    flex: 0.8;
  }
  /deep/.van-cell__title span{
    font-family: Arial;
    font-size: 0.4rem;
    font-weight: normal;
  }
  /deep/.van-cell__value{
    flex: 0.2;
    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;
  }
  .delete-button {
    height: 100%;
  }
  .van-row{
    height: 100%;
  }
  .van-col{
    height: 100%;
  }
</style>
