<template>
  <div id="unit" class="flex-row">
    <div class="basic-main" @keyup.enter="search">
      <div class="flex">
        <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>
                  <input type="text" style="width:60%" class="input_search" v-model="model.f_userinfo_code"
                         condition="f_userinfo_code = '{}' " 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.f_user_name"
                         condition=" f_user_name like '%{}%'" 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.f_address"
                         condition="f_address like '%{}%'" placeholder='客户地址'>
                </div>
                <!--                <div class="col-sm-2">-->
                <!--                  <label class="font_normal_body">组织机构</label>-->
                <!--                  <res-select :initresid="$parent.$parent.org"-->
                <!--                              style="width:60%"-->
                <!--                              @res-select="$parent.$parent.getorg"-->
                <!--                              restype='organization'>-->
                <!--                  </res-select>-->

                <!--                </div>-->
                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">&emsp;公司&emsp;</label>
                  <right-tree @re-res="$parent.$parent.getorg"
                              :initresid='$parent.$parent.org'></right-tree>
                </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"
                                sqlurl="api/af-revenue/logic/openapi/exportfile" sql-name="manage_getReportDataQuery"
                                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">表&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;号</label>
                  <input type="text" style="width:60%" class="input_search" v-model="model.f_meternumber"
                         condition="f_meternumber like '%{}%'" placeholder='表号'>
                </div>
                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">&nbsp;&nbsp;&nbsp;小区&nbsp;&nbsp;&nbsp;</label>
                  <input type="text" style="width:60%" class="input_search" v-model="model.f_residential_area"
                         condition="f_residential_area like '%{}%'" placeholder='小区'>
                </div>
                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">气表品牌</label>
                  <v-select
                    placeholder='气表品牌'
                    :value.sync="model.f_meter_brand"
                    v-model="model.f_meter_brand"
                    :options='$parent.$parent.meterbrands'
                    close-on-select
                    condition="f_meter_brand='{}'">
                  </v-select>
                </div>
                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">气表型号</label>
                  <v-select
                    placeholder='气表型号'
                    :value.sync="model.f_meter_style"
                    v-model="model.f_meter_style"
                    :options='$parent.$parent.metertypes'
                    close-on-select
                    condition="f_meter_style='{}'">
                  </v-select>
                </div>
                <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="f_insert_date >= '{}'">
                  </datepicker>
                </div>
                <div class="col-sm-2 form-group">
                  <label for="endDate"
                         class="font_normal_body">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;至&nbsp;&nbsp;&nbsp;&nbsp;</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="f_insert_date <= '{}'">
                  </datepicker>
                </div>
                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">客户类型</label>
                  <v-select :value.sync="model.f_user_type"
                            @change="$parent.$parent.userTypeChange()"
                            :options='$parent.$parent.usertypes' placeholder='请选择' v-model="model.f_user_type"
                            condition="f_user_type = '{}'"
                            close-on-select></v-select>
                </div>
                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">用气性质</label>
                  <v-select :value.sync="model.f_gasproperties" v-model="model.f_gasproperties"
                            :options='$parent.$parent.gasproperties' placeholder='请选择'
                            condition="f_gasproperties = '{}'"
                            close-on-select></v-select>
                </div>
                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">用气类型</label>
                  <v-select :value.sync="model.f_user_nature" v-model="model.f_user_nature"
                            :options='$parent.$parent.usernatures' placeholder='请选择'
                            condition="f_user_nature = '{}'"
                            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 v-for="row in $parent.$parent.$parent.headData">
                  <nobr>{{ row }}</nobr>
                </th>
              </tr>
            </template>
            <template partial='body'>
              <tr v-for="row in $parent.$parent.$parent.model.rows">
                <td style="text-align: center;" v-for="item in $parent.$parent.$parent.config.fieldMapping">
                  <nobr v-if="item == 'f_lowlithiumbattery'">{{ row[item] == 1 ? '是' : '否' }}</nobr>
                  <nobr v-if="item == 'f_magneticinterference'">{{ row[item] == 1 ? '异常' : '正常' }}</nobr>
                  <nobr v-if="item != 'f_magneticinterference' && item != 'f_lowlithiumbattery'">{{ row[item] }}</nobr>
                </td>
              </tr>
            </template>
            <template partial='foot'></template>
          </data-grid>
        </criteria-paged>
      </div>
    </div>
  </div>
</template>

<script>
import { PagedList } from 'vue-client'
import tableConfig from './config/tableConfig'
import exportConfig from './config/exportConfig'
import plugins from '../../plugins/GetLoginInfoService'
import plugin from 'system-clients/src/plugins/GetLoginInfoService'

