<template>
  <div class="main_box">
    <div class="">
      <div class="banner-box">
        <div class="search-input">
          <van-row>
            <van-col span="8" v-if="activeTab == '0'">
              <van-search
                type="text"
                v-model="searchForm.town"
                placeholder="请输入镇/街道"
                @search="onSearch"
                @clear="onClear"
                left-icon=""
              />
            </van-col>
            <van-col span="8" v-if="activeTab == '0' && activeBtn == '0'">
              <van-search
                type="text"
                v-model="searchForm.village"
                placeholder="请输入村/社区"
                @search="onSearch"
                @clear="onClear"
                left-icon=""
              />
            </van-col>
            <van-col span="8" v-if="activeTab == '2'">
              <van-search
                type="text"
                v-model="shapeTypeNm"
                placeholder="请选择区域类型"
                @search="onSearch"
                @clear="onClear"
                left-icon=""
                class="special-search"
                readonly
                @click="showShapePicker = true"
              >
                <template #right-icon>
                  <van-icon name="arrow-down" size="14" v-if="!shapeTypeNm" />
                  <van-icon
                    name="clear"
                    size="14"
                    v-else
                    @click.stop="clearShapeType"
                  />
                </template>
              </van-search>
            </van-col>
            <van-col span="8" v-if="activeTab == '2'">
              <van-search
                type="text"
                v-model="searchForm.shapeName"
                placeholder="请输入区域名称"
                @search="onSearch"
                @clear="onClear"
                left-icon=""
                class="special-search"
              />
            </van-col>
            <van-col span="8" v-if="activeTab == '1' || activeTab == '2'">
              <van-search
                type="text"
                v-model="searchForm.gridName"
                placeholder="请输入网格名称"
                @search="onSearch"
                @clear="onClear"
                left-icon=""
                :class="activeTab == '2' ? 'special-search' : ''"
              />
            </van-col>
            <van-col span="8" v-if="activeTab == '1'">
              <van-search
                type="text"
                v-model="gridTypeNm"
                placeholder="请选择网格类型"
                @search="onSearch"
                @clear="onClear"
                left-icon=""
                readonly
                @click="showGridPicker = true"
              >
                <template #right-icon>
                  <van-icon name="arrow-down" size="16" v-if="!gridTypeNm" />
                  <van-icon
                    name="clear"
                    size="16"
                    v-else
                    @click.stop="clearGridType"
                  />
                </template>
              </van-search>
            </van-col>
            <van-col span="8">
              <van-search
                v-model="searchForm.dt"
                readonly
                placeholder="请选择统计日期"
                :class="activeTab == '2' ? 'special-search' : ''"
                @click="handleChooseDate"
              />
            </van-col>
            <van-col span="8" :offset="activeTab == '2' ? 8 : 16">
              <div class="button-box">
                <div class="search_btn" @click="onSearch">
                  <van-icon name="search" class="icon"></van-icon>
                </div>
                <div class="search_btn reset" @click="resetForm">
                  <van-icon name="cross" class="icon"></van-icon>
                </div>
              </div>
            </van-col>
          </van-row>
        </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>
                  <!-- <img
                    src="@/images/home/wdwg.png"
                    alt=""
                    style="width: 25px; height: 25px"
                    class="grid_item_top_icon"
                  /> -->
                </div>
                <div class="grid_item_top_title" @click="handleClick(item)">
                  <span v-if="activeTab == '0' && activeBtn == '0'"
                    >{{ item.town }}-{{ item.village }}</span
                  >
                  <span v-if="activeTab == '0' && activeBtn == '1'">{{
                    item.town
                  }}</span>
                  <span v-if="activeTab == '1'">{{ item.gridName }}</span>
                  <span v-if="activeTab == '2'">{{ item.shapeName }}</span>
                </div>
              </div>
              <!-- 行政网格 -->
              <div class="grid_item_middle" v-if="activeTab == '0'">
                <div class="grid_item_middle_item">
                  <div class="middle_title">总行行政网格</div>
                  <div
                    class="middle_num"
                    @click="showNameDetail('总行行政网格', item.gridName)"
                  >
                    {{ item.gridName || "--" }}
                  </div>
                </div>
                <div class="line"></div>
                <div class="grid_item_middle_item">
                  <div class="middle_title">支行行政网格</div>
                  <div
                    class="middle_num"
                    @click="showNameDetail('支行行政网格', item.gridName2)"
                  >
                    {{ item.gridName2 || "--" }}
                  </div>
                </div>
                <div class="line"></div>
                <div class="grid_item_middle_item">
                  <div class="middle_title">归属支行</div>
                  <div
                    class="middle_num"
                    @click="showNameDetail('归属支行', item.deptName)"
                  >
                    {{ item.deptName || "--" }}
                  </div>
                </div>
              </div>
              <!-- 自定义名单 -->
              <div class="grid_item_middle" v-if="activeTab == '1'">
                <div class="grid_item_middle_item">
                  <div class="middle_title">网格类型</div>
                  <div class="middle_num">{{ item.gridType || "--" }}</div>
                </div>
                <div class="line"></div>
                <div class="grid_item_middle_item">
                  <div class="middle_title">归属支行</div>
                  <div
                    class="middle_num"
                    @click="showNameDetail('归属支行', item.deptName)"
                  >
                    {{ item.deptName || "--" }}
                  </div>
                </div>
                <div class="line"></div>
                <div class="grid_item_middle_item">
                  <div class="middle_title">归属网点</div>
                  <div
                    class="middle_num"
                    @click="showNameDetail('归属网点', item.outletsName)"
                  >
                    {{ item.outletsName || "--" }}
                  </div>
                </div>
              </div>
              <!-- 自定义地图 -->
              <div class="grid_item_middle" v-if="activeTab == '2'">
                <div class="grid_item_middle_item">
                  <div class="middle_title">区域类型</div>
                  <div
                    class="middle_num"
                    @click="showNameDetail('区域类型', item.shapeType)"
                  >
                    {{ item.shapeType || "--" }}
                  </div>
                </div>
                <div class="line"></div>
                <div class="grid_item_middle_item">
                  <div class="middle_title">网格名称</div>
                  <div
                    class="middle_num"
                    @click="showNameDetail('网格名称', item.gridName)"
                  >
                    {{ item.gridName || "--" }}
                  </div>
                </div>
                <div class="line"></div>
                <div class="grid_item_middle_item">
                  <div class="middle_title">归属支行</div>
                  <div
                    class="middle_num"
                    @click="showNameDetail('归属支行', item.deptName)"
                  >
                    {{ item.deptName || "--" }}
                  </div>
                </div>
              </div>
              <div class="grid_item_bottom">
                <div
                  class="grid_item_bottom_item"
                  v-if="activeTab == '0' || activeTab == '2'"
                >
                  <span class="bottom_item_left">归属网点：</span>
                  <span
                    class="bottom_item_right"
                    @click="showNameDetail('归属网点', item.outletsName)"
                    >{{ item.outletsName }}</span
                  >
                </div>
                <div class="grid_item_bottom_item" v-if="activeTab == '1'">
                  <span class="bottom_item_left">网格创建者：</span>
                  <span>{{ item.createBy }}</span>
                </div>
                <div class="grid_item_bottom_item">
                  <span class="bottom_item_left">归属客户经理：</span>
                  <span
                    class="bottom_item_right"
                    @click="showNameDetail('归属客户经理', item.userName)"
                    >{{ item.userName }}</span
                  >
                </div>
                <div class="grid_item_bottom_item item-date">
                  <span>
                    <span class="bottom_item_left">统计日期：</span>
                    <span>{{ item.dt }}</span>
                  </span>
                  <van-button
                    type="primary"
                    round
                    class="custom-btn"
                    @click="goToCustomDetail(item)"
                    >客户明细</van-button
                  >
                </div>
              </div>
            </div>
          </van-list>
        </van-pull-refresh>
      </div>
    </div>
    <van-dialog
      class-name="myDialog"
      v-model="showDialog"
      :title="title + '详情'"
      confirm-button-text="关闭"
      confirm-button-color=""
    >
      <div class="content">
        <div class="my-title">{{ title }}名称:</div>
        <div class="grid-box">
          <div v-for="(item, index) in nameDetail" :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> -->
    <van-popup v-model="showShapePicker" position="bottom">
      <van-picker
        show-toolbar
        :columns="shapeTypeList"
        value-key="label"
        @cancel="showShapePicker = false"
        @confirm="onConfirmShape"
      />
    </van-popup>
    <van-popup v-model="showGridPicker" position="bottom">
      <van-picker
        show-toolbar
        :columns="gridTypeList"
        value-key="label"
        @cancel="showGridPicker = false"
        @confirm="onConfirmGrid"
      />
    </van-popup>
    <van-popup v-model="showDatePicker" position="bottom">
      <van-datetime-picker
        v-model="searchDate"
        type="date"
        title="选择统计日期"
        :max-date="maxDate"
        @cancel="showDatePicker = false"
        @confirm="handleConfirmChooseDate"
      />
    </van-popup>
  </div>
