<template>
  <div class="main_box">
    <navbar title="网格详情"></navbar>
    <div class="wrapper">
      <div class="grid_item">
        <div class="grid_item_top">
          <div class="icon_box">
            <van-icon name="wap-home" class="grid_item_top_icon"></van-icon>
          </div>
          <div class="grid_item_top_title">{{ item.gridName }}</div>
        </div>
        <div class="grid_item_middle">
          <div class="grid_item_middle_item" v-if="item.custType == '2'">
            <div class="middle_title">企业客户数</div>
            <div class="middle_num">{{ item.businessCount || 0 }}</div>
          </div>
          <!-- <div class="line"></div> -->
          <div class="grid_item_middle_item" v-if="item.custType == '1'">
            <div class="middle_title">商户客户数</div>
            <div class="middle_num">{{ item.merchantCount || 0 }}</div>
          </div>
          <!-- <div class="line"></div> -->
          <div class="grid_item_middle_item" v-if="item.custType == '0'">
            <div class="middle_title">个人客户数</div>
            <div class="middle_num">{{ item.retailCount || 0 }}</div>
          </div>
        </div>
        <div class="grid_item_bottom">
          <div class="grid_item_bottom_item">
            <span class="bottom_item_left">业务部门归属：</span>
            <span>{{
              item.opsDept == "0"
                ? "零售部"
                : item.opsDept == "1"
                ? "公司部"
                : item.opsDept == "2"
                ? "运管部"
                : ""
            }}</span>
          </div>
          <div class="grid_item_bottom_item">
            <span class="bottom_item_left">责任类型：</span>
            <span>{{
              item.gridDutyType == "1"
                ? "责任网格"
                : item.gridDutyType == "2"
                ? "竞争网格"
                : ""
            }}</span>
          </div>
          <div class="grid_item_bottom_item">
            <span class="bottom_item_left">关联支行：</span>
            <span>{{ item.relateBranchNames || item.branchNames }}</span>
          </div>
          <div class="grid_item_bottom_item"  v-if="(item.activeTab == 0 && item.gridLevel == '2')||item.activeTab == 1||item.activeTab == 2">
            <span class="bottom_item_left">归属客户经理：</span>
            <span>{{ item.relateUserNames || item.userNames }}</span>
          </div>
          <div
            class="grid_item_bottom_item"
            v-if="item.activeTab == 0 && item.gridLevel == '1'"
          >
            <span class="bottom_item_left">支行行政网格个数：</span>
            <span>{{
              (item.secGrids && item.secGrids.split(",").length) || 0
            }}</span>
            <!-- <span
              @click="openDetail(item.secGrids)"
              class="branch-detail"
              v-if="item.secGrids && item.secGrids.split(',').length > 0"
              >详情</span
            > -->
          </div>
          <div
            class="grid_item_bottom_item"
            v-if="item.activeTab == 0 && item.gridLevel == '2'"
          >
            <span class="bottom_item_left">所属总行行政网格：</span>
            <span>{{ item.parentGridName }}</span>
          </div>
          <div class="grid_item_bottom_item" v-if="item.activeTab !== 0">
            <span class="bottom_item_left">关联网点：</span>
            <span>{{ item.outletNames }}</span>
          </div>
        </div>
      </div>
      <!-- <div class="kh_title">考核情况</div>
      <div class="kh_box">
        <span class="tigong">行社提供数据</span>
        <span>0</span>
      </div> -->
      <div class="kh_title">客户明细</div>
      <div class="search-input">
        <van-search
          type="text"
          v-model="custName"
          placeholder="请输入客户名称进行模糊查询"
          @search="onSearch"
          @clear="onClear"
        />
        <div class="search_btn" @click="onSearch">
          <van-icon name="search"></van-icon>
        </div>
      </div>
      <!-- 客户 -->
      <div class="firm_box">
        <!-- <div class="firm_box_top">
          <div>
            {{
              item.custType == "0"
                ? "个人客户"
                : item.custType == "1"
                ? "商户客户"
                : item.custType == "2"
                ? "企业客户"
                : ""
            }}
          </div>
        </div> -->
        <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
          <van-list
            v-model="loading"
            :finished="finished"
            finished-text="没有更多了"
            :immediate-check="false"
            @load="onLoad"
          >
            <div class="firm_item" v-for="(i, index) in list" :key="index">
              <div class="item_top" v-if="item.custType == '0'">
                <div class="item_top_left">
                  <div
                    :class="
                      item.custGender == '男' ? 'userMan_box' : 'userWoMan_box'
                    "
                  >
                    <van-icon name="manager item_user"></van-icon>
                  </div>
                  <div class="item_name">{{ i.custName }}</div>
                </div>
                <div class="special_row">
                  <div class="item_viewDetailnew" @click="handleViewDetail(item.custType,i.custId)">
                    查看
                  </div>
                  <div class="item_viewDetailnew" @click="handleAppointment(i)">
                    预约
                  </div>
                </div>
                
              </div>
              <div class="firm_item_top" v-else>
                <div class="firm_item_top_left">
                  <div class="firm_icon_box">
                    <img
                      src="../../images/part_one/icon_company.png"
                      alt=""
                      style="width: 14px; height: 14px"
                    />
                  </div>
                  <div class="firm_item_name">{{ i.custName }}</div>
                </div>
                <div class="special_row">
                  <div class="item_viewDetailnew" @click="handleViewDetail(item.custType,i.custId)">
                    查看
                  </div>
                  <div class="item_viewDetailnew" @click="handleAppointment(i)">
                    预约
                  </div>
                </div>
              </div>
              <div class="item_main" v-if="item.custType == '0'">
                <div class="main_item">
                  <span class="mian_title">身份证号：</span>{{ i.custId?i.custId.substring(3):'' }}
                </div>
                <div class="main_item">
                  <span class="mian_title">性别：</span>{{ i.custGender }}
                </div>
                <div class="main_item">
                  <span class="mian_title">联系方式：</span>
                  <a :href="'tel:'+ i.lpTel">{{ i.lpTel }}</a>
                  <van-icon name="phone" v-if="i.lpTel" class="phone-box"/>
                </div>
                <div class="main_item">
                  <span class="mian_title">客户内码：</span>{{ i.custId }}
                </div>
                <div class="main_item">
                  <span class="mian_title">客户地址：</span>{{ i.address }}
                  <van-icon name="location" class="location-icon" v-if="i.address" />
                </div>
              </div>
              <div class="item_main" v-else>
                <div class="main_item">
                  <span class="mian_title">统信码：</span
                  >{{ i.custId?i.custId.substring(3):'' }}
                </div>
                <div class="main_item">
                  <span class="mian_title">法定代表人：</span>{{ i.lpName }}
                </div>
                <div class="main_item">
                  <span class="mian_title">法人身份证：</span>{{ i.lpIdc }}
                </div>
                <div class="main_item">
                  <span class="mian_title">经营状态：</span>{{handleStatus(i.status)}}
                </div>
                <div class="main_item">
                  <span class="mian_title">联系方式：</span>
                  <a :href="'tel:'+ i.lpTel">{{ i.lpTel }}</a>
                  <van-icon name="phone" v-if="i.lpTel" class="phone-box"/>
                </div>
                <div class="main_item">
                  <span class="mian_title">{{item.custType == '2'?'企业地址':'商户地址'}}：</span
                  >{{ i.address }}
                  <van-icon name="location" class="location-icon" v-if="i.address"/>
                </div>
              </div>
            </div>
          </van-list>
        </van-pull-refresh>
      </div>
    </div>
    <van-dialog
      class-name="myDialog"
      v-model="showDialog"
      title="支行行政网格详情"
      confirm-button-text="关闭"
      confirm-button-color=""
    >
      <div class="content">
        <div class="my-title">支行行政网格名称:</div>
        <div v-for="(item, index) in branchDetail" :key="index" class="item">
          <span class="index">{{ index + 1 }}</span>
          <span class="branch-name">{{ item }}</span>
        </div>
      </div>
    </van-dialog>
    <div class="backTop" @click="scrollToTop">
      <van-icon name="down"></van-icon>
      <span class="heng"></span>
    </div>
  </div>
