<template>
  <div class="normal-panel drewer__panel">
    <div>
      <p class="normal-title mb-16">角色信息</p>
      <el-form :inline="true" :model="form" :rules="rules" ref="form" size="small" label-width="100px" label-position="left" class="demo-form-inline">
        <el-form-item label="角色名称" prop="rolename">
          <el-input v-model="form.rolename" placeholder="输入角色名称"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="remarks">
          <el-input v-model="form.remarks" placeholder="输入角色描述"></el-input>
        </el-form-item>
        <el-form-item label="角色类型" prop="usertype">
          <el-select v-model="form.usertype" placeholder="角色类型">
            <el-option v-for="item in options" :key="item.index" :label="item.remarks" :value="Number(item.value)"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <div>
      <p class="normal-title mb-16">角色授权</p>
      <el-row :gutter="20">
        <el-col :span="12">
          <div class="mb-16">
            <auth @onSuccess="roleMain"></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">
              <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'))"></cancelAuth>
              </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 class="fixed__btn__panel">
      <slot name="cancel"></slot>
      <el-button type="primary" size="small" class="normal-btn-width" @click="onSubmit">保 存</el-button>
    </div>
  </div>
</template>

<script>
import auth from './auth_list.vue'
import cancelAuth from './cancel_auth.vue'
import cancelFields from './cancel_fields.vue'

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' }
        ],
      },
      form:{
        "roleid":0,
        "rolename":"",
        "remarks":""
      },
      roleMainInfo:{},
      options:[],
      tablecols:[],
      appoptions:[],
      hiddenfields:[],
      modulesList:[],
      checkModuleList:[],
      apps:[],
      checkedApps:[],
      roleid:0,
      active_systemappid:''
    }
  },
  methods:{
    async roleMain () {
      const res = await this.$api.requested({
        "classname": "webmanage.role.role",
        "method": "query_roleMain",
        "content": {
          "roleid":this.data.roleid
        }
      })
      this.form = {
        "roleid":res.data.roleid,
        "rolename":res.data.rolename,
        "remarks":res.data.remarks,
        "usertype":res.data.usertype
      }
      this.roleMainInfo = res.data

      //显示默认授权信息,一般默认信息为第一条数据
      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.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.$router.push({path:this.$route.path,query:{id:res.data.roleid}})
            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
    },
    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>