<template>
  <div class="page_homeIndex">
    <van-sticky>
      <div class="homeTitle">
        <div
          class="homeTitleLeft"
          style="visibility: hidden"
          v-if="isCreateOrg == false"
        >
          <van-dropdown-menu>
            <van-dropdown-item
              @change="changeOrg"
              v-model="value1"
              :options="org_arr"
            />
          </van-dropdown-menu>
        </div>
        <div class="homeTitleLeft" v-if="isCreateOrg == true">
          <van-dropdown-menu>
            <van-dropdown-item
              @change="changeOrg"
              v-model="value1"
              :options="org_arr"
            />
          </van-dropdown-menu>
        </div>
        <div class="homeTitleRight">
          <i class="iconfont icon-sousuo search1" @click="toSearch"></i>
          <!--<van-icon class="iconOne" name="comment-o"/>-->
          <van-icon name="plus" class="iconOne" @click="toCreateOrg" />
        </div>
      </div>
    </van-sticky>
    <div class="banner">
      <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
        <van-swipe-item
          v-for="(banner, index) in this.banners"
          @click="jump(banner.url)"
          :key="index"
        >
          <img :src="banner.thumb" alt />
        </van-swipe-item>
      </van-swipe>
    </div>
    <van-overlay :show="show" @click="show = false" >
      <div class="toparrow">
        <img src="../../assets/images/toparrow.png" alt="">
      </div>
      <div class="tipsBox">
        <p>请点击右上角“+”图标</p>
        <p style="color:#1989fa">创建自己的医院/组织/团队</p>
      </div>
    </van-overlay>
    <!--<div class="noticeBox">-->
    <!--<i class="iconfont icon-public-notice notice"></i>-->
    <!--<div class="noticeNews">-->
    <!--<div>-->
    <!--今日上机：-->
    <!--<span>6&ensp;人</span>-->
    <!--</div>-->
    <!--<div>-->
    <!--今日透析：-->
    <!--<span>6&ensp;人</span>-->
    <!--</div>-->
    <!--<div>-->
    <!--今日生日：-->
    <!--<span>6&ensp;人</span>-->
    <!--</div>-->
    <!--</div>-->
    <!--</div>-->
    <div class="background1"></div>

    <div
      class="management"
      v-if="
        (this.$store.getters.user.admin.id ==
          this.$store.getters.user.org.creator &&
          this.isCreateOrg) ||
          this.isSubSuperAdmin
      "
    >
      <div class="managementTitle">
        <div class="managementName">
          管理控制台
          <span>（仅管理员可见）</span>
        </div>
        <div class="setting" @click="toConsole()">
          <van-icon name="setting-o" />
          管理
        </div>
      </div>
      <div class="managementBox">
        <div class="managementOne" @click="toStaffManage()">
          <img src="../../assets/images/M1.png" alt />员工管理
        </div>
        <div class="managementOne" @click="toRoleManage()">
          <img src="../../assets/images/M2.png" alt />权限管理
        </div>
        <div class="managementOne" @click="toConfigCenter()">
          <img src="../../assets/images/M3.png" alt />配置中心
        </div>
        <div class="managementOne" @click="toOrgSetting()">
          <img src="../../assets/images/M4.png" alt />机构设置
        </div>
      </div>
    </div>

    <div class="management" v-if="!this.isCreateOrg">
      <div class="managementTitle">
        <div class="managementName">
          管理控制台<span>（仅管理员可见）</span>
        </div>
        <div class="setting"><van-icon name="setting-o" />管理</div>
      </div>
      <div class="managementBox">
        <div class="managementOne" @click="toStaffManage()">
          <img src="../../assets/images/M1.png" alt="" />员工管理
        </div>
        <div class="managementOne" @click="toRoleManage()">
          <img src="../../assets/images/M2.png" alt="" />权限管理
        </div>
        <div class="managementOne" @click="toConfigCenter()">
          <img src="../../assets/images/M3.png" alt="" />配置中心
        </div>
        <div class="managementOne" @click="toOrgSetting()">
          <img src="../../assets/images/M4.png" alt="" />机构设置
        </div>
      </div>
    </div>

    <div class="application">
      <div class="applicationTitle">
        <div class="applicationName">应用工作台</div>
        <!--<div class="applicationService">+ 更多应用</div>-->
      </div>
      <div class="applicationBox">
        <div
          v-for="(item, index) in this.apps"
          :key="index"
          :class="
            item.open_status == 1 ? 'applicationOne' : 'applicationOne opacity'
          "
          @click="jumpApp(item)"
        >
          <img :src="item.url" alt />{{ item.name }}
        </div>
      </div>
    </div>
  </div>
