<template>
  <div class="page_qualityControlManagement">
    <van-sticky>
      <div class="qualityControlManagementTitle">
        <i class="iconfont icon-zuojiantou jiantou" @click="$router.go(-1)"></i>
        <span class="titleName">透析记录</span>
      </div>
    </van-sticky>
    <div class="allTitle">
      <van-cell center 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 :title="item.name" v-for="(item, index) in allPurview" :key="index">
        <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 {GetAllFunctionPurview,GetFunctionRoleInfo,EditFuncPurview} 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.pid)

    }, methods: {
      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()

            }
          }

          let type = ""
          if (val){
            type = 1 //打开
          }else{
            type = 2 //关闭
          }
          let params = {
            id : this.$route.query.id,
            purview_id:purview_ids,
            type : type,
            is_all : 1,
            pid: this.pid,
          }
          EditFuncPurview(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

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

            }
          }

          let type = ""
          if (val){
            type = 1 //打开
          }else{
            type = 2 //关闭
          }
          let params = {
            id : this.$route.query.id,
            purview_id:purview_ids,
            type : type,
            is_all : 1,
            pid: this.pid,
          }
          EditFuncPurview(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 (tempPurview.length == this.allPurview.length){
          this.checked = true
        }else{
          this.checked = false
        }


        if(!val.isCheck){ //打开
          let purview_ids = ""
          let type = ""
          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,
            pid : this.pid
          }
          EditFuncPurview(params).then(response => {
            if (response.data.state === 1) {
              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 = ""
          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,
            pid : this.pid

          }
          EditFuncPurview(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
              }else{
                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
              });
            }
          });


        }

      },

      GetFunctionRoleInfo(role_id) {
        let params = {
          id: role_id,
        }
        GetFunctionRoleInfo(params).then(response => {
          if (response.data.state === 1) {
            this.rolePurview = response.data.data.role_purview

            this.ids = this.rolePurview.purview_ids.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(pid) {
        GetAllFunctionPurview(pid).then(response => {
          if (response.data.state === 1) {
            for (let i = 0; i < response.data.data.funtion_purview.length; i++) {
              response.data.data.funtion_purview[i]['isCheck'] = false
              this.allPurview.push(response.data.data.funtion_purview[i])
            }
            this.GetFunctionRoleInfo(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>
