<template>
  <div class="auto">
    <validator name='v'>
      <div class="form-horizontal" style="min-height: 400px">
        <h3 class="txtline">&nbsp;&nbsp;组织信息</h3>
        <h3 class="txtline">&nbsp;&nbsp;上一级【{{parent.name}}】</h3>
        <div class="row">
          <div class="col-sm-3"></div>
          <div class="col-sm-3">
            <label class="font_normal_body">&nbsp;&nbsp;&nbsp;编号&nbsp;&nbsp;&nbsp;</label>
            <input type="text" v-model="model.number"
                   class="input_search" style="width:60%" id="number" placeholder="编号">
          </div>
          <div class="col-sm-3" :class="[$v.name.required ? 'has-error' : 'has-success']">
          <label class="font_normal_body">组织名称</label>
            <!--<div>-->
               <!--:class="{'has-warning':$v.name.required,'has-success': !$v.name.required}">-->
            <input type="text" v-model="model.name" class="input_search" id="user" style="width:60%"
                   v-validate:name="{ required: true}"
                   placeholder="组织名称" title="组织名称不能为空">
            <!--<span class="glyphicon glyphicon-remove form-control-feedback" v-if="$v.name.required"></span>-->
            <!--<span class="glyphicon glyphicon-ok form-control-feedback" v-if="!$v.name.required"></span>-->
          <!--</div>-->
          </div>
        </div>


        <div class="row">
          <div class="col-sm-3"></div>
          <!--<div class="col-sm-3">-->
            <!--<label class="font_normal_body">组织性质：</label>-->
            <!--<div class="form-group">-->
              <!--<div class="has-feedback"-->
                   <!--:class="{'has-warning':$v.f_companylevel.required,'has-success': !$v.f_companylevel.required}">-->
                <!--<input type="text" v-model="model.f_companylevel" class="input_search" style="width:60%" id="user"-->
                       <!--v-validate:f_companylevel="{ required: true}"-->
                       <!--placeholder="组织性质" title="组织性质不能为空">-->
                <!--<span class="glyphicon glyphicon-remove form-control-feedback" v-if="$v.f_companylevel.required"></span>-->
                <!--<span class="glyphicon glyphicon-ok form-control-feedback" v-if="!$v.f_companylevel.required"></span>-->
              <!--</div>-->
            <!--</div>-->
          <!--</div>-->
          <div class="col-sm-3">
            <label class="font_normal_body">组件目录</label>
            <input type="text" v-model="model.f_dir"
                   class="input_search" style="width:60%" id="user">
          </div>
        </div>
        <div class="row">
          <div class="col-sm-3"></div>
          <div class="col-sm-6">
            <label class="font_normal_body" style="float: bottom">&nbsp;&nbsp;&nbsp;描述&nbsp;&nbsp;&nbsp;</label>
            <textarea class="ver-textarea" style="width:80%" rows="4"  v-model="model.f_description"></textarea>

          </div>
        </div>
        <button @click="confirm()" 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='$back()' 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) {
  //let msg = yield self.$post('rs/data/save', {data:self.model},{resolveMsg:null, rejectMsg:null})
  console.log(self.model)
  self.$http.post('rs/data/save',{data:self.model}).then((msg)=>{
    if(Number.isNaN(Number(msg.bodyText))){
      self.$showMessage(msg.bodyText)
    }else {
      self.$showMessage("添加成功")
    }
    console.log(msg.bodyText)
    self.thisRefersh()
    self.$back('refresh')
  })

}

export default {
  title: '组织信息',
  props: ['parent', 'data'],
  ready() {
    this.model.parentid = this.parent.id
    this.model = Object.assign({}, this.model, this.data)
  },
  data () {
    return {
      model: {
        loginid:this.$login.f.id,
        number: '',
        name: '',
        f_description: '',
        selfid: '',
        id: '',
        f_companylevel: '',
        resourcetype: 'organization',
        parentid: '',
        f_dir: ''
      }
    }
  },
  methods: {
    confirm () {
      if (this.model.name.replace(/\s*/g,"").length==0) {
        this.$showMessage("组织名称不能为空")
        return
      }
      let gen = saveGen(this)
      return co(gen)
    },
    thisRefersh(){
      this.$parent.$parent.$parent.$parent.flush()
    },
  },

}

</script>
