<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">&nbsp;&nbsp;&nbsp;编号&nbsp;&nbsp;&nbsp;</label>
            <input type="text" v-model="model.number"
                   :disabled="look"
                   class="input_search" style="width:60%" id="number" placeholder="编号">
          </div>
          <div class="col-sm-6"   :class="[$v.name.required ? 'has-error' : '']">
            <label class="font_normal_body">组织名称</label>
            <input type="text" v-model="model.name" class="input_search" id="user" style="width:60%"
                   :disabled="look"
                   v-validate:name="{ required: true}"
                   placeholder="组织名称" title="组织名称不能为空">
          </div>
        </div>
        <div class="row">
          <div class="col-sm-6">
            <label class="font_normal_body">组件目录</label>
            <input type="text" v-model="model.f_dir"
                   :disabled="look"
                   class="input_search" style="width:60%" id="f_dir">
          </div>
        </div>
        <div class="row" v-if="showcardip" >
          <div class="col-sm-12">
            <label class="font_normal_body">卡云请求</label>
            <textarea  class="ver-textarea showtextarea" style="width:80%" rows="2"
                      :disabled="look"
                      v-model="model.f_serailport"></textarea>
          </div>
        </div>
        <div class="row">
          <div class="col-sm-12">
            <label class="font_normal_body" style="float: bottom">&nbsp;&nbsp;&nbsp;描述&nbsp;&nbsp;&nbsp;</label>
            <textarea class="ver-textarea showtextarea" 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.$http.post('rs/data/save',{data:self.model}).then((msg)=>{
      self.$dispatch('success')
    })
  }

  export default {
    title: '组织信息',
    props: ['parent', 'data','look'],
    ready() {
      this.initdata()
      this.$resetValidation()
    },
    watch: {
       data: {
        handler(n, o) {
          this.initdata()
        },
        deep: true //对象内部属性的监听，关键。
      },
    },
    data () {
      return {
        showcardip:true,
        model: {
          loginid:this.$login.f.id,
          number: '',
          name: '',
          f_description: '',
          selfid: '',
          id: '',
          f_companylevel: '',
          resourcetype: 'organization',
          parentid: '',
          f_dir: ''
        }
      }
    },
    methods: {
      //初始化数据
      initdata(){
        this.model={
          loginid:this.$login.f.id,
          number: '',
          name: '',
          f_description: '',
          selfid: '',
          id: '',
          f_companylevel: '',
          resourcetype: 'organization',
          parentid: '',
          f_dir: ''
        }
        if(this.parent){
          this.model.parentid = this.parent.id
        }
        this.model = Object.assign({}, this.model, this.data)
      },
      confirm () {
        if (this.model.name.replace(/\s*/g,"").length==0) {
          this.$showMessage("组织名称不能为空")
          return
        }
        let gen = saveGen(this)
        return co(gen)
      },
      cancel () {
        this.$dispatch('cancel')
      },
    },

  }

</script>
<style scoped>
  .showtextarea{
    font-size: 14px;
    color: #000;
    font-weight: 400;
    padding: 7px;
  }
</style>
