<template>
  <div class="main_box">
    <navbar title="名单统计"></navbar>
    <div class="wrapper">
      <div class="banner-box">
        <van-tabs
          @change="tabChange()"
          v-model="custType"
          class="content"
          type="line"
          color="#4682ff"
          title-active-color="#4682ff"
          line-width="80"
        >
          <van-tab title="企业" name="2" :disabled="isPrivate"></van-tab>
          <van-tab title="个人" name="0" :disabled="isPublic"></van-tab>
          <van-tab title="商户" name="1" :disabled="isPublic"></van-tab>
        </van-tabs>
        <div class="search-input">
          <van-search
            type="text"
            placeholder="请选择开始时间"
            v-model="searchForm.startTime"
            left-icon=""
            class="special-search"
            readonly
            @click="showStartDatePicker = true"
            right-icon=""
          >
            <template #right-icon>
              <van-icon
                name="arrow-down"
                size="16"
                v-if="!searchForm.startTime"
              />
              <van-icon
                name="clear"
                size="16"
                v-else
                @click.stop="clearOption('startTime')"
              />
            </template>
          </van-search>
          <van-search
            type="text"
            placeholder="请选择结束时间"
            v-model="searchForm.endTime"
            left-icon=""
            class="special-search"
            readonly
            @click="showEndDatePicker = true"
            right-icon=""
          >
            <template #right-icon>
              <van-icon
                name="arrow-down"
                size="16"
                v-if="!searchForm.endTime"
              />
              <van-icon
                name="clear"
                size="16"
                v-else
                @click.stop="clearOption('endTime')"
              />
            </template>
          </van-search>
        </div>
        <div class="search-input special-input-box">
          <van-search
            type="text"
            placeholder="请选择任务来源"
            v-model="searchForm.campsource"
            left-icon=""
            class="special-search"
            readonly
            @click="showSourcePicker = true"
            right-icon=""
            v-if="isBranchAdmin || isOutletAdmin || isCommonManager"
          >
            <template #right-icon>
              <van-icon
                name="arrow-down"
                size="16"
                v-if="!searchForm.campsource"
              />
              <van-icon
                name="clear"
                size="16"
                v-else
                @click.stop="clearOption('campsource')"
              />
            </template>
          </van-search>
          <van-search
            type="text"
            placeholder="请输入任务名称"
            v-model="searchForm.campaignName"
            left-icon=""
            class="special-search"
            right-icon=""
            @search="onSearch"
            @clear="onClear"
          >
          </van-search>
          <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="task-box"
        v-if="
          isHeadAdmin || isPublic || isPrivate || isHeadOps || isBranchAdmin
        "
      >
        <div class="task-title">我创建的总任务数</div>
        <div class="grid_item_middle">
          <div class="grid_item_middle_item">
            <div class="middle_title">本周</div>
            <div class="middle_num">{{ cardInfo.createCampNumWeek }}</div>
          </div>
          <div class="line"></div>
          <div class="grid_item_middle_item">
            <div class="middle_title">本月</div>
            <div class="middle_num">{{ cardInfo.createCampNumMonth }}</div>
          </div>
          <div class="line"></div>
          <div class="grid_item_middle_item">
            <div class="middle_title">本年</div>
            <div class="middle_num">{{ cardInfo.createCampNumYear }}</div>
          </div>
        </div>
      </div>
      <!-- 推送给我的总任务数 -->
      <div
        class="task-box"
        v-if="isBranchAdmin || isOutletAdmin || isCommonManager"
      >
        <div class="task-title">推送给我的总任务数</div>
        <div class="grid_item_middle">
          <div class="grid_item_middle_item">
            <div class="middle_title">本周</div>
            <div class="middle_num">{{ cardInfo.pushCampNumWeek }}</div>
          </div>
          <div class="line"></div>
          <div class="grid_item_middle_item">
            <div class="middle_title">本月</div>
            <div class="middle_num">{{ cardInfo.pushCampNumMonth }}</div>
          </div>
          <div class="line"></div>
          <div class="grid_item_middle_item">
            <div class="middle_title">本年</div>
            <div class="middle_num">{{ cardInfo.pushCampNumYear }}</div>
          </div>
        </div>
      </div>
      <!-- 其他的总任务数 -->
      <div class="task-box" v-if="isBranchAdmin || isOutletAdmin">
        <div class="task-title">其他的总任务数</div>
        <div class="grid_item_middle">
          <div class="grid_item_middle_item">
            <div class="middle_title">本周</div>
            <div class="middle_num">{{ cardInfo.otherCampNumWeek }}</div>
          </div>
          <div class="line"></div>
          <div class="grid_item_middle_item">
            <div class="middle_title">本月</div>
            <div class="middle_num">{{ cardInfo.otherCampNumMonth }}</div>
          </div>
          <div class="line"></div>
          <div class="grid_item_middle_item">
            <div class="middle_title">本年</div>
            <div class="middle_num">{{ cardInfo.otherCampNumYear }}</div>
          </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="top-box">
                <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.campaignName }}
                  </div>
                </div>
                <div class="right-label">
                  <span
                    class="label"
                    v-if="isBranchAdmin || isOutletAdmin || isCommonManager"
                    >{{ item.campsource }}</span
                  >
                  <span class="label" v-if="item.claimType">{{
                    item.claimType == "0" ? "强制认领" : "可选认领"
                  }}</span>
                </div>
              </div>
              <div class="grid_item_middle">
                <div class="grid_item_middle_item">
                  <div class="middle_title">开始时间</div>
                  <div class="middle_num">{{ item.startTime || "-" }}</div>
                </div>
                <div class="line"></div>
                <div class="grid_item_middle_item">
                  <div class="middle_title">结束时间</div>
                  <div class="middle_num">{{ item.endTime || "-" }}</div>
                </div>
                <div class="line"></div>
                <div class="grid_item_middle_item">
                  <div class="middle_title">认领时限</div>
                  <div class="middle_num">{{ item.claimEndTime || "-" }}</div>
                </div>
              </div>
              <div class="item_content_box">
                <div
                  class="item_content"
                  v-if="
                    isHeadAdmin ||
                    isPublic ||
                    isPrivate ||
                    isHeadOps ||
                    isBranchAdmin
                  "
                >
                  <span class="item_left">活动总客户数：</span>
                  <span class="item_right">{{ item.custNum }}</span>
                </div>
                <div
                  class="item_content"
                  v-if="isHeadAdmin || isPublic || isPrivate || isHeadOps"
                >
                  <span class="item_left">推送成功客户数：</span>
                  <span class="item_right">{{ item.pushSuccessNum }}</span>
                </div>
                <div
                  class="item_content"
                  v-if="isHeadAdmin || isPublic || isPrivate || isHeadOps"
                >
                  <span class="item_left">机构分配率：</span>
                  <span class="item_right">{{
                    item.allocateRate == -1 ? "无需二次分配" : item.allocateRate
                  }}</span>
                </div>
                <div class="item_content">
                  <span class="item_left">已下发客户数：</span>
                  <span class="item_right">{{ item.dispatchNum }}</span>
                </div>
                <div class="item_content">
                  <span class="item_left">客户认领率：</span>
                  <span class="item_right">{{ item.issuedNum }}</span>
                </div>
                <div class="item_content">
                  <span class="item_left">有效待走访客户数：</span>
                  <span class="item_right">{{ item.validVisitNum }}</span>
                </div>
                <div class="item_content">
                  <span class="item_left">走访率：</span>
                  <span class="item_right">{{ item.visitRate }}</span>
                </div>
                <div class="item_content">
                  <span class="item_left">贷款合同签约率：</span>
                  <span class="item_right">{{ item.loanSignRate }}</span>
                </div>
                <div class="item_content">
                  <span class="item_left">活期存款金额提升：</span>
                  <span class="item_right">{{
                    item.increaseDepositAmount
                  }}</span>
                </div>
              </div>
              <div class="item_footer">
                <van-button
                  type="primary"
                  round
                  class="btn"
                  @click="goToBranch(item)"
                  v-if="isHeadAdmin || isPublic || isPrivate || isHeadOps"
                  >查看支行</van-button
                >
                <van-button
                  type="primary"
                  round
                  class="btn"
                  @click="goToOutlet(item)"
                  v-if="
                    isHeadAdmin ||
                    isPublic ||
                    isPrivate ||
                    isHeadOps ||
                    isBranchAdmin
                  "
                  >查看网点</van-button
                >
                <van-button
                  type="primary"
                  round
                  class="btn"
                  @click="goToCustom(item)"
                  v-if="isBranchAdmin || isOutletAdmin"
                  >查看个人</van-button
                >
              </div>
            </div>
          </van-list>
        </van-pull-refresh>
      </div>
      <van-popup v-model="showStartDatePicker" position="bottom">
        <van-datetime-picker
          v-model="startTime"
          type="date"
          show-toolbar
          @cancel="showStartDatePicker = false"
          @confirm="onConfirmStartDate"
        />
      </van-popup>
      <van-popup v-model="showEndDatePicker" position="bottom">
        <van-datetime-picker
          v-model="endTime"
          type="date"
          show-toolbar
          @cancel="showEndDatePicker = false"
          @confirm="onConfirmEndDate"
        />
      </van-popup>
      <van-popup v-model="showSourcePicker" position="bottom">
        <van-picker
          show-toolbar
          :columns="campsourceList"
          @cancel="showSourcePicker = false"
          @confirm="onConfirmSource"
        />
      </van-popup>
    </div>
  </div>
