<template>
  <div class="container">
    <div class="titleBox">
      <div class="title">勤务排班</div>
    </div>
    <!-- 排班表 -->
    <div class="schedulTable" :class="drawer ? 'isWidth' : ''">
      <el-calendar ref="calendar" v-model="value">
        <template #header="{ date }">
          <span>{{ date }}</span>
          <div class="choose-month-box">
            <div
              class="choose-month-item-box"
              @click="selectDate('prev-month', date)"
            >
              上个月
            </div>
            <div
              class="choose-month-item-box center-month"
              @click="selectDate('today', date)"
            >
              本月
            </div>
            <div
              class="choose-month-item-box"
              @click="selectDate('next-month', date)"
            >
              下个月
            </div>
          </div>
        </template>
        <template #dateCell="{ data }">
          <div class="dateTxt">
            <div :class="data.isSelected ? 'is-selected' : ''">
              {{ data.day.split("-").slice(2).join("-") }}
            </div>
          </div>
          <div
            style="width: 100%"
            v-for="item in list.calendarList"
            :key="item.id"
            @click="getday(item)"
            @mouseenter="showChecked(item)"
            @mouseleave="leaveChecked(item)"
          >
            <div
              class="data-block"
              v-if="
                item.day.indexOf(data.day) != -1 &&
                !(item.day === list.currentFlag)
              "
            >
              <div
                class="police-block"
                v-show="item.pbs && item.mjSet && item.fjSet"
              >
                <div class="item-block jingzubg">
                  <div>排班</div>
                  <div>{{ item.pbs }}</div>
                </div>
                <div class="item-block minjingbg">
                  <div>民警</div>
                  <div>{{ item.mjSet }}</div>
                </div>
                <div class="item-block fujingbg">
                  <div>辅警</div>
                  <div>{{ item.fjSet }}</div>
                </div>
              </div>
            </div>
            <div
              class="checked-box"
              v-if="
                item.day.indexOf(data.day) != -1 &&
                item.day === list.currentFlag
              "
            >
              <div v-show="item.pbs && item.mjSet && item.fjSet">
                <div class="item-block">
                  <div class="qw-box" @click="serviceDuty(data.day)">排班</div>
                </div>
                <div class="item-block">
                  <div class="zb-box" @click="onDuty(data.day)">值班</div>
                </div>
              </div>
            </div>
          </div>
        </template>
      </el-calendar>
    </div>
    <el-drawer
      v-model="drawer"
      :direction="direction"
      :modal="false"
      size="100%"
      modal-class="drawer-contain"
    >
      <template #default>
        <div class="drawer-box">
          <div class="current-part-box">
            <div class="current-text-box">当前单位</div>
            <div class="partFrame-box">
              <!-- 特警支队 -->
              <MOSTY
                :placeholder="placeholder"
                width="200px"
                clearable
                filterable
                v-model="ssbmid"
              />
            </div>
          </div>
          <div class="jz-box">
            <div class="qw-text">排班</div>
            <div class="qw-group-num">
              <div class="left">{{ list.serviceList.pbNum }}</div>
              <div class="center">{{ list.serviceList.mNum }}</div>
              <div class="right">{{ list.serviceList.fNum }}</div>
            </div>
            <div class="qw-group-text">
              <div class="left">排班班次</div>
              <div class="center">排班民警</div>
              <div class="right">排班辅警</div>
            </div>
          </div>
          <div class="zb1-box">
            <div class="qw-text">值班</div>
            <div class="qw-group-num">
              <div class="text1">{{ list.pbNum.wzgjl }}</div>
              <div class="text2">{{ list.pbNum.zbjl }}</div>
              <div class="text4">{{ list.pbNum.kdyjl }}</div>
            </div>
            <div class="qw-group-text">
              <div class="text1">未在岗警力</div>
              <div class="text2">值班警力</div>
              <div class="text4">可抽调警力</div>
            </div>
          </div>
        </div>
      </template>
    </el-drawer>
  </div>
</template>

