<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 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_hand_date >= '{}'">
                  </datepicker>
                </div>
                <div class="col-sm-2 form-group">
                  <label for="endDate" class="font_normal_body">结束日期</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_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" :defaultselect="$parent.$parent.selectfields"
                                :field="$parent.$parent.getfield"
                                :defaultselect="$parent.$parent.selectfields"
                                sqlurl="rs/logic/saleExport" progress="saleGetExportProgress"
                                sql-name="farawayprice" 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>
                  <v-select :value.sync="model.f_meter_brand"
                            v-model="model.f_meter_brand"
                            :options='$parent.$parent.meterbrands' placeholder='请选择'
                            condition="f_meter_brand = '{}'"
                            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_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.istrue" v-model="model.istrue"
                            :options='$parent.$parent.istrueoptions' placeholder='请选择'
                            condition="istrue = '{}'"
                            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>
                <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="{background:(row.istrue=='1'? '#f1dec2':'')}" style="text-align: center;">
                <span @click="$parent.$parent.$parent.showmsg(row)"><a>{{row.f_userinfo_code}}</a></span>
              </td>
              <td :style="{background:(row.istrue=='1'? '#f1dec2':'')}" style="text-align: center;">
                <nobr>{{row.f_user_name}}</nobr>
              </td>
              <td :style="{background:(row.istrue=='1'? '#f1dec2':'')}" style="text-align: center;">
                <nobr>{{row.f_user_type}}</nobr>
              </td>
              <td :style="{background:(row.istrue=='1'? '#f1dec2':'')}" style="text-align: center;">
                <nobr>{{row.f_gasproperties}}</nobr>
              </td>
              <td :style="{background:(row.istrue=='1'? '#f1dec2':'')}" style="text-align: center;">
                <nobr>{{row.f_table_state}}</nobr>
              </td>
              <td :style="{background:(row.istrue=='1'? '#f1dec2':'')}" style="text-align: center;">
                <nobr>{{row.f_meter_classify}}</nobr>
              </td>
              <td :style="{background:(row.istrue=='1'? '#f1dec2':'')}" style="text-align: center;">
                <nobr>{{row.f_meter_brand}}</nobr>
              </td>
              <td :style="{background:(row.istrue=='1'? '#f1dec2':'')}" style="text-align: center;">
                <nobr>{{row.f_meternumber}}</nobr>
              </td>
              <td :style="{background:(row.istrue=='1'? '#f1dec2':'')}" style="text-align: center;">
                <nobr>{{row.newvalue}}</nobr>
              </td>
              <td :style="{background:(row.istrue=='1'? '#f1dec2':'')}" style="text-align: center;">
                <nobr>{{row.oldvalue}}</nobr>
              </td>
              <td :style="{background:(row.istrue=='1'? '#f1dec2':'')}" style="text-align: center;">
                <nobr>{{row.f_hand_date}}</nobr>
              </td>
              <td :style="{background:(row.istrue=='1'? '#f1dec2':'')}" style="text-align: center;">
                <nobr>{{row.f_input_date}}</nobr>
              </td>
              <td :style="{background:(row.istrue=='1'? '#f1dec2':'')}" style="text-align: center;">
                <nobr>{{row.f_orgname}}</nobr>
              </td>

            </template>
            <template partial='foot'></template>
          </data-grid>

        </criteria-paged>


      </div>
      <div v-if="show">
        <user-info-detail-manage-new :f_userinfo_id="rowData.f_userinfo_id" @cancel-main="cancel"></user-info-detail-manage-new>
      </div>
    </div>
  </div>
</template>

<script>
  import {PagedList} from 'vue-client'
  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.initParams();
    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('rs/sql/farawayprice', 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"}]
      }
    },
    ready() {

      readySomething(this).then(() => {
        this.$emit('ready')
      }).catch((error) => {
        this.$emit('error', error)
      })
    },
    methods: {
      initParams() {
        // 初始化气表品牌
        let brandArr = []
        this.$MagGetSaleParam.getGasbrand().forEach((item) => {
          let temp = {}
          temp.label = item.label
          temp.value = item.value.f_meter_brand
          brandArr.push(temp)
        })
        this.meterbrands = [{label: '全部', value: ''}, ...brandArr]

      },
      getorg (obj) {
        if(obj.resids.length>0){
          this.selectorgid = obj.resids
          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) {
        args.condition = args.condition+ ` ${this.filialeCodeStr}`
        this.model.search(args.condition, args.model)
        this.sumsmodel = this.$refs.paged.$refs.grid.model.sums
      },
      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] = []
        })

      },
      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)
      },
      async updateParams() {
        await this.$MagLoadParams.loadParam(this.f_filialeid)
        this.initParams()
      },
    },
    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]
          this.updateParams()
        }
      }
    },
    computed: {
      getCondition() {
        return {condition: `${this.$refs.paged.$refs.cri.condition}` + this.filialeCodeStr}
      },
      usertypes() {
        return [{label: ' 全部 ', value: ''}, ...this.$appdata.getParam('用户类型')]
      },
      getfield() {
        return {'f_userinfo_code': '客户编号','f_user_name':'客户名称','f_user_type':'用户类型','f_gasproperties':'用气性质','f_table_state':'表具状态',
          'f_meter_brand':'表具品牌','f_meternumber':'表号','value':'正确结算价格','curentprice':'抄表结算价格','f_hand_date':'抄表下发时间','f_input_date':'抄表结算时间',
          'f_orgname': '组织名称'}
      },

    }
  }
</script>
