<template>
  <div id="unit" class="flex-row">
    <div class="basic-main" @keyup.enter="search">
      <div class="flex">
        <criteria-paged :model="model" v-ref:paged  @sort="sort">
          <criteria partial='criteria' @condition-changed='$parent.selfSearch' v-ref:cri >
            <div novalidate class="form-horizontal select-overspread container-fluid auto" partial>
              <div class="row">
                <div class="col-sm-2 form-group">
                  <label for="startDate" class="font_normal_body">开始日期</label>
                  <datepicker id="startDate" placeholder="开始日期" style="width:60%"
                              v-model="model.startDate"
                              :value.sync="model.startDate"
                              :format="'yyyy-MM-dd HH:mm:ss'"
                              :show-reset-button="true"
                              condition="logindate >= '{}'">
                  </datepicker>
                </div>
                <div class="col-sm-2 form-group">
                  <label for="endDate" class="font_normal_body">结束日期</label>
                  <datepicker id="endDate" placeholder="结束日期" style="width:60%"
                              v-model="model.endDate"
                              :value.sync="model.endDate"
                              :format="'yyyy-MM-dd HH:mm:ss'"
                              :show-reset-button="true"
                              condition="logindate <= '{}'">
                  </datepicker>
                </div>
                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">登录人编号</label>
                  <input type="text" style="width:60%" class="input_search" v-model="model.userid"
                         condition="userid = '{}' " placeholder="登录人编号">
                </div>
                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">登录人名称</label>
                  <input type="text" style="width:60%" class="input_search" v-model="model.username"
                         condition="username like '%{}%'" placeholder="客户名称">
                </div>
                <div class="span" style="float:right;">
                  <button class="button_search button_spacing" @click="search()">查询</button>
                  <button class="button_clear  button_spacing" @click="$parent.$parent.clear()">清空</button>

                  <export-excel :data="$parent.$parent.getCondition" :footer="$parent.$parent.footer"
                                :field="$parent.$parent.getfield" :header="$parent.$parent.other"
                                :defaultselect="$parent.$parent.defaultexport"
                                sqlurl="api/af-revenue/logic/openapi/exportfile" sql-name="manage_singleTable" template-name='登录信息查询导出'
                                :choose-col="true">
                  </export-excel>
                  <div style="float: right"  class="button_spacing" :class="{'button_shrink_top':$parent.$parent.criteriaShow,'button_shrink_bottom':!$parent.$parent.criteriaShow}"    @click="$parent.$parent.hidden()"></div>
                </div>
              </div>
              <div class="row" v-show="$parent.$parent.criteriaShow">
                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">类&emsp;&emsp;型</label>
                  <v-select :value.sync="model.type"
                            :options='$parent.$parent.logintype' placeholder='请选择' v-model="model.type"
                            condition="type = '{}'"
                            :value-single="true"
                            close-on-select>
                  </v-select>
                </div>
              </div>
            </div>
          </criteria>

          <data-grid :model="model" partial='list' class="list_area table_sy" style="height: 95%" v-ref:grid>
            <template partial='head'>
              <tr>
                <th>
                  <data-order field="userid" name="登录人编号"
                              :order.sync="$parent.$parent.$parent.orderFields.userid">
                  </data-order>
                </th>
                <th>
                  <nobr>登录人名称</nobr>
                </th>
                <th>
                  <nobr>登录时间</nobr>
                </th>
                <th>
                  <nobr>类型</nobr>
                </th>
              </tr>
            </template>
            <template partial='body'>
              <td style="text-align: center;">
                <nobr>{{row.userid}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.username}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.logindate}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.type}}</nobr>
              </td>
            </template>
            <template partial='foot'>
            </template>
          </data-grid>
        </criteria-paged>
      </div>
    </div>
  </div>
</template>

<script>
  import {HttpResetClass, PagedList} from 'vue-client'
  import exportConfig from '../config/exportConfig'
    export default {
    title: '登录信息查询',
     data() {
      return {
        other:[],
        footer:[],
        config: {
          defaultPrint: ['userid', 'username', 'logindate']
        },
        //排序
        orderitem:'logindate desc',
        orderFields: {
          logindate: ''
        },
        data: {},
        rowdata:{},
        initres: {
          org:[this.$login.f.orgid],
          dep:[],
          user:[]
        },
        model: new PagedList('api/af-revenue/sql/manage_singleTable', 50, {items: '"*"', tablename: '"t_loginrecord"', orderitem: this.orderitem?'"' + this.orderitem + ' "':`'logindate desc'`}),
        criteriaShow: false,
        orgCondtionStr: '',
        defaultfield: [],
        sumsmodel: {},
        logintype:[
          {label: '登入', value: '登入'},
          {label: '登出', value: '登出'}
        ],
        showbottomsum:false  //页面下方是否有合计: 默认为false, 在watch修改
      }
    },
    ready() {
      this.$refs.paged.$refs.cri.model.type = '登入'
    },
    methods: {
      getotherfooter () {
        this.other = [];
        this.footer = [];
        let exportdata = this.getCondition;
        let otherInData = [];
        otherInData.push(`导出时间: ${this.$login.toStandardTimeString()}`);
        let footerData = [], exportfield = this.getfield;
        footerData.push("合计");
        let self = this;
        for (var field in self.sumsmodel) {
          footerData.push(`${exportfield[field]}合计:${self.sumsmodel[field]}`);
        }
        this.footer.push(footerData);
        this.other.push(otherInData);
      },
      //把数据库查询数据转换为下拉数据
      search(){
        this.$refs.paged.$refs.cri.search()
      },
      selfSearch(args) {
        let f_orgstr = this.orgCondtionStr
        args.condition = `${args.condition} ` + f_orgstr
        this.model.search(args.condition, args.model)
      },
      clear() {
        Object.keys(this.$refs.paged.$refs.cri.model).forEach((key) => {
          this.$refs.paged.$refs.cri.model[key] = []
        })

      },
      show() {
        this.criteriaShow = true
      },
      hidden() {
        this.criteriaShow = !this.criteriaShow
      },
      sort (field, rule) {
        // 将所有排序方式设为不排序，实现相互排斥
        for (let key in this.orderFields) {
          if (key === field) {
            this.orderFields[key] = rule
          } else {
            this.orderFields[key] = 'no'
          }
        }
        // 如果新规则不排序，还原为默认排序
        if (rule === 'no') {
          this.model.paramSource.orderitem = `'${this.orderitem}'`
        } else {
          this.model.paramSource.orderitem = `'${field} ${rule}'`
        }

        this.search()
      }

    },
    events:{
    },
    watch: {
      'model.rows'(val) {
        //如果有合计 这里为true, 其他为false ,反正相反
        this.showbottomsum=false;
        if(val.length === 0){
          this.sumsmodel = {}
          this.showbottomsum=false;
        }
      },
      sumsmodel:{
        handler: function(val) {
          this.getotherfooter();
        },
        deep: true
      }
    },
    computed: {
      chooserow(){
        let aaa= true
        if(this.$login.r && this.$login.r.includes('打印项选择')){
           aaa =  false
        }
        return aaa
      },
      getCondition() {
        return {
          condition: `${this.$refs.paged.$refs.cri.condition}` + this.orgCondtionStr,
          orderitem: this.orderitem?`${this.orderitem}`: " logindate desc",
          tablename: 't_loginrecord'
        }
      },
      getfield() {
        return exportConfig.loginQueryConfig
      },
      defaultexport() {
        return exportConfig.loginQueryexportConfig
      }
    }
  }
</script>
