<template>
  <div class="auto select-overspread flex">
    <validator name='v'>
      <ul class="nav nav-tabs">
        <li class="active"><a href="#">角色信息</a></li>
      </ul>
      <form class="form-horizontal" style="margin-top:20px;flex: 1;">
        <div class="row">
          <div class="col-sm-6"  :class="[$v.f_role_id.required ? 'has-error' : '']">
            <label class="font_normal_body" >角色编号</label>
            <input type="text"  v-model="model.f_role_id" v-validate:f_role_id='{required: true }' class="input_search" style="width:60%" placeholder="角色编号" v-next-el="name">
          </div>

          <div class="col-sm-6"   :class="[$v.name.required ? 'has-error' : '']">
            <label for="f_adjustable_name" class="font_normal_body" >角色名称</label>
            <input type="text"  v-model="model.name"
                   v-validate:name="{required: true }" class="input_search" style="width:60%" placeholder="角色名称" v-el:name v-next-el="address">
          </div>
        </div>
      </form>
      <div style="text-align:right;height:auto;margin-top:6px;">
        <button class="button_clear button_spacing" @click="confirm()" :disabled='!$v.valid'>确认</button>
        <button class="btn btn-default" @click="close()">取消</button>
      </div>
    </validator>
    <div class="span" style="margin-top: 5%">
    <role-permissions :data="data" :roots.sync="permissions"></role-permissions>
  </div>
  </div>
</template>

<script>

  export default {
    title: '添加角色',
    data () {
      return {
        model: {
          id: '',
          name: '',
          f_role_id:'',
          parentid: this.parentid,
          selfid: '',
          f_description: '',
          resourcetype: 'role'
        },
        permissions: []
      }
    },
    props: ['parentid','data'],
    ready () {
      this.model = Object.assign({}, this.model, this.data)
    },
    watch: {
      'data' (val) {
        this.model = Object.assign({}, this.model, this.data)
      }
    },
    methods: {
      async confirm () {
        // 保存用户
        console.log('要保存用户了', this.data, this.model, this.permissions)
        // 保存角色
        this.model.f_create_date = this.$login.toStandardTimeString()
        let saveRole = await this.$resetpost('rs/data/save',{data:this.model},{resolveMsg: null, rejectMsg: '保存角色出错！！'})

        let roleId = this.model.id ? this.model.id : saveRole.data + ''
        // 保存权限
        console.log('开始保存权限', roleId)

        // 组织角色权限数据
        let param = {
          parentid: roleId,
          loginid: this.$login.f.id,
          licenses:[]
        }
        this.permissions.forEach((item) => {
          let license = {}
          license.isinherit = "是"
          license.byisinherit = "是"
          license.resourceresult = ""
          license.parentid = roleId
          license.resourceid = roleId
          license.byresourceid = item.id
          license.righttype = ''
          license.byresourceresult = ''
          license.licensepolicy = ''
          param.licenses.push(license)
        })

        await this.$resetpost('rs/license/save', param, {resolveMsg: '保存成功', rejectMsg: '保存权限失败！请您重试！！'})
        this.$dispatch('refresh')
      },
      close () {
        this.$dispatch('refresh')
      }
    }
  }

</script>
