<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 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>
    <van-overlay :show="show1" @click="show1 = false">
      <div class="toparrow">
        <img src="../../assets/images/toparrow.png" alt="">
      </div>
      <div class="tipsBox1">
        <p>请点击右上角“+”图标</p>
        <p style="color:#1989fa">你还未创建自己的医院/组织/团队</p>
        <p style="color:#1989fa">请先创建</p>
      </div>
    </van-overlay>
    <div class="background1"></div>
    <van-notice-bar color="#1989fa" background="#ecf9ff" left-icon="volume-o" :scrollable="false">
      <van-swipe
        vertical
        class="notice-swipe"
        :autoplay="3000"
        :show-indicators="false"
      >
        <van-swipe-item>{{ text }}<span @click="LoginOut">【切换账号】</span></van-swipe-item>
        <van-swipe-item>{{ text2 }} <a :href="'tel:' + phone">【电话联系】</a></van-swipe-item>
      </van-swipe>
    </van-notice-bar>
    <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>
      <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>
        <!-- <van-button type="success" @click="show">成功按钮</van-button> -->

        <!-- 新添加 -->
        <div class="applicationOne" @click="huanzhe">
          <img src="../../assets/images/S2.png" alt="" />患者排班
        </div>
      </div>
    </div>
    <!-- <van-loading type="spinner" color="#1989fa" v-show="loading"/> -->
    <!-- <van-loading type="spinner" color="#1989fa" /> -->
  </div>
</template>
<script>
// import "../libs/rem.js";
import {Toast,Loading} from 'vant'
// import {}
// import { Loading } from 'vant'
import {ChangeOrg, GetHomeData} from '@/api/home'
import {run} from '@/api/temp'

import '../../libs/rem.js'
import '../../styles/newStyle.scss'
import {setRem} from '@/libs/functionRem'
import {
  LoginOut
} from '@/api/patient/patient'
export default {
  data () {
    return {
      loading:false,
      show: false,
      show1: false,
      value1: 0,
      isCreateOrg: false,
      org_arr: [],
      banners: [],
      apps: [],
      isSubSuperAdmin: false,
      option1: [
        {text: '新款商品', value: 0},
        {text: '全部商品全部商品全部商品全部商品全部商品全部商品', value: 1},
        {text: '活动商品', value: 2}
      ],
      isSubSuperAdmin: '',
      center: 1,
      text: '',
      text2: '',
      phone: ''
    }
  },
  methods: {

    LoginOut () {
      LoginOut(this.adminid).then(response => {
        if (response.data.state === 1) {
          var msg = response.data.data.msg
          // console.log("msg是", msg);
          this.$store.dispatch('SetScheduals', {})
          this.$store.dispatch('SetWaitScheduals', {})
          this.isRouterAlive = false
          this.$nextTick(function () {
            this.isRouterAlive = true
          })
          localStorage.removeItem('searchWord')
          this.$router.push('/')
        }
      })
    },
    toStaffManage () {
      if (this.isCreateOrg) {
        this.$router.push({path: '/manageconsole/staff'})
      } else {
        // this.$toast({
        //   message: "你尚未创建机构，请先创建机构"
        // });
        this.show1 = true
      }
    },
    toRoleManage () {
      if (this.isCreateOrg) {
        this.$router.push({path: '/manageconsole/role'})
      } else {
        // this.$toast({
        //   message: "你尚未创建机构，请先创建机构"
        // });
        this.show1 = true
      }
    },
    toConfigCenter () {
      if (this.isCreateOrg) {
        this.$router.push('/configurecenter')
      } else {
        // this.$toast({
        //   message: "你尚未创建机构，请先创建机构"
        // });
        this.show1 = true
      }
    },
    toOrgSetting () {
      if (this.isCreateOrg) {
        this.$router.push('/editorg?id=' + this.value1)
      } else {
        // this.$toast({
        //   message: "你尚未创建机构，请先创建机构"
        // });
        this.show1 = true
      }
    },
    changeOrg (value) {
      Toast.loading({forbidClick: true, duration: 0})
      let params = {
        org_id: value
      }
      ChangeOrg(params).then(response => {
        if (response.data.state === 1) {
          // 清除之前的数据
          this.$store.dispatch('SetScheduals', {})
          this.$store.dispatch('SetWaitScheduals', {})

          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 dict_config_list = response.data.data.dict_config_list
          var token = response.data.data.token
          localStorage.setItem('token', token)

          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.$store.dispatch('SetDictConfigList', dict_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("你尚未创建机构，请先创建机构");
        this.show1 = true
        return
      }
      if (app.open_status == 0) {
        Toast('近期发布')
        return
      }
      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:
          this.$router.push({path: '/patientSchedule'})
          break
        case 10:
          break
        case 11:
          this.$router.push({path: '/dqc/index'})
          break
        case 12:
          break
        case 13:
          break

        case 14:
          break
        case 16:
          this.$router.push({path: '/busIndex'})
          break
        case 18:
          this.$router.push({path: '/staffScheduleIndex'})
          break
        case 19:
          this.$router.push({path: '/infor/index'})
          break
      }

    },
    toCreateOrg () {
      this.show = false
      this.show1 = false
      this.$router.push({path: '/perfectOrg'})
    },
    toPatients () {
      this.$router.push({path: '/patients'})
    },
    toSearch () {
      if (!this.isCreateOrg) {
        this.show1 = true
        // 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
          this.username = response.data.data.username
          this.text = '欢迎使用酷医云，' + response.data.data.username.user_name
          this.text2 = '您的专属客户经理：' + response.data.data.information.name
          this.phone = response.data.data.information.phone
          if (this.isCreateOrg) {
            this.orgs = response.data.data.orgs
            this.apps = response.data.data.apps

            this.apps = this.apps.filter(o => o.name != '微官网')
            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 {
            this.show = true
            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'})
      localStorage.removeItem('center')
    },
    // 新添加
    huanzhe(){
      this.$router.push({path: '/new_Patientscheduling'})
    }
  },
  created () {
    this.GetHomeData()
    setRem()
    localStorage.setItem('center', this.center)
  }
}
</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;
  }
  ::v-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;
    padding: 1.25rem 1.125rem 0;
  }
  .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;
    width: 25%;

  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;
  }

  }
  .tipsBox1 {
    width: 18.75rem;
    height: 5.625rem;
    margin: 0 auto;
    background: #fff;
    border-radius: 0.25rem;
    text-align: center;
    padding-top: 0.65rem;
  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;
  }
  .notice-swipe {
    height: 40px;
    line-height: 40px;
  }
  }
</style>
