<template>
  <div id="unit" class="flex-row">
    <div class="basic-main" @keyup.enter="search">
      <div class="flex" v-if="!show">
        <criteria-paged :model="model" v-ref:paged>
          <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 class="font_normal_body">财务月度</label>
                  <datepicker id="aaa" placeholder="开始日期" style="width:60%"
                              v-model="model.accountingStartDate"
                              :value.sync="model.accountingStartDate"
                              format="yyyy-MM-dd HH:mm:ss"
                              :show-reset-button="true"
                              condition="f_accounting_date >= '{}'">
                  </datepicker>
                </div>
                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">财务月度</label>
                  <datepicker id="aaa" placeholder="结束日期" style="width:60%"
                              v-model="model.accountingEndDate"
                              :value.sync="model.accountingEndDate"
                              format="yyyy-MM-dd HH:mm:ss"
                              :show-reset-button="true"
                              condition="f_accounting_date <= '{}'">

                  </datepicker>
                </div>
                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">自然月度</label>
                  <datepicker id="startDate" placeholder="开始日期" style="width:60%"
                              v-model="model.handStartDate"
                              :value.sync="model.handStartDate"
                              format="yyyy-MM-dd HH:mm:ss"
                              :show-reset-button="true"
                              condition="f_hand_date >= '{}'">
                  </datepicker>
                </div>
                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">自然月度</label>
                  <datepicker id="startDate" placeholder="结束日期" style="width:60%"
                              v-model="model.handEndDate"
                              :value.sync="model.handEndDate"
                              format="yyyy-MM-dd HH:mm:ss"
                              :show-reset-button="true"
                              condition="f_hand_date <= '{}'">
                  </datepicker>
                </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"
                                :field="$parent.$parent.getfield"
                                :footer="$parent.$parent.footer"
                                :header="$parent.$parent.other"
                                sqlurl="api/af-revenue/logic/openapi/exportfile"
                                sql-name="HandPlanCollection"
                                template-name='抄表情况记录表'
                                :choose-col="true"></export-excel>
                  <print-data :model="$parent.model"
                              :field="$parent.$parent.getfield"
                              :sum-field="$parent.$parent.getfield"
                              titletable="抄表情况记录表"
                              :sumsmodel="$parent.$parent.sumsmodel"></print-data>
                  <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 for="f_gasproperties"class="font_normal_body">用气性质</label>
                  <v-select id="f_gasproperties"
                            style="width:60%"
                            v-model="model.f_gasproperties"
                            placeholder='用气性质'
                            :value.sync="model.f_gasproperties"
                            :options='$parent.$parent.gasproperties'
                            condition="f_gasproperties = '{}'"
                            close-on-select >
                  </v-select>
                </div>
              </div>
            </div>
          </criteria>

          <data-grid :model="model" partial='list' class="list_area table_sy"  v-ref:grid>
            <template partial='head'>
              <tr>
                <th rowspan="2">
                  <nobr>用户号</nobr>
                </th>
                <th rowspan="2">
                  <nobr>小区</nobr>
                </th>
                <th rowspan="2">
                  <nobr>地址</nobr>
                </th>
                <th rowspan="2">
                  <nobr>用户名</nobr>
                </th>
                <th rowspan="2">
                  <nobr>表钢号</nobr>
                </th>
                <th rowspan="2">
                  <nobr>客户类型</nobr>
                </th>
                <th rowspan="2">
                  <nobr>用气性质</nobr>
                </th>
                <th colspan="12">
                  <nobr>月份</nobr>
                </th>
              </tr>
              <tr>
                <th>
                  <nobr>一月</nobr>
                </th>
                <th>
                  <nobr>二月</nobr>
                </th>
                <th>
                  <nobr>三月</nobr>
                </th>
                <th>
                  <nobr>四月</nobr>
                </th>
                <th>
                  <nobr>五月</nobr>
                </th>
                <th>
                  <nobr>六月</nobr>
                </th>
                <th>
                  <nobr>七月</nobr>
                </th>
                <th>
                  <nobr>八月</nobr>
                </th>
                <th>
                  <nobr>九月</nobr>
                </th>
                <th>
                  <nobr>十月</nobr>
                </th>
                <th>
                  <nobr>十一月</nobr>
                </th>
                <th>
                  <nobr>十二月</nobr>
                </th>
              </tr>



            </template>
            <template partial='body'>
              <td style="text-align: center;">{{row.f_userinfo_code}}</td>
              <td style="text-align: center;">{{row.f_residential_area}}</td>
              <td style="text-align: center;">{{row.f_address}}</td>
              <th style="text-align: center;">{{row.f_user_name}}</th>
              <th style="text-align: center;">{{row.f_meternumber}}</th>
              <th style="text-align: center;">{{row.f_user_type}}</th>
              <th style="text-align: center;">{{row.f_gasproperties}}</th>
              <th style="text-align: center;">{{row.f_month1}}</th>
              <th style="text-align: center;">{{row.f_month2}}</th>
              <th style="text-align: center;">{{row.f_month3}}</th>
              <th style="text-align: center;">{{row.f_month4}}</th>
              <th style="text-align: center;">{{row.f_month5}}</th>
              <th style="text-align: center;">{{row.f_month6}}</th>
              <th style="text-align: center;">{{row.f_month7}}</th>
              <th style="text-align: center;">{{row.f_month8}}</th>
              <th style="text-align: center;">{{row.f_month9}}</th>
              <th style="text-align: center;">{{row.f_month10}}</th>
              <th style="text-align: center;">{{row.f_month11}}</th>
              <th style="text-align: center;">{{row.f_month12}}</th>
            </template>
          </data-grid>
        </criteria-paged>
      </div>
    </div>
  </div>

