<template>
  <div class="app-container">

    <!-- <van-nav-bar left-arrow fixed placeholder @click-left="$router.push({path:'/contracted/village/contractor'})" >
      <template #title>
        <div class="tb_main">
          B村<p><span class="tb">签字完成</span><span class="tb">异常挂起</span></p>
        </div>
      </template>
    </van-nav-bar> -->
    <contractor-header :dept-id="$route.params.deptId" :contractor-code="$route.params.cbfbm" :contractor-name="$route.params.cbfmc" :survey-status="$route.params.surveyStatus" :task-status="taskStatus"></contractor-header>

    <div class="tap_block">
      <p @click="skip('contractedVillageContractorDetail')">承包方</p>
      <p class="active">家庭成员</p>
      <p @click="skip('contractedVillageContractorWord')">承包合同</p>
      <p @click="skip('contractedVillageContractorLand')">承包地</p>
    </div>

    <div class="list_main">

      <van-swipe-cell v-for="(item, index) in familyList" :key="item.id">
        <div class="item" @click="goDetail(item.id)">
          <div class="item_left">
            <div class="item_left_tt">
              <p class="name">
                <img src="../../../../../static/images/contracted/family_icon_01.png" alt="">
                {{ item.cyxm }}
              </p>
              <p>{{ item.yhzgx }}</p>
              <p class="sex">
                {{ item. cyxb }}
                <img v-if="item.cyxb === '男'" src="../../../../../static/images/contracted/family_icon_man.png" alt="">
                <img v-else-if="item.cyxb === '女'" src="../../../../../static/images/contracted/family_icon_wman.png" alt="">
              </p>
            </div>
            <p class="id_card">
              <img src="../../../../../static/images/contracted/family_icon_02.png" alt="">
              {{ item.cyzjhm }}
            </p>
          </div>
          <p class="item_right">{{ item.sfgyr }}</p>
        </div>
        <template #right v-if="taskStatus === '2' && $route.params.surveyStatus !== '3'">
          <div class="operation">
            <!-- delete 删除 edit编辑 view查看 list榜单 -->
            <div class="opera_btn edit" @click="householdDivision(item, index)">
              <p>分户</p>
            </div>
            <div class="opera_btn view" @click="householdTransfer(item)">
              <p>移户</p>
            </div>
            <div class="opera_btn list" @click="houseHolder(item)">
              <p>设为<br/>户主</p>
            </div>
            <div class="opera_btn delete" @click="deleteFamilyMember(item.id, index)">
              <p>删除</p>
            </div>
          </div>
        </template>
      </van-swipe-cell>
      <!-- getDetail不加(), 第一个参数默认传递的是event，getDetail加(), 需要手动传递$event -->
      <p class="btn" @click="goDetail()" v-if="taskStatus === '2' && $route.params.surveyStatus !== '3'">新增</p>
    </div>
    <van-popup v-model="showContractorPopup" position="bottom">
      <van-search v-model="searchKeyword"  placeholder="请输入姓名搜索" />
      <van-picker
        show-toolbar
        :columns="filteredContractors"
        @confirm="onConfirmContractorOptions"
        @cancel="showContractorPopup = false"
      >
        <template #option="option">
          <div style="display: flex; flex-direction: column; align-items: center;">
            <div>{{option.cbfmc}}-{{option.cbfbm}}</div>
          </div>
        </template>
      </van-picker>
    </van-popup>

  </div>
