<template>
  <div class="basic-main" style="height: 98%">
    <criteria-paged :model="model" :pager='false' v-ref:paged>
      <criteria partial='criteria' @condition-changed='$parent.selfSearch' v-ref:criteria>
        <div novalidate class="form-horizontal select-overspread container-fluid auto" partial >
          <div class="row">
            <div class="col-sm-4" >
              <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-4" >
              <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-4">
              <label class="font_normal_body">&nbsp;&nbsp;&nbsp;公司&nbsp;&nbsp;&nbsp; </label>
              <manage-right-tree @re-res="$parent.$parent.getRes"></manage-right-tree>
            </div>
            <div class="col-sm-4">
              <label class="font_normal_body">&nbsp;&nbsp;&nbsp;部门&nbsp;&nbsp;&nbsp; </label>
              <manage-res-select restype='department'
                          is-mul="false"
                          @res-select="$parent.$parent.getdep"
                          :parentresid="$parent.$parent.depresid"
                          :initresid='$parent.$parent.depid'>
              </manage-res-select>

            </div>
            <div class="col-sm-4">
              <label class="font_normal_body">&nbsp;&nbsp;&nbsp;人员&nbsp;&nbsp;&nbsp; </label>
              <manage-res-select restype='user'
                          is-mul="false"
                          @res-select="$parent.$parent.getuser"
                          :parentresid="$parent.$parent.userresid"
                          :initresid='$parent.$parent.operatorid'>
              </manage-res-select>
            </div>
            <div class="col-sm-3 form-group">
              <label class="font_normal_body" for="oper1">格式一</label>
              <input type="radio" name="reportStyle" id="oper1" v-model="$parent.$parent.reportStyle" value="1">
              <label class="font_normal_body" for="oper2">格式二</label>
              <input type="radio" name="reportStyle" id="oper2"  v-model="$parent.$parent.reportStyle" value="2">
            </div>
          </div>
          <div class="span" style = "float:right;">
            <button class="button_search" @click="$parent.$parent.searchData()">查询</button>
            <report-print id='gasprice' top='3cm' left='0' width='100%' height='100%' :preview="true"></report-print>
            <report-excel id='gasprice'></report-excel>
          </div>
        </div>
      </criteria>
      <div  partial='list' v-el:handcollect id='gasprice' style="overflow-y: scroll">
        <table class='tableprint' style="margin: 0px auto;">
          <thead>
          <tr>
            <th :colspan='$parent.spans' style="font-weight: normal; text-align: left;">
              <h3 style="text-align: center">气价明细报表</h3>
            </th>
          </tr>
          <tr>
            <th :colspan='$parent.spans' style="font-weight: normal; text-align: center;">
              开始时间：{{model.model.startDate}}&nbsp;&nbsp;&nbsp;
              结束时间：{{ model.model.endDate }}&nbsp;&nbsp;<br/>
              打印时间：{{{$parent.printTime}}}
            </th>
          </tr>
          <tr>
            <th :colspan='$parent.spans' style="font-weight: normal; text-align: center;">
              <div>
                <span v-show="$parent.orgname.trim()!=''">公司：{{$parent.orgname}}</span>
                <span v-show="$parent.depname.trim()!=''"> 部门：{{$parent.depname}}</span>
                <span v-show="$parent.operatorname.trim()!=''">人员：{{$parent.operatorname}}</span>
              </div>
            </th>
          </tr>
          </thead>
          <tr>
            <td :colspan='$parent.spans'>
              {{{ model.data.substring(26,model.data.length-8) }}}
            </td>
          </tr>
          <tfoot>
          <tr style="text-align: left">
            <th :colspan='Math.floor($parent.spans/3)'>财务审核：</th>
            <th :colspan='Math.floor($parent.spans/3)'>收款审核：</th>
            <th :colspan='Math.floor($parent.spans/3)'>收款员：</th>
          </tr>
          </tfoot>
        </table>
      </div>
    </criteria-paged>
  </div>
</template>