</template>
<script>
  /**
   *用户档案查询列表以及添加操作组件
   */
  import {PagedList} from 'vue-client'
  import defaultPrint from '../config/DefaultPrint'
  import exportConfig from '../config/exportConfig'

  let readySomething = async function (self) {
    self.$refs.paged.$refs.cri.model.startDate = self.$login.toStandardDateString() + ' 00:00:00'
    self.$refs.paged.$refs.cri.model.endDate = self.$login.toStandardDateString() + ' 23:59:59'
    // await self.$refs.paged.$refs.cri.search()
    // await self.$getConfig(self, 'SellingHand')
    // self.defaultfield = [...self.defaultfield, ...self.config.defaultPrint]

    await self.$MagLoadParams.loadParam()

    self.sumsmodel = self.$refs.paged.$refs.grid.model.sums
    console.log('self.sumsmodel', self.sumsmodel)
  }


  export default {
    title: '抄表情况记录',
    data() {
      return {
        rows: null,
        other: [],
        footer: [],
        criteriaShow: false,
        model: new PagedList('api/af-revenue/sql/HandPlanCollection', 50,
          {startDate: 'this.model.startDate', endDate: 'this.model.endDate'},
          {
            f_month1:0,
            f_month2:0,
            f_month3:0,
            f_month4:0,
            f_month5:0,
            f_month6:0,
            f_month7:0,
            f_month8:0,
            f_month9:0,
            f_month10:0,
            f_month11:0,
            f_month12:0

          }),
        orgCondtionStr: '',

        getfield: '',
        show:false,
        rowdata:{},
        // config: {
        //   defaultPrint: ['f_userinfo_code', 'f_user_name', 'f_last_balance', 'f_balance']
        // },
        initres: {
          org:[this.$login.f.orgid],
          dep:[],
          user:[],
        },
        defaultfield: [],
        sumsmodel: {}
      }
    },
    ready() {
      readySomething(this).then(() => {
        this.$emit('ready')
      }).catch((error) => {
        this.$emit('error', error)
      })
    },
    methods: {
      getotherfooter() {
        // this.$refs.paged.$refs.cri.$refs.exports.otherData=[];
        // this.$refs.paged.$refs.cri.$refs.exports.footerData=[];
        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) {
        this.sumsmodel = this.$refs.paged.$refs.grid.model.sums
        args.condition = `${args.condition}` + this.orgCondtionStr
        this.model.search(args.condition, args.model)
      },
      userTypeChange () {
        this.gasproperties=[]
        if(this.$refs.paged.$refs.cri.model !==null) {
          this.$refs.paged.$refs.cri.model.f_gasproperties=''
          this.gasproperties = this.$appdata.getParam(this.$refs.paged.$refs.cri.model.f_user_type[0])
        }
        else{
          this.gasproperties =[{label: '全部', value: ''}]
        }
      },
      show() {
        this.criteriaShow = true
      },
      cancel() {
        this.show = false
      },
      showmsg(obj){
        this.rowdata=obj
        this.show=true
      },
      dealmsg(val) {
        console.log('---------------dealmsg')
        val.model = this.model.model
        this.$dispatch('deal-msg', val)
      },
      hidden() {
        this.criteriaShow = !this.criteriaShow
      },
      clear() {
        Object.keys(this.$refs.paged.$refs.cri.model).forEach((key) => {
          this.$refs.paged.$refs.cri.model[key] = []
        })
      },
      getRes(obj) {
        this.orgCondtionStr = obj
      },
      stamp() {
        this.all = false
        //默认选择要打印的列
        this.modelval = defaultPrint.config
        this.fields = this.getfield
        console.log('所有打印字段', this.fields)
        this.printshow = true
        this.put()
      },
      put() {
        // 对Modelval进行排序
        this.sortModelval()
        this.thead = `<tr><th colspan=${this.modelval.length}>抄表收费统计报表</th></tr><tr>`
        for (let key of this.modelval) {
          this.thead += '<th>' + this.fields[key] + '</th>'
        }
        this.thead += '</tr>'
      },
      print() {
        this.$refs.print.PrintAsFile()
        this.printshow = false
      },
      close() {
        this.printshow = false
        this.all = false
      },
      // 对选择的列进行排序
      sortModelval() {
        let sortModel = []
        Object.keys(this.fields).forEach((key) => {
          if (this.modelval.includes(key)) {
            sortModel.push(key)
          }
        })
        this.modelval = sortModel
        console.log('选择的打印的字段', this.modelval)
      }
    },
    watch: {
      'all'(val) {
        if (val) {
          this.modelval = this.bodyData
        } else {
          this.modelval = defaultPrint.config
          this.put()
        }
      },
      'modelval.length'() {
        this.put()
      },
      sumsmodel: {
        handler: function (val) {
          this.getotherfooter();
        },
        deep: true
      }
    },
    computed: {
      getCondition() {
        let result = {
          condition: this.$refs.paged.$refs.cri.condition + this.orgCondtionStr,
          startDate: this.$refs.paged.$refs.cri.model.startDate,
          endDate: this.$refs.paged.$refs.cri.model.endDate
        }
        return result
      },
      getfield() {
        return exportConfig.handplanInfoConfig
      },
      gasproperties() {
        return [{label: ' 全部 ', value: ''}, ...this.$appdata.getParam('用气性质')]
      },
      // payment() {
      //   return [...this.$appdata.getParam('付款方式查询')]
      // }
    }
  }
</script>
<style media="screen">
  .divtext {
    min-width: 50px;
    max-width: 200px;
    border: 1px solid green;
    display: inline-block;
  }
</style>
