<template>
  <view class="setDept">
    <view class="page_tab">
      <view class="page_tab_bar active">
				<view class="dept_title">{{ configName }}</view>
        <view class="tab_dept">
          <button type="default" size="mini" class="addDept" @click="addDeptClick">
            添加科室
          </button>
          <button type="default" size="mini" class="addBuilding" @click="addBuildingClick">
            楼栋选择
          </button>
        </view>
      </view>
    </view>
    <view v-if="zxzData.length == 0" class="zwsj">
      <image class="zwsj-img" mode="widthFix" src="../../static/img/zanwushuju.png"></image>
      <view class="zwsj-txt">暂无数据</view>
    </view>
    <view v-if="zxzData.length" class="page_items">
      <view class="page_items_scroll">
        <block v-for="row in zxzData" :key="row.id">
          <view class="keyword-entry" hover-class="keyword-entry-tap">
            <view class="keyword-text">
              <rich-text :nodes="row.dept"></rich-text>
            </view>
            <view class="keyword-img" @tap.stop="deleteDept(row.id)">
              删除科室
            </view>
          </view>
        </block>
      </view>
    </view>
    <!-- 底部确认负责科室 -->
    <view class="toolbar" @click="determine()" hover-class="seimin-btn-hover">
      <text class="toolbar-sao">确认负责科室</text>
    </view>
    <!-- 弹窗 -->
    <showModel :title="models.title" :icon="models.icon" :disjunctor="models.disjunctor" :content="models.content"
      @ok="ok" @cancel="cancel" @know="know" :operate="models.operate" :phone="models.phone"></showModel>
    <!-- 提示下班弹窗 -->
    <showModel :title="models1.title" :icon="models1.icon" :disjunctor="models1.disjunctor" :content="models1.content"
      @know="know1" :operate="models1.operate"></showModel>
  </view>
