<template>
  <div class="basic-main" style="height: 100%">
    <div class="form-group" >
      <div novalidate class="form-horizontal select-overspread container-fluid auto" partial >
        <div class="row">
          <div class="col-sm-3 form-group" >
            <label class="font_normal_body" for="startDate">开始日期:</label>
            <datepicker id="startDate" placeholder="开始日期" style="width: 60%"
                        v-model="model.startDate"
                        :value.sync="model.startDate"
                        :disabled-days-of-Week="[]"
                        :format="'yyyy-MM-dd HH:mm:ss'"
                        :show-reset-button="reset">
            </datepicker>
          </div>
          <div class="col-sm-3 form-group" >
            <label  class="font_normal_body" for="endDate">结束日期:</label>
            <datepicker id="endDate" placeholder="结束日期" style="width: 60%"
                        v-model="model.endDate"
                        :value.sync="model.endDate"
                        :disabled-days-of-Week="[]"
                        :format="'yyyy-MM-dd HH:mm:ss'"
                        :show-reset-button="reset">
            </datepicker>
          </div>
          <div class="col-sm-3 form-group" >
            <label class="font_normal_body" for="org">公&emsp;&emsp;司:</label>
            <right-tree id="org" @re-res="getRes"></right-tree>
          </div>
          <div class="col-sm-3 form-group">
            <label class="font_normal_body" for="dep" >部&emsp;&emsp;门:</label>
            <res-select id="dep" restype='department'
                        @res-select="getdep"
                        :parentresid="depresid"
                        :initresid='queryData.depid' >
            </res-select>

          </div>
          <div class="col-sm-3 form-group">
            <label class="font_normal_body" for="oper">人&emsp;&emsp;员:</label>
            <res-select id="oper" restype='user'
                        is-mul="false"
                        @res-select="getuser"
                        :parentresid="userresid"
                        :initresid='queryData.operatorid'>
            </res-select>
          </div>
          <div class="col-sm-3 form-group">
              <label class="font_normal_body">用户类型</label>
              <v-select :value.sync="queryData.f_user_type"
                        v-model="queryData.f_user_type"
                        :options='user_type' placeholder='请选择'
                        close-on-select :value-single="true"></v-select>
          </div>
          <div class="col-sm-2 form-group" style="float:right;">
            <button class="button_search" @click="searchData()">查询</button>
            <div style="float: right"  class="button_spacing" :class="{'button_shrink_top':criteriaShow,
                  'button_shrink_bottom':!criteriaShow}"  @click="hidden()"></div>
          </div>
        </div>
      </div>
      <div style="width:100%;height: 100%;margin-top: 2%" class="div-iframe">
        <iframe class="iframe_style" style="width:100%;height:100%" :src="url" v-if="isShow"></iframe>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  title: '新收费汇总报表',
  data () {
    return {
      orgCondtionStr: '1=1',
      model: {
        startDate: '',
        endDate: '',
        f_orgid: []
      },
      isShow: false,
      url: '',
      urlPrefix: '/report/jmreport/shareView/783966412390916096',
      queryData: {
        f_user_type: '',
        f_depid: [],
        f_operatorid: []
      },
      orgname: '',
      depresid: [],
      userresid: [],
      depname: '',
      operatorname: '',
      operatorid: [],
      depid: [],
      criteriaShow: false
    }
  },
  ready () {
    this.model.startDate = this.$login.toStandardDateString() + ' 00:00:00'
    this.model.endDate = this.$login.toStandardDateString() + ' 23:59:59'
    this.searchData()
  },
  methods: {
    searchData () {
      let queryData = ''
      queryData = this.orgCondtionStr
      let f_orgid = ''
      f_orgid = this.orgCondtionStr
      if (this.model.f_orgid.length > 0) {
        f_orgid = f_orgid + ' and f_orgid in (' + "'" + this.model.f_orgid + "'" + ')'
      }
      console.log(f_orgid)
      for (let key in this.queryData) {
        if (this.queryData[key] !== null && this.queryData[key] !== '' && this.queryData[key] !== undefined && this.queryData[key].length > 0) {
          if (Array.isArray(this.queryData[key])) {
            let stringValue = ''
            for (var i = 0; i < this.queryData[key].length; i++) {
              if (i === this.queryData[key].length - 1) {
                stringValue = stringValue + "'" + this.queryData[key][i] + "'"
              } else {
                stringValue = stringValue + "'" + this.queryData[key][i] + "',"
              }
            }
            queryData = queryData + ' and ' + key + ' in (' + stringValue + ')'
          } else {
            queryData = queryData + ' and ' + key + ' = ' + "'" + this.queryData[key] + "'"
          }
        }
      }
      console.log(queryData)
      // 重置url
      this.url = ''
      // encodeURIComponent是将该参数中的特殊符号进行编码，后端会进行解码
      this.url = this.urlPrefix + '?condition=' + encodeURIComponent(queryData) +
        '&startDate=' + encodeURIComponent(this.model.startDate) +
        '&f_orgid=' + encodeURIComponent(f_orgid) +
        '&endDate=' + encodeURIComponent(this.model.endDate)
      console.log('报表===' + queryData)
      console.log('报表===>' + this.url)
      this.isShow = true
    },
    load () {
      this.isShow = true
    },
    getRes (obj) {
      this.orgname = obj.res[0]
      this.depresid = obj.resids
      this.userresid = obj.resids
      this.model.f_orgid = obj.resids
    },
    getdep (obj, val) {
      this.depname = val[0]
      this.userresid = obj
      this.queryData.f_depid = obj
    },
    getuser (obj, val) {
      this.operatorname = val[0]
      this.queryData.f_operatorid = obj
    },
    hidden () {
      this.criteriaShow = !this.criteriaShow
    }
  },
  computed: {
    user_type () {
      return [{label: '全部', value: ''}, ...this.$appdata.getParam('用户类型')]
    }
  }
}
</script>
<style scoped>
.noborder{
  border: none;
}
.span{
  height: 0;
}
.iframe_style{
  border: medium none;
}
</style>
