<template>
  <div class="auto select-overspread" style="overflow-y: hidden">
    <validator name='v'>
      <ul class="nav nav-tabs">
        <p class="bg-info text-center" style="padding: 8px;">功能信息</p>
        <h3 class="txtline" v-if="parent&&parent.name">上一级【{{parent.name}}】</h3>
        <h3 class="txtline"  v-if="!(parent&&parent.name)">上一级【{{model.parentname}}】</h3>
      </ul>
      <div class="form-horizontal" style="margin-top:20px;flex: 1;">
        <div class="row">
          <div class="col-sm-6">
            <label class="font_normal_body" >功能名称</label>
            <input type="text"
                   :disabled="look"
                   v-model="model.name" style="width:60%"
                   class="input_search full-with" id="name" placeholder="功能名称">
          </div>
          <div class="col-sm-6">
            <label class="font_normal_body" >功能连接</label>
            <input type="text"  v-model="model.link" style="width:60%"
                   :disabled="look"
                   class="input_search full-with" placeholder="功能连接">
          </div>
        </div>
        <div class="row">
          <div class="col-sm-6">
            <label class="font_normal_body" >页面连接</label>
            <input type="text"  v-model="model.navigate" style="width:60%"
                   :disabled="look"
                   class="input_search full-with" placeholder="页面连接">
          </div>
          <div class="col-sm-6">
            <label class="font_normal_body" >&nbsp;&nbsp;&nbsp;图标&nbsp;&nbsp;&nbsp;</label>
            <input type="text" v-model="model.icon" style="width:60%"
                   :disabled="look"
                   class="input_search full-with" id="f_userid" placeholder="图标路径">
          </div>
        </div>
        <div class="row">
          <div class="col-sm-12">
            <label class="font_normal_body" >&nbsp;&nbsp;&nbsp;描述&nbsp;&nbsp;&nbsp;</label>
            <textarea class="ver-textarea" style="width:80%" rows="4"
                      :disabled="look"
                      v-model="model.f_description"></textarea>
          </div>
        </div>

        <button @click="confirm()" v-if="!look" 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>
    </validator>
  </div>
</template>

<script>
  import co from 'co'

  let saveGen = function * (self) {
    self.$resetpost('rs/data/save',{data:self.model}).then((msg)=>{
      self.$dispatch('success')
    })
  }

  export default {
    title: '功能',
    props: ['parent', 'data','look'],
    data () {
      return {
        model: {
          loginid:this.$login.f.id,
          id: '',
          f_parentname: '',
          name: '',
          link: '',
          navigate: '',
          showmodel: '',
          icon: '',
          f_description: '',
          parentid: '',
          resourcetype: 'function'
        },
      }
    },
    watch: {
      data: {
        handler(n, o) {
          this.initdata()
        },
        deep: true //对象内部属性的监听，关键。
      },
    },
    ready() {
      this.initdata()
    },
    methods: {
      //初始化数据
      initdata(){
        this.model= {
          loginid:this.$login.f.id,
            id: '',
            f_parentname: '',
            name: '',
            link: '',
            navigate: '',
            showmodel: '',
            icon: '',
            f_description: '',
            parentid: '',
            resourcetype: 'function'
        }
        if(this.parent){
          this.model.f_parentname = this.parent.name
          this.model.parentid = this.parent.id
        }
        this.model = Object.assign({}, this.model, this.data)
      },
      cancel () {
        this.$dispatch('cancel')
      },
      confirm () {
        let gen = saveGen(this)
        return co(gen)
      }
    }
  }

</script>

<style media="screen">
  .full-with {
    width: 100%;
  }
</style>
