<template >
  <div class="flex" style="justify-content: space-between;">
    <div @keyup.enter="search" style="">
      <criteria-paged :model="model" v-ref:paged>
        <criteria @condition-changed='$parent.search' partial='criteria' v-ref:criteria>
          <div class="form-horizontal select-overspread container-fluid auto" novalidate partial>
            <div class="row" width="100%">
              <div class="col-sm-2 form-group">
                <label class="font_normal_body">开始时间</label>
                <datepicker :format="'yyyy-MM-dd'" :value.sync="model.f_start_date"
                            class="datepicker"
                            condition="f_operate_date >= '{} 00:00:00' "
                            placeholder="起始时间"
                            style="width:60%"
                            v-model="model.f_start_date"
                ></datepicker>
              </div>
              <div class="col-sm-2 form-group" >
                <label class="font_normal_body" >结束时间</label>
                <datepicker :format="'yyyy-MM-dd'" :value.sync="model.f_end_date"
                            class="datepicker"
                            condition="f_operate_date <= '{} 23:59:59' "
                            placeholder="结束时间"
                            style="width:60%"
                            v-model="model.f_end_date"
                ></datepicker>
              </div>
              <div class="col-sm-2 form-group">
                <label class="font_normal_body">缴费状态</label>
                <v-select :value.sync="model.f_state" v-model='model.f_state'
                          style="width:60%"
                          :options='$parent.$parent.jiaofeistates' placeholder='缴费状态'
                          close-on-select
                          condition="s.f_state = '{}'"></v-select>
              </div>

              <div style="float: right">
                <button @click="search()" class="button_search" style="margin-right: 10px" v-el:cba>查询</button>
                <print-data :defaultfield="$parent.$parent.defaultfield" :field="$parent.$parent.fields" :is-selected="true" :model="$parent.$parent.printModel"
                            @print-data="$parent.$parent.print()" print-name="购气流水"></print-data>
              </div>
            </div>
          </div>


        </criteria>
        <data-grid :model="model" class="list_area table_sy" 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>使用方量</th>
              <th><nobr>扣费单价</nobr></th>
              <th><nobr>扣费金额</nobr></th>
              <th><nobr>表内余额</nobr></th>
            </tr>
          </template>
          <template partial='body' partial='list' v-ref:grid>
            <tr >
              <td style="text-align:center">{{row.f_operate_date.substring(0,10)}}</td>
              <td style="text-align:center">{{row.f_operate_date.substring(11,19)}}</td>
              <td style="text-align:center">{{row.f_last_tablebase}}</td>
              <td style="text-align:center">{{row.f_tablebase}}</td>
              <td style="text-align:center">{{row.f_pregas}}</td>
              <td style="text-align:center">{{row.f_price}}</td>
              <td style="text-align:center">{{row.f_preamount}}</td>
              <td style="text-align:center">{{row.f_meterBalanceAmt}}</td>
            </tr>
          </template>
          <template partial='foot'></template>
        </data-grid>
      </criteria-paged>
    </div>
    <table class="table-hover">
      <tr class="table-bordered" style="position: relative">
        <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;{{model.sums.f_pregas}}
        </td>
        <td style="display: inline-block;width: auto;border-right: 1px solid #F2F6FA;padding: 0px 10px 0px 10px">
          金额合计:&emsp;{{model.sums.f_preamount}}
        </td>
      </tr>
    </table>
    <reissue-bill :data="reissue_data" :show.sync="reissue_show" @cancel="reissueOper()" @reissue-success="reissueSucc()" v-if="reissue_show"></reissue-bill>

  </div>
</template>

<script>
  import {HttpResetClass, PagedList} from 'vue-client'
  import co from 'co'
