<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_state" multiple
                      v-model="queryData.f_state"
                      :options='charge_state' placeholder='请选择'
                      close-on-select></v-select>
          </div>
          <div class="col-sm-3 form-group">
            <button class="button_search" @click="searchData()">查询</button>
          </div>
        </div>
      </div>
      <div style="width:100%;height: 100%;margin-top: 2%;text-align: center" class="div-iframe">
        <div style="width: 100%;height: 100%;display: inline-block" >
          <iframe id="iframe" class="iframe_style" style="width:100%;height:100%" :src="url" v-show="isShow"></iframe>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  title: '新营业厅分项汇总表',
  data () {
    return {
      orgCondtionStr: 'and 1=1',
      model: {
        startDate: '',
        endDate: ''
      },
      isShow: false,
      url: '',
      urlPrefix: '/report/jmreport/shareView/779255398271029248',
      queryData: {
        f_state: ['有效'],
        f_orgid: [],
        f_depid: [],
        f_operatorid: []
      },
      orgname: '',
      depresid: [],
      userresid: [],
      depname: '',
      operatorname: '',
      operatorid: [],
      depid: []
    }
  },
  ready () {
    this.model.startDate = this.$login.toStandardDateString() + ' 00:00:00'
    this.model.endDate = this.$login.toStandardDateString() + ' 23:59:59'
    this.searchData()
  },
  methods: {
    // iframe () {
    //   let iframe = document.getElementById('iframe')
    //   if (iframe.attachEvent) {
    //     iframe.attachEvent('onload', function () {
    //       console.log('加载完成-----------')
    //     })
    //   } else {
    //     iframe.onload = function () {
    //       console.log('加载完成===========')
    //       // let iframeDoc = iframe.contentDocument
    //       // iframeDoc.body.style.backgroundColor = 'blue'
    //       // console.log(iframeDoc.body.style.backgroundColor + '------------------------------')
    //       var y = (iframe.contentWindow || iframe.contentDocument)
    //       if (y.document)y = y.document
    //       console.log(y.body.style.backgroundColor)
    //     }
    //   }
    // },
    searchData () {
      let queryData = ''
      queryData = this.orgCondtionStr
      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] + "'"
          }
        }
      }
      // 重置url
      this.url = ''
      // encodeURIComponent是将该参数中的特殊符号进行编码，后端会进行解码
      this.url = this.urlPrefix + '?f_orgid=' + encodeURIComponent(queryData) +
          '&startDate=' + encodeURIComponent(this.model.startDate) +
          '&endDate=' + encodeURIComponent(this.model.endDate)
      console.log('报表===' + queryData)
      console.log('报表===>' + this.url)
      this.isShow = true
      // this.iframe()
    },
    load () {
      this.isShow = true
    },
    getRes (obj) {
      this.orgname = obj.res[0]
      this.depresid = obj.resids
      this.userresid = obj.resids
      this.queryData.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
    }
  },
  computed: {
    charge_state () {
      return [{label: '全部', value: ''}, ...this.$appdata.getParam('收费状态')]
    }
  }
}
</script>
<style scoped>
.iframe_style{
  border: medium none;
}
</style>