</template>
<script>
  import Cookies from "js-cookie";
  import contractorHeader from "./contractorHeader";
  import { listJtcy, deleteJtcy, divisionJtcy, transferJtcy, setHouseholder } from "@/api/contracted/cbfjtcy";
  import { listCbf } from "@/api/contracted/cbf";

  export default {
    name: "contractedVillageContractor",
    components: {
      contractorHeader
    },
    data() {
      return {
        familyList: [],                 // 家庭成员列表
        contractorList: [],             // 承包方列表
        showContractorPopup: false,     // 控制承包方信息弹出层的显示和隐藏
        transferMemberId: null,         // 转出的成员ID
        taskStatus: null,               // 调查任务的完成状态：1表示已完成，2表示未完成
        searchKeyword: '',              // 弹出框查询关键字
      };
    },
    created() {
      this.taskStatus = Cookies.get('taskStatus');
      this.getList();
      this.getContractorList();
    },
    computed: {
      filteredContractors() {
        return this.contractorList.filter(item => {
          return item.cbfmc.indexOf(this.searchKeyword) !== -1;
        })
      }
    },
    methods: {
      getList(){
        listJtcy({cbfbm: this.$route.params.cbfbm, translate_dict: 1}).then(response => {
          this.familyList = response.rows;
        });
      },
      getContractorList() {
        listCbf({deptId: this.$route.params.deptId}).then(response => {
          this.contractorList = response.rows;
        });
      },
      skip(name) {
        this.$router.push({
          name: name,
          params: {
            deptId: this.$route.params.deptId,
            cbfbm: this.$route.params.cbfbm,
            cbfmc: this.$route.params.cbfmc,
            surveyStatus: this.$route.params.surveyStatus
          }
        });
      },
      goDetail(id) {
        this.$router.push({
          name: 'contractedVillageContractorFamilyDetail',
          params: {
            id: id,
            deptId: this.$route.params.deptId,
            cbfbm: this.$route.params.cbfbm,
            cbfmc: this.$route.params.cbfmc,
            surveyStatus: this.$route.params.surveyStatus
          }
        });
      },
      deleteFamilyMember(id, index) {
        this.$dialog.confirm({
          message: '是否确认删除此条家庭成员信息？',
        }).then(() => {
          // on confirm
          deleteJtcy(id).then(res => {
            if(res.code == 200){
              this.$toast.success('删除成功');
              this.familyList.splice(index, 1);
            }
          });
        }).catch(() => {
          // on cancel
        });
      },
      householdDivision(item, index) {
        if (item.yhzgx === '户主') {
          this.$toast.fail('该成员身份为户主，不能分户');
          return false;
        }
        this.$dialog.confirm({
          message: '是否确认将该成员新分一户并设为户主？',
        }).then(() => {
          // on confirm
          divisionJtcy(item.id).then(res => {
            if (res.code == 200) {
              this.$toast({
                icon: 'success',
                message: '分户成功',
                duration: "1000",
                onClose: () => {
                  this.familyList.splice(index, 1);
                }
              });
            }
          });
        }).catch(() => {
          // on cancel
        });
      },
      householdTransfer(item) {
        if (item.yhzgx === '户主') {
          this.$toast.fail('该成员身份为户主，不能移户');
          return false;
        }
        this.transferMemberId = item.id;
        this.searchKeyword = '';
        this.showContractorPopup = true;
      },
      onConfirmContractorOptions(value) {
        this.$dialog.confirm({
          message: '是否确认将该成员移到' + value.cbfmc + '户内？',
        }).then(() => {
          // on confirm
          this.showContractorPopup = false;
          transferJtcy({id: this.transferMemberId, cbfbm: value.cbfbm}).then(response => {
            if (response.code == 200) {
              this.$toast({
                icon: 'success',
                message: '移户成功',
                duration:"1000",
                onClose: () => {
                  const index = this.familyList.findIndex(item => item.id === this.transferMemberId);
                  this.familyList.splice(index, 1);
                }
              });
            }
          });
        }).catch(() => {
          // on cancel
        });
      },
      houseHolder(item) {
        if (item.yhzgx === '户主') {
          this.$toast.fail('该成员身份为户主，无需设置');
          return false;
        }
        this.$dialog.confirm({
          message: '是否确认将该成员设为户主？',
        }).then(() => {
          // on confirm
          setHouseholder(item.id).then(res => {
            if (res.code == 200) {
              this.$toast({
                icon: 'success',
                message: '设置户主成功',
                duration: "1000",
                onClose: () => {
                  this.$route.params.cbfmc = item.cyxm;
                  this.getList();
                }
              });
            }
          });
        }).catch(() => {
          // on cancel
        });
      }
    },
  };
