<template>
  <div class="auto select-overspread" style="overflow-y: hidden">
    <validator name='v'>
      <ul class="nav nav-tabs">
        <p class="bg-info text-center" style="padding: 8px;">区域信息</p>
        <h3 class="txtline" v-if="parent&&parent.name">上一级【{{parent.name}}】</h3>
        <h3 class="txtline"  v-if="!(parent&&parent.name)">上一级【{{model.parentname}}】</h3>
      </ul>
      <div class="form-horizontal" style="margin-top:20px;flex: 1;">
        <div class="row">
          <div class="col-sm-6">
            <label class="font_normal_body">区域编码</label>
            <input type="text" v-model="model.number"
                   :disabled="look"
                   class="input_search" style="width:60%" id="number" placeholder="区域编码">
          </div>
          <div class="col-sm-6" :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"
                   :disabled="look"
                   v-validate:name="{ required: true}"
                   placeholder="区域名称" title="区域名称不能为空">
          </div>
        </div>
        <div class="row">
          <div class="col-sm-12">
            <label class="font_normal_body">&nbsp;&nbsp;&nbsp;描述&nbsp;&nbsp;&nbsp;</label>
            <textarea class="ver-textarea" style="width:80%"
                      :disabled="look"
                      rows="4" v-model="model.f_description"></textarea>
          </div>
        </div>
        <button @click="confirm()" v-if="!look" 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='cancel()'  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) {
    self.$resetpost('rs/data/save',{data:self.model}).then((msg)=>{
      self.$dispatch('success')
    })
  }

  export default {
    title: '区域信息',
    props: ['parent', 'data','look'],
    ready() {
      this.initdata()
      this.$resetValidation()
    },
    watch: {
      data: {
        handler(n, o) {
          this.initdata()
        },
        deep: true //对象内部属性的监听，关键。
      },
    },
    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: {
      //初始化数据
      initdata(){
        this.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: ''
        }
        if(this.parent){
          this.model.parentid = this.parent.id
        }
        this.model = Object.assign({}, this.model, this.data)
      },
      confirm () {
        if (this.model.name.replace(/\s*/g,"").length==0) {
          this.$showMessage("区域名称不能为空")
          return
        }
        let gen = saveGen(this)
        return co(gen)
      },
      cancel () {
        this.$dispatch('cancel')
      },
    },
  }

</script>