</template>
<script>
// import "../libs/rem.js";
import { Toast } from "vant";

import { GetHomeData, ChangeOrg } from "@/api/home";
import "../../libs/rem.js";
import "../../styles/newStyle.scss";
import { setRem } from "@/libs/functionRem";

export default {
  data() {
    return {
      show: false,
      value1: 0,
      isCreateOrg: false,
      org_arr: [],
      banners: [],
      apps: [],
      isSubSuperAdmin: false,
      option1: [
        { text: "新款商品", value: 0 },
        { text: "全部商品全部商品全部商品全部商品全部商品全部商品", value: 1 },
        { text: "活动商品", value: 2 }
      ],
      isSubSuperAdmin: ""
    };
  },
  methods: {
    toStaffManage() {
      if (this.isCreateOrg) {
        this.$router.push({ path: "/manageconsole/staff" });
      } else {
        this.$toast({
          message: "你尚未创建机构，请先创建机构"
        });
      }
    },
    toRoleManage() {
      if (this.isCreateOrg) {
        this.$router.push({ path: "/manageconsole/role" });
      } else {
        this.$toast({
          message: "你尚未创建机构，请先创建机构"
        });
      }
    },
    toConfigCenter() {
      if (this.isCreateOrg) {
        this.$router.push("/configurecenter");
      } else {
        this.$toast({
          message: "你尚未创建机构，请先创建机构"
        });
      }
    },
    toOrgSetting() {
      if (this.isCreateOrg) {
        this.$router.push("/editorg?id=" + this.value1);
      } else {
        this.$toast({
          message: "你尚未创建机构，请先创建机构"
        });
      }
    },
    changeOrg(value) {
      Toast.loading({ forbidClick: true, duration: 0 });
      let params = {
        org_id: value
      };
      ChangeOrg(params).then(response => {
        if (response.data.state === 1) {
          // 清除之前的数据

          var user = response.data.data.user;
          var admin = response.data.data.admin;
          var org = response.data.data.org;
          var subscibe = response.data.data.subscibe;
          var config_list = response.data.data.config_list;
          var template_info = response.data.data.template_info;
          var filed_list = response.data.data.filed_list;
          this.$store.dispatch("InitUserInfo", {
            admin: admin,
            user: user,
            org: org,
            subscibe: subscibe,
            template_info: template_info,
            filed_list: filed_list
          });
          this.$store.dispatch("SetConfigList", config_list);
          this.GetHomeData();
          Toast.success("切换机构成功");
          // this.$toast({
          //   message: "切换机构成功"
          // });
        } else {
          Toast(response.data.msg);
        }
      });
    },
    jump(url) {
      if (url.length != 0) {
        window.location.href = url;
      }
    },
    jumpApp(app) {
      if (!this.isCreateOrg) {
        Toast("你尚未创建机构，请先创建机构");
        return;
      }
      if (app.open_status == 0) {
        Toast("近期发布");

        return;
      }
      console.log(app.name);
      switch (app.app_type) {
        case 6:
          this.toPatients();
          break;
        case 7:
          this.$router.push({ path: "/main" });
          break;
        case 8:
          this.$router.push({ path: "/shop" });

          break;
        case 9:
          break;
        case 10:
          break;
        case 11:
          break;
        case 12:
          break;
        case 13:
          break;
        case 14:
          break;
      }
    },
    toCreateOrg() {
      this.show = false
      this.$router.push({ path: "/perfectOrg" });
    },
    toPatients() {
      this.$router.push({ path: "/patients" });
    },
    toSearch() {
      if (!this.isCreateOrg) {
        Toast("你尚未创建机构，请先创建机构");
        return;
      }
      this.$router.push({ path: "/search" });
    },
    GetHomeData() {
      GetHomeData().then(response => {
        if (response.data.state === 1) {
          this.isCreateOrg = response.data.data.isCreateOrg;
          this.isSubSuperAdmin = response.data.data.isSubSuperAdmin;

          if (this.isCreateOrg) {
            this.orgs = response.data.data.orgs;
            this.apps = response.data.data.apps;
            this.banners = response.data.data.banners;
            this.org_arr = [];
            for (let i = 0; i < this.orgs.length; i++) {
              let obj = {
                text: this.orgs[i].org_name,
                value: this.orgs[i].id
              };
              this.org_arr.push(obj);
            }
            this.value1 = this.$store.getters.user.org.id;
            this.show = false;
          } else {
            console.log(1)
            this.show = true;
            console.log(2)
            this.apps = response.data.data.apps;
            this.banners = response.data.data.banners;

          }
        } else {
          this.$toast({
            message: response.data.msg
          });
        }
      });
    },
    toConsole() {
      this.$router.push({ path: "/manageconsole" });
    }
  },
  created() {
    this.GetHomeData();
    setRem();
  }
};
</script>
<style lang="scss" scoped>
.page_homeIndex {
  .homeTitle {
    height: 3.125rem;
    padding: 0 1.125rem;
    display: flex;
    justify-content: space-between;
    background: #fff;
  }
  .homeTitleLeft {
    width: 60%;
  }
  .homeTitleRight {
    width: 18%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: #232323;
  }
  /deep/ .van-dropdown-menu__item {
    justify-content: left;
  }
  .van-hairline--top-bottom {
    position: static;
  }
  .iconOne {
    font-size: 1.5rem;
  }
  .iconfont {
    clear: both;
  }

  .search1 {
    font-size: 1.5rem;
    color: #232323;
  }
  .banner {
    padding: 1px 1.125rem 0;
    img {
      height: 6.75rem;
      width: 100%;
    }
  }
  .noticeBox {
    padding: 0 1.125rem;
    height: 2.5rem;
    display: flex;
    align-items: center;
    color: #cccccc;
    .notice {
      font-size: 1.25rem;
      margin-right: 0.375rem;
    }
    .noticeNews {
      width: 90%;
      display: flex;
      justify-content: space-between;
      color: #666666;
      font-size: 0.8125rem;
    }
  }
  .background1 {
    background: #f6f6f6;
    width: 100%;
    height: 0.8125rem;
  }
  .management {
    padding: 1.25rem 1.125rem 0;
  }
  .managementTitle {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .managementName {
    font-weight: bold;
    font-size: 1rem;
    color: #000;
    span {
      font-size: 0.8125rem;
      color: #989898;
      font-weight: 400;
    }
  }
  .setting {
    display: flex;
    align-items: center;
    color: #989898;
    font-size: 0.8125rem;
    .van-icon-setting-o {
      margin-right: 0.25rem;
    }
  }
  .managementBox {
    display: flex;
    justify-content: space-between;
    .managementOne {
      display: flex;
      align-items: center;
      flex-direction: column;
      font-size: 0.8125rem;
      color: #666666;
      img {
        width: 1.375rem;
        height: 1.375rem;
        margin: 1rem 0 0.2rem;
      }
    }
  }
  .application {
    .applicationTitle {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 1.25rem 1.125rem 0.625rem;
    }
    .applicationName {
      font-weight: bold;
      font-size: 1rem;
      color: #000;
    }
    .applicationService {
      font-size: 0.8125rem;
      color: #666666;
    }
  }
  .applicationBox {
    display: flex;
    flex-wrap: wrap;
    .applicationOne {
      width: 25%;
      display: flex;
      align-items: center;
      flex-direction: column;
      padding: 0.625rem 0;
      font-size: 0.8125rem;
      color: #666666;
      img {
        width: 3.25rem;
        height: 3.25rem;
        margin-bottom: 0.375rem;
      }
    }
  }
  .van-popup {
    border-radius: 0 !important;
    top: 0 !important;
    transform: translate3d(0, 0, 0) !important;
  }
  .opacity {
    opacity: 0.5;
    filter: alpha(opacity=40);
  }
  .van-overlay {
    top: 3.125rem;
    z-index: 9;
  }
  .toparrow {
    text-align: right;
    margin-right: 1.75rem;
    img {
      width: 1.875rem;
    }
  }
  .tipsBox {
    width: 18.75rem;
    height: 5.625rem;
    margin: 0 auto;
    background: #fff;
    border-radius: 0.25rem;
    text-align: center;
    padding-top: 1.25rem;
    p {
      font-size: 1rem;
      line-height: 1.5rem;
    }
  }
}
</style>
<style lang="scss">
.page_homeIndex {
  .van-dropdown-menu__title {
    padding-left: 0;
    color: #000;
    font-size: 1rem;
    font-weight: bold;
  }
}
</style>
