<template>
  <div class="main_box">
    <navbar title="我的网格"></navbar>
    <div class="wrapper">
      <div class="banner-box">
        <van-tabs
          @change="tabChange()"
          v-model="activeTab"
          class="content"
          type="line"
          color="#4682ff"
          title-active-color="#4682ff"
          line-width="80"
        >
          <van-tab title="行政网格"> </van-tab>
          <van-tab title="自绘地图"> </van-tab>
          <van-tab title="自建名单"> </van-tab>
        </van-tabs>
        <div class="search-input">
          <van-search
            type="text"
            v-model="gridName"
            placeholder="请输入网格名称进行模糊查询"
            @search="onSearch"
            @clear="onClear"
          />
          <div class="search_btn" @click="onSearch">
            <van-icon name="search"></van-icon>
          </div>
        </div>
        <div class="radio_box" v-if="activeTab == 0">
          <div
            class="btn_box"
            :class="firstChecked ? 'isChecked' : ''"
            @click="handleClickFirst"
          >
            总行行政网格
          </div>
          <div
            class="btn_box"
            :class="secondChecked ? 'isChecked' : ''"
            @click="handleClickSecond"
          >
            支行行政网格
          </div>
        </div>
      </div>
      <div class="container-list">
        <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
          <van-list
            v-model="loading"
            :finished="finished"
            finished-text="没有更多了"
            :immediate-check="false"
            @load="onLoad"
          >
            <div class="grid_item" v-for="(item, index) in list" :key="index">
              <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"
                  @click="handleClickItem(item, item.businessCount, '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"
                  @click="handleClickItem(item, item.merchantCount, '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"
                  @click="handleClickItem(item, item.retailCount, '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">
                  <span class="bottom_item_left">归属客户经理：</span>
                  <span>{{ item.relateUserNames || item.userNames }}</span>
                </div>
                <div
                  class="grid_item_bottom_item"
                  v-if="firstChecked && activeTab == 0"
                >
                  <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="secondChecked">
                  <span class="bottom_item_left">所属总行行政网格：</span>
                  <span>{{ item.parentGridName }}</span>
                </div>
                <div class="grid_item_bottom_item" v-if="activeTab !== 0">
                  <span class="bottom_item_left">关联网点：</span>
                  <span>{{ item.outletNames }}</span>
                </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 class="grid-box">
          <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>
      </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 {
  regionBaseList,
  mapBaseList,
  nameBaseList,
} from "@/service/api/myGrid.js";
import {
  Tab,
  Tabs,
  Icon,
  Button,
  Search,
  RadioGroup,
  Radio,
  List,
  PullRefresh,
  Dialog,
  Toast,
} from "vant";
export default {
  data() {
    return {
      activeTab: 0,
      gridName: "",
      checked: "1",
      firstChecked: true,
      secondChecked: false,
      pageNum: 1,
      pageSize: 5,
      refreshing: false,
      list: [],
      loading: false,
      finished: false,
      gridPattern: "1",
      gridLevel: "1",
      currentPage: 1,
      list: [],
      showDialog: false,
      branchDetail: [],
    };
  },
  components: {
    navbar: navbar,
    VanTab: Tab,
    VanTabs: Tabs,
    VanIcon: Icon,
    VanButton: Button,
    VanSearch: Search,
    VanRadioGroup: RadioGroup,
    VanRadio: Radio,
    VanList: List,
    VanPullRefresh: PullRefresh,
    VanToast: Toast,
    [Dialog.Component.name]: Dialog.Component,
  },

  created() {
    console.log("返回1111", this.$route);
    const str = sessionStorage["gridValues"] || "{}";
    const item = JSON.parse(str);
    const { activeTab } = item;
    if (activeTab) {
      this.activeTab = Number(activeTab);
      sessionStorage["gridValues"] = "";
    }
    this.queryList();
  },
  methods: {
    scrollToTop() {
      window.scrollTo({
        top: 0,
        behavior: "smooth",
      });
    },
    openDetail(detail) {
      this.branchDetail = detail.split(",");
      this.showDialog = true;
    },
    tabChange() {
      // console.log(this.activeTab);
      this.currentPage = 1;
      this.pageNum = 1;
      this.gridLevel = "1";
      this.firstChecked = true;
      this.secondChecked = false;
      this.onRefresh();
    },
    handleClickFirst() {
      this.firstChecked = true;
      this.secondChecked = false;
      this.gridLevel = "1";
      this.onRefresh();
    },
    handleClickSecond() {
      this.firstChecked = false;
      this.secondChecked = true;
      this.gridLevel = "2";
      this.onRefresh();
    },
    handleClickItem(item, count, custType) {
      if (!count || count == 0) {
        Toast("暂无数据");
        return;
      }
      this.$router.push({
        path: "/gridDetails",
      });
      const str = JSON.stringify({
        ...item,
        custType,
        activeTab: this.activeTab,
        gridLevel: this.gridLevel,
      });
      sessionStorage.setItem("gridValues", str);
    },
    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.activeTab == 0
          ? regionBaseList
          : this.activeTab == 1
          ? mapBaseList
          : this.activeTab == 2
          ? nameBaseList
          : () => {};
      const params = {
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        gridPattern: this.gridPattern,
        gridName: this.gridName,
      };
      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;
        }
      }
    },
  },
};
</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;
  // position: relative;
  .banner-box {
    // position: fixed;
  }
}

.wrapper_top {
  background-color: #fff;
  padding: 15px 20px;
}

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

.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;
}

.radio_box {
  display: flex;
  align-items: center;
  margin: 15px 0 15px 15px;
}

.btn_box {
  padding: 8px 16px;
  background-color: #fff;
}

.isChecked {
  background-color: rgb(35, 114, 235);
  color: #fff;
  border-radius: 4px;
}
.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 15px;
  margin-top: 10px;
}
.grid_item_middle_item {
  text-align: center;
}
.middle_num {
  margin-top: 10px;
  font-weight: 600;
  font-size: 14px;
}
.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);
}
.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;
      }
    }
    .grid-box {
      max-height: 150px;
      overflow-y: auto;
    }
  }
  ::v-deep .van-button__content {
    color: #666;
  }
}
</style>
