<template>
  <div class="content">
    <div class="search">
      <div class="time-box">
        截止时间范围:
        <div class="time-picker-block" @click="calendarShow = true">
          {{ startDate == "" ? "请选择" : startDate }}
        </div>
        <div>&nbsp;-&nbsp;</div>
        <div class="time-picker-block" @click="calendarShow2 = true">
          {{ endDate == "" ? "请选择" : endDate }}
        </div>
      </div>
      <mobile-input
        :reset="resetvalue"
        input-align="left"
        name="CUSTOMERNAME"
        v-model="searchContent"
        :value="searchContent"
        placeholder="请输入关键字"
        size="small"
      />
      <div
        style="
          display: flex;
          justify-content: space-evenly;
          align-items: self-start;
        "
      >
        <van-button
          color="#FF574C"
          type="primary"
          @click="screen"
          plain
          style="width: 70px; height: 34px"
          size="small"
          >筛选</van-button
        >
        <van-button
          color="#FF574C"
          type="primary"
          @click="search"
          style="width: 70px; height: 34px; margin-left: 20px"
          size="small"
          >搜索</van-button
        >
      </div>
      <van-popup
        v-model="calendarShow"
        position="bottom"
        :style="{ height: '50%' }"
      >
        <van-datetime-picker
          v-model="currentDate"
          type="date"
          title="选择年月日"
          :min-date="minDate"
          :max-date="maxDate"
          @confirm="onConfirmTime"
          @cancel="calendarShow = false"
        />
      </van-popup>
      <van-popup
        v-model="calendarShow2"
        position="bottom"
        :style="{ height: '50%' }"
      >
        <van-datetime-picker
          v-model="currentDate2"
          type="date"
          title="选择年月日"
          :min-date="minDate"
          :max-date="maxDate"
          @confirm="onConfirmTime2"
          @cancel="calendarShow2 = false"
        />
      </van-popup>
      <van-popup
        class="screenPopup"
        v-model="show"
        position="top"
        :style="{ height: '30%' }"
      >
        <div class="searchStyle">
          <mobile-input
            label="客户名称"
            label-width="10px"
            :reset="resetvalue"
            input-align="left"
            name="CUSTOMERNAME"
            v-model="searchContent"
            :value="searchContent"
            placeholder="请输入关键字"
            size="small"
          />
          <!-- <d-select
            label="任务状态"
            border="true"
            v-model="selectTaskStatus"
            :value="selectTaskStatus"
            sfield="task_status"
            size="small"
          ></d-select> -->
          <d-select
            label="审批状态"
            border="true"
            v-model="selectApproveStatus"
            :value="selectApproveStatus"
            sfield="approve_status"
            size="small"
          ></d-select>
          <d-select
            label="主管机构"
            border="true"
            v-model="selectOrgName"
            :value="selectOrgName"
            :options="orgList"
            size="small"
          ></d-select>
        </div>
        <div class="screenbutton">
          <van-button
            color="#FF574C"
            type="primary"
            @click="search"
            style="width: 100px; height: 50px; display: block"
            size="small"
            >确认</van-button
          >
          <van-button
            color="#FF574C"
            type="primary"
            @click="reset()"
            plain
            style="width: 100px; height: 50px; display: block"
            size="small"
            >重置</van-button
          >
        </div>
      </van-popup>
    </div>
    <div class="Managementdata">
      <scroller-view
        v-if="nodata"
        v-calculate-height
        :pulldown="pulldown"
        :pullup="pullup"
        :refresh-data="onrefreshdata"
        :load-data="onloaddata"
      >
        <div
          v-for="(item, index) in itemDataList"
          :key="index"
          class="customer-bottom-list"
          @click="collection(item)"
        >
          <div class="customer-bottom-list-top">
            <div>
              <svg class="icon" aria-hidden="true">
                <use xlink:href="#icon-gongsimingcheng"></use>
              </svg>
            </div>
            <div>{{ item.cus_name }}</div>
          </div>
          <div class="customer-bottom-list-bottom">
            <!-- <span
              class="point-box"
              :style="{ color: colorList[item.rew_level / 10 - 1] }"
              ><div
                :style="{ background: colorList[item.rew_level / 10 - 1] }"
                class="point-rew"
              ></div>
              &nbsp;{{ item.rew_level_cnName }}预警</span
            > -->
            <span class="point-box" style="color: orange"
              ><div style="background: orange" class="point-rew"></div>
              &nbsp;{{ item.task_status_cnName }}</span
            >
            <span class="point-box" style="color: orange; margin-left: 20px"
              ><div style="background: orange" class="point-rew"></div>
              &nbsp;{{ item.approve_status_cnName }}</span
            >
            <span
              class="dead-line-content"
              v-if="item.finallyDate && item.finallyDate != ''"
              >处理截止日期:{{ item.finallyDate }}</span
            >
            <span class="delayTime-box">
              <span>{{ item.over_flag == '1' ? '已超期' : '未超期' }}</span>
            </span>
            <span class="manager-box"
              >{{ item.cust_mgr_cnName }} | {{ item.main_br_cnName }}</span
            >
          </div>
        </div>
      </scroller-view>
    </div>
  </div>
