<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">区域编码</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>
                <input type="text" v-model="model.name" class="input_search" style="width:60%" id="user"
                       v-validate:name="{ required: true}"
                       placeholder="区域名称" title="区域名称不能为空">
          </div>
        </div>
        <div class="row">
          <div class="col-sm-3"></div>
          <div class="col-sm-6">
            <label class="font_normal_body">&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()' style="width: 70px" class="btn button_clear 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: {
          loginid:this.$login.f.id,
          number: '',
          name: '',
          f_description: '',
          selfid: '',
          id: '',
          f_companylevel: '',
          tablename: 't_zone',
          entitytype: 't_zone',
          position: '1',
          resourcetype: 'zone',
          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>
