<template>
  <div style="height: 100%;">
    <criteria-paged :model="model" v-ref:paged>
      <criteria partial='criteria' v-ref:criteria @condition-changed="$parent.selfSearch">
        <div novalidate class="form-inline auto" partial>
          <div class="form-group">
            <input type="text" class="form-control" v-model="model.f_userinfo_id" placeholder='客户编号'
                   condition="f_userinfo_id = '{}'">
          </div>
          <div class="form-group">
            <label class="control-label">操作日期</label>
            <datepicker placeholder="开始日期"
                        v-model="model.startDate"
                        :value.sync="model.startDate"
                        :format="'yyyy-MM-dd'"
                        condition="f_operate_date >= '{}'"
            ></datepicker>
            <datepicker placeholder="结束日期"
                        v-model="model.endDate"
                        :value.sync="model.endDate"
                        :format="'yyyy-MM-dd'"
                        condition="f_operate_date <= '{}'"
            ></datepicker>
          </div>
          <div class="form-group">
            <v-select :value.sync="model.f_operat_type" v-model="model.f_operat_type"
                      :options='$parent.$parent.operatType' placeholder='操作类型'
                      close-on-select
                      condition="f_operat_type = '{}'"
                      ></v-select>
          </div>
          <div class="form-group">
            <label>组织机构</label>
            <right-tree :userid.sync='$parent.$parent.userid' :source.sync='$parent.$parent.source'
                        @re-res="$parent.$parent.getRes"></right-tree>
          </div>
          <div class="form-group">
            <button class="btn btn-success" @click="search()">查 询</button>
            <button class="btn btn-success" @click="$parent.$parent.clear()">清空</button>
            <print-data :model="$parent.$parent.printModel" :field="$parent.$parent.fields" :defaultfield="$parent.$parent.defaultfield" print-name="购气流水"  :sumsmodel="$parent.$parent.sumsmodel" :is-selected="true" @print-data="$parent.$parent.print()"></print-data>
          </div>
        </div>
      </criteria>
      <data-grid :model="model" 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>
          </tr>
        </template>
        <template partial='body'>
          <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;">{{$index+1}}</td>
          <th style="text-align: center;">{{row.f_preamount}}</th>
          <th style="text-align: center;">{{row.f_pregas}}</th>
          <th style="text-align: center;">{{row.f_operate_date}}</th>
          <th style="text-align: center;">{{row.f_operat_type}}</th>
        </template>
      </data-grid>
    </criteria-paged>
  </div>
</template>

<script>
  let readySomething = async function (self) {
    self.$refs.paged.$refs.criteria.model.startDate = self.$login.toStandardDateString()
    self.$refs.paged.$refs.criteria.model.endDate = self.$login.toStandardDateString()
    self.$refs.paged.$refs.criteria.search()
    self.printData = self.model
  }
  import { PagedList } from 'vue-client'

  export default {
    title: '购气流水',
    data() {
      return {
        condition: '',
        model: new PagedList('rs/sql/getPurchaseWater', 20),
        modelval: [],
        printshow: false,
        //控制单选
        radio: [],
        //选中的页
        all: [],
        //row数据
        rowsdata: [],
        fields:{'f_preamount': '金额','f_pregas':'气量','f_operate_date': '购气时间','f_operat_type': '操作类型'},
        defaultfield: [
          "f_preamount","f_pregas","f_operate_date","f_operat_type"
        ],
        sumsmodel:{},
        printModel: {
          rows: []
        },
        filialeNameStr: null,
        filialeCodeStr: this.$login.f.f_orgids,
        userid: this.$login.f.id,
        source: `tool.getFullTree(this.getRights().where(row.getType() == $organization$))`,
      }
    },
    ready() {
      readySomething(this).then(() => {
        this.$emit('ready')
      }).catch((error) => {
        this.$emit('error', error)
      })
    },
    methods: {
      search() {
        this.selfSearch()
      },
      selfSearch(args) {
        this.condition = `${args.condition} and f_filialeids like '${this.filialeCodeStr}%'`
        this.model.search(this.condition, args.model)
        this.selectInit()
      },
      clear() {
        Object.keys(this.$refs.paged.$refs.criteria.model).forEach((key) => {
          this.$refs.paged.$refs.criteria.model[key] = null
        })
      },
      getRes(obj) {
        this.filialeNameStr = obj.res
        this.filialeCodeStr = obj.resids
      },
      //多选框初始化
      selectInit(){
        this.rowsdata = []
        this.all = []
        this.radio = []

      },
      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;
        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.printModel.rows[z++] = this.rowsdata[i][j]
            }
          }
        }
      }
    },
    computed: {
      ischecked(){
        return function (index,i) {
          if(!this.radio[index]){
            return false
          }
          return this.radio[index][i]
        }
      },
      operatType(){
          return [{label: '全部', value: ''}, ...this.$appdata.getParam('操作类型')]
      }
    },
    watch: {
    }
  }
</script>

<style lang="less">
</style>