let reprintGen = function *(self, row) {
    try {
      let reissueData = {}
      reissueData = Object.assign({}, reissueData, row)

      reissueData.f_bill_type = row.type
      reissueData.f_bill_style = '普通收据'
      reissueData.f_operator = self.$login.f.name
      reissueData.f_operatorid = self.$login.f.id
      reissueData.f_orgid = self.$login.f.orgid
      reissueData.f_orgname = self.$login.f.orgs
      reissueData.f_depid = self.$login.f.depids
      reissueData.f_depname = self.$login.f.deps
      reissueData.billUrl = self.getBillUrl(row.type)

      self.reissue_data = reissueData
      self.reissue_show = true
    } catch (error) {
      if (error.status) {
        self.$warn(`加载数据出错, ${JSON.stringify(error)}`, 'CardList')
      }
      throw error
    }
  }
  export default {
    title: '自动下账',
    data () {
      return {
        reissue_show: false,
        reissue_data: null,
        condition: '1=1',
        model: new PagedList(`${this.row.f_meter_type === '物联网表'?'rs/sql/sale_WebAutomaticPurse':'rs/sql/sale_AutomaticPurse'}`, 20, {}, {
          f_preamount: 0,
          f_pregas: 0
        }),
        show: false,
        list: [],
        jiaofeistates: this.$appdata.getParam('缴费状态') ? [{
          label: '全部',
          value: ''
        }, ...this.$appdata.getParam('缴费状态')] : [],
        // 控制单选
        radio: [],
        // 选中的页
        all: [],
        // row数据
        rowsdata: [],
        fields: {'f_userinfo_code': '客户编号', 'f_user_name': '客户姓名', 'f_preamount': '购气金额', 'f_pregas': '购气量', 'f_operate_date,': '缴费时间', 'f_operator': '操作员', 'f_comments': '备注'},
        defaultfield: [
          'f_userinfo_code', 'f_user_name', 'f_preamount', 'f_pregas', 'f_operate_date', 'f_operator', 'f_comments'
        ],
        printModel: {
          rows: []
        }
      }
    },
    props: ['row'],
    ready () {
      this.$refs.paged.$refs.criteria.model.f_state = ['有效']
    },
    methods: {
      async search () {
        console.log('aaa:', this.$refs.paged.$refs.criteria.condition)
        this.condition = `${this.$refs.paged.$refs.criteria.condition} and f_user_id = '${this.row.f_user_id}' and f_orgid = '${this.$login.f.orgid}'`
        this.model.search(this.condition, this.model)
        let http = new HttpResetClass()
        let res = await http.load('POST', `${this.row.f_meter_type === '物联网表'?'rs/sql/sale_WebAutomaticPurse':'rs/sql/sale_AutomaticPurse'}`, {
          data: {
            condition: this.condition
          }
        }, {resolveMsg: null, rejectMsg: null})
        this.printModel.rows = res.data
      },
      // 多选框初始化
      selectInit () {
        this.rowsdata = []
        this.all = []
        this.radio = []
      },
      reissueSucc () {
        this.reissue_show = false
        this.$dispatch('refresh')
      },
      reissueOper () {
        this.reissue_data = null
        this.reissue_show = false
      },
      getBillUrl (type) {
        let name = ''
        // 根据type数据 选择调用的Logic
        if (type === '机表收费') {
          name = 'rs/report/machine_bill'
        } else if (type === '物联网收费') {
          name = 'rs/report/iot_bill'
        } else if (type === '超用收费') {
          name = 'rs/report/overuse_bill'
        } else if (type === '其他收费') {
          name = 'rs/report/otherCharge_bill'
        } else if (type === '发卡售气') {
          name = 'rs/report/sendCard_bill'
        } else if (type === '卡表收费') {
          name = 'rs/report/card_bill'
        }
        return name
      },
      report (row) {
        let reprint = reprintGen(this, row)
        return co(reprint)
      },
      print () {
        this.selectInit()
      },
      select () {
        let index = this.model.pageIndex - 1
        if (!this.radio[index]) {
          this.radio.$set(index, [])
        }
        if (this.all[index]) {
          // 数据
          this.rowsdata[index] = Object.assign([], this.model.rows)
          // 勾选
          for (var i = 0; i < this.model.rows.length; i++) {
            this.radio[index].$set(i, true)
          }
        } else {
          // 数据
          this.rowsdata[index] = []
          // 不勾选
          for (var i = 0; i < this.model.rows.length; i++) {
            this.radio[index].$set(i, false)
          }
        }
        let z = 0
        for (let i = 0; i < this.all.length; i++) {
          for (let j = 0; this.rowsdata[i] && j < this.rowsdata[i].length; j++) {
            this.printModel.rows[z++] = this.rowsdata[i][j]
          }
        }
      },
      selectOne (event, row, i) {
        let index = this.model.pageIndex - 1
        if (!this.rowsdata[index]) {
          this.rowsdata[index] = []
        }
        if (!this.radio[index]) {
          this.radio.$set(index, [])
        }
        if (event.target.checked) {
          // 数据
          this.rowsdata[index][i] = row
          // 勾选
          this.radio[index].$set(i, true)
          // 判断是否全部选中
          var allState = true
          if (this.model.rows.length != this.radio[index].length) {
            allState = false
          }
          for (var state of this.radio[index]) {
            if (!state) {
              allState = false
            }
          }
          if (allState) {
            this.all.$set(index, true)
          } else {
            this.all.$set(index, false)
          }
        } else {
          // 数据
          this.rowsdata[index][i] = []
          // 不勾选
          this.radio[index].$set(i, false)
          // 任意取消一个则全选状态设为false
          this.all.$set(index, false)
        }
        let z = 0
        this.printModel.rows = []
        for (let i = 0; i < this.all.length; i++) {
          for (let j = 0; this.rowsdata[i] && j < this.rowsdata[i].length; j++) {
            if (this.rowsdata[i][j] && this.rowsdata[i][j].f_user_id) {
              this.printModel.rows[z++] = this.rowsdata[i][j]
            }
          }
        }
      }
    },
    computed: {
      ischecked () {
        return function (index, i) {
          if (!this.radio[index]) {
            return false
          }
          return this.radio[index][i]
        }
      }
    }
  }
</script>

