<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="全部权限">
        <van-switch v-model="checked" slot="right-icon" size="24" @change="allChange"/>
      </van-cell>
    </div>
    <div class="allTitle1">
      <van-cell center title="模块权限配置"></van-cell>
    </div>

    <div class="allBox">
      <van-cell
        is-link
        center
        :title="item.name"
        v-for="(item, index) in allPurview"
        :key="index"
        v-if="!item.no_sub&&item.pid == 0"
        @click="toSubPurview(item)"
      >
      </van-cell>

      <van-cell
        center
        :title="item.name"
        v-for="(item, index) in allPurview"
        :key="index"
        v-if="item.no_sub"
      >
        <van-switch v-model="item.isCheck" slot="right-icon" size="24"/>
      </van-cell>
    </div>
  </div>
</template>

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

  export default {
    data() {
      return {
        title: "",
        desc: "",
        name: "",
        checked: false,
        allPurview: [],
        ids: [],
        pid: 0,
        module: 0,
        subPurview: [],
      };
    },
    created() {
      this.title = this.$route.query.title;
      this.pid = this.$route.query.pid;
      this.module = this.$route.query.module;
      setRem();
      this.GetAllPurview();

      // this.GetRoleInfo(this.$route.query.id);
    },
    methods: {
      allChange(val) {
        if (val) {
          let purview_ids = ""

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

            }
          }

          const obj = {}
          // 去重复
          var purview_arr = purview_ids.split(",").reduce((cur, next) => {
            obj[next] ? '' : obj[next] = true && cur.push(next)
            return cur
          }, [])

          purview_ids = purview_arr.join(",")


          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.subPurview.length; i++) {
            if (purview_ids.length > 0) {
              purview_ids = purview_ids + "," + this.subPurview[i].id.toString() + "," + this.subPurview[i].pid.toString()
            } else {
              purview_ids = this.subPurview[i].id.toString() + "," + this.subPurview[i].pid.toString()

            }
          }

          const obj = {}
          // 去重复
          var purview_arr = purview_ids.split(",").reduce((cur, next) => {
            obj[next] ? '' : obj[next] = true && cur.push(next)
            return cur
          }, [])

          purview_ids = purview_arr.join(",")

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


        }
      },

      toSubPurview(purview) {
        this.$router.push({
          path:
            "/role/subpurview?id=" +
            this.$route.query.id +
            "&pid=" +
            purview.id +
            "&module=" +
            purview.module +
            "&title=" +
            purview.name
        });
      },
      del() {
        let params = {
          role_id: this.$route.query.id,
          enable: false
        };
        DeleteRoleInfo(params).then(response => {
          if (response.data.state === 1) {
            this.$toast({
              message: "删除成功"
            });
            this.$router.go(-1);
          } else {
            this.$toast({
              message: response.data.msg
            });
          }
        });
      },
      editRole() {
        let params = {
          id: this.$route.query.id,
          desc: this.desc
        };
        EditRoleInfo(params).then(response => {
          if (response.data.state === 1) {
            this.$toast({
              message: "修改成功"
            });
          } else {
            this.$toast({
              message: response.data.msg
            });
          }
        });
      },
      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.name = response.data.data.role.name;
            this.desc = response.data.data.role.intro;
            this.ids = this.rolePurview.PurviewIds.split(",");

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

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


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

      GetAllPurview() {
        this.pid = 0;
        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;
              for (let b = 0; b < purview.length; b++) {
                this.subPurview.push(purview[b])
              }
              if (purview.length > 1) {
                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)
            }
          } 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>