</template>
<script>
import navbar from "../../components/navbar/navbar.vue";
import {
  regionBaseDetail,
  mapBaseDetail,
  nameBaseDetail,
  getBusinessList,
} from "@/service/api/myGrid.js";
import { appointmentCust } from "@/service/api/filing";
import {
  Tab,
  Tabs,
  Icon,
  Button,
  Search,
  Dialog,
  List,
  PullRefresh,
  Toast
} from "vant";
export default {
  data() {
    return {
      custName: "",
      item: {},
      showDialog: false,
      branchDetail: [],
      pageNum: 1,
      pageSize: 5,
      refreshing: false,
      list: [],
      loading: false,
      finished: false,
      currentPage: 1,
      businessList:[],
    };
  },
  components: {
    navbar: navbar,
    VanTab: Tab,
    VanTabs: Tabs,
    VanIcon: Icon,
    VanButton: Button,
    VanSearch: Search,
    VanList: List,
    VanPullRefresh: PullRefresh,
    [Dialog.Component.name]: Dialog.Component,
    VanToast: Toast,
  },
  created() {
    console.log(this.$route);
    const str = sessionStorage['gridValues'] || '{}';
    const item = JSON.parse(str);
    this.item = item;
    this.searchBusinessList();
    this.queryList();
  },
  methods: {
    scrollToTop(){
      window.scrollTo({
        top:0,
        behavior:'smooth'
      })
    },
    // 返回
    handleClickLeft(){
      this.$router.replace({
        path:'/gridHome',
        query:{
          activeTab:this.item.activeTab
        }
      })
    },
    async searchBusinessList(){
        const res = await getBusinessList();
        if(res.code == 200){
            this.businessList = res.data.map(item=>{
                return {
                    key:item.dictValue,
                    value:item.dictLabel,
                }
            })
        }
    },
    handleStatus(status){
        const index = this.businessList.findIndex(item=>item.key == status);
        if(index>-1){
            return this.businessList[index].value
        }
        return ''
    },
    onSearch() {
      this.onRefresh();
    },
    onCancel() {
      this.onRefresh();
    },
    onClear() {
      this.onRefresh();
    },
    //下拉刷新
    onRefresh() {
      this.list = [];
      this.finished = false; //解除上拉加载之后数据不能再次上拉
      this.currentPage = 1;
      this.pageNum = this.currentPage;
      this.queryList();
    },
    //上拉加载
    onLoad() {
      console.log("上拉加载");
      this.pageNum = this.currentPage;
      this.queryList();
    },
    // 查询列表
    async queryList() {
      if (this.currentPage === 1) {
        this.list = [];
      }
      this.loading = true;
      const API =
        this.item.activeTab == 0
          ? regionBaseDetail
          : this.item.activeTab == 1
          ? mapBaseDetail
          : this.item.activeTab == 2
          ? nameBaseDetail
          : () => {};
      const params = {
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        gridId: this.item.gridId,
        custType: this.item.custType,
        custName: this.custName,
      };
      if (this.activeTab == 0) {
        params.gridLevel = this.gridLevel;
      }
      const res = await API(params);
      console.log(res, "res");
      if (res.code == 200) {
        if (this.refreshing) {
          this.refreshing = false;
        }
        this.loading = false;
        if (res.rows) {
          this.list =
            params.pageNum == "1" ? res.rows : [...this.list, ...res.rows];
          if (res.rows.length < params.pageSize) {
            this.finished = true;
          }
           if(this.list.length == res.total){
            this.finished = true;
          }
          this.currentPage++;
        } else {
          this.finished = true;
        }
      }
    },
    openDetail(detail) {
      this.branchDetail = detail.split(",");
      this.showDialog = true;
    },
    handleViewDetail(custType,custId){
      if(!custType || !custId){
          Toast('客户类型或客户内码不存在');
          return;
        }
        const path = custType == '0'?'/customePersonDetail':custType == '1'?'/customeCommercialDetail':
        custType == '2'?'/customerFirmDetails':"";
        this.$router.push({
            path,
        })
        sessionStorage.setItem('custId', custId);
        sessionStorage.setItem("fromMap",'0');
    },
    async handleAppointment(item){
      // let custType="";
      // if(item.custType == '个人'){
      //   custType=0;
      // }else if(item.custType == '企业'){
      //   custType=2;
      // }else if(item.custType == '商户'){
      //   custType=1;
      // }
      let params={
        custIdc:item.custId?item.custId.substring(3):'',
        custName:item.custName,
        custType:item.custType,
      }
      const res = await appointmentCust(params)
      if (res.code == 200) {
        Toast.success('预约成功');
      }
    },
  },
};
</script>

