<template>
  <div class="normal-panel drewer__panel">
    <div>
      <el-row :gutter="20">
        <el-col :span="12">
          <div class="mb-16">
            <el-input style="width:250px" v-model="params.content.where.condition" size="mini" @keyup.native.enter="roleMain" @clear="roleMain" placeholder="应用名称" clearable></el-input>
            <auth style="margin-left:10px" @onSuccess="roleMain" v-if="tool.checkAuth($route.name,'auth')"></auth>
          </div>
          <el-collapse>
            <el-collapse-item title="筛选" name="1">
              <el-row class="normal-margin">
                <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAllChange">全选</el-checkbox>
                <div style="margin: 15px 0;"></div>
                <el-checkbox-group v-model="checkModuleList"  @change="handleCheckedCitiesChange">
                  <el-col style="margin-bottom:5px" :span="6"  v-for="item in modulesList" :key="item.index">
                    <el-checkbox :label="item"></el-checkbox>
                  </el-col>
                </el-checkbox-group>
              </el-row>
            </el-collapse-item>
          </el-collapse>
          <tableLayout :layout="tablecols" :data="apps" :custom="false" height="500px" @rowClick="appoptionselect">
          </tableLayout>
        </el-col>
        <el-col :span="11">
          <el-row :gutter="40">
            <el-col :span="12" v-if="tool.checkAuth($route.name,'auth')">
              <p class="title">功能</p>
              <el-row>
                <el-checkbox-group v-model="checkedApps">
                  <el-col class="normal-margin" v-for="item in appoptions" :key="item.rowindex">
                    <el-checkbox :label="item.optionid">{{item.optionname}}</el-checkbox>
                  </el-col>
                </el-checkbox-group>
              </el-row>
              <div class="flex-align-center flex-between option-item">
                <el-checkbox :indeterminate="isIndeterminate2" v-model="checkAll2" @change="handleCheckAllChange2">全选</el-checkbox>
                <cancelAuth v-if="checkedApps.length > 0" :data="{roleid:data.roleid,systemappid:active_systemappid,optionid:checkedApps}" @onSuccess="(roleMain(),$emit('onSuccess'),checkedApps=[])"></cancelAuth>
              </div>
            </el-col>
            <el-col :span="12" v-else>
              <p class="title">功能</p>
              <div class="flex-align-center flex-between option-item" v-for="item in appoptions" :key="item.index">
                <p>{{item.optionname}}</p>
              </div>
            </el-col>
            <el-col :span="12">
              <p class="title">隐藏字段</p>
              <div class="flex-align-center flex-between option-item" v-for="item in hiddenfields" :key="item.index">
                <p>{{item.fieldname}}</p>
                <cancelFields :data="{roleid:roleid,systemappid:active_systemappid,hiddenfieldid:item.hiddenfieldid}" @onSuccess="(roleMain(),$emit('onSuccess'))"></cancelFields>
              </div>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import auth from './auth_list.vue'
import cancelAuth from './cancel_auth.vue'
import cancelFields from './cancel_fields.vue'
import { log } from '@antv/g2plot/lib/utils'

export default {
  props:['data'],
  components:{
    auth,
    cancelAuth,
    cancelFields
  },
  data () {
    return{
      isIndeterminate:false,
      isIndeterminate2:false,
      checkAll: true,
      checkAll2: false,
      rules:{
        rolename: [
          { required: true, message: '请输入角色名称', trigger: 'blur' },
        ],
        remarks: [
          { required: true, message: '请输入角色描述', trigger: 'blur' },
        ],
        usertype: [
          { required: true, message: '请选择角色类型', trigger: 'change' }
        ],
      },
      params: {
        "classname": "webmanage.role.role",
        "method": "query_roleMain",
        "content": {
          "roleid":"",
          "where": {
            "condition":""
          }
        }
      },
      form:{
        "roleid":0,
        "rolename":"",
        "remarks":""
      },
      roleMainInfo:{},
      options:[],
      tablecols:[],
      appoptions:[],
      hiddenfields:[],
      modulesList:[],
      checkModuleList:[],
      apps:[],
      checkedApps:[],
      roleid:0,
      active_systemappid:''
    }
  },
  methods:{
    async roleMain () {
      this.params.content.roleid = this.data.roleid
      const res = await this.$api.requested(this.params)
      this.form = {
        "roleid":res.data.roleid,
        "rolename":res.data.rolename,
        "remarks":res.data.remarks,
        "usertype":res.data.usertype
      }
      this.roleMainInfo = res.data
      console.log(this.roleMainInfo);
      
      //显示默认授权信息,一般默认信息为第一条数据
      res.data.apps[0]?this.appoptionselect(res.data.apps[0]):''
      this.filterModules()
    },
    async usertype () {
      let param = {
        "classname": "sysmanage.develop.optiontype.optiontype",
        "method": "optiontypeselect",
        "content": {
            "pageNumber": 1,
            "pageSize": 20,
            "typename": "usertype",
            "parameter": {
            }
        }
      }
      const res = await this.$api.requested(param)
      this.options = res.data
    },
    async appoptionselect (row) {
      this.checkAll2 = false
      this.checkedApps = []
      this.active_systemappid = row.systemappid
      this.appoptions = row.options
      this.hiddenfields = row.hiddenfields
    },
    onSubmit () {
      this.$refs['form'].validate((valid) => {
        if (!valid) return false
        this.$api.requested({
          "classname": "webmanage.role.role",
          "method": "insertormodify_role",
          "content": this.form
        }).then(res=>{
          this.tool.showMessage(res,()=>{
            this.$emit('onSuccess')
          })
        })
      })
    },
    //遍历权限模块用于筛选
    filterModules () {
      let arr = this.roleMainInfo.apps.map(e=>{
        return e.systemmodulename
      })
      this.modulesList = this.checkModuleList = [...new Set(arr)]
      this.filterApps()
    },
    handleCheckAllChange(val) {
      this.checkModuleList = val ? this.modulesList : [];
      this.isIndeterminate = false;
      this.filterApps()
    },
    handleCheckedCitiesChange(value) {
      let checkedCount = value.length;
      this.checkAll = checkedCount === this.modulesList.length;
      this.isIndeterminate = checkedCount > 0 && checkedCount < this.modulesList.length;
      this.filterApps()
    },
    filterApps () {
      let arr = []
      this.roleMainInfo.apps.forEach(app => {
        this.checkModuleList.filter(e=>{
          if (e === app.systemmodulename) {
            arr.push(app)
          }
        })
      });
      
      this.apps = arr
      console.log(this.apps);
      
    },
    handleCheckAllChange2 (val) {
      let arr = this.appoptions.map(e=>{return e.optionid})
      this.checkedApps = val ? arr : [];
      this.isIndeterminate2 = false;
    },
  },
  mounted () {
    // 获取应用表结构
    this.tablecols = this.tool.tabelCol(this.$route.name).detailsAppsTable.tablecols
    this.data.roleid !== '0'?this.roleMain():''
    this.usertype()
  },
  created () {
    // 验证是否有编辑权限,若无权限则弹出
    // // this.tool.checkAuth(this.$route.name,'update')?'':this.$router.go(-1)
  }
}

</script>
<style>
</style>
<style scoped>
.mb-16{
  margin-bottom:16px
}
.title{
  height: 20px;
  line-height: 20px;
  font-size: 14px;
  text-indent: 7px;
  font-weight: bold;
  color: #333333;
  margin-bottom: 20px;
  border-left: .3rem solid #3874F6;
}
.option-item{
  color:#333333;
  font-size: 14px;
  border-top:1px solid #f1f2f3;
  padding-top:16px;
}
</style>