<template>
  <div style="flex:1;" 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" v-show="$parent.$parent.criteriaShow">
            <div class="col-sm-2 form-group form-fix-width" >
              <label class="font_normal_body _zoom4" >卡号</label>
              <input  type="text" class="form-control _zoom6"  v-numfilter v-model="model.f_card_id"
                      condition="f_card_id like '%{}%'" placeholder='卡号'>
            </div>
            <div class="col-sm-2 form-group form-fix-width" >
              <label class="font_normal_body _zoom4" >用户姓名</label>
              <input  type="text" class="form-control _zoom6" v-model="model.f_user_name"
                      condition="f_user_name like '%{}%'" placeholder="用户姓名">
            </div>
            <div class="col-sm-2 form-group form-fix-width" >
              <label class="font_normal_body _zoom4" >用户地址</label>
              <input  type="text" class="form-control _zoom6" v-model="model.f_address"
                      condition="f_address like '%{}%'" placeholder='用户地址'>
            </div>
            <div class="col-sm-2 form-group form-fix-width" >
              <label class="font_normal_body _zoom4" >补卡类型</label>
              <v-select :value.sync="model.f_fillcard_type"
                        v-model="model.f_fillcard_type"
                        :options='$parent.$parent.fillCardTypes' placeholder='请选择'
                        condition="f_fillcard_type = '{}'"
                        close-on-select></v-select>
            </div>
         <!--   <div class="col-sm-2 form-group form-fix-width" >
              <label class="font_normal_body _zoom4" style="width: 40%;">气表品牌</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 form-fix-width" >
              <label class="font_normal_body _zoom4" >补卡状态</label>
              <v-select :value.sync="model.f_state"
                        v-model="model.f_state"
                        :options='$parent.$parent.charge_state' placeholder='请选择'
                        condition="f_state = '{}'"
                        close-on-select></v-select>
            </div>
            <div class="col-sm-2 form-group form-fix-width" >
              <label class="font_normal_body _zoom4" >付款方式</label>
              <v-select :value.sync="model.f_payment"
                        v-model="model.f_payment"
                        multiple
                        :options='$parent.$parent.payments' placeholder='请选择'
                        condition="f_payment in {}"
              ></v-select>
            </div>
            <div class="col-sm-2 form-group form-fix-width" >
              <label class="font_normal_body _zoom4" >打印格式</label>
              <v-select :value.sync="model.f_bill_style"
                        v-model="model.f_bill_style"
                        multiple
                        :options='$parent.$parent.printstyle' placeholder='请选择'
                        condition="f_bill_style in {}"
              ></v-select>
            </div>
           <!-- <div class="col-sm-2 form-group form-fix-width" >
              <label class="font_normal_body _zoom4" style="width: 40%;">补卡状态</label>
              <v-select :value.sync="model.f_state"
                        v-model="model.f_state"
                        :options='$parent.$parent.chargeState' placeholder='请选择'
                        condition="f_state = '{}'"
                        close-on-select></v-select>
            </div>-->
            <div class="col-sm-2 form-group form-fix-width" >
              <label for="startDate" class="font_normal_body _zoom4" >开始日期</label>
              <datepicker id="startDate" placeholder="开始日期"
                          class="_zoom6"
                          v-model="model.startDate"
                          :value.sync="model.startDate"
                          :format="'yyyy-MM-dd'"
                          :show-reset-button="true"
                          condition="f_operate_date >= to_date('{} 00:00:00','yyyy-mm-dd hh24:mi:ss')">
              </datepicker>
            </div>
            <div class="col-sm-2 form-group form-fix-width" >
              <label for="endDate" class="font_normal_body _zoom4" >结束日期</label>
              <datepicker id="endDate" placeholder="结束日期" class="_zoom6"
                          v-model="model.endDate"
                          :value.sync="model.endDate"
                          :format="'yyyy-MM-dd'"
                          :show-reset-button="true"
                          condition="f_operate_date <= to_date('{} 23:59:59','yyyy-mm-dd hh24:mi:ss')">
              </datepicker>
            </div>
            <div class="col-sm-2 form-group form-fix-width" >
              <label class="font_normal_body _zoom4" >是否开票</label>
              <v-select :value.sync="model.is_bill"
                        v-model="model.is_bill"
                        :options='$parent.$parent.billstates' placeholder='请选择'
                        condition="is_bill = '{}'"
                        close-on-select></v-select>
            </div>
            <div class="col-sm-2 form-group form-fix-width" >
              <label class="font_normal_body _zoom4" >发票编号</label>
              <input  type="text" class="form-control _zoom6" v-model="model.bill_num"
                      condition="bill_num = '{}'" placeholder="发票编号">
            </div>
            <div class="col-sm-2 form-group form-fix-width" >
              <label class="font_normal_body _zoom4" >售气单位</label>
              <v-select :value.sync="model.f_filiale"
                        v-model="model.f_filiale"
                        :options='$parent.$parent.filialelist' placeholder='请选择'
                        condition="f_filiale = '{}'"
                        close-on-select></v-select>
            </div>
            <div class="col-sm-2 form-group form-fix-width" >
              <label class="font_normal_body _zoom4" >售气部门</label>
              <v-select :value.sync="model.f_outlets"
                        v-model="model.f_outlets"
                        :options='$parent.$parent.selldep' placeholder='请选择'
                        condition="f_outlets = '{}'"
                        close-on-select></v-select>
            </div>
            <div class="col-sm-2 form-group form-fix-width" >
              <label class="font_normal_body _zoom4" >售气站点</label>
              <input  type="text" class="form-control _zoom6" v-model="model.op_spot"
                      condition="op_spot = '{}'" placeholder="售气站点">
            </div>
            <div class="col-sm-2 form-group form-fix-width" >
              <label class="font_normal_body _zoom4" >人员编号</label>
              <input  type="text" class="form-control _zoom6" v-model="model.op_id"
                      condition="op_id = '{}'" placeholder="操作员编号">
            </div>
          </div>
          <div class="row">
            <div  style="float:right" :class="{'_btnclitested':!$parent.$parent.criteriaShow}">
              <button class="btn btn-success" @click="search()">查询</button>
              <button class="btn btn-default" @click="$parent.$parent.clear()">清空</button>
              <!--<button class="btn btn-default" @click="$parent.$parent.stamp()">打印</button>-->

              <export-excel :data="$parent.$parent.getCondition"
                            :field="$parent.$parent.getfield"
                            sqlurl="rs/logic/exportfile" sql-name="fillCardQuery" template-name='补卡查询导出'  :choose-col="true"></export-excel>

              <div
                :class="{'button_shrink_top':$parent.$parent.criteriaShow,'button_shrink_bottom':!$parent.$parent.criteriaShow}"
                @click="$parent.$parent.hidden()" class="button_spacing" style="float: right"></div>
            </div>
          </div>
        </div>
      </criteria>

      <data-grid  :model="model" partial='list' 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>
            <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;"><nobr>{{row.f_userinfo_code}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_user_name}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_address}}</nobr></td>
          <td style="text-align: center;"><nobr><span @click="$parent.$parent.$parent.dealmsg(row)"><a>{{row.f_card_id}}</a></span></nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_meter_brand}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_payment}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_bill_style}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_fillcard_type}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_state}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_reason}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_cardfees}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_operate_date}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.op_spot}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.op_id}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_operator}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_outlets}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_filiale}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.is_bill}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.bill_num}}</nobr></td>
        </template>
        <template partial='foot'></template>
      </data-grid>

    </criteria-paged>

    <table class="table-hover" >
      <tr  class="table-bordered">
        <td style="display: inline-block;width: auto;border-right: 1px solid #F2F6FA;padding: 0px 10px 0px 10px;color: #5CB95C;font-weight: bold">汇总信息</td>
        <td style="display: inline-block;width: auto;border-right: 1px solid #F2F6FA;padding: 0px 10px 0px 10px">卡费合计:&emsp;{{sumsmodel.f_cardfees}}</td>
      </tr>
    </table>

  </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()
    self.$refs.paged.$refs.cri.model.endDate = self.$login.toStandardDateString()
    self.$refs.paged.$refs.cri.model.f_state = ['有效']
    await self.$MagLoadParams.loadParam(self.$login.f)
    self.initParams()
    /*await self.$refs.paged.$refs.cri.search()
    self.sumsmodel = self.$refs.paged.$refs.grid.model.sums*/
  }
  export default {
    data() {
      return {
        data: {},
        model: new PagedList('rs/sql/fillCardQuery', 20,{},{f_cardfees: 0}),
        criteriaShow: true,
        headData: ["用户编号","用户姓名","用户电话","身份证号","地址","卡类型","表编号","卡号","气表品牌","气表状态","卡费","补卡日期","操作员","营业网点","分公司"],
        bodyData: ["f_userinfo_code","f_user_name","f_user_phone","f_idnumber","address","f_card_type","f_user_id","f_card_id","f_meter_brand","f_table_state","f_cardfees","f_operate_date","f_operator","f_outlets","f_filiale"],
        modelval: [],
        printshow: false,
        all: false,
        fields: {},
        thead: '',
        tfoot: '',

        meterbrands: [],
        payments: this.$appdata.getParam('付款方式') ? [{label: '全部', value: ''}, ...this.$appdata.getParam('付款方式')] : [],
        // tablestate: [{label: '全部', value: ''}, {label: '正常', value: '正常'}, {label: '停用', value: '停用'}],
        fillCardTypes: [
		      {label: '全部', value: ''},
          {label: '补卡', value: '补卡'},
          {label: '换卡', value: '换卡'},
        ],
        printstyle: this.$appdata.getParam('打印格式') ? [{label: '全部', value: ''}, ...this.$appdata.getParam('打印格式')] : [],
        charge_state:  [{label: '全部', value: ''},{label: '有效', value: '有效'},{label: '无效', value: '无效'}],
        filialelist: this.$appdata.getParam('售气单位') ? [{label: '全部', value: ''},...this.$appdata.getParam('售气单位')] : [],
        billstates:  [{label: '全部', value: ''},{label: '是', value: '是'}],

        //合计数据
        sumsmodel: {}
      }
    },
    ready(){
      readySomething(this).then(() => {
        this.$emit('ready')
      }).catch((error) => {
        this.$emit('error', error)
      })
    },
    methods: {
      selfSearch (args) {
        this.model.search(args.condition, args.model)
        this.sumsmodel = this.$refs.paged.$refs.grid.model.sums
      },

      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]
      },

      clear(){
        Object.keys(this.$refs.paged.$refs.cri.model).forEach((key) => {
          this.$refs.paged.$refs.cri.model[key] = ''
        })
      },
      show(){
        this.criteriaShow = true
      },
      hidden(){
        this.criteriaShow = !this.criteriaShow
      },
      getRes(obj) {
        this.filialeNameStr = obj.res
        this.filialeCodeStr = obj.resids
      },
      dealmsg (val) {
        console.log('---------------dealmsg')
        val.model = this.model.model
        this.$dispatch('deal-msg',val)
      },
      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()
      },
    },
    computed:{
      getCondition(){
        return {condition: `${this.$refs.paged.$refs.cri.condition}  and f_orgstr like '${this.filialeCodeStr}%'`}
      },
      getfield(){
        return exportConfig.fillcardConfig
      },
      selldep () {
        let rs = []
        if(this.$refs.paged.$refs.cri.model.f_filiale){
          this.$refs.paged.$refs.cri.model.f_outlets = ''
          return [{label: '全部', value: ''}, ...this.$appdata.getParam(this.$refs.paged.$refs.cri.model.f_filiale) ]
        }
        else {
          return [{label: '全部', value: ''}]
        }
      },
    }
  }
</script>
