<template>
  <div class="auto">
    <validator name='v'>
      <div class="datapanel 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-6 form-group form-fix-width">
            <label class="control-label">编号：</label>
            <input type="text" v-model="model.number"
                   class="form-control" id="number" placeholder="编号">
          </div>
          <div class="col-sm-6 form-group form-fix-width">
            <label class="control-label">组织名称：</label>
            <div class="form-group">
              <div class="has-feedback"
                   :class="{'has-warning':$v.name.required,'has-success': !$v.name.required}">
                <input type="text" v-model="model.name" class="form-control" id="user"
                       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="col-sm-6 form-group form-fix-width">
            <label class="control-label">组织性质：</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="form-control" 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-6 form-group form-fix-width">
            <label class="control-label">组件目录：</label>
            <input type="text" v-model="model.f_dir"
                   class="form-control" id="user">
          </div>
          <div class="col-sm-6 form-group form-fix-width">
            <label class="control-label">备用ID：</label>
            <input type="text" required class="form-control"
                   placeholder="备用ID" v-model="model.standbyid">
          </div>
          <div class="col-sm-12 form-group form-fix-width">
            <label class="control-label">描述：</label>
            <textarea class="form-control" rows="3" style="width: 100%;" v-model="model.f_description"></textarea>

          </div>
        </div>
        <button @click="confirm()" class="btn btn-success col-sm-1 col-sm-offset-2" :disabled="!$v.valid">
          <span class="glyphicon glyphicon-floppy-disk"></span>保存
        </button>
        <button @click='$back()' class="btn btn-default col-sm-1 col-sm-offset-1">
          <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)
  let msg = yield self.$http.post('rs/data/save',{data:self.model})
  if(Number.isNaN(Number(msg.bodyText))){
    yield self.$showMessage(msg.bodyText)
  }else {
    yield self.$showMessage("添加成功")
  }
  console.log(msg.bodyText)
  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: '',
        //备用id
        standbyid:'',
        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
      }else if (this.model.f_companylevel.replace(/\s*/g,"").length==0) {
        this.$showMessage("组织性质不能为空")
        return
      }
      let gen = saveGen(this)
      return co(gen)
    }
  }
}

</script>