<style lang="less" scoped>
.main_box{
  position: relative;
  .backTop{
    position: fixed;
    bottom: 150px;
    right: 15px;
    width: 64px;
    height: 64px;
    border-radius: 50%;
    background: #4682ff;
    box-shadow: 0px -2px 6px 0px rgba(70,130,255,1);
    color: #fff;
    transform: rotate(180deg);
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    font-size: 20px;
    .heng{
      display: block;
      width: 15px;
      border: 1px solid #fff;
    }
  }
}
.wrapper {
  padding-top: 46px;
  min-height: 100vh;
  font-size: 16px;
  padding-bottom: 60px;
}

.grid_item {
  margin-top: 8px;
  padding: 10px 15px;
  background-color: #fff;
}

.grid_item_top {
  display: flex;
  align-items: center;
}

.icon_box {
  background-color: rgb(35, 114, 235);
  border-radius: 4px;
  width: 25px;
  height: 25px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.grid_item_top_icon {
  font-size: 16px;
  color: #fff;
}

.grid_item_top_title {
  font-size: 16px;
  font-weight: 600;
  margin-left: 10px;
}

.grid_item_middle {
//   display: flex;
//   justify-content: space-between;
//   align-items: center;
  background-color: rgb(249, 249, 251);
  padding: 10px 10px;
  margin-top: 10px;
}

.grid_item_middle_item {
//   text-align: center;
  display: flex;
  justify-content: space-between;
  align-items: center;

}

.middle_num {
//   margin-top: 10px;
  font-weight: 600;
  font-size: 12px;
  line-height: 29px;
}

.middle_title {
  font-size: 12px;
}

.line {
  width: 1px;
  height: 30px;
  background-color: rgb(255, 255, 255);
  border: 1px solid rgb(218, 220, 224);
}

.grid_item_bottom {
  margin-top: 8px;
  font-size: 12px;
}

.grid_item_bottom_item {
  margin-top: 5px;
  .branch-detail {
    color: #4886f8;
    margin-left: 5px;
  }
}

.bottom_item_left {
  color: rgb(140, 140, 140);
}

.kh_title {
  margin: 10px 15px;
  font-size: 16px;
  font-weight: 600;
}

.kh_box {
  width: 100%;
  height: 45px;
  background-color: #fff;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 16px;
  .tigong{
    font-size: 16px;
    color: rgb(140,140,140);
  }
}

.search-input {
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 10px 0px;
}

.search_btn {
  background-color: rgb(24, 144, 255);
  border-radius: 4px;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 40px;
  height: 40px;
  color: #fff;
  font-size: 20px;
}

/deep/ .van-search {
  width: 100%;
  padding: 5px 8px;
}

.firm_box {
  margin-top: 8px;
  padding: 10px 15px;
  background-color: #fff;
}

.firm_box_top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  color: rgb(140, 140, 140);
}

