<template>
  <div>
    <van-nav-bar
      left-arrow
      title="证书查看"
      fixed
      placeholder
      @click-left="$router.back()"
    >
      <template #right>
        <van-icon v-if="false" name="@/../static/images/icon/icon_flow.png" size="20" @click="openFilter"/>
      </template>
    </van-nav-bar>

    <van-pull-refresh v-model="refreshing" @refresh="getList()">
      <van-list
        v-model="loading"
        :finished="finished"
        :immediate-check="false"
        finished-text="没有更多了"
        @load="getList('+1')"
      >
        <van-swipe-cell v-for="(item,index) in list" :key="index" class="delegate">
          <van-cell :label="item.idcard" center>
            <template #icon>
              <van-icon name="@/../static/images/onlineHome/icon_zjd7.png" size="30" color="#7790FE" style="margin-right: 10px;" />
            </template>
            <template #title>
              <p style="font-weight: bold;">{{item.projectName}}</p>
            </template>
            <template #right-icon>
              <van-button plain type="info" style="border: 0; font-weight: bold;" @click="openLicense(item)">查看证书</van-button>
            </template>
          </van-cell>
          <template #right>
            <van-row style="height: 100%;">
              <van-col style="height: 100%;">
                <van-button square text="查看申请" type="primary" style="height: 100%;" @click="viewItem(item)"/>
              </van-col>
              <van-col style="height: 100%;">
                <van-button square text="许可证修改" type="info" style="height: 100%;" @click="editLicensePermit(item)"/>
              </van-col>
              <van-col style="height: 100%;">
                <van-button square text="批准书修改" type="info" style="height: 100%;" @click="editLicenseRatification(item)"/>
              </van-col>
            </van-row>
          </template>
        </van-swipe-cell>
      </van-list>
    </van-pull-refresh>

    <onlineHomeIndex :current="1"></onlineHomeIndex>

    <van-popup
      v-model="filterVisible"
      closeable
      position="top"
      :close-on-click-overlay="true"
      :lazy-render="false"
    >
      <van-form>
        <van-cell title="筛选查询"></van-cell>
        <van-field v-model="queryParams.memberName" label="申请户主" placeholder="申请户主" input-align="right"/>
        <van-field v-model="queryParams.phone" label="联系电话" placeholder="联系电话" input-align="right"/>
        <div style="margin: 0.2rem; text-align: right;">
          <van-button type="info" native-type="submit" size="small" @click="resetQuery"> 重置 </van-button>
          <van-button type="primary" native-type="submit" size="small" @click="getList(0)"> 查询 </van-button>
        </div>
      </van-form>
    </van-popup>
  </div>
</template>

<script>
import FieldSelect from "@/components/form/FieldSelect";
import onlineHomeIndex from "@/views/onlineHomeIndex";
import { getList } from "@/api/onlineHome/homestead/application";

export default {
  components: {FieldSelect, onlineHomeIndex},
  name: "HouseLicense",
  data() {
    return {
      list: [],
      total: 0,
      // ?pageNum=1&pageSize=10&orderByColumn=createTime&isAsc=desc&printLicense=Y
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        printLicense: 'Y',
        orderByColumn: 'createTime',
        isAsc: 'desc',
        phone: '',
        memberName: '',
      },
      refreshing: false,
      loading: false,
      finished: false,
      options: {
      },
      filterVisible: false,
    }
  },
  created() {
    this.initOptions();
    this.getList();
  },
  methods: {
    getList(target) {
      let type = typeof (target);
      console.log(type, target);
      if (target === 0) {
        this.refreshing = true;
        this.finished = true;
        this.total = 0;
        this.queryParams.pageNum = 1;
        this.list = [];
        this.filterVisible = false;
      }
      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.resetQuery();
        this.total = 0;
        this.queryParams.pageNum = 1;
        this.list = []
      }
      getList(this.queryParams).then((response) => {
        console.log(response)
        if (response.rows.length === 0) {
          this.finished = true;
          return;
        }
        response.rows.forEach((e) => {
          this.list.push(e);
        });
        this.total += response.rows.length;
        this.finished = this.total >= response.total;
      }).catch(() => {
        this.finished = true;
      }).finally(() => {
        this.loading = false;
        this.refreshing = false;
      });
    },
    viewItem(item) {
      this.$router.push({
        name: 'proposerLite',
        query: {
          id: item.id,
          type: 'view',
        },
      });
    },
    initOptions() {
      for(let k in this.options)
      {
        this.houseGetDicts(k).then((res) => {
          this.options[k] = res.data;
        });
      }
    },
    formatDict(dict, value) {
      return this.selectDictLabel(dict, value);
    },
    openFilter() {
      this.filterVisible = true;
    },
    resetQuery() {
      this.queryParams.phone = '';
      this.queryParams.memberName = '';
    },
    openLicense(item) {
      this.$router.push({
        name: 'certificateSee',
        query: {
          id: item.id,
        },
      });
    },
    editLicensePermit(item) {
      this.$router.push({
        name: 'licensePermitEdit',
        query: {
          id: item.id,
        },
      });
    },
    editLicenseRatification(item) {
      this.$router.push({
        name: 'licenseRatificationEdit',
        query: {
          id: item.id,
        },
      });
    },
  },
}
</script>

<style scoped>
.delegate {
  width: 96%;
  margin: 3% 2% 3% 2%;
  border-radius: 0.18rem;
  overflow: hidden;
  box-shadow: 0.1rem 0.1rem 0.15rem 0.02rem rgba(0,0,0,0.16);
}
</style>
