<script>
  import * as service from '../../service/LicenseService'

  // 资源显示组件，显示资源名称，授权信息等内容
  export default {
    props: {
      // 要显示的资源
      resource: {
        type: Object,
        require: true
      }
    },
    data() {
      return {
        // 资源授权全路径串
        licenseNames: ''
      }
    },
    ready() {
      this.refresh()
    },
    watch: {
      resource () {
        this.refresh()
      }
    },
    methods: {
      // 刷新数据
      refresh () {
        // 取得授权内容
        if (this.resource) {
          console.log("资源名字",this.resource.name)
          service.getDataRights(this.resource).then((names) => {
            this.licenseNames = names
            this.$emit('ready')
          })
        }
      },
      // 删除权限
      deleteres (val) {
        this.$showMessage('确定要删除【'+val.licensepolicy+'】权限', ['confirm', 'cancel']).then((res) => {
          if (res === 'confirm') {
            this.$resetpost("rs/license/save", this.dealLic(val)).then((req) => {
                service.getDataRights(this.resource).then((names) => {
                  this.licenseNames = names
                })
            })
          }
        })
      },
      dealLic (val) {
        var reslicenses=[]
        this.licenseNames.forEach((onelic) => {
          if(onelic.id!=val.id){
            reslicenses.push(onelic)
          }
        })
        return {parentid:val.parentid,
          licenses:reslicenses}
      },
    }
  }
</script>

<template>
  <table class="table table-bordered datapanel">
    <thead class="text-left">
    <tr>
      <th><nobr>所属权限</nobr></th>
      <th><nobr>操作</nobr></th>
    </tr></thead>
    <tbody>
    <tr v-for="one in licenseNames">
      <td>{{one.licensepolicy}}</td>
      <td>
      <button-link type="button" class="btn btn-link" @click="deleteres(one)">删除权限</button-link>
      </td>
    </tr>
    </tbody>
  </table>
</template>