</template>
<script>
  import {
    post,
    get
  } from "../../http/http.js";
  export default {
    data() {
      return {
        configName: "", //快捷组合名称
        id: "", //快速组合的id
        changedept: 0, //是否是从切换负责科室过来的
        zxzData: [], //已选择的科室
        // 弹窗model
        models: {
          disjunctor: false,
        },
        // 下班弹窗model
        models1: {
          disjunctor: false,
        },
        delId: "", //科室删除的id
        userId: uni.getStorageSync("userData").user.id, //当前用户的id
      };
    },
    methods: {
      //返回(下班弹窗)
      know1() {
        this.models1.disjunctor = false;
        uni.redirectTo({
          url: "../homePage/homePage",
        });
      },
      //返回
      know() {
        this.models.disjunctor = false;
      },
      //确定
      ok() {
        this.models.disjunctor = false;
        let i = this.zxzData.findIndex((item) => item.id == this.delId);
        this.zxzData.splice(i, 1);
        uni.setStorageSync("setDepts", this.zxzData);
      },
      //取消
      cancel() {
        this.models.disjunctor = false;
      },
      // 添加科室
      addDeptClick() {
        let setDeptConfg = uni.getStorageSync("setDeptConfg") || {};
        uni.navigateTo({
          url: `../search/search?type=setDept&configName=${this.configName}&id=${this.id}&changedept=${this.changedept}&quickCombinationId=${setDeptConfg.id || ''}`,
        });
      },
      // 楼栋选择
      addBuildingClick() {
        let setDeptConfg = uni.getStorageSync("setDeptConfg") || {};
        uni.navigateTo({
          url: `../searchMuti/searchMuti?type=setBuilding&configName=${this.configName}&id=${this.id}&changedept=${this.changedept}&quickCombinationId=${setDeptConfg.id || ''}`,
        });
      },
      // 删除科室
      deleteDept(id) {
        this.delId = id;
        this.models = {
          disjunctor: true,
          title: "提示",
          content: "您确定要删除该科室吗？",
          icon: "warn",
          operate: {
            ok: "确定",
            cancel: "取消",
          },
        };
      },
      // 确认负责科室
      determine() {
        if (this.zxzData.length === 0) {
          this.models = {
            disjunctor: true,
            title: "提示",
            content: "请选择至少一个科室",
            icon: "warn",
            operate: {
              know: "返回",
            },
          };
        } else {
          let setDeptConfg = uni.getStorageSync("setDeptConfg");
          let ids1 = this.zxzData.map((item) => item.id);
          let ids2 = this.zxzData.map((item) => {
            return {
              id: item.id,
            };
          });
          let postData1 = {
            quickId: this.id,
            deptIds: ids1.join(),
            userId: this.userId,
            changeDept: 1,
            classId: setDeptConfg.classesId,
          };
          if (setDeptConfg.workSchemeType == 2 && setDeptConfg.ruleType == 4 && this.changedept == 1) {
            //自选排班，科室绑定分组
            console.log(setDeptConfg)
            postData1.groupIds = setDeptConfg.selectGroupId.toString();
            postData1.isManager = true;
          }
          let postData2 = {
            workConfigHistory: {
              user: {
                id: this.userId,
              },
              workAllocationQuick: {
                id: this.id,
              },
              deptList: ids2,
              hosId: uni.getStorageSync("userData").user.currentHospital.id,
              classId: setDeptConfg.classesId,
            },
          };
          uni.showLoading({
            title: "加载中",
            mask: true,
          });
          if (this.changedept == 1) {
            //单纯切换科室！！！！
            //切换负责科室，不重新上班
            post("/auth/customOnline", postData1).then((res) => {
              if (res.status == 200) {
                post("/configuration/addData/workConfigHistory", postData2).then(
                  (result) => {
                    uni.hideLoading();
                    if (result.status == 200) {
                      uni.redirectTo({
                        url: "../receiptpage/receiptpage",
                      });
                    } else {
                      uni.showToast({
                        icon: "none",
                        title: result.msg || "接口获取数据失败！",
                      });
                    }
                  }
                );
              } else if (res.status == 504) {
                uni.hideLoading();
                let dept = res.department.dept;
                let user = res.user.name;
                let phone = res.user.phone;
                this.models = {
                  disjunctor: true,
                  title: "提示",
                  content: `您选择的科室已经被使用，请换个科室，或者联系：科室[${dept}]人员[${user}]号码[${phone}] 切换科室解决`,
                  icon: "warn",
                  phone,
                  operate: {
                    know: "返回",
                  },
                };
              } else if (res.status == 505) {
                uni.hideLoading();
                this.models1 = {
                  disjunctor: true,
                  title: "提示",
                  content: res.msg, //baba
                  icon: "warn",
                  operate: {
                    know: "返回",
                  },
                };
              }
            });
          } else {
            let postData = {
              type: "on",
              customWorking: "on",
              quickId: this.id,
              deptIds: ids1.join(),
              userId: this.userId,
              classId: setDeptConfg.classesId,
            };
            if (setDeptConfg.workSchemeType == 2 && setDeptConfg.ruleType == 4) {
              //自选排班，科室绑定分组
              console.log(setDeptConfg)
              postData.groupIds = setDeptConfg.selectGroupId.toString();
              postData.isManager = true;
            }
            post("/auth/onOrOffLine", postData).then((res) => {
              if (res.status == 200) {
                post("/configuration/addData/workConfigHistory", postData2).then(
                  (result) => {
                    uni.hideLoading();
                    if (result.status == 200) {
                      let obj = uni.getStorageSync("userData");
                      obj.user.online = true;
                      uni.setStorageSync("userData", obj);
                      uni.redirectTo({
                        url: "../receiptpage/receiptpage",
                      });
                    } else {
                      uni.showToast({
                        icon: "none",
                        title: result.msg || "接口获取数据失败！",
                      });
                    }
                  }
                );
              } else if (res.status == 504) {
                uni.hideLoading();
                let dept = res.department.dept;
                let user = res.user.name;
                let phone = res.user.phone;
                this.models = {
                  disjunctor: true,
                  title: "提示",
                  content: `您选择的科室已经被使用，请换个科室，或者联系：科室[${dept}]人员[${user}]号码[${phone}] 切换科室解决`,
                  icon: "warn",
                  phone,
                  operate: {
                    know: "返回",
                  },
                };
              }
            });
          }
        }
      },
      // 获取科室列表
      getDeptList() {
        uni.showLoading({
          title: "加载中",
          mask: true,
        });
        let setDeptConfg = uni.getStorageSync("setDeptConfg");
        let postData = {};
        if (setDeptConfg.workSchemeType == 2 && setDeptConfg.ruleType == 4 && this.changedept != 1) {
          //自选排班，科室绑定分组,切换科室的时候
          console.log(setDeptConfg)
          postData.quickId = this.id;
          postData.groupId = setDeptConfg.selectGroupId.toString();
        }
        if (setDeptConfg.workSchemeType == 2 && setDeptConfg.ruleType == 3 && this.changedept != 1) {
          //自选排班，科室绑定人员,切换科室的时候
          postData.quickId = this.id;
        }
        post("/auth/getUserWorkDept", postData).then((res) => {
          uni.hideLoading();
          if (res.status == 200) {
            this.zxzData = res.data;
            if (res.settings) {
              if (res.settings.groupManager && this.changedept == 1) {
                setDeptConfg.selectGroupId = res.settings.groupManager.id;
                uni.setStorageSync('setDeptConfg', setDeptConfg);
              }
            }
            uni.setStorageSync("setDepts", this.zxzData); //存科室列表
          }
        });
      },
    },
    onLoad(options) {
      console.log(options);
      this.configName = options.configName;
      this.id = options.id;
      this.changedept = options.changedept;
      if (uni.getStorageSync("setDepts")) {
        this.zxzData = uni.getStorageSync("setDepts");
      } else {
        this.getDeptList();
      }
    },
  };
