<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">&nbsp;&nbsp;&nbsp;编号&nbsp;&nbsp;&nbsp;</label>
            <input type="text" v-model="model.number"
                   :disabled="look"
                   @blur="numberBlur()"
                   class="input_search" style="width:60%" id="number" placeholder="编号">
          </div>
          <div class="col-sm-6"   :class="[$v.name.required ? 'has-error' : '']">
            <label class="font_normal_body">组织名称</label>
            <input type="text" v-model="model.name" class="input_search" id="user" style="width:60%"
                   :disabled="look"
                   @blur="nameBlur()"
                   v-validate:name="{ required: true}"
                   placeholder="组织名称" title="组织名称不能为空">
          </div>
        </div>
        <div class="row">
          <div class="col-sm-6">
            <label class="font_normal_body">组件目录</label>
            <input type="text" v-model="model.f_dir"
                   :disabled="look"
                   class="input_search" style="width:60%" id="f_dir">
          </div>
        </div>
        <div class="row">
          <div class="col-sm-12">
            <label class="font_normal_body" style="float: bottom">&nbsp;&nbsp;&nbsp;描述&nbsp;&nbsp;&nbsp;</label>
            <textarea class="ver-textarea" style="width:80%" rows="4"
                      :disabled="look"
                      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()'  class="btn button_clear  col-sm-1 col-sm-offset-1"style="width: 70px">
          <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: '',
          resourcetype: 'organization',
          parentid: '',
          f_dir: ''
        }
      }
    },
    methods: {
      async numberBlur () {
        if (this.model.number) {
          let result = await this.$resetpost('rs/sql/saleSingleTable',
            {
              data: {
                tablename: 't_organization',
                condition: `number = '${this.model.number}'`
              }
            },{resolveMsg: null, rejectMsg: null}
          )
          if (result.data.length > 0) {
            this.model.number = ''
            this.$showMessage("组织编号已存在，请重新输入")
          }
        }
      },
      async nameBlur () {
        if (this.model.name) {
          let result = await this.$resetpost('rs/sql/saleSingleTable',
            {
              data: {
                tablename: 't_organization',
                condition: `name = '${this.model.name}'`
              }
            },{resolveMsg: null, rejectMsg: null}
          )
          if (result.data.length > 0) {
            this.model.name = ''
            this.$showMessage("组织名称已存在，请重新输入")
          }
        }
      },
      //初始化数据
      initdata(){
        this.model={
          loginid:this.$login.f.id,
          number: '',
          name: '',
          f_description: '',
          selfid: '',
          id: '',
          f_companylevel: '',
          resourcetype: 'organization',
          parentid: '',
          f_dir: ''
        }
        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>