</template>
<script>
import moment from "moment";
import navbar from "../../components/navbar/navbar.vue";
import { cardList, visitingTaskList } from "@/service/api/listStatistics.js";
import {
  Tab,
  Tabs,
  Icon,
  Button,
  Search,
  RadioGroup,
  Radio,
  List,
  PullRefresh,
  Dialog,
  Toast,
  Picker,
  DatetimePicker,
} from "vant";
export default {
  data() {
    return {
      custType: "",
      pageNum: 1,
      pageSize: 5,
      refreshing: false,
      loading: false,
      finished: false,
      gridPattern: "1",
      gridLevel: "1",
      currentPage: 1,
      list: [],
      searchForm: {
        startTime: "",
        endTime: "",
        campsource: "",
        campaignName: "",
      },
      startTime: undefined,
      endTime: undefined,
      campsource: "",
      campsourceList: [],
      isHeadAdmin: false,
      isPublic: false,
      isPrivate: false,
      isHeadOps: false,
      isBranchAdmin: false,
      isOutletAdmin: false,
      isCommonManager: false,
      showStartDatePicker: false,
      showEndDatePicker: false,
      showSourcePicker: false,
      cardInfo: {
        createCampNumWeek: "0",
        createCampNumMonth: "0",
        createCampNumYear: "0",
        pushCampNumWeek: "0",
        pushCampNumMonth: "0",
        pushCampNumYear: "0",
        otherCampNumWeek: "0",
        otherCampNumMonth: "0",
        otherCampNumYear: "0",
      },
    };
  },
  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,
    VanPicker: Picker,
    VanDatetimePicker: DatetimePicker,
  },

  created() {
    sessionStorage.removeItem("branchItem");
    sessionStorage.removeItem("outletItem");
    sessionStorage.removeItem("customItem");
    const str = sessionStorage.getItem("userInfo") || "{}";
    const userInfo = JSON.parse(str);
    if (userInfo?.roles?.includes("headAdmin")) {
      this.isHeadAdmin = true;
    } else if (userInfo?.roles?.includes("headPublic")) {
      this.isPublic = true;
    } else if (userInfo?.roles?.includes("headPrivate")) {
      this.isPrivate = true;
    } else if (userInfo?.roles?.includes("headOps")) {
      this.isHeadOps = true;
    } else if (userInfo?.roles?.includes("branchAdmin")) {
      this.isBranchAdmin = true;
    } else if (userInfo?.roles?.includes("outletAdmin")) {
      this.isOutletAdmin = true;
    } else if (userInfo?.roles?.includes("commonManager")) {
      this.isCommonManager = true;
    }
    if (this.isPublic) {
      this.custType = "2";
    } else if (this.isPrivate) {
      this.custType = "0";
    } else {
      this.custType = "2";
    }
    if (this.isBranchAdmin) {
      this.campsourceList = ["总行推送的", "我创建的"];
    } else if (this.isOutletAdmin || this.isCommonManager) {
      this.campsourceList = ["总行下发的", "支行下发的"];
    }
    const custType = sessionStorage['custType'];
    if(custType){
      this.custType = custType;
    }
    this.initCardList();
    this.queryList();
  },
  methods: {
    scrollToTop() {
      window.scrollTo({
        top: 0,
        behavior: "smooth",
      });
    },
    // 查询卡片信息
    async initCardList() {
      const res = await cardList(this.custType);
      if (res.code == 200 && res.rows.length > 0) {
        this.cardInfo = res.rows[0];
      } else {
        this.cardInfo = {
          createCampNumWeek: "0",
          createCampNumMonth: "0",
          createCampNumYear: "0",
          pushCampNumWeek: "0",
          pushCampNumMonth: "0",
          pushCampNumYear: "0",
          otherCampNumWeek: "0",
          otherCampNumMonth: "0",
          otherCampNumYear: "0",
        };
      }
    },
    // 查看支行
    goToBranch(item) {
      this.$router.push("/listStatisBranchDetail");
      const branchItem = JSON.stringify({
        campaignId: item.campaignId,
        campaignName: item.campaignName,
      });
      sessionStorage.setItem("branchItem", branchItem);
      sessionStorage.setItem("custType", this.custType);
    },
    // 查看网点
    goToOutlet(item) {
      this.$router.push("/listStatisOutletDetail");
      let outletItem = {
        campaignId: item.campaignId,
        campaignName: item.campaignName,
      };
      if (this.isBranchAdmin) {
        outletItem.deptId = item.deptId;
      }
      sessionStorage.setItem("outletItem", JSON.stringify(outletItem));
      sessionStorage.setItem("custType", this.custType);
    },
    // 查看个人
    goToCustom(item) {
      this.$router.push("/listStatisCustomDetail");
      let customItem = {
        campaignId: item.campaignId,
        campaignName: item.campaignName,
        deptName: item.deptName,
        outlets: item.outlets || "",
      };
      if (this.isOutletAdmin || this.isCommonManager) {
        customItem.personType = "outlet";
        customItem.outletsId = item.outletsId || "";
      } else {
        customItem.personType = "branch";
      }
      sessionStorage.setItem("customItem", JSON.stringify(customItem));
      sessionStorage.setItem("custType", this.custType);
    },
    // 清除选项框条件
    clearOption(key) {
      this.searchForm[key] = "";
      this[key] = undefined;
      this.onRefresh();
    },
    tabChange() {
      this.currentPage = 1;
      this.pageNum = 1;
      this.initCardList();
      this.onRefresh();
    },
    onConfirmStartDate(item) {
      this.searchForm.startTime = moment(item).format("YYYY-MM-DD");
      this.startTime = item;
      this.showStartDatePicker = false;
      this.onRefresh();
    },
    onConfirmEndDate(item) {
      this.searchForm.endTime = moment(item).format("YYYY-MM-DD");
      this.endTime = item;
      this.showEndDatePicker = false;
      this.onRefresh();
    },

    onConfirmSource(item) {
      this.searchForm.campsource = item;
      this.campsource = item;
      this.showSourcePicker = false;
      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();
    },
    resetForm() {
      this.searchForm = {
        startTime: "",
        endTime: "",
        campsource: "",
        campaignName: "",
      };
      this.onRefresh();
    },
    //下拉刷新
    onRefresh() {
      console.log(this.searchForm);
      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 params = {
        ...this.searchForm,
        custType: this.custType,
        pageNum: this.pageNum,
        pageSize: this.pageSize,
      };
      const res = await visitingTaskList(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;
  }
}
.task-box {
  margin: 12px;
  background: #1990ff;
  border-radius: 4px;
  padding-bottom: 9px;
  .task-title {
    font-size: 16px;
    color: #fff;
    text-align: center;
    line-height: 43px;
    font-weight: 600;
  }
  .grid_item_middle {
    margin: 0 8px;
    background: rgba(255, 255, 255, 0.85);
    border-radius: 4px;
  }
}

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

.search-input {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 15px;
  background: #fff;
  .special-search {
    ::v-deep .van-field__control {
      font-size: 12px;
    }
  }
}

.special-input-box {
  margin-top: 0;
}

.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;
  margin-right: 8px;
}

/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;
  .item_content_box {
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;
    .item_content {
      margin-top: 8px;
      flex: 0 0 50%;
      font-size: 12px;
      .item_left {
        color: #8c8c8c;
      }
      .item_right {
        color: #262626;
        font-weight: 600;
        word-break: break-all;
      }
    }
  }
  .item_footer {
    text-align: right;
    margin-top: 10px;
    .btn {
      height: 30px;
      margin-left: 8px;
    }
  }
}
.top-box {
  display: flex;
  justify-content: space-between;
  .right-label {
    .label {
      display: inline-block;
      background: rgba(73, 177, 108, 0.1);
      color: rgba(73, 177, 108, 1);
      font-size: 14px;
      border-radius: 4px;
      margin-left: 5px;
      line-height: 20px;
      padding: 0 5px;
    }
  }
}
.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, 250);
  padding: 10px 15px;
  margin-top: 10px;
}
.grid_item_middle_item {
  text-align: center;
  width: 30%;
}
.middle_num {
  margin-top: 10px;
  font-weight: 600;
  font-size: 14px;
  color: #262626;
}
.middle_title {
  font-size: 12px;
  color: #595959;
}
.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>