</script>
<style lang="less">
  .setDept {
    width: 100%;
    height: 100%;
    position: relative;

    .page_tab {
      width: 100%;
      height: 96rpx;
      display: flex;
      position: fixed;
      left: 0;
      top: 0;
      z-index: 999;

      .page_tab_bar {
        flex: 1;
        font-size: 36rpx;
        background: #fff;
        display: flex;
        justify-content: center;
        align-items: center;
        position: relative;

        &:after {
          content: "";
          position: absolute;
          left: 0;
          bottom: 0;
          height: 2rpx;
          width: 100%;
          background-color: transparent;
        }
				
				.dept_title{
					font-size: 30rpx;
					width: 220rpx;
					overflow: hidden;
					white-space: nowrap; 
					text-overflow: ellipsis;
				}
				
        .tab_dept {
          position: relative;
          .addDept {
            white-space: nowrap;
            margin: 0;
            position: absolute;
            right: 0;
            top: 50%;
            transform: translate(105%, -50%);
            padding: 0 0.5em;
            line-height: 2;
          }
          .addBuilding{
            white-space: nowrap;
            margin: 0;
            position: absolute;
            top: 50%;
            transform: translate(105%, -50%);
            padding: 0 0.5em;
            line-height: 2;
          }
        }

        &.active {
          color: #49b856;

          &:after {
            background-color: #49b856;
          }
        }
      }
    }

    .zwsj {
      position: absolute;
      left: 50%;
      top: 180rpx;
      transform: translateX(-50%);

      .zwsj-img {
        width: 560rpx;
      }

      .zwsj-txt {
        font-size: 36rpx;
        font-weight: 700;
        margin-top: 20rpx;
        text-align: center;
      }
    }

    .page_items {
      height: calc(100vh - 184rpx);
      padding-top: 96rpx;
      overflow: auto;

      .page_items_scroll {
        height: 100%;

        .keyword-entry-tap {
          background-color: #eee;
        }

        .keyword-entry {
          width: 94%;
          height: 80upx;
          padding: 0 3%;
          font-size: 30upx;
          color: #333;
          display: flex;
          justify-content: space-between;
          align-items: center;
          border-bottom: solid 1upx #e7e7e7;

          .keyword-text,
          .keyword-img {
            height: 80upx;
            display: flex;
            align-items: center;
          }

          .keyword-text {
            width: 90%;
          }

          .keyword-img {
            width: 20%;
            justify-content: center;
            color: #49b856;
          }
        }
      }
    }

    // 底部
    .toolbar {
      position: fixed;
      left: 0;
      right: 0;
      bottom: 0;
      z-index: 9999;
      height: 88rpx;
      display: flex;
      justify-content: center;
      align-items: center;
      box-sizing: border-box;
      border-radius: 4rpx;
      box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.24);
      background-color: #e5e9ed;

      .toolbar-sao {
        font-size: 36rpx;
        color: #49b856;
      }
    }
  }
</style>
