<template>
  <div class="auto">
    <validator name='v'>
      <form novalidate class="form-horizontal select-overspread">
        <ul class="nav nav-tabs">
          <li class="active"><a href="#">分配信息</a></li>
        </ul>
        <div class="row" style="margin-top:10px;">
          <div class="form-group form-input-group col-sm-6" :class="[$v.f_bill_user.required ? 'has-error' : 'has-success']">
            <input type="text" v-show="false" v-model="model.f_bill_user" v-validate:f_bill_user='{required: true }'>
            <label class="font_normal_body">票据使用人&emsp;</label>
            <v-select :value.sync="model.f_bill_user"
                      v-model="model.f_bill_user"
                      :options='billusers'
                      title="参数:票据使用人"
                      placeholder='请选择'
                      :width='100'
                      :value-single="true"
                      close-on-select>
            </v-select>
          </div>
          <div class="form-group form-input-group col-sm-6" :class="[$v.f_bill_type.required ? 'has-error' : 'has-success']">
            <input type="text" v-show="false" v-model="model.f_bill_type" v-validate:f_bill_type='{required: true }'>
            <label class="font_normal_body">票据类型&emsp;</label>
            <v-select :value.sync="model.f_bill_type"
                      v-model="model.f_bill_type"
                      :options='printtypes'
                      title="参数:分配票据格式"
                      placeholder='请选择'
                      :width='100'
                      :value-single="true"
                      close-on-select>
            </v-select>
          </div>

        </div>

        <div class="row" style="margin-top:10px;">

          <div class="form-group form-input-group col-sm-6" :class="[$v.f_start_number.required ? 'has-error' : 'has-success']">
            <label for="f_start_number" class="font_normal_body">起始发票号&emsp;</label>
            <input type="number" class="input_search"
                   v-validate:f_start_number='{required: true}'
                   v-model="model.f_start_number" placeholder='0'>
          </div>
          <div class="form-group form-input-group col-sm-6" :class="[$v.f_end_number.required || $v.f_end_number.dctest? 'has-error' : 'has-success']">
            <label for="f_end_number" class="font_normal_body">终止发票号</label>
            <input  type="number" class="input_search"
                    v-validate:f_end_number='{required: true, dctest: [model.f_start_number, ">" ] }'
                    v-model="model.f_end_number" placeholder='0'>
          </div>


        </div>
        <div class="row" style="margin-top:10px;">
          <div class="form-group form-input-group col-sm-6">
            <label for="f_number" class=" font_normal_body">收据总份数&emsp;</label>
            <input  type="number" class="input_search" v-model="number" placeholder='0' disabled>
          </div>
          <div class="form-group form-input-group col-sm-6" :class="[$v.f_bill_code.required ? 'has-error' : 'has-success']">
            <label for="f_bill_code" class="font_normal_body">发票代码&emsp;</label>
            <input type="string" class="input_search"
                   v-validate:f_bill_code='{required: true}'
                   v-model="model.f_bill_code" placeholder='发票代码'>
          </div>
        </div>
        <div class="row" style="margin-top:10px;">
          <div class="form-group form-input-group col-sm-6" >
            <label class="font_normal_body">使用类型&emsp;</label>
            <v-select :value.sync="model.f_use_type"
                      v-model="model.f_use_type"
                      title="参数:发票使用类型"
                      :options='usetypes'
                      placeholder='请选择'
                      :width='100'
                      :value-single="true"
                      close-on-select>
            </v-select>
          </div>
        </div>
      </form>
      <div style="text-align:right;margin-top:30px;margin-right:50px;">
        <button class="btn button_search" @click="confirm()"  :disabled='!$v.valid'>确认</button>
        <button class="btn button_clear" @click="clean()">取消</button>
      </div>
    </validator>
  </div>
</template>
<script>
  export default {
    title: '重新分配',
    data () {
      return {
        model: {
          f_bill_code: '',
          f_total_number: 0,
          f_start_number: null,
          f_end_number: null,
          f_bill_type: null,
          f_use_type: null,
          f_bill_user: null
        },
        printtypes: this.$appdata.getParam('分配票据格式'),
        usetypes: this.$appdata.getParam('发票使用类型'),
        billusers: this.$appdata.getParam('票据使用人')
        // usetypes: [{label:"test",value:"test"}]
      }
    },
    props: ['data'],
    watch: {
      'data' (val) {
        this.initData()
        this.model = Object.assign({}, this.model, val)
        if (val) {
          this.model.f_start_number = val.f_using_number + 1
        }
      }
    },
    ready () {
      this.initData()
      this.model = Object.assign({}, this.model, this.data)
      if (this.data) {
        this.model.f_start_number = this.data.f_using_number + 1
      }
    },
    methods: {
      initData () {
        this.model = {
          f_bill_code: '',
          f_total_number: 0,
          f_start_number: null,
          f_end_number: null,
          f_bill_type: null,
          f_use_type: null,
          f_bill_user: null
        }
      },
      async confirm () {
        this.model.f_total_number = this.number

        if (!this.model.id) {
          let res = await this.$resetpost('rs/logic/validatePaperBill', {data: this.model}, {resolveMsg: null, rejectMsg: '验证发票号出错!!!'})

          console.log('验证发票号返回', res)
          if (res.data) {
        // 说明该票据使用人已存在正在使用过的票号，提示。若确定将原来使用的票号段停用操作
            let getOper = await this.$showMessage('该票据使用人已存在正在使用的票号。点击确定将会对原票号段停用。确定要新增吗？', ['confirm', 'cancel'])

            if (getOper === 'cancel') {
              return
            }
          }
        }

        await this.$BillService.replaceDistribution(this.model, this.data)
        this.$dispatch('success')
  },
  clean () {
    this.$dispatch('refresh', this.data)
  }
  },
  computed: {
    'number' () {
      let res = 0
      if (this.model.f_end_number && this.model.f_start_number) {
        res = (this.model.f_end_number - 0) - (this.model.f_start_number - 0) + 1
      }
      return res < 0 ? 0 : res
    }
    // 'billusers' () {
    //  let rs = new Array()
    //  for (let i = 0; i < this.$login.f.f_paperUseman.length; i++) {
    //    let temp = {
    //      label: this.$login.f.f_paperUseman[i].name,
    //      value: this.$login.f.f_paperUseman[i].name
    //    }
    //    rs.push(temp)
    //  }
    //  return rs
    // }
  }
  }
</script>

<style>
</style>
