<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" partial>
         <div class="form-group">
           <v-select id="f_whether"
           v-model="$parent.$parent.f_whether"
           placeholder='用户状态'
           :value.sync="$parent.$parent.f_whether"
           :options='$parent.$parent.whether'
           close-on-select >
         </v-select>
         </div>
         <div class="form-group">
           <input  type="number" class="form-control" v-model="model.f_userinfo_id" placeholder='用户编号'
           condition="f_userinfo_id = '{}'" v-next-el='yhxm'
           :size="model.f_userinfo_id ? model.f_userinfo_id.length  : 4">
         </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_area" placeholder='区域'
           condition="f_area like '%{}%'" v-el:xq v-next-el='cx' v-el:resentialarea
           :size="model.f_area ? model.f_area.length : 2">
         </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">
           <button class="btn btn-success" @click="search()" v-el:cx>查询</button>
           <button class="btn btn-success" @click="$parent.$parent.create()"
           v-if="$parent.$parent.userdefinedMessage">生成短信</button>
           <button class="btn btn-success" @click="$parent.$parent.defined()">自定义</button>
           <!-- <export-excel :data="{orderitem: 'f_userinfo_id',condition: condition}" sqlurl="rs/excel/report/getArrearsInfo/欠费记录导出Excel模板"></export-excel> -->
         </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>
          </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_id}}</td>
          <td style="text-align:center">{{row.f_user_name}}</td>
          <td style="text-align:center">{{row.f_user_phone}}</td>
          <td style="text-align:center">{{row.f_area}}</td>
          <td style="text-align:center">{{row.f_address}}</td>
        </template>
      </data-grid>
    </criteria-paged>
    <user-defined-message :show="definedShow" v-ref:printbill v-on:toggle="close"></user-defined-message>
  </div>
</template>

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

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: [],
      whether: [{ label: '欠费', value: '欠费' },
                { label: '全部', value: '全部' }],
      f_whether: '全部'
    }
  },
  props: {
    orderitem: {
      type: String,
      default: 'f_userinfo_id'
    },
    tablename: {
      type: String,
      default: 't_userinfo'
    },
    items: {
      type: String,
      default: '*'
    }
  },
  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: {
    create () {
      if (this.ids.length !== 0) {
        this.model.condition = this.model.condition + 'and f_userinfo_id in (' + this.ids + ')'
      }
      this.$dispatch('create', this.model.condition, this.userdefinedMessage)
    },
    search (args) {
      // console.log('1111111111111111111111111111111111111111111111111', args);
      // 如果隶属总公司，不加任何判断
      args.condition = `${args.condition} and f_user_state = '正常'`
      if (!this.$login.f.parent.parent.name.includes('统一资源管理')) {
        args.condition = `${args.condition} and f_filialeids = '${this.$login.f.f_orgids}'`
      }
      this.model.search(args.condition, args.model)
    },
    defined () {
      this.definedShow = true
    },
    close () {
      this.definedShow = 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
          })
        })
      }
    }
  },
  events: {
    'definedMessage' (val) {
      this.userdefinedMessage = val
      this.definedShow = false
      this.create()
    }
  }
}
</script>