</template>

<script lang="ts">
import { Component, Vue } from "vue-property-decorator";
import { Empty, Search } from "vant";
import nettyApi from "@/constants/api/ms-netty/netty.api.ts";
import IF from "@/public/factory/InterFaceFactory";
import { NativeUI } from "@/public/ts/NativeUI";
import { Popup } from "vant";
import { log } from "mathjs";

Vue.use(Popup);

Vue.use(Search);
/**
 * @Description 对公风险预警
 * @Author wanghang
 * @Date 2022-01-26
 */
@Component({
  name: "UpcomingRiskWarning",
})
export default class UpcomingRiskWarning extends Vue {
  resetvalue = false;
  CUSTOMERNAME = "";
  orgList = []; //催收机构列表
  itemDataList = [];
  pageNo = 1;
  pullup = true;
  pulldown = true;
  nodata = true;
  show = false;
  showOrgPicker = false;
  minDate = new Date(2000, 1, 1);
  maxDate = new Date(2050, 12, 1);
  currentDate = new Date();
  currentDate2 = new Date();
  calendarShow: boolean = false; //日历1展开
  calendarShow2: boolean = false; //日历2展开
  startDate = ""; //开始时间
  endDate = ""; //结束时间
  searchContent = ""; //搜索内容
  selectTaskStatus = "10"; //待办任务只有未处理,直接传固定值
  selectApproveStatus = ""; //选中的审批状态
  selectCheckStatus = ""; //选中的检查状态
  selectOrgName = ""; //选中的主管机构
  colorList = ["red", "orange", "yellow", "blue"];
  query_type = '';

  created() {
    if (sessionStorage.getItem("businessType") == "Upcoming") {
      this.query_type = "1";
    } else if (sessionStorage.getItem("businessType") == "Common") {
      this.query_type = "2";
    }
    this.selectFXYJCX();
    this.selectOrgList();
  }
  activated() {
    this.calendarShow = false;
    this.calendarShow2 = false;
    this.show = false;
     if (sessionStorage.getItem("businessType") == "Upcoming") {
      this.query_type = "1";
    } else if (sessionStorage.getItem("businessType") == "Common") {
      this.query_type = "2";
    }
    this.reset();
    this.selectFXYJCX();
    this.selectOrgList();
  }
  mounted() {
    this.calendarShow = false;
    this.calendarShow2 = false;
    this.show = false;
    if (sessionStorage.getItem("businessType") == "Upcoming") {
      this.query_type = "1";
    } else if (sessionStorage.getItem("businessType") == "Common") {
      this.query_type = "2";
    }
    this.reset();
    this.selectFXYJCX();
    this.selectOrgList();
  }
  /**
   * @Description 对公风险预警
   * @Author wanghang
   * @Date 2022-01-26
   */
  collection(item: any) {
    this.$store.commit("setEmpty");
    this.$store.commit("setCusInfo", {});
    this.$store.commit("setCusInfo", item);
    this.$router.push({
      path: "/RiskWarningDetail",
    });
  }
  /**
   * @Description 对公风险预警
   * @Author wanghang
   * @Date 2022-01-26
   */
  screen() {
    this.show = true;
  }
  /**
   * @Description 对公风险预警
   * @Author wanghang
   * @Date 2022-01-26
   */
  search() {
    if (
      (this.startDate == "" && this.endDate == "") ||
      (this.startDate != "" && this.endDate != "")
    ) {
      this.selectFXYJCX(1);
    } else {
      NativeUI.toast("开始时间和结束时间不能只选一个");
    }
    this.show = false;
  }
  /**
   * @Description 对公风险预警
   * @Author wanghang
   * @Date 2022-01-26
   */
  reset() {
    // location.reload();
    // this.selectTaskStatus = "";
    this.selectApproveStatus = "";
    this.selectCheckStatus = "";
    this.searchContent = "";
    this.selectOrgName = "";
    this.searchContent = "";
    this.startDate = "";
    this.endDate = "";
    this.selectFXYJCX(1);
  }
  /**
   * @Description 对公风险预警
   * @Author wanghang
   * @Date 2022-01-26
   */
  onloaddata() {
    this.pageNo = Math.ceil(this.itemDataList.length / 10) + 1;
    return this.selectFXYJCX(this.pageNo);
  }
  /**
   * @Description 对公风险预警
   * @Author wanghang
   * @Date 2022-01-26
   */
  onrefreshdata() {
    return this.selectFXYJCX();
  }
  /**
   * @Description 对公风险预警
   * @Author wanghang
   * @Date 2022-01-26
   */
  selectFXYJCX(pageNo = 1) {
    if (
      this.startDate != "" &&
      this.endDate != "" &&
      !this.compareTime(this.startDate, this.endDate)
    ) {
      NativeUI.toast("终止时间不能早于开始时间");
    } else {
      let param = {
        maxLine: "10",
        targetPage: pageNo,
        query_type: this.query_type, //1.非全量查询 2.全量查询
        task_status: this.selectTaskStatus,
        approve_status: this.selectApproveStatus,
        input_br_id: this.selectOrgName,
        input_date_begin: this.startDate,
        input_date_end: this.endDate,
        cus_name: this.searchContent, //用于模糊查询
        tc: nettyApi.TRADE_CODE.selectFXYJCX,
      };
      NativeUI.showWaiting("正在查询...");
      return IF.transferDataInter(nettyApi.commonRq, param).then((res: any) => {
        console.log('查询状态',this.query_type)
        console.log(res);
        NativeUI.closeWaiting();
        if (res.rc == "1") {
          if (pageNo == 1) {
            this.itemDataList = res.RewTaskList;
          } else {
            this.itemDataList = this.itemDataList.concat(res.RewTaskList);
          }
        } else {
          NativeUI.toast(res.msg);
        }
        if (this.itemDataList.length < 10) {
          // 如果小于10条,说明没有更多数据了,去掉加载更多
          this.pullup = false;
        } else {
          //如果等于10条那么就保留加载更多
          this.pullup = true;
        }
      });
    }
  }

