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

      <div class="header_main">
        家庭成员
        <div class="return_btn" @click="onClickLeft"></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 center :to="{name:'familyMemberDetail', query: {id:item.id}}" >
            <template #title>
              {{item.memberName}} <span style="font-size: 14px;color: #1D6FE9;margin-left: 5px">{{item.phone}}</span>
            </template>
            <template #default>
              <p style="display: flex;flex-direction: column;align-items: center;">
                <span style="color: #1D6FE9;display: block;">{{item.familyStatus}}</span>
                <span style="color: #1D6FE9;display: block;">{{item.sex}}</span>
              </p>
            </template>
            <template #icon>
              <van-icon name="../../../static/images/onlineHome/jtcy2.png" size="30" color="#539FFD" style="margin-right: 10px;" />
            </template>
            <template #label>
              <p><b style="color: #539FFD;">{{item.idcard}}</b></p>
            </template>
          </van-cell>
        </van-swipe-cell>
      </van-list>
    </div>
</template>

<script>
import { getFamilyMemberList, removeFamilyMember } from "@/api/sunVillage_info/homestead/familyMember";
import Cookies from "js-cookie";
export default {
  name: "familyMemberList",
  data() {
    return {
      applicationList:[],
      houseApplyStatus:[],
      familyStatus:[],
      sexStatus:[],
      tcfsStatus:[],
      auditStatus:[],
      loading: false,
      finished: false,
      idcard:null,
      farmerCode:null,
      queryParams:{
        pageNum:1,
        pageSize:10,
        farmerCode:null,
        deptId: Cookies.get('deptId'),
        orderByColumn:'createTime',
        isAsc:'desc',
        // 申请类型  1-宅基地退出
        sqlx: '1',
        // 退出类型  01-有偿退出   02-无偿退出
        tclx: '01',
      }
    };
  },
  created() {
    this.houseGetDicts("family_status").then((res) => {
      this.familyStatus = res.data;
    });
    this.houseGetDicts("sex").then((res) => {
      this.sexStatus = res.data;
    });
    this.houseGetDicts("tcfs").then((res) => {
      this.tcfsStatus = res.data;
    });
    this.queryParams.farmerCode = Cookies.get('farmerCode');
    this.houseGetDicts("yctcsp_status").then((res) => {
      // this.auditStatus = res.data;
      let _this = this;
      res.data.forEach((item) => {
        if (item.dictValue < 5) {
          _this.auditStatus.push(item);
        } else if (item.dictValue == 5) {
          item.dictLabel = "审核通过";
          _this.auditStatus.push(item);
        }
      });
    });
  },
  methods: {
    goAdd(){
      window.location = '/sunVillage_info/paidExitAdd';
    },
    getList(){
      setTimeout(() => {
        getFamilyMemberList(this.queryParams).then(response => {
          for (var i = 0; i < response.rows.length; i++) {
            response.rows[i].familyStatus = this.selectDictLabel(this.familyStatus, response.rows[i].familyStatus);
            response.rows[i].sex = this.selectDictLabel(this.sexStatus, response.rows[i].sex);
            if(response.rows[i].phone !== null){
              response.rows[i].familyStatus = response.rows[i].familyStatus;
              // + " "+ response.rows[i].phone
            }
            let res = this.parseIDCard( response.rows[i].idcard);
            if(res)
            {
              this.$set(response.rows[i], 'age', res[1]);
            }
            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);
    },
    parseIDCard(idcard) {
      if(!idcard)
        return false;
      if(idcard.length !== 18)
        return false;
      if(!/^[1-9]\d{5}(18|19|20|(3\d))\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/.test(idcard))
        return false;
      let sex = (parseInt(idcard.substr(16, 1)) % 2) ^ 1;
      let now = new Date().getFullYear();
      let y = parseInt(idcard.substr(6, 4));
      let age = Math.max(now - y, 0);
      return [sex, age];
    },
    deleteList(id,index){
      this.$dialog.confirm({
        message: '您确认删除此条信息？',
      })
      .then(() => {
        // on confirm
        this.applicationList.splice(index,1)
        removeFamilyMember(id).then(res => {
          if(res.code = 200){
            this.$toast.success('删除成功');
          }
        });
      })
      .catch(() => {
        // on cancel
      });
    },
  },
}
</script>

<style scoped lang="scss">
  .app-container {
    .header_main{
      height: 116px;
      background: url('../../../assets/images/sunVillage_info/list_head_green.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;
    text-align: right;
    display: flex;
    justify-content: right;
  }
  /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>