.firm_item {
  margin: 10px 0;
  background-color: #fff;
  padding: 15px 0;
  border-top: 1px solid rgba(140, 140, 140, 0.1);
}

.firm_item_top {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.firm_item_top_left {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  width: calc(100% - 100px)
}

.firm_item_name {
  font-size: 16px;
  font-weight: 600;
  margin-left: 10px;
  width: 210px;
}

.item_viewDetail {
  background-color: rgba(233, 241, 253);
  color: rgb(35, 114, 235);
  padding: 5px 10px;
  border-radius: 2px;
  font-size: 12px;
  width: 100px;
  height: 26px;
}
.special_row{
  display: flex;
  flex-direction: row;
  align-items: center;
}
.item_viewDetailnew {
  background-color: rgba(233, 241, 253);
  color: rgb(35, 114, 235);
  padding: 5px 5px;
  border-radius: 5px;
  width: 44px;
}
.mgrt5{
  margin-right: 5px;
}

.firm_icon_box {
  background-color: rgb(35, 114, 235);
  display: flex;
  justify-content: center;
  align-items: center;
  width: 24px;
  height: 24px;
}

.item_main {
  margin-top: 10px;
}

.main_item {
  font-size: 12px;
  line-height: 25px;
  .location-icon{
    color: #4682ff;
  }
}

.mian_title {
  color: rgb(140, 140, 140);
}

.item_top {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.item_top_left {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  width: calc(100% - 100px)
}

.item_name {
  font-size: 20px;
  font-weight: 600;
  margin-left: 10px;
}

.item_user {
  font-size: 14px;
  color: #fff;
}

.userMan_box {
  background-color: #2372eb;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 24px;
  height: 24px;
}

.userWoMan_box {
  background-color: rgb(252, 123, 80);
  display: flex;
  justify-content: center;
  align-items: center;
  width: 24px;
  height: 24px;
}
.myDialog {
  .content {
    padding: 5px 15px;
    .my-title {
      font-size: 14px;
      color: rgba(0, 0, 0, 0.45);
    }
    .item {
      margin: 8px 0;
      .index {
        display: inline-block;
        width: 16px;
        height: 16px;
        background: rgba(22, 132, 252, 1);
        color: #fff;
        border-radius: 4px;
        font-size: 14px;
        text-align: center;
        line-height: 16px;
      }
      .branch-name {
        font-size: 14px;
        color: rgba(0, 0, 0, 0.85);
        margin-left: 5px;
      }
    }
  }
  ::v-deep .van-button__content {
    color: #666;
  }
}
 .phone-box {
      font-size: 12px;
      color: #4682ff;
    }
</style>