<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-4" >-->
            <!--<label class="font_normal_body" >&nbsp;开始时间</label>-->
            <!--<datepicker placeholder="起始时间" style="width:60%"-->
            <!--:value.sync="model.f_start_date"-->
            <!--v-model="model.f_start_date"-->
            <!--:format="'yyyy-MM-dd'"-->
            <!--condition="f_operate_date >= '{} 00:00:00' "-->
            <!--&gt;</datepicker>-->
            <!--</div>-->
            <!--<div class="col-sm-4" >-->
            <!--<label class="font_normal_body" >结束时间</label>-->
            <!--<datepicker placeholder="结束时间" style="width:60%"-->
            <!--:value.sync="model.f_end_date"-->
            <!--v-model="model.f_end_date"-->
            <!--:format="'yyyy-MM-dd'"-->
            <!--condition="f_operate_date <= '{} 23:59:59' "-->
            <!--&gt;</datepicker>-->
            <!--</div>-->
            <!--<div class="span" style="float:right;">-->
            <!--<button class="button_search"  @click="search()" v-el:cba>查询</button>-->
            <!--&lt;!&ndash;<print-data :model="$parent.$parent.printModel" :field="$parent.$parent.fields" :defaultfield="$parent.$parent.defaultfield" print-name="购气流水"&ndash;&gt;-->
            <!--&lt;!&ndash;:is-selected="true" @print-data="$parent.$parent.print()"></print-data>&ndash;&gt;-->
            <!--</div>-->
            <!--</div>-->
          </div>


        </criteria>
        <data-grid :model="model" class="list_area table_sy" partial='list' v-ref:grid>
          <template partial='head'>
            <tr>
              <!--<th style="padding: 0;border: 0">-->
              <!--<input type="checkbox" id="f_operator" v-model="$parent.$parent.$parent.all[$parent.model.pageIndex-1]"-->
              <!--@change="$parent.$parent.$parent.select()" style="width: 22px;height: 22px"/>-->
              <!--</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' partial='list' v-ref:grid>
            <tr >
              <!--<td style="text-align: center;padding: 0;border: 0">-->
              <!--<input type='checkbox' @change="$parent.$parent.$parent.selectOne($event,row,$index)"-->
              <!--:checked="$parent.$parent.$parent.ischecked($parent.model.pageIndex-1,$index)" style="width: 22px;height: 22px;"/>-->
              <!--</td>-->
              <td style="text-align:center"><nobr>{{row.newprice}}</nobr></td>
              <td style="text-align:center"><nobr>{{row.oldprice}}</nobr></td>
              <td style="text-align:center"><nobr>{{row.f_perform_date}}</nobr></td>
              <td style="text-align:center"><nobr>{{row.f_operate_date}}</nobr></td>
              <td style="text-align:center"><nobr>{{row.f_operator}}</nobr></td>
              <td style="text-align:center"><nobr>{{row.f_change_reason}}</nobr></td>
            </tr>
          </template>
          <template partial='foot'></template>
        </data-grid>
      </criteria-paged>
    </div>
  </div>
</template>

<script>
  import {PagedList} from 'vue-client'
  import co from 'co'
export default {
    title: '气价变更',
    data () {
      return {
        condition: '1=1',
        model: new PagedList('rs/sql/sale_priceChangeQuery', 20, {}),
        // 控制单选
        radio: [],
        // 选中的页
        all: [],
        // row数据
        rowsdata: [],
        fields: {'f_preamount': '购气金额', 'f_pregas': '购气量', 'f_operate_date': '缴费时间', 'f_operator': '操作员', 'f_comments': '备注'},
        defaultfield: [
          'f_preamount', 'f_pregas', 'f_operate_date', 'f_operator', 'f_comments'
        ]
      }
    },
    props: ['row'],
    ready () {
      this.$refs.paged.$refs.criteria.search()
    },
    methods: {
      search () {
        this.condition = `${this.$refs.paged.$refs.criteria.condition} and f_userinfo_id = '${this.row.f_userinfo_id}'`
        this.model.search(this.condition, this.model)
      },
      // 多选框初始化
      selectInit () {
        this.rowsdata = []
        this.all = []
        this.radio = []
      },
      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>

