<template>
  <div class="flex" >
    <criteria-paged :model="model" v-ref:paged>
      <criteria partial='criteria' @condition-changed='$parent.search' v-ref:cri>
       <div novalidate class="form-inline auto" partial>
         <div >
           <v-select id=""
           v-model="$parent.$parent.f_whether"
           placeholder='用户状态'
           :value.sync="$parent.$parent.f_whether"
           :options='$parent.$parent.whether'
           close-on-select >
         </v-select>
         </div>
         <div >
           <input  type="text" class="form-control" v-model="model.f_userinfo_code" placeholder='客户编号'
           condition="f_userinfo_code = '{}'" v-next-el='f_userinfo_code'
           :size="model.f_userinfo_code ? model.f_userinfo_code.length  : 4">
         </div>
         <div >
           <input  type="text" class="form-control" v-model="model.f_user_name" placeholder='用户姓名'
           condition="f_user_name like '{}'" v-next-el='area' v-el:username
           :size="model.f_user_name ? model.f_user_name.length * 2 : 4">
         </div>
         <div >
           <input  type="text" class="form-control" v-model="model.f_address" placeholder='用户地址'
           condition="f_address like '%{}%'" v-next-el='cx' v-el:address
           :size="model.f_address ? model.f_address.length  : 4">
         </div>
         <div >
             <v-input :value.sync="$parent.f_balance" :expression.sync = 'model.f_balance'
              condition="f_balance {}" v-model="model.f_balance" v-ref:balance
              placeholder="账户结余" v-next-el="cx" v-el:balance></v-input>
         </div>
         <div >
           <button class="btn btn-success" @click="search()" v-el:cx>查询</button>
           <button class="btn btn-success" @click="$parent.$parent.template()">选择短信模版</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><nobr>序号</nobr></th>
             <th><nobr>用户信息</nobr></th>
             <th><nobr>用户地址</nobr></th>
             <th><nobr>应交气量</nobr></th>
             <th><nobr>应交气费</nobr></th>
             <th><nobr>滞纳金</nobr></th>
             <th><nobr>欠费总额</nobr></th>
             <th><nobr>账户结余</nobr></th>
          </tr>
        </template>
        <template partial='body'>
          <td style="text-align:center">
            <input type="checkbox" v-model="$parent.$parent.$parent.ids"
            :value="row.f_userinfo_id">
          </td>
          <td>{{$index+1}}</td>
          <td style="text-align:center">{{row.f_userinfo_code}}&nbsp;&nbsp;{{row.f_user_name}}&nbsp;&nbsp;{{row.f_user_phone}}</td>
          <td style="text-align:center" title="{{row.f_address}}">{{row.f_address.substring(0,10)}}...</td>
          <td style="text-align:center">{{row.f_oughtamount}}</td>
          <td style="text-align:center">{{row.f_oughtfee}}</td>
          <td style="text-align:center">{{row.overdue}}</td>
          <td style="text-align:center">{{row.qianfei}}</td>
          <td style="text-align:center">{{row.f_balance}}</td>
        </template>
      </data-grid>
    </criteria-paged>
    <template-message :show="templateShow" :condition="model.condition" :ids="ids" v-ref:printbill v-on:toggle="close"></template-message>
  </div>
</template>

<script>
/**
*阶梯水价查询列表
*/
import { PagedList } from 'vue-client'

export default {
  data () {
    return {
      model: new PagedList('rs/sql/getAllOverdueUser', 20),
      definedShow: false,
      templateShow: false,
      userdefinedMessage: '',
      ids: [],
      pages: [],
      whether: [{ label: '欠费', value: '欠费' },
                { label: '全部', value: '全部' }],
      f_whether: '欠费',
      meters: [
        {label: '全部', value: ''},
        {label: '机表', value: '机表'}
      ],
      condition: ''
    }
  },
  ready () {
    this.search({condition: '1=1', model: this.model})
  },
  watch: {
    'f_whether' (val) {
      if (val[0] === '欠费') {
        this.$dispatch('all', '欠费')
      } else if (val[0] === '全部') {
        this.$dispatch('all', '全部')
      }
    }
  },
  methods: {
    search (args) {
      args.condition = `${args.condition} and f_filialeids like '${this.$login.f.f_orgids}%'`
      this.model.search(args.condition, args.model)
    },
    template () {
      this.templateShow = true
    },
    defined () {
      this.definedShow = true
    },
    close () {
      this.templateShow = false
    },
    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中
        this.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>