  /**
   * @Description 对公风险预警
   * @Author wanghang
   * @Date 2022-01-26
   */
  selectOrgList() {
    const params = {
      tc: nettyApi.TRADE_CODE.selectOrgList,
      QFLGE: "2", //1是登录,2是机构查询
    };
    NativeUI.showWaiting();
    return IF.transferDataInter(nettyApi.commonRq, params).then((res: any) => {
      NativeUI.closeWaiting();
      if (res.rc == 1) {
        // res.CustomerGRArray = res.CustomerGRArray;
        let temp = {};
        for (let i = 0; i < res.SOrgList.length; i++) {
          temp = {};
          (temp as any).text = res.SOrgList[i].organname;
          (temp as any).value = res.SOrgList[i].organno;
          this.orgList.push(temp as never);
        }
      } else {
        NativeUI.toast("查询机构失败");
      }
    });
  }
  onConfirmTime(date: Date) {
    this.calendarShow = false;
    this.startDate = this.formatDate(date);
    if (this.endDate != "") {
      if (!this.compareTime(this.startDate, this.endDate)) {
        NativeUI.toast("开始时间不能晚于结束时间");
        this.startDate = "";
      } else {
        this.selectFXYJCX();
      }
    }
  }
  onConfirmTime2(date: Date) {
    this.calendarShow2 = false;
    this.endDate = this.formatDate(date);
    if (this.startDate != "") {
      if (!this.compareTime(this.startDate, this.endDate)) {
        NativeUI.toast("结束时间不能早于开始时间");
        this.endDate = "";
      } else {
        this.selectFXYJCX();
      }
    } else {
      NativeUI.toast("开始时间未选择");
    }
  }
  //处理时间格式
  formatDate(date: Date) {
    let month = (date.getMonth() + 1).toString();
    if (month.length == 1) {
      month = "0" + month;
    }
    let day = date.getDate().toString();
    if (day.length == 1) {
      day = "0" + day;
    }
    return date.getFullYear() + "-" + month + "-" + day;
  }
  //比较时间早晚
  compareTime(time1: any, time2: any) {
    let strs1 = time1.split("-");
    let strs2 = time2.split("-");
    let start = "";
    let end = "";
    for (let i = 0; i < strs1.length; i++) {
      start += strs1[i];
      end += strs2[i];
    }
    return parseFloat(start) <= parseFloat(end);
  }
  getOrgId() {
    var res: String = "";
    for (let i = 0; i < this.orgList.length; i++) {
      if ((this.orgList[i] as any).text === this.selectOrgName) {
        res = (this.orgList[i] as any).value;
      }
    }
    return res;
  }
  //获取是否超期
  getText(time: String) {
    var strs = time.split("-"); //之前的时间
    var temp = "";
    for (let i = 0; i < strs.length; i++) {
      temp += strs[i];
    }
    var strs2 = new Date().toLocaleDateString().split("/");
    var now = "";
    for (let i = 0; i < strs2.length; i++) {
      if (strs2[i].length == 1) {
        now += "0";
        now += strs2[i];
      } else {
        now += strs2[i];
      }
    }
    return now > temp ? "已超期" : "未超期";
  }
}
</script>