<script>
import { ref, reactive, onMounted, nextTick, watch } from "vue";
import { useRouter } from "vue-router";
import { getItem } from "@/utils/storage";
import {
  getPbbbByMonthBm,
  getJlByBm
} from "@/api/service/watchmanSchedulingPbrl.js";
import { ElMessage, ElMessageBox } from "element-plus";
import { useStore } from "vuex";
import { findIndex } from "ol/array";
import { selectDeptPage } from "@/api/user-manage";
import MOSTY from "@/components/MyComponents/Department/index";
export default {
  components: {
    // ProgressBar
    MOSTY
  },
  setup(props, context) {
    const router = useRouter();
    const depList = ref([]); //部门数据
    const ssbmid = ref(""); //部门id
    const store = useStore();
    const calendar = ref();
    const value = ref(new Date());
    const rangeDate = ref("");
    const station = ref("");
    const stationGroup = [];
    const scheduling = ref(new Date());
    const drawer = ref(true);
    const getday = (item) => {
      drawer.value = true;
    };
    const placeholder = ref("");
    const dept = ref("");
    const direction = ref("rtl");
    const form = reactive({
      jz: "", //警组
      mjSet: "", //民警数量
      fjSet: "", //辅警数量
      zbsl: "" //装备数量
    });
    const list = reactive({
      currentVal: "",
      currentFlag: "",
      open: false,
      //   title: `报备单位: ${store.state.user.user.userOrgName}`,
      //   part: store.state.user.user.userOrgName,
      //   partCode: store.state.user.user.userOrgCode,
      partList: [],
      // 日历数据
      calendarList: [],
      serviceList: {
        pbNum: 0,
        mNum: 0,
        fNum: 0
      },
      shiftList: {
        wzgNum: 0,
        zbNum: 0,
        bqNum: 0,
        kcdNum: 0
      },
      list0: [],
      list1: [],
      list2: [],
      apartment: "",
      pbNum: {
        wzgjl: 0,
        zbjl: 0,
        kdyjl: 0
      }
      //   ssbmdm: store.state.user.user.userOrgCode,
      //   gajgjgdm: store.state.user.user.userOrgCode
    });
    // 联级选择
    const cascader = ref();
    const handleChange = (data) => {
      let nodes = cascader.value.getCheckedNodes();
      if (
        cascader.value.getCheckedNodes() &&
        cascader.value.getCheckedNodes().length > 0
      ) {
        list.partCode = nodes[0].value;
        list.part = nodes[0].label;
        list.ssbmdm = nodes[0].value;
        list.gajgjgdm = nodes[0].value;
        pbxx();
      }
    };
    onMounted(() => {
      dept.value = getItem("deptId");
      placeholder.value = dept.value[0].deptName;
      ssbmid.value = dept.value[0].deptId;
    });
    // 获取用户的部分
    const selectDate = (val, date) => {
      calendar.value.selectDate(val);
      pbxx();
      list.serviceList.pbNum = "";
      list.serviceList.mNum = "";
      list.serviceList.fNum = "";
      list.pbNum.wzgjl = "";
      list.pbNum.zbjl = "";
      list.pbNum.kdyjl = "";
    };
    // 截取事件
    const findStrIndex = (str, cha, num) => {
      let x = str.indexOf(cha);
      for (let i = 0; i < num; i++) {
        x = str.indexOf(cha, x + 1);
      }
      return str.substring(0, x);
    };
    // 格式化日期
    const formatDate = (time) => {
      time = time.replace(/\//g, "-"); // 格式化日期
      time = time.replace(/\//g, "");
      time = findStrIndex(time, "-", 1);
      return time;
    };
    // 获取当前时间
    const GetDate = () => {
      const date = new Date();
      const YY = date.getFullYear();
      const mm = (date.getMonth() + 1).toString().padStart(2, 0);
      const dd = date.getDate().toString().padStart(2, 0);
      return `${YY}-${mm}-${dd}`;
    };
    // 获取排班数据
    const pbxx = (val, id) => {
      let times = formatDate(value.value.toLocaleDateString()); // 获取当前日期
      getPbbbByMonthBm({
        time: times,
        ssbmid: id ? id : dept.value[0].deptId
      }).then((res) => {
        list.calendarList = res;
        if (val === 1) {
          let date = new Date();
          let month = date.getMonth() + 1;
          let day = date.getDate();
          if (month < 10) {
            month = "0" + month;
          }
          if (day < 10) {
            day = "0" + day;
          }
          let todayTime = date.getFullYear() + "-" + month + "-" + day;
          list.calendarList.forEach((element) => {
            if (element.day === todayTime) {
              list.currentFlag = todayTime;
              list.serviceList.pbNum = element.pbs;
              list.serviceList.mNum = element.mjSet;
              list.serviceList.fNum = element.fjSet;
            }
          });
          countZbrys(1);
        }
      });
    };
    
    const chooseDay = ref("");
    const onDuty = (val) => {
      router.push({
        path: "/shifTscheduling/index",
        query: { time: val, bm: dept.value[0].deptId }
      });
    };
    // go to the scheduling page
    const serviceDuty = (val) => {
      router.push({
        path: "/watchmanScheduling/pbjl",
        query: { time: val, bm: dept.value[0].deptId }
      });
    };
    const showChecked = (val) => {
      if (val.pbs && val.mjSet && val.fjSet) {
        list.currentFlag = val.day;
        list.serviceList.pbNum = val.pbs;
        list.serviceList.mNum = val.mjSet;
        list.serviceList.fNum = val.fjSet;
        countZbrys(2, val);
      }
    };
    const leaveChecked = (val) => {};
    const submit = () => {
      list.open = false;
      list.list0 = [];
      list.list1 = [];
      list.list2 = [];
    };
    // 查值班数据 流程图
    const countZbrys = (val, el) => {
      let time = "";
      if (val === 1) {
        time = GetDate(); // 获取当前日期
        for (let i = 0; i < list.calendarList.length; i++) {
          const element = list.calendarList[i];
          if (element.day == time) {
            el = element;
          }
        }
      } else {
        time = el.day;
      }
      getJlByBm({
        time: time,
        ssbmid: dept.value[0].deptId
      }).then((res) => {
        list.pbNum.wzgjl = res.wzgjl;
        list.pbNum.zbjl = res.zbjl;
        list.pbNum.kdyjl =
          res.all - (res.zbjl + res.wzgjl + el.mjSet + el.fjSet);
      });
    };
    watch(ssbmid, (val, oldval) => {
      dept.value[0].deptId = val;
      pbxx(1, dept.value[0].deptId);
    });
    return {
      rangeDate, //日期选择
      station, // 站下拉选
      stationGroup, // 站数组
      calendar,
      selectDate,
      value,
      scheduling, // 排班当前时间
      pbxx,
      formatDate,
      list,
      getday,
      direction,
      drawer,
      onDuty,
      serviceDuty,
      router,
      showChecked,
      leaveChecked,
      ssbmid,
      store,
      chooseDay,
      submit,
      countZbrys,
      handleChange,
      cascader,
      depList,
      placeholder
      //   orgprops
    };
  }
};
</script>

<style lang="scss" scoped>
@import "~@/styles/index.scss";
.search-box {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  .datePicker {
    margin-right: 30px;
  }
}
.schedulTable {
  width: 100%;
  overflow: hidden;
  overflow-y: scroll;
  margin-top: 10px;
}
.data-block {
  height: 60px;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  bottom: 0;
  .item-block {
    width: 100%;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    text-align: center;
    align-content: center;
    width: 35px;
    height: 35px;
    border-radius: 5px;
    font-size: 12px;
    margin-top: 20px;
  }
  .jingzubg {
    border: 1px solid rgb(22, 213, 225);
    color: rgb(22, 213, 225);
  }
  .minjingbg {
    border: 1px solid rgb(83, 167, 233);
    color: rgb(83, 167, 233);
  }
  .fujingbg {
    // background-color: rgba(255, 178, 66, 0.2);
    border: 1px solid rgb(140, 143, 253);
    color: rgb(140, 143, 253);
  }
  // .zhuangbeibg {
  //   // background-color: rgba(50, 140, 236, 0.2);
  //   border: 1px solid rgb(50, 140, 236);
  //   color: rgb(50, 140, 236);
  // }
}
.isWidth {
  width: 70%;
}
.drawer-box {
  color: #fff;
}
.current-part-box {
  display: flex;
  margin-top: 100px;
  .current-text-box {
    height: 50px;
    line-height: 50px;
    margin-right: 20px;
  }
  .partFrame-box {
    width: 300px;
    height: 50px;
    line-height: 50px;
    border: 1px solid rgb(25, 246, 254);
    border-radius: 50px;
    padding-left: 20px;
    color: rgb(25, 246, 254);
  }
}
.checked-box {
  width: 100%;
  text-align: center;
  font-size: 12px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  .item-block {
    width: 100%;
    display: flex;
    justify-content: center;
  }
  .qw-box {
    width: 50%;
    height: 30px;
    line-height: 30px;
    background-color: rgb(10, 158, 204);
    border-radius: 32px;
    margin-bottom: 5px;
  }
  .zb-box {
    width: 50%;
    height: 30px;
    line-height: 30px;
    background-color: rgb(10, 158, 204);
    border-radius: 32px;
  }
}
.dateTxt {
  // margin: 0 auto;
  text-align: right;
}
.is-selected {
  // position: absolute;
  // width: 20px;
  // height: 20px;
  // line-height: 20px;
  // border-radius: 50%;
  background-color: #2b8eeb;
  color: #fff;
  text-align: right;
  float: right;
  // font-size: 12px;
}

.jz-box {
  margin-top: 50px;
  height: 250px;
  background-image: url("~@/assets/images/3frame.png");
  background-repeat: no-repeat;
  background-size: 100% 100%;
  .qw-text {
    height: 55px;
    line-height: 55px;
    text-align: center;
  }
  .qw-group-num {
    margin-top: 100px;
    // height: 55px;
    // line-height: 55px;
    text-align: center;
    display: flex;
    justify-content: space-around;
  }
  .qw-group-text {
    margin-top: 10px;
    // height: 25px;
    // line-height: 25px;
    text-align: center;
    display: flex;
    justify-content: space-between;
  }
  .left {
    width: 25%;
  }
  .center {
    width: 50%;
  }
  .right {
    width: 25%;
  }
}
.zb1-box {
  margin-top: 50px;
  height: 250px;
  background-image: url("~@/assets/images/3frame.png");
  background-repeat: no-repeat;
  background-size: 100% 100%;
  .qw-text {
    height: 55px;
    line-height: 55px;
    text-align: center;
  }
  .qw-group-num {
    margin-top: 100px;
    // height: 55px;
    // line-height: 55px;
    text-align: center;
    display: flex;
    justify-content: space-between;
  }
  .qw-group-text {
    margin-top: 10px;
    // height: 25px;
    // line-height: 25px;
    text-align: center;
    display: flex;
    justify-content: space-between;
  }
  .text1 {
    width: 23%;
  }
  .text2 {
    width: 27%;
  }
  .text3 {
    width: 27%;
  }
  .text4 {
    width: 23%;
  }
}

.choose-month-box {
  display: flex;
  .choose-month-item-box {
    width: 100px;
    height: 30px;
    line-height: 30px;
    text-align: center;
    border: 1px solid rgb(25, 246, 254);
    color: rgb(25, 246, 254);
    font-size: 14px;
    border-radius: 5px;
    cursor: pointer;
  }
  .center-month {
    margin: 0 10px;
  }
}

.content-box {
  display: flex;
  height: 500px;
  color: #fff;
  // flex-direction: column;
  .duty-box {
    height: 500px;
    color: #fff;
    .police-box {
      display: flex;
    }
    .police-center-box {
      margin: 80px 0;
    }
    .duty-text {
      width: 70px;
      height: 30px;
      line-height: 30px;
    }
    .police-choose {
      width: 100px;
      height: 30px;
      line-height: 30px;
      border-radius: 30px;
      background-color: rgb(10, 158, 204);
      text-align: center;
      margin-left: 20px;
    }
    .policeList-box {
      margin-left: 50px;
      padding: 10px;
      line-height: 25px;
      width: 800px;
      // height: 100px;
      border: 1px solid rgb(10, 158, 204);
      border-radius: 5px;
    }
  }
  .choosed-box {
    margin-left: 20px;
  }
}
.police-block {
  width: 100%;
  display: flex;
  justify-content: space-around;
}

::-webkit-scrollbar-thumb {
  background-color: rgb(17, 66, 96);
  border-radius: 3px;
}

::-webkit-scrollbar {
  background-color: rgb(6, 42, 72);
}
::v-deep .el-calendar-table thead th:before {
  content: "周";
}
::v-deep .el-calendar-table td.is-today {
  color: rgb(191, 241, 210);
}
// ::v-deep .el-calendar-table:not(.is-range) td.next{
//   visibility: hidden;
// }
// ::v-deep .el-calendar-table:not(.is-range) td.prev{
//   visibility: hidden;
// }
::v-deep .el-calendar-table td.is-selected {
  background-color: rgb(17, 66, 96);
}
::v-deep .el-drawer.rtl {
  background-color: rgba(12, 42, 82, 0.8);
}
::v-deep [data-v-e282fbea] .el-input__inner {
  background-color: rgb(3, 43, 96, 0.8);
  border: 1px solid rgb(3, 43, 96, 0.8);
}
::v-deep [data-v-e282fbea] .el-dialog {
  background-color: rgb(0, 19, 66);
}
::v-deep [data-v-e282fbea] .el-tree-node__content > .el-tree-node__expand-icon {
  display: inline;
}
</style>
<style>
.drawer-contain {
  width: 25% !important;
  z-index: 2004 !important;
  position: fixed !important;
  top: 0px !important;
  right: 0px !important;
  bottom: 0px !important;
  left: unset !important;
}
.schedulTable .el-calendar-day{
  height: 96px ;
}
</style>
