<template>
  <div style="flex:1;" class="flex">
    <criteria-paged :model="model" v-ref:paged>
      <criteria partial='criteria' @condition-changed='search' v-ref:cri>
        <div novalidate class="form-inline" partial>
          <div >
            <v-select id="f_user"
                      v-model="model.f_user"
                      placeholder='票据使用人'
                      :value.sync="model.f_user"
                      :options='$parent.$parent.user'
                      condition= "f_user = '{}'"
                      close-on-select>
            </v-select>
          </div>
          <div >
            <v-select id="f_operator"
                      v-model="model.f_operator"
                      placeholder='票据分配人'
                      :value.sync="model.f_operator"
                      :options='$parent.$parent.operator'
                      condition= "f_operator = '{}'"
                      close-on-select>
            </v-select>
          </div>
          <div >
            <button class="btn btn-success width-100" @click="$parent.$parent.search()" v-el:cba>查询</button>
          </div>
          <div >
            <button class="btn btn-success width-100" @click="$parent.$parent.add()" v-el:cba>新增</button>
          </div>
          <div >
            <button class="btn btn-success width-100" @click="$parent.$parent.update()" v-if="$parent.$parent.xiugai">重新分配</button>
          </div>
        </div>
      </criteria>
      <data-grid  :model="model" partial='list' v-ref:grid>
        <template partial='head'>
          <tr>
            <th>序号</th>
            <th>起始号</th>
            <th>终止号</th>
            <th>当前使用</th>
            <th>总份数</th>
            <th>收据代码</th>
            <th>票据类型</th>
            <th>使用人</th>
            <th>分配人</th>
            <th>分配日期</th>
            <th>操作</th>
          </tr>
        </template>
        <template partial='body'>
          <tr>
            <td style="text-align:center">{{$index+1}}</td>
            <td style="text-align:center">{{row.f_start_number}}</td>
            <td style="text-align:center">{{row.f_end_number}}</td>
            <td style="text-align:center">
              <input  type="number" class="col-sm-1" style="width:50%;margin-left:30%"v-model="row.f_bill_id"
                      v-next-el="{id: $index+1}"   :id="$index" @blur="$parent.$parent.$parent.updateNumber(row)"></td>
            <td style="text-align:center">{{row.f_number}}</td>
            <td style="text-align:center">{{row.f_bill_number}}</td>
            <td style="text-align:center">{{row.f_bill_type}}</td>
            <td style="text-align:center">{{row.f_user}}</td>
            <td style="text-align:center">{{row.f_operator}}</td>
            <td style="text-align:center">{{row.f_createfile_date}}</td>
            <!-- <td style="text-align:center">{{row.f_bill_state}}</td> -->
            <td style="text-align:center">
              <button type="button" name="button" class="btn btn-link" v-if="row.f_bill_id != row.f_end_number && row.f_bill_state ==='停用' || row.f_bill_state ==='作废'"
                      @click.stop="$parent.$parent.$parent.modifyState(row, 'startup')">启用</button>
              <button type="button" name="button" class="btn btn-link" v-if="row.f_bill_id != row.f_end_number && row.f_bill_state ==='启用'"
                      @click.stop="$parent.$parent.$parent.modifyState(row, 'breakoff')">停用</button>
              <button type="button" name="button" class="btn btn-link" v-if="row.f_bill_id === row.f_start_number && row.f_bill_state ==='启用'"
                      @click.stop="$parent.$parent.$parent.modifyState(row, 'nullify')">作废</button>
            </td>
          </tr>
        </template>
        <template partial='foot'></template>
      </data-grid>
    </criteria-paged>
  </div>
</template>

<script>
  /**
   *阶梯水价查询列表
   */
  import { PagedList } from 'vue-client'
  export default {
    data () {
      return {
        model: new PagedList('rs/sql/getBillInfo', 20),

        operator: this.$appdata.getParam('票据分配人') ? [{label: '全部', value: ''}, ...this.$appdata.getParam('票据分配人')] : [],
        user: this.$appdata.getParam('票据使用人') ? [{label: '全部', value: ''}, ...this.$appdata.getParam('票据使用人')] : [],
      }
    },
    props: {
      row: {},
      xiugai: false
    },
    ready () {
      this.search()
      this.$refs.paged.$refs.grid.select(this.model.rows[0])
    },
    methods: {
      add () {
        this.$dispatch('add', 'add')
      },
      update () {
        this.$dispatch('add', 'update')
      },
      search () {
        this.$refs.paged.$refs.cri.search()
      },
      modifyState (row, type) {
        this.$BillService.modifyState(row, type)
          .then((res) => {
            this.$dispatch('success', '发票管理', this.data, res)
          }).catch((error) => {
          this.$dispatch('error', '发票管理', this.data, error)
        })
      },
      updateNumber (row) {
        this.$BillService.updateNumber(row)
          .then((res) => {
            this.$dispatch('success', '发票管理', this.data, res)
          }).catch((error) => {
          this.$dispatch('error', '发票管理', this.data, error)
        })
      }
    },
    watch: {
      'row' (val) {
        this.xiugai = true
      }
    }
  }
</script>
