<template>
  <div class="main_box">
    <navbar title="客户明细"></navbar>
    <div class="wrapper">
      <div class="banner-box">
        <div class="search-input">
          <van-search
            type="text"
            v-model="searchForm.custName"
            placeholder="请输入客户名称"
            @search="onSearch"
            @clear="onClear"
            left-icon=""
          />
          <van-search
            type="text"
            v-model="searchForm.custIdc"
            placeholder="请输入客户证件号"
            @search="onSearch"
            @clear="onClear"
            left-icon=""
          />
          <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>
      </div>
      <div class="title-box">
        <div
          class="title-top"
          :class="gridCustomItem.activeTab == '0' ? 'special-title' : ''"
        >
          <span
            v-if="
              gridCustomItem.activeTab == '0' && gridCustomItem.activeBtn == '0'
            "
            class="title-left"
            >{{ gridCustomItem.town }}-{{ gridCustomItem.village }}</span
          >
          <span
            v-if="
              gridCustomItem.activeTab == '0' && gridCustomItem.activeBtn == '1'
            "
            class="title-left"
            >{{ gridCustomItem.town }}</span
          >
          <span v-if="gridCustomItem.activeTab == '1'" class="title-left"
            >{{ gridCustomItem.gridName }}
          </span>
          <span v-if="gridCustomItem.activeTab == '2'" class="title-left"
            >{{ gridCustomItem.shapeName }}
          </span>
          <span class="date-left"
            >统计日期：<span class="date-right">{{
              gridCustomItem.dt
            }}</span></span
          >
        </div>
        <span class="other-title" v-if="gridCustomItem.activeTab == '1'">{{
          gridCustomItem.gridType
        }}</span>
        <span class="other-title" v-if="gridCustomItem.activeTab == '2'">{{
          gridCustomItem.shapeType
        }}</span>
      </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="userMan_box">
                  <van-icon
                    name="manager item_user"
                    v-if="gridCustomItem.activeBtn == '0'"
                  ></van-icon>
                  <van-icon
                    name="wap-home"
                    class="grid_item_top_icon"
                    v-if="gridCustomItem.activeBtn == '1'"
                  ></van-icon>
                </div>
                <div
                  class="grid_item_top_title"
                  @click="
                    handleClick(
                      item.custIdc || item.socialCreditCode,
                      item.custType
                    )
                  "
                >
                  {{ item.custName }}
                </div>
              </div>
              <div class="content">
                <div
                  v-for="(i, index) in currentItemList1"
                  :key="index"
                  class="item-one"
                >
                  <span class="label">{{ i.label }}：</span>
                  <span class="right-text">{{ item[i.prop] }}</span>
                </div>
                <div class="tag-flex">
                  <div
                    v-for="(i, index) in currentItemList2"
                    :key="index"
                    class="item-two"
                    :class="
                      item[i.prop] == '是'
                        ? 'hasClass'
                        : item[i.prop] == '否'
                        ? 'notClass'
                        : 'unClass'
                    "
                  >
                    {{
                      item[i.prop] == "是"
                        ? i.label1
                        : item[i.prop] == "否"
                        ? i.label2
                        : i.label3
                    }}
                  </div>
                </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> -->
  </div>
