<template>
  <div id='app' class="flex">
    <criteria-paged :model="model" :pager='true' v-ref:paged>
      <criteria partial='criteria' @condition-changed='search' v-ref:cri>
        <div novalidate class="form-inline auto " partial >
          <p class="bg-info text-center" style="padding: 8px;">异常统计报表</p>
          <div class="flex-row auto" style="justify-content: space-between">
            <div class="form-group" >
            <div class="form-group">
              <input  type="text" class="form-control" v-model="model.f_username" placeholder='用户姓名'
                      condition="u.f_username = '{}'" v-next-el='userid' v-el:username
                      :size="model.f_username ? model.f_username.length * 2 : 4">
            </div>
            <div class="form-group">
              <v-select id="f_user_type"
                        v-model="model.f_usertype"
                        placeholder='用户类型'
                        :value.sync="model.f_usertype"
                        :options='$parent.$parent.usertypes'
                        condition="u.f_user_type = '{}'"
                        close-on-select >
              </v-select>
            </div>
            <div class="form-group">
              <input type="text" class="form-control" v-model="model.f_districtname"
                     condition="u.f_districtname like '%{}%'" placeholder='小区'
                     :size="model.f_districtname ? model.f_districtname.length * 2 : 4">
            </div>
            <div class="form-group">
              <v-select id="f_meter_type"
                        v-model="model.f_metertype"
                        placeholder='表具类型'
                        :value.sync="model.f_metertype"
                        :options='$parent.$parent.metertypes'
                        condition="m.f_metertype = '{}'"
                        close-on-select >
              </v-select>
            <div class="form-group">
              <v-select v-model="model.f_meterstate"
                        placeholder="异常状态"
                        :value.sync="model.f_meterstate"
                        :options='$parent.$parent.abnormalstatus'
                        condition="m.f_meterstate = '%{}%'"
                        close-on-select >
              </v-select>
            </div>
              <button class="btn btn-success" @click="search()">查询</button>
              <button class="btn btn-success" @click="$parent.$parent.confirmReport()">保存报表</button>
            </div>
            <span></span>
            <div class="form-group">
              <button class="btn btn-default" v-if="!$parent.$parent.echarts" @click="$parent.$parent.showEcharts()">显示图表</button>
              <button class="btn btn-default" v-if="$parent.$parent.echarts" @click="$parent.$parent.showEcharts()">隐藏图表</button>
              <print-table :model='$parent.model'
                           :top='40' :left='60' :right='40' :bottom='60'
                           :thead='"<th>序号</th>"+
                          "<th>用户姓名</th>" +
                            " <th>表号</th>"+
                              "<th>用户类型</th>"+
                             "<th>表具类型</th>"+
                                "<th>剩余金额</th>"+
                                "<th>档案编号</th>"+
                                  "<th>异常类型</th>"+
                                  "<th>小区</th>"+
                                  "<th>地址</th>"+
                                   "<th>所属机构</th>"'
                           :fields='["$index+1","f_username","f_meternumber","f_usertype","f_metertype","f_has_money","f_userfileid","f_meterstate","f_districtname","f_address","yytdepa"]'>
              </print-table>
              <export-excel :data="{orderitem: 'lastinputdate desc', condition: '1=1'}"
                            :field="{f_username: '用户姓名', f_meternumber: '表号',f_usertype:'用户类型',f_metertype:'表具类型',f_has_money:'剩余金额',
                            f_userfileid:'档案编号',f_meterstate:'异常类型',f_districtname:'小区', f_address: '地址',yytdepa:'所属机构'}"
                            sqlurl="rs/sql/getAbnormal"></export-excel>
            </div>
          </div>
        </div>
        </div>
      </criteria>
      <data-grid :model="model" partial='list' v-ref:grid>
        <template partial='head'>
          <tr>
            <th  style="text-align:center">序号</th>
            <th  style="text-align:center">用户姓名</th>
            <th  style="text-align:center">表号</th>
            <th  style="text-align:center">用户类型</th>
            <th  style="text-align:center">表具类型</th>
            <th  style="text-align:center">剩余金额</th>
            <th  style="text-align:center">档案编号</th>
            <th  style="text-align:center">异常类型</th>
            <th  style="text-align:center">地址</th>
            <th  style="text-align:center">所属机构</th>
          </tr>
        </template>
        <template partial='body'>
          <td style="text-align:center">{{$index+1}}</td>
          <td style="text-align:center">{{row.f_username}}</td>
          <td style="text-align:center">{{row.f_meternumber}}</td>
          <td style="text-align:center">{{row.f_usertype}}</td>
          <td style="text-align:center">{{row.f_metertype}}</td>
          <td style="text-align:center">{{row.f_hasmoney}}</td>
          <td style="text-align:center">{{row.f_userfileid}}</td>
          <td style="text-align:center">{{row.f_meterstate}}</td>
          <td style="text-align:center">{{row.f_address}}</td>
          <td style="text-align:center">{{row.yytdepa}}</td>
        </template>
      </data-grid>
    </criteria-paged>
    <div class="span" v-show="echarts">
      <abnormal-info-echarts :condition="condition"></abnormal-info-echarts>
    </div>

    <modal :show.sync="show" v-ref:modal small backdrop="false">
      <header slot="modal-header" class="modal-header">
        <h4 class="modal-title">输入文件名称</h4>
      </header>
      <article slot="modal-body" class="modal-body">
        <div class="form-group">
          <input  type="text" class="form-control" v-model="filename" placeholder='保存的文件名'>
        </div>
      </article>
      <footer slot="modal-footer" class="modal-footer">
        <button v-show="show" type="button" class="btn btn-default" @click='close'>取消</button>
        <button v-show="show" type="button" class="btn btn-success" @click='confirm(filename)'>确认</button>
      </footer>
    </modal>
  </div>
</template>

<script>
  import { DataModel, HttpResetClass } from 'vue-client'
  import * as Util from '../../Util'
  import AppData from '../../stores/AppData'
  import co from 'co'
  import ValueEcharts from './ValueEcharts'
  import AbnormalInfoEcharts from './AbnormalInfoEcharts'
  import { PagedList } from 'vue-client'

  export default {
    title: '异常统计报表',
    props: ['data'],
    components: {AbnormalInfoEcharts},
    data () {
      return {
        model: new PagedList('rs/sql/getAbnormal', 20),
        reportStr: null,
        show: false,
        outlets: [],
        operator: [],
        filename: '',
        echarts: true,
        condition: '1=1'
      }
    },
    ready () {
      this.search()
    },
    methods: {
      search () {
        this.$refs.paged.$refs.cri.search()
        this.condition = this.$refs.paged.$refs.cri.condition
      },
      showEcharts () {
        this.echarts = !this.echarts
      },
      close () {
        this.show = false
      },
      // 将报表保存成文件
      confirmReport () {
        this.show = true
        this.filename = this.data.f_report_name + Util.toStandardDateString()
      }
    },
    watch: {
      'data' (val) {
        if (val.f_files_path) {
          this.getFileContent(val.f_files_path)
        } else {
          this.reportStr = null
        }
      }
    },
    computed: {
      usertypes () {
        return this.$getParams('用户类型', [{label: '全部', value: ''}])
      },
      f_outlets () {
        return this.$getParams('网点查询用', [{label: '全部', value: ''}])
      },
      metertypes(){
        return this.$getParams('气表型号', [{label: '全部', value: ''}])
      },
      abnormalstatus(){
        return this.$getParams('异常状态用', [{label: '全部', value: ''}])
      }
    }
  }
</script>
