<template>
  <div>
    <div class="container normal-panel mb-16">
      <el-button type="warning" size="small" icon="el-icon-s-claim" style="background:#FA8C16" @click="onSubmit">保 存</el-button>
    </div>
    <div class="container normal-panel mb-16">
      <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 class="container normal-panel mb-16">
      <p class="normal-title mb-16">角色授权</p>
      <el-row :gutter="20">
        <el-col :span="12">
          <div class="mb-16">
            <auth :data="{roleid:roleid}" @onSuccess="roleMain"></auth>
          </div>
          <tableLayout :layout="tablecols" :data="roleMainInfo.apps" :opwidth="200" :custom="false" @rowClick="appoptionselect">
          </tableLayout>
        </el-col>
        <el-col :span="12">
          <el-row :gutter="40">
            <el-col :span="12">
              <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>
                <cancelAuth :data="{roleid:roleid,systemappid:active_systemappid,optionid:item.optionid}" @onSuccess="roleMain"></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"></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'

export default {
  props:['data'],
  components:{
    auth,
    cancelAuth,
    cancelFields
  },
  data () {
    return{
      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:[],
      roleid:0,
      active_systemappid:''
    }
  },
  methods:{
    async roleMain () {
      const res = await this.$api.requested({
        "classname": "webmanage.role.role",
        "method": "query_roleMain",
        "content": {
            "roleid":this.roleid
        }
      })
      this.form = {
        "roleid":res.data.roleid,
        "rolename":res.data.rolename,
        "remarks":res.data.remarks,
        "usertype":res.data.usertype
      }
      this.roleMainInfo = res.data

      //显示默认授权信息,一般默认信息为第一条数据
      this.appoptionselect(res.data.apps[0])
    },
    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=>{
          if (res.code === 1) {
            this.$notify({
              title: '成功',
              message: '保存成功',
              type: 'success'
            });
            this.$router.replace({path:'/roleEdit',query:{id:res.data.roleid}})
          } else {
            this.$notify({
              title: '失败',
              message: res.data,
              type: 'error'
            });
          }
        })
      })
    }
  },
  mounted () {
    // 获取应用表结构
    this.tablecols = this.tool.tabelCol(this.$route.name).detailsAppsTable.tablecols
    this.roleid = this.$route.query.id
    this.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: 4px solid #3874F6;
}
.option-item{
  color:#333333;
  font-size: 14px;
  border-bottom:1px solid #f1f2f3
}
</style>