<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-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'
  import {HttpResetClass} from "vue-client";
  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 new HttpResetClass().load('POST', 'rs/data/save',{data:self.model})
    yield self.$showMessage(msg.bodyText)
    console.log(msg.bodyText)
    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: '',
          name: '',
          f_description: '',
          selfid: '',
          id: '',
          f_companylevel: '',
          tablename: 't_zone',
          entitytype: 't_zone',
          position: '1',
          resourcetype: 'zone',
          parentid: ''
        }
      }
    },
    methods: {
      confirm () {
        if (this.model.name === '') {
          this.$showMessage("区域名称不能为空")
          return
        }else if (this.model.f_companylevel === '') {
          this.$showMessage("区域性质不能为空")
          return
        }
        let gen = saveGen(this)
        return co(gen)
      }
    }
  }

</script>