</template>
<script>
import {
  getCollectList1,
  getCollectList2,
  getCollectListNm1,
  getCollectListNm2,
  getCollectListMap1,
  getCollectListMap2,
  queryAreas,
} from "@/service/api/dataCenter.js";
import {
  Tab,
  Tabs,
  Icon,
  Button,
  Search,
  RadioGroup,
  Radio,
  List,
  PullRefresh,
  Dialog,
  Toast,
  Popup,
  Picker,
  DatetimePicker,
  Row,
  Col,
} from "vant";
export default {
  props: {
    activeBtn: String,
    activeTab: String,
  },
  watch: {
    activeBtn(newValue) {
      this.resetForm();
    },
    activeTab(newValue) {
      this.resetForm();
    },
  },
  data() {
    return {
      searchForm: {
        town: "",
        village: "",
        gridName: "",
        gridType: "",
        shapeType: "",
        shapeName: "",
        dt: "",
      },
      searchDate: "",
      maxDate: "",
      shapeTypeNm: "",
      gridTypeNm: "",
      pageNum: 1,
      pageSize: 5,
      refreshing: false,
      list: [],
      loading: false,
      finished: false,
      currentPage: 1,
      showDialog: false,
      nameDetail: [],
      title: "",
      shapeTypeList: [],
      showShapePicker: false,
      showGridPicker: false,
      showDatePicker: false,
      gridTypeList: [
        { label: "菜场", value: "1" },
        { label: "政府单位", value: "2" },
        { label: "事业单位", value: "3" },
        { label: "社区", value: "4" },
        { label: "商圈", value: "5" },
        { label: "企业", value: "6" },
        { label: "市场园区", value: "7" },
        { label: "教育", value: "8" },
        { label: "其他", value: "9" },
      ],
    };
  },
  components: {
    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,
    VanPopup: Popup,
    VanPicker: Picker,
    VanDatetimePicker: DatetimePicker,
    VanRow: Row,
    VanCol: Col,
  },

  created() {
    const date = new Date();
    date.setDate(date.getDate() - 1);
    this.searchDate = date;
    this.maxDate = date;
    this.searchForm.dt = this.getPreviousDate();
    this.queryAreasList();
    this.queryList();
  },
  methods: {
    getPreviousDate() {
      const date = new Date();
      date.setDate(date.getDate() - 1);
      const year = date.getFullYear();
      const month = String(date.getMonth() + 1).padStart(2, "0");
      const day = String(date.getDate()).padStart(2, "0");
      return `${year}-${month}-${day}`;
    },
    handleChooseDate() {
      this.showDatePicker = true;
    },
    handleConfirmChooseDate(val) {
      const year = val.getFullYear();
      const month = String(val.getMonth() + 1).padStart(2, "0");
      const day = String(val.getDate()).padStart(2, "0");
      this.searchForm.dt = `${year}-${month}-${day}`;
      this.showDatePicker = false;
      this.queryList();
    },
    scrollToTop() {
      window.scrollTo({
        top: 0,
        behavior: "smooth",
      });
    },
    // 获取区域类型
    async queryAreasList() {
      const res = await queryAreas();
      if (res.code == 200) {
        this.shapeTypeList = res.data.map((item) => {
          return {
            label: item.layerName,
            value: item.layerId,
          };
        });
      }
    },
    onConfirmShape(item) {
      this.searchForm.shapeType = item.label;
      this.shapeTypeNm = item.label;
      this.showShapePicker = false;
      this.onRefresh();
    },
    onConfirmGrid(item) {
      this.searchForm.gridType = item.value;
      this.gridTypeNm = item.label;
      this.showGridPicker = false;
      this.onRefresh();
    },
    // 展示详情
    showNameDetail(title, nameDetail) {
      if (!nameDetail) return;
      this.showDialog = true;
      this.title = title;
      this.nameDetail = nameDetail.split(",");
    },
    // 跳转客户明细
    goToCustomDetail(item) {
      let newItem = {
        ...item,
        activeTab: this.activeTab,
        activeBtn: this.activeBtn,
      };
      sessionStorage.setItem("gridCustomItem", JSON.stringify(newItem));
      this.$router.push("/gridCustomTable");
    },
    // 查看汇总详情
    handleClick(item) {
      let newItem = {
        ...item,
        gridTitle:
          this.activeTab == "0"
            ? "行政区域报表详情"
            : this.activeTab == "1"
            ? "自建名单详情"
            : this.activeTab == "2"
            ? "自绘地图详情"
            : "",
        activeTab: this.activeTab,
        activeBtn: this.activeBtn,
      };
      sessionStorage.setItem("gridTotalInfo", JSON.stringify(newItem));
      this.$router.push("/gridTableDetail");
    },
    openDetail(detail) {
      this.branchDetail = detail.split(",");
      this.showDialog = true;
    },

    onSearch() {
      this.onRefresh();
    },
    clearGridType() {
      this.gridTypeNm = "";
      this.searchForm.gridType = "";
      this.onRefresh();
    },
    clearShapeType() {
      this.shapeTypeNm = "";
      this.searchForm.shapeType = "";
      this.onRefresh();
    },
    resetForm() {
      this.searchForm = {
        town: "",
        village: "",
        gridName: "",
        gridType: "",
        shapeType: "",
        shapeName: "",
        dt: this.getPreviousDate(),
      };
      this.shapeTypeNm = "";
      this.gridTypeNm = "";
      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.activeBtn == "0"
          ? this.activeTab == "0"
            ? getCollectList1
            : this.activeTab == "1"
            ? getCollectListNm1
            : this.activeTab == "2"
            ? getCollectListMap1
            : () => {}
          : this.activeTab == "0"
          ? getCollectList2
          : this.activeTab == "1"
          ? getCollectListNm2
          : this.activeTab == "2"
          ? getCollectListMap2
          : () => {};
      const params = {
        ...this.searchForm,
        pageNum: this.pageNum,
        pageSize: this.pageSize,
      };
      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_top {
  background-color: #fff;
  padding: 15px 20px;
}

.search-input {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 3px;
  background: #fff;
  flex-wrap: wrap;
  padding-bottom: 4px;

  .button-box {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    padding-right: 8px;
  }
  ::v-deep .van-field__control::-webkit-input-placeholder {
    font-size: 12px;
  }
  .special-search {
    padding-right: 0 !important;
    ::v-deep .van-search__content {
      padding-left: 4px !important;
    }
    ::v-deep .van-cell {
      padding-right: 0 !important;
    }
    // ::v-deep .van-field__control::-webkit-input-placeholder{
    //   font-size: 12px;
    // }
  }
}

.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;
  .icon {
    display: inline-block;
    width: 40px;
    text-align: center;
  }
}
.reset {
  background: #f7f8fa;
  color: rgba(0, 0, 0, 0.45);
  margin-left: 3px;
}

/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 {
  width: 30%;
  text-align: center;
}
.middle_num {
  margin-top: 10px;
  font-weight: 600;
  font-size: 14px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.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 {
  display: flex;
  align-items: center;
  margin-top: 5px;
  .branch-detail {
    color: #4886f8;
    margin-left: 5px;
  }
  .bottom_item_right {
    display: inline-block;
    max-width: calc(100% - 90px);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .custom-btn {
    height: 24px;
  }
}
.item-date {
  justify-content: space-between;
  margin-top: 1px;
}
.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>