</template>
<script>
import {
  privateTotalColumns,
  publicTotalColumns,
  privateTotalColumnsNm,
  publicTotalColumnsNm,
  privateTotalColumnsMap,
  publicTotalColumnsMap,
} from "./list";
import {
  getColDetailList1,
  getColDetailList2,
  getColDetailListNm1,
  getColDetailListNm2,
  getColDetailListMap1,
  getColDetailListMap2,
} from "@/service/api/dataCenter.js";
import {
  Tab,
  Tabs,
  Icon,
  Button,
  Search,
  RadioGroup,
  Radio,
  List,
  PullRefresh,
  Dialog,
  Toast,
} from "vant";
import navbar from "@/components/navbar/navbar.vue";
export default {
  props: {},
  watch: {},
  data() {
    return {
      searchForm: {
        custName: "",
        custIdc: "",
      },
      pageNum: 1,
      pageSize: 5,
      refreshing: false,
      list: [],
      loading: false,
      finished: false,
      currentPage: 1,
      showDialog: false,
      nameDetail: [],
      title: "",
      gridCustomItem: {},
      currentItemList: [],
      currentItemList1: [],
      currentItemList2: [],
    };
  },
  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,
    navbar: navbar,
  },

  created() {
    const str = sessionStorage["gridCustomItem"] || "{}";
    const gridCustomItem = JSON.parse(str);
    this.gridCustomItem = gridCustomItem;
    this.currentItemList =
      this.gridCustomItem.activeBtn == "0"
        ? this.gridCustomItem.activeTab == "0"
          ? privateTotalColumns
          : this.gridCustomItem.activeTab == "1"
          ? privateTotalColumnsNm
          : this.gridCustomItem.activeTab == "2"
          ? privateTotalColumnsMap
          : []
        : this.gridCustomItem.activeTab == "0"
        ? publicTotalColumns
        : this.gridCustomItem.activeTab == "1"
        ? publicTotalColumnsNm
        : this.gridCustomItem.activeTab == "2"
        ? publicTotalColumnsMap
        : [];
    this.currentItemList1 = this.currentItemList.filter((item) => !item.isTag);
    this.currentItemList2 = this.currentItemList.filter((item) => item.isTag);
    // sessionStorage["gridCustomItem"] = "";
    this.queryList();
  },
  methods: {
    scrollToTop() {
      window.scrollTo({
        top: 0,
        behavior: "smooth",
      });
    },

    handleClick(custIdc, custType) {
      const custId = custType == "0" ? `101${custIdc}` : `202${custIdc}`;
      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');
    },
    showNameDetail(title, nameDetail) {
      if (!nameDetail) return;
      this.showDialog = true;
      this.title = title;
      this.nameDetail = nameDetail.split(",");
    },

    onSearch() {
      this.onRefresh();
    },
    resetForm() {
      this.searchForm = {
        custName: "",
        custIdc: "",
      };
      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.gridCustomItem.activeBtn == "0"
          ? this.gridCustomItem.activeTab == "0"
            ? getColDetailList1
            : this.gridCustomItem.activeTab == "1"
            ? getColDetailListNm1
            : this.gridCustomItem.activeTab == "2"
            ? getColDetailListMap1
            : () => {}
          : this.gridCustomItem.activeTab == "0"
          ? getColDetailList2
          : this.gridCustomItem.activeTab == "1"
          ? getColDetailListNm2
          : this.gridCustomItem.activeTab == "2"
          ? getColDetailListMap2
          : () => {};
      const params = {
        ...this.searchForm,
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        regionCode: this.gridCustomItem.regionCode,
        socialCreditCode: this.searchForm.custIdc,
        gridId: this.gridCustomItem.gridId,
        shapeId: this.gridCustomItem.shapeId,
        dt: this.gridCustomItem.dt
      };
      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;
  .wrapper {
    padding-top: 46px;
    min-height: 100vh;
    font-size: 16px;
    padding-bottom: 60px;
    .title-box {
      height: 47px;
      padding: 0 16px;
      background: #f7f8fa;
      .special-title {
        height: 100%;
      }
      .title-top {
        display: flex;
        justify-content: space-between;
        align-items: center;
        .title-left {
          font-size: 16px;
          color: #262626;
          font-weight: 600;
        }
        .date-left {
          font-size: 12px;
          color: #8c8c8c;
        }
        .date-right {
          color: #262626;
        }
      }
      .other-title {
        font-size: 12px;
        color: #1990ff;
        background: rgba(25, 144, 255, 0.1);
        border-radius: 4px;
        padding: 0 6px;
      }
    }
  }
  .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;
  ::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;
  &:first-child {
    margin-top: 0;
  }
  .content {
    padding: 0 35px;
    padding-top: 2px;
    .item-one {
      font-size: 12px;
      color: #8c8c8c;
      margin-bottom: 8px;
      .label {
      }
      .right-text {
        color: #262626;
      }
    }
    .tag-flex {
      display: flex;
      flex-wrap: wrap;
      .item-two {
        font-size: 12px;
        height: 20px;
        line-height: 20px;
        padding: 0 5px;
        border-radius: 4px;
        margin-right: 4px;
        margin-bottom: 5px;
      }
      .hasClass {
        color: rgba(35, 114, 235, 1);
        background: rgba(25, 144, 255, 0.1);
      }
      .notClass {
        color: rgba(238, 132, 21, 1);
        background: rgba(238, 132, 21, 0.1);
      }
      .unClass {
        color: #ccc;
        background: rgba(224, 224, 224, 0.3);
      }
    }
  }
}
.grid_item_top {
  display: flex;
  align-items: center;
  .item_user {
    font-size: 14px;
    color: #fff;
  }

  .userMan_box {
    background-color: #2372eb;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 24px;
    height: 24px;
  }
}
.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;
  color: #4886f8;
}
.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>