<template>
  <div class="auto" >
    <criteria-paged :model="model" v-ref:paged>
      <criteria partial='criteria' @condition-changed='$parent.search' v-ref:cri>
       <div novalidate class="form-inline" partial>
         <div class="form-group">
           <input  type="number" class="form-control" v-model="model.f_userinfo_id" placeholder='户编号'
                   condition="f_userinfo_id like '%{}%'" v-next-el='yhxm'
                   :size="model.f_userinfo_id ? model.f_userinfo_id.length  : 4">
         </div>
         <div class="form-group">
           <input  type="number" class="form-control" v-model="model.f_user_id" placeholder='表编号'
           condition="f_user_id like '%{}%'" v-next-el='yhxm'
           :size="model.f_user_id ? model.f_user_id.length  : 4">
         </div>
         <div class="form-group">
           <input  type="text" class="form-control" v-model="model.f_meterbook_id" placeholder='表册号'
                   condition="f_meterbook_id like '%{}%'"
                   :size="model.f_meterbook_id ? model.f_meterbook_id.length : 2">
         </div>
         <div class="form-group">
           <input  type="text" class="form-control" v-model="model.f_user_name" placeholder='用户姓名'
           condition="f_user_name like '%{}%'" v-next-el='resentialarea' v-el:yhxm
           :size="model.f_user_name ? model.f_user_name.length  : 4">
         </div>
         <div class="form-group">
           <input  type="text" class="form-control" v-model="model.f_address" placeholder='用户地址'
           condition="f_address like '%{}%'" v-next-el='cx' v-el:resentialarea
           :size="model.f_address ? model.f_address.length * 2 : 4">
         </div>
         <div class="form-group">
           <input  type="text" class="form-control" v-model="model.f_meterbook_id" placeholder='表册号'
             condition="f_meterbook_id like '%{}%'"
             :size="model.f_meterbook_id ? model.f_meterbook_id.length : 2">
         </div>
         <div class="form-group">
           <input  type="text" class="form-control" v-model="model.f_meter_type" placeholder='总表/子表'
                   condition="f_meter_type like '%{}%'"
                   :size="model.f_meter_type ? model.f_meter_type.length : 2">
         </div>
         <div class="form-group">
           <button class="btn btn-success" @click="search()" v-el:cx>查询</button>
         </div>
       </div>
      </criteria>
      <data-grid  :model="model" partial='list' v-ref:grid>
        <template partial='head'>
          <tr>
            <th>
              <input type="checkbox"  @click="$parent.$parent.$parent.isAll(model.pageIndex)">
            </th>
            <th>序号</th>
            <th>用户姓名</th>
            <th>户编号</th>
            <th>表编号</th>
            <th>表册号</th>
            <th>表品牌</th>
            <th>表型号</th>
            <th>是否总表</th>
          </tr>
        </template>
        <template partial='body'>
          <td style="text-align:center">
            <input type="checkbox" v-model="$parent.$parent.$parent.ids"
            :value="row.f_user_id">
          </td>
          <td>{{$index+1}}</td>
          <td style="text-align:center">{{row.f_user_name}}</td>
          <td style="text-align:center">{{row.f_userinfo_id}}</td>
          <td style="text-align:center">{{row.f_user_id}}</td>
          <td style="text-align:center">{{row.f_meterbook_id}}</td>
          <td style="text-align:center">{{row.f_meter_brand}}</td>
          <td style="text-align:center">{{row.f_meter_style}}</td>
          <td style="text-align:center">{{row.f_meter_type}}</td>
        </template>
      </data-grid>
    </criteria-paged>
  </div>
</template>

<script>
/**
*阶梯水价查询列表
*/
import { PagedList } from 'vue-client'
import * as Util from '../../Util'
export default {
  data () {
    return {
      model: new PagedList('rs/sql/singleTable_OrderBy', 50, {orderitem: '"' + this.orderitem + '"',
        items: '"' + this.items + '"',
        tablename: '"' + this.tablename + '"'}),
      definedShow: false,
      userdefinedMessage: '',
      ids: [],
      pages: []
    }
  },
  props: {
    orderitem: {
      type: String,
      default: 'f_userinfo_id'
    },
    tablename: {
      type: String,
      default: 'V_user_table'
    },
    items: {
      type: String,
      default: '*'
    }
  },
  ready () {
    this.search({condition: '1=1', model: this.model})
  },
  methods: {
    search (args) {
      args.condition = `${args.condition} and f_meter_id is null `
      if (!Util.f.parent.parent.name.includes('统一资源管理')) {
        args.condition = `${args.condition} and f_filiale = '${Util.f.f_fengongsi}'`
      }
      this.model.search(args.condition, args.model)
    },
    isAll (page) {
      if (this.pages.includes(page)) { // 已添加,取消动作
        // 从ids中删除本页所有数据的ids
        this.addOrRemove(false)
        this.pages = this.pages.filter((item) => {
          return item !== page
        })
      } else { // 未添加
        this.pages.push(page) // 添加id，添加动作
        this.addOrRemove(true)
      }
    },
    addOrRemove (is) {
      let that = this
      if (is) { // 添加本页所有数据到ids中
        that.model.rows.forEach((row) => {
          if (!that.ids.includes(row.f_userinfo_id)) {
            that.ids.push(row.f_userinfo_id)
          }
        })
      } else { // 从ids中取消所有的数据
        this.model.rows.forEach((row) => {
          that.ids = that.ids.filter((item) => {
            return item !== row.f_userinfo_id
          })
        })
      }
    }
  }
}
</script>
