<template>
  <div class="page_qualityControlManagement">
    <van-sticky>
      <div class="qualityControlManagementTitle">
        <i class="iconfont icon-zuojiantou jiantou" @click="$router.go(-1)"></i>
        <span class="titleName">{{title}}</span>
      </div>
    </van-sticky>
    <div class="allTitle">
      <van-cell center title="全部权限" v-if="title != '透析管理'">
        <van-switch v-model="checked" @change="allChange" slot="right-icon" size="24" />
      </van-cell>
    </div>
    <div class="allTitle1">
      <van-cell center title="功能权限配置"></van-cell>
    </div>
    <div class="allBox">
      <van-cell center is-link :title="item.name" v-for="(item, index) in allPurview" :key="index" v-if="item.name == '透析记录' || item.name == '门诊挂号' ||  item.name=='医生工作台'" @click="toFunctonPurview(item)">
      </van-cell>
      <van-cell center :title="item.name" v-for="(item, index) in allPurview" :key="index" v-if="item.name != '透析记录' && item.name != '门诊挂号' &&  item.name !='医生工作台'">
        <van-switch v-model="item.isCheck" slot="right-icon" size="24"  @change="changeStatus(item)" />
      </van-cell>
    </div>
  </div>
</template>

<script>
  import {setRem} from "@/libs/functionRem";
  import {GetAllPurview,GetRoleInfo,EditRoleInfo,DeleteRoleInfo,EditPurview,GetFunctionRoleInfo} from "@/api/admin_user";

  export default {
    data() {
      return {
        title:"",
        desc: "",
        name: "",
        checked: false,
        allPurview: [],
        ids:[],
        pid:0,
        module:0,

      };
    }, created() {
      setRem()
      this.title = this.$route.query.title
      this.pid = this.$route.query.pid
      this.module = this.$route.query.module
      this.GetAllPurview()
      // this.GetRoleInfo(this.$route.query.id)



    }, methods: {
      isShow(){



      },
      allChange(val){
        if (val){
          let purview_ids = ""

          for (let i = 0; i < this.allPurview.length; i++){
              this.allPurview[i].isCheck = true
              if (purview_ids.length> 0) {
                purview_ids = purview_ids +","+ this.allPurview[i].id.toString()
              }else{
                purview_ids = this.allPurview[i].id.toString()

              }
          }
          purview_ids = purview_ids +","+ this.$route.query.pid


          let type = ""
          if (val){
            type = 1 //打开
          }else{
            type = 2 //关闭
          }
          let params = {
            id : this.$route.query.id,
            purview_id:purview_ids,
            type : type,
          }
          EditPurview(params).then(response => {
            if (response.data.state === 1) {
              this.checked = true

            } else {
              if(type == 1){
                this.checked = false
              }else{
                this.checked = true
              }
              this.$toast({
                message: response.data.msg
              });
            }
          });




        }else {
          let purview_ids = ""

          for (let i = 0; i < this.allPurview.length; i++) {
            this.allPurview[i].isCheck = false
            // purview_ids = purview_ids + this.allPurview[i].id.toString()

            if (purview_ids.length> 0) {
              purview_ids = purview_ids +","+ this.allPurview[i].id.toString()
            }else{
              purview_ids = this.allPurview[i].id.toString()

            }
          }
          purview_ids = purview_ids +","+ this.$route.query.pid

          let type = ""
          if (val){
            type = 1 //打开
          }else{
            type = 2 //关闭
          }
          let params = {
            id : this.$route.query.id,
            purview_id:purview_ids,
            type : type,
          }
          EditPurview(params).then(response => {
            if (response.data.state === 1) {
              this.checked = false

            } else {
              if(type == 1){
                val.isCheck = false
              }else{
                val.isCheck = true
              }
              this.$toast({
                message: response.data.msg
              });
            }
          });



        }
      },
      changeStatus(val){
        let tempPurview = []
        for (let i = 0; i < this.allPurview.length; i++){
          if(this.allPurview[i].isCheck){
            tempPurview.push(this.allPurview[i])
          }
        }

        if(!val.isCheck){ //打开
          let purview_ids = ""
          let type = ""

          if(tempPurview.length <= 0){ //之前没分配过权限,需要将父节点的id上传
            purview_ids = val.pid + ","+val.id
          }else{ //分配过权限，说明父节点id已经上传过，则不需要上传
            purview_ids = val.id
          }

          if (val.isCheck){
            type = 1 //打开
          }else{
            type = 2 //关闭
          }
          let params = {
            id : this.$route.query.id,
            purview_id:purview_ids,
            type : type,
          }


          EditPurview(params).then(response => {
            if (response.data.state === 1) {
              this.checked = false
              if(type == 1){
                val.isCheck = true
              }else{
                val.isCheck = false
              }
            } else {
              if(type == 1){
                val.isCheck = false
              }else{
                val.isCheck = true
              }
              this.$toast({
                message: response.data.msg
              });
            }
          });



        }else{//关闭
          let purview_ids = ""
          let type = ""

          if(tempPurview.length == 1){ //说明只剩当前操作的,则需要将父节点也上传，保证数据不重复
            purview_ids = val.pid + ","+val.id
          }else{ //分配过权限，说明父节点id已经上传过，则不需要上传
            purview_ids = val.id
          }

          if (val.isCheck){
            type = 1 //打开
          }else{
            type = 2 //关闭
          }
          let params = {
            id : this.$route.query.id,
            purview_id:purview_ids,
            type : type,
          }

          EditPurview(params).then(response => {
            if (response.data.state === 1) {

              var b = 0
              for (let i =0; i < this.allPurview.length; i++){
                if(this.allPurview[i].isCheck){
                  b++
                }
              }
              if (b == this.allPurview.length){
                this.checked = true
              }

              if(type == 1){
                val.isCheck = true
              }else{
                val.isCheck = false
              }

            } else {
              if(type == 1){
                val.isCheck = false
              }else{
                val.isCheck = true
              }

              this.$toast({
                message: response.data.msg
              });
            }
          });


        }

      },
      toFunctonPurview(item){
        this.$router.push({path:"/role/functionpurview?id="+this.$route.query.id+"&pid="+item.id})
      }, GetRoleInfo(role_id) {
        let params = {
          id: role_id,
        }
        GetRoleInfo(params).then(response => {
          if (response.data.state === 1) {
            this.rolePurview = response.data.data.role_purview

            // this.rolePurview = response.data.data.role_purview

            this.name =  response.data.data.role.name
            this.desc = response.data.data.role.intro
            this.ids = this.rolePurview.PurviewIds.split(',')

            for(let a = 0; a < this.ids.length; a++ ){
              for (let i = 0; i < this.allPurview.length; i++){
                if (parseInt(this.ids[a]) == this.allPurview[i].id){
                  this.allPurview[i].isCheck = true
                }
              }
            }



            //设置全选按键
            var b = 0
            for (let i =0; i < this.allPurview.length; i++){
              if(this.allPurview[i].isCheck){
                b++
              }
            }

            if (b == this.allPurview.length){
              this.checked = true
            }else{

              this.checked = false

            }

          } else {
            this.$toast({
              message: response.data.msg
            });
          }
        });
      },
      GetAllPurview() {
        let params = {
          module: this.module,
          id: this.pid,
        }
        GetAllPurview(params).then(response => {
          if (response.data.state === 1) {
            for (let i = 0; i < response.data.data.purviews.length; i++) {
              var purview = response.data.data.purviews[i].sub_purview
              if (purview.length > 0) {
                response.data.data.purviews[i]['no_sub'] = false
              } else {
                response.data.data.purviews[i]['no_sub'] = true
              }
              response.data.data.purviews[i]['isCheck'] = false
              this.allPurview.push(response.data.data.purviews[i])
            }

            this.GetRoleInfo(this.$route.query.id)
            // this.GetFuncRoleInfo(this.$route.query.id)





          } else {
            this.$toast({
              message: response.data.msg
            });
          }
        });

      },
    }
  };
</script>

<style lang="scss" scoped>
  .page_qualityControlManagement {
    height: 100%;
    overflow-y: auto;
    background: #fafafa;
  .qualityControlManagementTitle {
    background: #fff;
    padding: 0 1.125rem;
    height: 3.125rem;
    display: flex;
    align-items: center;
  }
  .jiantou {
    font-size: 1.5rem;
    font-weight: 600;
    margin-right: 7rem;
  }
  .titleName {
    font-size: 1.125rem;
    font-weight: 600;
  }
  .allTitle {
    margin: 1rem 0;
  .van-cell__title {
    font-weight: bold;
    font-size: 1rem;
    color: #313234;
  }
  }
  .allTitle1 {
  .van-cell__title {
    font-weight: bold;
    font-size: 1rem;
    color: #313234;
  }
  }
  .allBox {
    margin-bottom: 1rem;
  .van-cell__title {
    font-size: 1rem;
    color: #313234;
  }
  }
  }
</style>
