<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">公&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;司</label>
                  <right-tree @re-res="$parent.$parent.getorg"
                              :initresid='$parent.$parent.org'></right-tree>
                </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_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 = '{}'"
                          placeholder="客户名称">
                </div>
                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">增值类型</label>
                  <v-select :value.sync="model.f_field_name"
                            v-model="model.f_field_name"
                            :options='$parent.$parent.valuetypeoptions' placeholder='请选择'
                            condition="itemname = '{}'"
                            close-on-select></v-select>
                </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"
                                     :defaultselect="$parent.$parent.selectfields"
                                     :field="$parent.$parent.getfield"
                                     :exportprogress="true"
                                     sqlurl="api/af-revenue/logic/openapi/exportfile"
                                     sql-name='addValueQuery'
                                     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">小区名称</label>
                  <input type="text" style="width:60%" class="input_search" v-model="model.f_residential_area"
                   condition="f_residential_area = '{}'"
                          placeholder="小区名称">
                </div>
                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">客户类型</label>
                  <v-select :value.sync="model.f_user_type"

                             condition="f_user_type = '{}'"
                            :options='$parent.$parent.usertypes' placeholder='请选择' v-model="model.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"
                     condition="f_gasproperties = '{}'"
                            :options='$parent.$parent.gasproperties' placeholder='请选择'
                            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_open_state"
                            v-model="model.f_open_state"
                            :options='$parent.$parent.backstates' placeholder='请选择'
                            condition="ishas = '{}'"
                            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>
                  <nobr>增值业务名称</nobr>
                </th>
                <th>
                  <nobr>户数</nobr>
                </th>
              </tr>
            </template>
            <template partial='body'>
              <td :style="{background:(row.istrue=='1'? '#f1dec2':'')}" style="text-align: center;">
                <nobr>{{row.itemname}}</nobr>
              </td>
              <td :style="{background:(row.istrue=='1'? '#f1dec2':'')}" style="text-align: center;">
                <nobr>{{row.num}}</nobr>
              </td>

            </template>
            <template partial='foot'></template>
          </data-grid>
        </criteria-paged>
      </div>
    </div>
  </div>
</template>

<script>
  import {PagedList} from 'vue-client'
  import exportConfig from '../sale/config/exportConfig'
  import defaultPrint from "../sale/config/DefaultPrint";
  import plugin from "system-clients/src/plugins/GetLoginInfoService";

  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'
    // self.$refs.paged.$refs.cri.search()
    self.defaultfield = [...self.defaultfield, ...self.config.defaultPrint]
    await self.$MagLoadParams.loadParam()
    self.sumsmodel = self.$refs.paged.$refs.grid.model.sums
  }
  export default {
    title: '增值业务汇总查询',
    data() {
      return {
        org:[this.$login.f.orgid],
        filialeCodeStr: `and f_orgid = ${this.$login.f.orgid}`,
        config: {
          defaultPrint: ['f_userinfo_code', 'f_address', 'f_pregas', 'f_preamount']
        },
        //f_price_name:[],
        gasproperties:[],
        showinfo: false,
        data: {},
        show:false,
        rowdata:{},
        initres: {
          org:[this.$login.f.orgid],
          dep:[],
          user:[],
        },
        model: new PagedList('api/af-revenue/sql/addValueQuery', 20, {}, ),
        criteriaShow: true,
        orgCondtionStr: '',
        // 下拉框
        meterbrands: [],
        pricenames: [],
        prices: [],
        //合计数据
        defaultfield: [],
        sumsmodel: {},
        f_filialeid: this.$login.f.f_orgid,
        selectfields:[],
        istrueoptions:[{label:"全部",value:""},{label:"是",value:"0"},{label:"否",value:"1"}],
        backstates:[{label: '全部', value: ''},{label: '有', value: '有'},{label: '无', value: '无'}],
      }
    },
    props:['selectorg'],
    ready() {
      if(this.selectorg != null && this.selectorg != null != ''){
        this.initres.org=[]
        this.initres.org.push(this.selectorg);
      }
      readySomething(this).then(() => {
        this.$emit('ready')
      }).catch((error) => {
        this.$emit('error', error)
      })
      for(var item in exportConfig.valuecountConfig){
        this.selectfields.push(item)
      }
    },
    methods: {
      getorg (obj) {
        if(obj.resids.length>0){
          this.filialeCodeStr =" and f_orgid in " + plugin.convertToIn(obj.resids)
        }
        else{
          this.filialeCodeStr= " and f_orgid = " + this.$login.f.orgid
        }
      },
      search(){
        this.$refs.paged.$refs.cri.search()
      },
      cancel() {
        this.show = false
      },
      showmsg(obj){
        this.rowdata=obj
        this.show=true
      },
      userTypeChange () {
        this.gasproperties=[]
        if(this.$refs.paged.model.model !==null) {
          this.$refs.paged.model.model.f_gasproperties=''

          console.log(this.$refs.paged.$refs.cri.model.f_user_type[0])
          this.gasproperties = this.$appdata.getParam(this.$refs.paged.$refs.cri.model.f_user_type[0])
          // this.gasproperties.push({label: '全部', value: ''})
        }
        else{
          this.gasproperties =[{label: '全部', value: ''}]
        }
      },

      selfSearch(args) {
        this.model.params.f_filialeid = this.f_filialeid == null ||  this.f_filialeid=='' ? this.$login.f.orgid: this.f_filialeid
        this.model.search(args.condition, args.model)
      },
      clear() {
        // this.$refs.paged.$refs.cri.$refs.sel.$children[2].$children[0].isSelectAll = false
        Object.keys(this.$refs.paged.$refs.cri.model).forEach((key) => {
          this.$refs.paged.$refs.cri.model[key] = []
        });
        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'

      },
      dealmsg(val) {
        val.model = this.model.model
        this.$dispatch('deal-msg', val)
      },
      show() {
        this.criteriaShow = true
      },
      hidden() {
        this.criteriaShow = !this.criteriaShow
      },
      getRes(condition,obj) {
        this.orgCondtionStr = condition
        this.f_orgid = obj.resids
      },
      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: {
      'model'(val) {
        console.log(val, "12222222222222222222")
      },
      'filialeCodeStr'(val) {
        let res = val.match(/'(.*?)'/)
        console.log('正则提取:',res && res[1])
        if (res) {
          this.f_filialeid = res[1]
        }
      }
    },
    computed: {
      getCondition() {
        let params={}
        params.f_filialeid = this.f_filialeid == null ||  this.f_filialeid=='' ? this.$login.f.orgid: this.f_filialeid;
        params.condition = `${this.$refs.paged.$refs.cri.condition}`;
        return params;
      },
      valuetypeoptions(){
        return [{label: ' 全部 ', value: ''}, ...this.$appdata.getParam('增值服务类型')]
      },
      stateoptions(){
        return [{label: ' 全部 ', value: ''}, ...this.$appdata.getParam('增值服务状态')]
      },
      usertypes() {
        return [{label: ' 全部 ', value: ''}, ...this.$appdata.getParam('用户类型')]
      },
      pricetypeoptions(){
        return [{label: ' 全部 ', value: ''}, ...this.$appdata.getParam('气价类型')]
      },
      getfield() {
        return exportConfig.valuecountConfig
      },

    }
  }
</script>