<style scoped>
::v-deep .van-cell__title,
::v-deep .van-field__label {
  display: flex;
  align-items: center;
}
.content {
  width: 100%;
}
.search {
  width: 100%;
  height: 70px;
  border: 1px solid #e1e2e3;
  display: flex;
  align-items: center;
  justify-content: flex-start;
}
::v-deep .d-form-field {
  padding-bottom: 0;
  /* width: 70%; */
  height: 50px;
}
::v-deep .van-field__control {
  height: 30px;
}
.customer-bottom-list {
  border-radius: 10px;
  background-color: #f8fdff;
  box-shadow: 0 0 6px #d7d7d7;
  margin: 0 10px 16px 10px;
  padding-bottom: 10px;
  position: relative;
}
.customer-bottom-list-top > div:nth-child(1) svg {
  width: 40px;
  height: 40px;
}
.customer-bottom-list-top > div:nth-child(2) {
  font-size: 115%;
  font-weight: bold;
  font-family: "微软雅黑 Bold", "微软雅黑 Regular", "微软雅黑";
  margin-left: 0.8%;
}
.customer-bottom-list-top {
  width: 100%;
  display: flex;
  justify-content: flex-start;
  align-items: center;
  padding-top: 1.5%;
  margin-left: 1.5%;
}
.customer-bottom-list-top > div:nth-child(3) {
  width: 28%;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  justify-content: space-around;
  grid-column-gap: 4px;
  align-items: center;
  margin-left: 1%;
}
.customer-bottom-list-top > div:nth-child(3) div {
  color: #ff6e0d;
  background-color: rgba(255, 247, 204, 1);
  font-size: 80%;
  padding-top: 2%;
  padding-bottom: 1.5%;
  border-radius: 2px;
  text-align: center;
  width: 100%;
}
.customer-bottom-list-bottom {
  /* width: 60%; */
  height: 26px;
  display: flex;
  margin-top: 10px;
  padding-left: 70px;
  justify-content: flex-start;
  align-items: center;
  flex-wrap: nowrap;
}
.Managementdata {
  margin-top: 20px;
  padding: 0 20px;
  height: calc(100vh - 130px);
}
::v-deep .Managementdata .bs-wrapper {
  height: 100%;
}
::v-deep .screenPopup {
  width: 75%;
  margin-left: 25%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 60px 5% 5px 5%;
  z-index: 99 !important;
}
.screenPopup > div:nth-child(1) {
  width: 85%;
  height: 100%;
}
::v-deep .van-overlay {
  z-index: 90 !important;
}
.screenbutton {
  width: 15%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-content: center;
  justify-content: center;
}
::v-deep .screenbutton button {
  margin: 20px auto;
}
::v-deep .screenbutton .van-button__content {
  font-size: 18px;
}
.searchStyle {
  display: grid;
  align-items: center;
  grid-template-columns: 1fr 1fr;
}
.searchStyle div {
  width: 90%;
}
::v-deep .van-picker {
  border-top-left-radius: 15px;
}
.time-box {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-left: 30px;
  height: auto;
  /* width: 200px; */
}
.time-picker-block {
  /* height: 50px; */
  display: flex;
  align-items: center;
  justify-content: center;
  width: 110px;
  height: 30px;
  border-radius: 5px;
  background-color: #eee;
}
.point-box {
  display: flex;
  align-items: center;
}
.point-rew {
  width: 12px;
  height: 12px;
  border-radius: 50%;
}
.dead-line-content {
  margin-left: 30px;
  color: #949596;
}
.delayTime-box {
  font-size: 12px;
  line-height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #e5f7f7;
  color: #4dcef5;
  border-radius: 3px;
  width: 50px;
  height: 24px;
  margin-left: 30px;
}
.manager-box {
  color: #9e9e9e;
  margin-left: 20px;
}
::v-deep .van-popup--bottom{
  margin-left: 25%;
  width: 75%;
}
</style>