<template>
  <div class='auto' id="adjustTree">
    <h3 class="txtline">&nbsp;&nbsp;需要调整功能【{{data.name}}】</h3>
    <h3 class="txtline">&nbsp;&nbsp;选中功能【{{$refs.tree.selected.data.name}}】</h3>
    <tree :model="model" v-ref:tree id='defaultTreeOpen'>
      <span partial>{{row.data.name}}
      </span>
    </tree>
    <button @click="save()" class="btn button_search col-sm-1 col-sm-offset-5" style="width: 70px" :disabled="!$v.valid">
      <span class="glyphicon glyphicon-floppy-disk"></span>保存
    </button>
    <button @click='cancel()' class="btn button_clear  col-sm-1 col-sm-offset-1"style="width: 70px">
      <span class=" glyphicon glyphicon-log-out"></span>返回
    </button>
  </div>
</template>

<script>
import co from 'co'
import {HttpResetClass} from 'vue-client'
let getFuns = async function (self) {
  let http = new HttpResetClass()
  let data={
    source: `tool.getFullTree(this.getRights().where(row.getType() == $function$))`,
    userid:self.$login.f.id
  }
  let ret = await http.load('POST', '/rs/search',data,
    {resolveMsg:null, rejectMsg: '获取功能树出错！！请联系管理员！！'})
  if (ret.data[0].children.length > 0 ) {
    self.model = ret.data[0].children
  } else {
    self.$showAlert('暂无数据', 'warning', 2000)
  }
}
let saveGen = function * (self) {
  if(self.data.id!=self.$refs.tree.selected.parent.data.id){
    self.$showMessage('确定要将【'+self.data.name+'】移动到'+self.$refs.tree.selected.data.name+'的后面', ['confirm', 'cancel']).then((res) => {
      if (res === 'confirm') {
        let msg = self.$resetpost(`rs/data/adjust/${self.$refs.tree.selected.id}/${self.data.id}`, {})
        self.$dispatch('success')
      }
    })
  }else {
    self.$showMessage("请检查调整层级！！！")
  }
}
export default {
  title: "调整资源位置",
  props: [
    'data' // 要调整的资源
  ],
  data () {
    return {
      model: [],
    }
  },
  ready () {
    this.initData()
  },
  methods: {
    //准备数据
    async initData (flag) {
      await getFuns(this)
    },
    cancel () {
      this.$dispatch('cancel')
    },
    // 保存调整结果
    async save () {
      let gen = saveGen(this)
      return co(gen)

    }
  }
}
</script>
<style>
  .all-checked {
    vertical-align: text-top;
    margin-left: 10px !important;
  }
  td > .glyphicon {
    cursor: pointer;
  }
  th > input[type="checkbox"], td > input[type="checkbox"] {
    cursor: pointer;
  }
  #adjustTree div{
    margin-left: 0px;
    color: #000;
  }
  #adjustTree span {
    color: #000;
  }
  #adjustTree .list-group-item {
    background-color: #fff;
    border: none;
  }
  #adjustTree .list-group-item.active,
  #adjustTree  .list-group-item.active:hover,
  #adjustTree  .list-group-item.active:focus {
    background-color: #fff;
  }

  #adjustTree .active {
    background-color: #5cb85c !important;
  }
</style>
