<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-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.name.required,'has-success': !$v.name.required}">
                <input type="text" v-model="model.name" class="input_search" style="width:60%" 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>

        <div class="row">
          <div class="col-sm-3"></div>
          <div class="col-sm-3">
            <label class="font_normal_body">描述：</label>
            <textarea class="input_search" style="width:60%" 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-5" :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)
    self.$http.post('rs/data/save',{data:self.model}).then((msg)=>{
      if(Number.isNaN(Number(msg.bodyText))){
        self.$showMessage(msg.bodyText)
      }else {
        self.$showMessage("添加成功")
      }
      self.refersh()
      self.$back('refresh')
    })
  }

  export default {
    title: '部门信息',
    props: ['parent', 'data'],
    ready() {
      console.log("------------------------parentdata", this.parent, this.data)
      this.model.parentid = this.parent.id
      this.model = Object.assign({}, this.model, this.data)
    },
    data () {
      return {
        model: {
          // number: '',
          loginid:this.$login.f.id,
          name: '',
          f_description: '',
          selfid: '',
          id: '',
          // f_companylevel: '',
          tablename: 't_department',
          entitytype: 't_department',
          position: '1',
          resourcetype: 'department',
          parentid: ''
        }
      }
    },
    methods: {
      confirm () {
        if (this.model.name.replace(/\s*/g,"").length==0) {
          this.$showMessage("部门名称不能为空")
          return
        }
        let gen = saveGen(this)
        return co(gen)
      },
      refersh() {
        this.$parent.$parent.$parent.$parent.flush()
      }
    }
  }

</script>
