<template>
<div>
    <validator name='v'>
        <form novalidate class="form-horizontal">
            <div class="form-group">
                <label class="col-sm-2 control-lable">标题：</label>
                <div class="col-sm-8">
                    <input class="form-control" type="text" v-model="notice.f_title" v-validate:f_title='{ required: true }' />
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-lable">内容：</label>
                <div class="col-sm-8">
                    <textarea class="form-control" type="text" v-model="notice.f_content" v-validate:f_content='{ required: true }'></textarea>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-2 control-lable">时间：</label>
                <div class="col-sm-8">
                    <input readonly='readonly' class="form-control" type="text" v-model="notice.f_time"/>
                </div>
            </div>
            <div class="form-group">
              <label class="col-sm-2 control-lable">分公司：</label>
              <div class="col-sm-8">
                <v-select :value.sync="notice.f_subcompany" :options='$parent.$refs.query.f_divisions' placeholder='请选择子公司'
                  close-on-select clear-button></v-select>
              </div>
            </div>
            <div class="form-group col-sm-10 text-right">
              <button type="button" class="btn btn-success width-80" @click="save" v-if="$v.valid">保存</button>
              <button type="button" class="btn btn-success width-80" @click="anew">新建</button>
              <button type="button" class="btn btn-success width-80" @click="delete" v-if="$v.valid">删除</button>
            </div>
        </form>
    </validator>
</div>

</template>

<script>
import * as Util from '../Util'
import co from 'co'
import Vue from 'vue'

let syncSave = function * (self) {
  self.notice.f_subcompany = self.$parent.$refs.query.f_divisions[0].value
  yield self.$resetpost('rs/entity/t_notice', self.notice)
  self.$parent.$refs.query.$refs.cp.$refs.criteria.search()
}

let syncDelete = function * (self) {
  yield self.$remove('rs/entity/t_notice/', {id:self.notice.id })
  self.$parent.$refs.query.$refs.cp.$refs.criteria.search()
}

export default {
  title: '公告表单',
  props: ['notice'],
  computed: {
    f_division: {
      get() {
        if (!this.notice || !this.notice.f_subcompany)
          return Vue.f_subcompany
        else
          return [this.notice.f_subcompany]
      },
      set(val) {
        this.notice.f_subcompany = val[0]
      }
    }
  },
  methods: {
    save() {
      if (!this.notice) {
        alert('请选择一个通知。')
        return
      }
      if (!this.notice.id) {
        this.$set('notice.f_time', Util.toStandardTimeString())
      }
      let gen = syncSave(this)
      return co(gen)
    },
    anew() {
      this.notice = {}
      this.$set('notice.f_time', Util.toStandardTimeString())
    },
    delete() {
      if (!this.notice) {
        alert('请选择一个通知。')
        return
      }
      let gen = syncDelete(this)
      return co(gen)
    }
  }
}

</script>