let readySomething = async function (self) {
  await self.$MagLoadParams.loadParam()
  self.initParams()
  // self.defaultfield = [...self.defaultfield, ...self.config.defaultPrint]
  self.sumsmodel = self.$refs.paged.$refs.grid.model.sums
}
export default {
  title: '上报数据查询',
  data () {
    return {
      other: [],
      sumsmodel: {},
      footer: [],
      model: new PagedList('api/af-revenue/sql/manage_getReportDataQuery', 20, {}),
      criteriaShow: false,
      config: {
        fieldMapping: {
          f_userinfo_code: '客户编号',
          f_user_name: '客户名称'
        }
      },
      WarningType: [
        {label: '全部', value: ''}
      ],
      Warningstyles: [
        {label: '全部', value: ''}
      ],
      styles: false,
      headData: [],
      bodyData: [],
      gasproperties: [],
      orgCondtionStr: ' and f_filialeid in (' + this.$login.f.orgid + ')',
      resshow: ['company'],
      org: [this.$login.f.orgid],
      meterbrands: []
    }
  },
  created () {
    this.config.fieldMapping = tableConfig.ReportDataQuery
    console.log('========================')
    console.log(this.config.fieldMapping)
    var item = '用户编号'
    console.log('~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
    console.log(this.config.fieldMapping[item])
    this.headData = Object.keys(this.config.fieldMapping)
  },
  ready () {
    this.$refs.paged.$refs.cri.model.startDate = this.$login.toStandardDateString() + ' 00:00:00'
    this.$refs.paged.$refs.cri.model.endDate = this.$login.toStandardDateString() + ' 23:59:59'
    console.log(this.$refs.paged.$refs.cri.model.startDate)
    console.log(this.$refs.paged.$refs.cri.model.endDate)
    readySomething(this).then(() => {
      this.$emit('ready')
    }).catch((error) => {
      this.$emit('error', error)
    })
  },
  methods: {
    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: ''}]
      }
    },
    initParams () {
      // 初始化气表品牌
      let brandArr = []
      this.$MagGetSaleParam.getGasbrand().forEach((item) => {
        let temp = {}
        if (item.value.f_meter_type === '物联网表') {
          temp.label = item.label
          temp.value = item.value.f_meter_brand
          brandArr.push(temp)
        }
      })
      this.meterbrands = [{label: '全部', value: ''}, ...brandArr]
      // 初始化气表价格
      this.prices = this.$MagGetSaleParam.getPrices()
    },
    showMeterStyle () {
      this.styles = true
    },
    search () {
      this.$refs.paged.$refs.cri.search()
    },
    async selfSearch (args) {
      args.condition = `${args.condition}` + this.orgCondtionStr
      await this.model.search(args.condition, args.model)
    },
    getorg (obj) {
      if (obj.resids.length > 0) {
        this.orgCondtionStr = ' and f_filialeid in ' + plugin.convertToIn(obj.resids)
      } else {
        this.orgCondtionStr = ' and f_filialeid = ' + this.$login.f.orgid
      }
    },
    clear () {
      Object.keys(this.$refs.paged.$refs.cri.model).forEach((key) => {
        this.$refs.paged.$refs.cri.model[key] = []
      })
    },
    hidden () {
      this.criteriaShow = !this.criteriaShow
    },
    getotherfooter () {
      this.other = []
      this.footer = []
      let otherInData = []
      let secondLine = [
        `开始时间: ${this.$refs.paged.$refs.cri.model.startDate}`,
        `结束时间: ${this.$refs.paged.$refs.cri.model.startDate}`
      ]
      otherInData.push(`导出时间: ${this.$login.toStandardTimeString()}`)
      let footerData = []

      this.footer.push(footerData)
      this.other.push(otherInData)
      this.other.push(secondLine)
    }
  },
  watch: {
    sumsmodel: {
      handler: function (val) {
        this.getotherfooter()
      },
      deep: true
    }
  },
  computed: {
    metertypes () {
      return [{label: ' 全部 ', value: ''}, ...this.$appdata.getParam('气表类型')]
    },
    usernatures () {
      return [{label: '全部', value: ''}, ...this.$appdata.getParam('用气类型')]
    },
    usertypes () {
      return [{label: ' 全部 ', value: ''}, ...this.$appdata.getParam('用户类型')]
    },
    getCondition () {
      return {
        startDate: this.$refs.paged.$refs.cri.model.startDate,
        endDate: this.$refs.paged.$refs.cri.model.endDate,
        condition: `${this.$refs.paged.$refs.cri.condition}` + this.orgCondtionStr
      }
    },
    getfield () {
      return exportConfig.ReportDataConfig
    }
  }
}
</script>
