<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=""
           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="text" class="form-control" v-model="model.f_userinfo_id" placeholder='用户编号'
           condition="u.f_userinfo_id = '{}'" v-next-el='username'
           :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="u.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 class="form-group">
           <input  type="text" class="form-control" v-model="model.f_area" placeholder='区域'
           condition="u.f_area like '%{}%'" v-next-el='address' v-el:area
           :size="model.f_area ? model.f_area.length  : 4">
         </div>
          <div class="form-group">
           <v-select :value.sync="model.f_meter_classify" v-model="model.f_meter_classify"
               :options='$parent.$parent.meters' placeholder='气表类型'
               close-on-select
               condition="f_meter_classify = '{}'"
               default-value="'机表'"></v-select>
         </div>
         <div class="form-group">
           <input  type="text" class="form-control" v-model="model.f_address" placeholder='用户地址'
           condition="u.f_address like '%{}%'" v-next-el='cx' v-el:address
           :size="model.f_address ? model.f_address.length  : 4">
         </div>
         <div class="form-group">
             <v-input :value.sync="$parent.f_oughtfee" :expression.sync = 'model.f_oughtfee'
              condition="f_oughtfee {}" v-model="model.f_oughtfee" v-ref:oughtfee
              placeholder="应交金额" v-next-el="cx" v-el:yjje></v-input>
         </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()">生成短信</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_userinfo_id">
          </td>
          <td>{{$index+1}}</td>
          <td style="text-align:center">{{row.f_userinfo_id}}&nbsp;&nbsp;{{row.f_user_name}}&nbsp;&nbsp;{{row.f_user_phone}}</td>
          <td style="text-align:center">{{row.f_last_tablebase.toFixed(2)}}</td>
          <td style="text-align:center">{{row.f_tablebase.toFixed(2)}}</td>
          <td style="text-align:center">{{row.f_oughtamount.toFixed(2)}}</td>
          <td style="text-align:center">{{row.f_oughtfee.toFixed(2)}}</td>
          <td style="text-align:center">{{row.f_balance.toFixed(2)}}</td>
          <td style="text-align:center">{{row.qianfei.toFixed(2)}}</td>
        </template>
      </data-grid>
    </criteria-paged>
  </div>
</template>

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

export default {
  data () {
    return {
      model: new PagedList('rs/sql/getArrearsInfo', 50, {orderitem: '"' + this.orderitem + '"'}),
      definedShow: false,
      userdefinedMessage: '',
      ids: [],
      pages: [],
      whether: [{ label: '欠费', value: '欠费' },
                { label: '全部', value: '全部' }],
      f_whether: '欠费',
      meters: [
        {label: '全部', value: ''},
        {label: '机表', value: '机表'},
        {label: '远传表', value: '远传表'}
      ],
      filialeName: [],
      outletsname: [],
      filiale: '',
      filialetype: ''
    }
  },
  props: {
    orderitem: {
      type: String,
      default: 'p.f_userinfo_id'
    }
  },
  ready () {
    this.getLoginFiliale(this.$login.f.parent)
    this.getAllFiliale(this.filiale, '分公司')
    this.getAllFiliale(this.filiale, '营业厅')
    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: {
    getLoginFiliale (val) {
      if (val.orgtype === '总公司' || val.orgtype === '分公司') {
        this.filiale = val.name
        this.filialetype = val.orgtype
        console.log(val.name)
      } else {
        this.getLoginFiliale(val.parent)
      }
    },
    getAllFiliale (data, type) {
      if (data === undefined || data === '') {
        return
      }
      let val = {source: `tool.getChildrenOfResName($${data}$)`, userid: ''}
      let http = new HttpResetClass()
      http.load('POST', '/rs/search', {data: val}, {resolveMsg: null, rejectMsg: null}).then((res) => {
        res.data.forEach((item) => {
          if (item.orgtype === type) {
            if (type === '分公司') {
              this.filialeName.push({label: item.name, value: item.name})
            }
            if (type === '营业厅') {
              this.outletsname.push({label: item.name, value: item.name})
            }
            this.getAllFiliale(item.name, type)
          }
        })
      })
    },
    cleanOutlet () {
      this.outletsname = []
    },
    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) {
      args.condition = this.filialetype === '总公司' ? args.condition : `${args.condition} and u.f_filiale = ` + this.filiale
      if (args.model.f_meter_brand && args.model.f_meter_brand.length > 0) {
        if (args.model.f_meter_brand[0].f_meter_brand) {
          args.condition = `${args.condition} and f_meter_brand = '${args.model.f_meter_brand[0].f_meter_brand}'`
        }
      }
      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
          })
        })
      }
    }
  },
  computed: {
    filiales () {
      let condition = this.filialetype === '总公司' ? '' : this.filiale
      return [{label: '全部', value: condition}, ...this.filialeName]
    },
    outlets () {
      return [{label: '全部', value: ''}, ...this.outletsname]
    }
  }
}
</script>