<script>
import {DataModel, HttpResetClass} from 'vue-client'
  import co from 'co'
  let saveFile = function * (self) {
  }
  export default {
    title: '气价明细报表',
    props: ['data'],
    data () {
      return {
        printTime: this.$login.toStandardTimeString(),
        depresid: [],

        userresid: [],
        f_orgid: this.$login.f.orgid,
        f_depid: this.$login.f.depids,
        f_operatorid: this.$login.f.id,
        operatorid: [],
        depid: [],
        orgname: '',
        depname: '',
        spans: 0,
        reportStyle: 1,
        operatorname: '',
        orgCondtionStr: '1=1',
        model: new DataModel('api/af-revenue/report/gasprice', {startDate: 'this.model.startDate', endDate: 'this.model.endDate',
          f_orgid: 'this.model.f_orgid'}),
        reportStr: null,
        show: false
      }
    },
    ready () {
      this.$refs.paged.$refs.criteria.model.startDate = this.$login.toStandardDateString() + ' 00:00:00'
      this.$refs.paged.$refs.criteria.model.endDate = this.$login.toStandardDateString() + ' 23:59:59'
      console.log(this.$login.f)
    },
    methods: {
      searchData () {
        this.$refs.paged.$refs.criteria.search()
      },
      distinctJson(obj){
        let at = []
        for (let i = 0; i < obj.length; i++) {
          let flag = false
          if (i == 0 ) {
            at.push(obj[i])
          } else {
              for (let j = 0; j < at.length; j++) {
                if (JSON.stringify(obj[i]) == JSON.stringify(at[j])) {
                  flag = true
                }
              }
              if (!flag) {
                at.push(obj[i])
              }
          }
        }
        return at
      },
      selfSearch (args) {
        this.printTime = this.$login.toStandardTimeString()
        let orgcondition = '1=1'
        if (this.f_orgid && this.f_orgid[0]) {
          orgcondition += `  and f_orgid in (${this.f_orgid})`
        }
        if (this.f_depid && this.f_depid[0]) {
          orgcondition += ` and f_depid  in (${this.f_depid})`
        }
        if (this.f_operatorid && this.f_operatorid[0]) {
          orgcondition += ` and f_operatorid  in (${this.f_operatorid})`
        }
        this.$refs.paged.$refs.criteria.model.f_orgid = orgcondition
        if (this.reportStyle == '1') {
          let load = new HttpResetClass()
          load.load("POST", 'api/af-revenue/sql/getGasPriceDetail',
            {data: {
                startDate: args.model.startDate,
                endDate: args.model.endDate,
                f_orgid: orgcondition
              }},
            {resolveMsg: null, rejectMsg: null})
            .then((res) => {
              let info = res.data
              let gasproperties = info.map(item => {return {f_gasproperties: item.f_gasproperties}})
              let district = info.map(item => {
                return { f_gasproperties: item.f_gasproperties, f_district: item.f_district }
              })
              let meter_type = info.map(item => {
                return { f_gasproperties: item.f_gasproperties, f_district: item.f_district, f_meter_type: item.f_meter_type }
              })
              let price_name = info.map(item => {
                return {
                  f_gasproperties: item.f_gasproperties,
                  f_district: item.f_district,
                  f_meter_type: item.f_meter_type,
                  f_price_name: item.f_price_name
                }
              })
              this.model.url = 'api/af-revenue/report/gasprice'
              this.$refs.paged.$refs.criteria.model.f_orgid = {
                left1: JSON.stringify(this.distinctJson(gasproperties)),
                left2: JSON.stringify(this.distinctJson(district)),
                left3: JSON.stringify(this.distinctJson(meter_type)),
                left4: JSON.stringify(this.distinctJson(price_name)),
                infoall: JSON.stringify(info)
              }
              this.$refs.paged.search(args)
            })
        } else {
          this.model.url = 'api/af-revenue/report/gaspricetwo'
          this.$refs.paged.search(args)
        }

      },
      getRes (obj) {
        this.orgname = obj.res[0]
        this.depresid = obj.resids
        this.f_orgid = obj.resids
      },
      getdep (obj, val) {
        // this.depname = val[0]
        this.userresid = obj
        this.f_depid = obj
      },
      getuser ( obj, val) {
        this.operatorname = val[0]
        this.f_operatorid = obj
      }
    },
    watch: {
      'reportStyle' (val) {
        this.$refs.paged.$refs.criteria.search()
      },
      'model.data' (val) {
        this.spans = val.split('</tr>').map(item => item.split('</td>').length-1).reduce((x, y) => x > y ? x : y)
      }
    },
    computed: {
    }
  }
</script>
<style scoped>
  .noborder{
    border: none;
  }
  .fontSize{
    font-size: 13px;
  }
</style>