</script>
<style scoped lang="scss">

  .app-container{
    background: #fff url("../../../../../static/images/contracted/contracted_index_bg.png") no-repeat center;
    background-size: 100% 100%;
    height: 100vh;
    padding: 0 4vw;
    overflow: hidden;
  }

  /deep/ .van-nav-bar{
    background: transparent;
  }

  /deep/ .van-nav-bar .van-icon{
    color: #000000;
  }

  /deep/ .van-hairline--bottom::after{
    border: none;
  }

  /deep/ .van-ellipsis{
    overflow: initial;
  }



  /* .tb_main{
    position: relative;
    p{
      position: absolute;
      display: inline-block;
      margin-left: 10PX;
    }
  }

  .tb{
    font-size: 12px;
    color: #ff8900;
    background: #daf6e7;
    border: 1px solid #d7be6e;
    padding: 2PX 8PX;
    border-radius: 50PX;
    margin-right: 5PX;
  } */

  .tap_block{
    width: 100%;
    display: flex;
    justify-content: space-between;
    background: #ebfaf2;
    padding: 2PX 4PX;
    border-radius: 10PX;
    margin-top: 1vh;
    .active{
      background-image: linear-gradient(to right,#c6fe8b,#48e5a2);
      box-shadow: 0 0 10PX #cccccc;
      color: #333333;
    }
    p{
      width: 25%;
      text-align: center;
      padding: 5PX 0;
      border-radius: 10PX;
      color: #666666;
    }
  }
  .list_main{
    margin-top: 2vh;
    text-align: center;
    height: 88vh;
    overflow-y: scroll;
    padding-bottom: 15vh;
    .item{
      border-radius: 30px;
      background: #fff;
      box-shadow: 4px 6px 5px rgba(63, 68, 75, 0.1);
      padding:25px 32px;
      margin-bottom: 20px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      .item_left{
        flex: 1;
        .item_left_tt{
          display: flex;
          justify-content: space-between;
          align-items: center;
          font-size: 14PX;
          .name{
            display: flex;
            align-items: center;
            font-size: 16PX;
            img{
              width: 5vw;
              margin-right: 2vw;
            }
          }
          .sex{
            display: flex;
            align-items: center;
            img{
              width: 3vw;
              margin-left: 1vw;
            }
          }
        }
        .id_card{
          display: flex;
          align-items: center;
          font-size: 14PX;
          margin-top: 1vh;
          img{
            width: 5vw;
            margin-right: 2vw;
          }
        }
      }
      .item_right{
        margin-left: 15vw;
        padding: 2PX 15PX;
        background: #d0f4e5;
        color: #2ecb8a;
        border: 1px solid #8be3be;
        border-radius: 30PX;
      }
    }
    .operation{
      display: flex;
      align-items: center;
      justify-content: right;
      text-align: center;
      background: #fff;
      box-shadow: 4px 6px 5px rgba(63, 68, 75, 0.1);
      height: 100%;
      border-radius: 30px;
      margin-left: 2vw;
      overflow: hidden;
      .opera_btn{
        height: 100%;
        display: flex;
        align-items: center;
        color: #ffffff;
        padding: 0 5vw;
        &.delete{
          background:#ff3737;
        }
        &.edit{
          background: #a5f790;
        }
        &.view{
          background: #48e5a2;
        }
        &.list{
          background: #fb9627;
        }
      }
    }
    .btn{
      background-image: linear-gradient(to right,#c6fe8b,#48e5a2);
      box-shadow: 0 0 10PX #cccccc;
      padding: 10PX 0PX;
      border-radius: 50PX;
      display: inline-block;
      width: 40%;
      margin: 2vh auto 0;
      text-align: center;
    }
  }

</style>
