<template>
  <div @keyup.enter="search" class="flex-row" id="unit">
    <div class="basic-main">
      <criteria-paged :model="model" v-ref:paged>
        <criteria @condition-changed='$parent.selfSearch' partial='criteria' v-ref:cri>
          <div class="form-horizontal select-overspread container-fluid auto" novalidate partial>
            <div class="row">
              <div class="col-sm-2 form-group">
                <label class="font_normal_body">组织机构</label>
                <res-select :initresid='$parent.$parent.curorgid' v-ref:ressel
                            @res-select="$parent.$parent.getorg"
                            is-mul="false"
                            restype='organization'>
                </res-select>
              </div>
              <div class="col-sm-2" >
                <label class="font_normal_body" >开始时间</label>
                <datepicker style="width:60% " placeholder="开始时间"
                            :value.sync="model.startDate"
                            v-model="model.startDate"
                            :format="'yyyy-MM-dd HH:mm:ss'"
                            condition="(f_operate_date >= '{}')"
                ></datepicker>
              </div>
              <div class="col-sm-2" >
                <label class="font_normal_body" >结束时间</label>
                <datepicker style="width:60% " placeholder="结束时间"
                            :value.sync="model.endDate"
                            v-model="model.endDate"
                            :format="'yyyy-MM-dd HH:mm:ss'"
                            condition="(f_operate_date < '{}' )"
                ></datepicker>
              </div>
              <div class="form-group col-sm-2 button-range" style="float: left">
                <button @click="search()" class="button_search button_spacing">查询</button>
                <button @click="$parent.$parent.submit()" class="button_export button_spacing"
                        style="width: max-content">导入到T+系统中
                </button>
                <button @click="clear()" class="button_search button_spacing">清空</button>
              </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">选择导入</th>
              <th>
                <nobr>总金额</nobr>
              </th>
              <th>
                <nobr>总气量</nobr>
              </th>
              <th>
                <nobr>缴费类型</nobr>
              </th>
            </tr>
          </template>
          <template partial='body'>
            <tr>
              <td style="text-align: center;padding: 0;border: 0">
                <input :checked="$parent.$parent.$parent.ischecked(row)"
                       @change="$parent.$parent.$parent.selectOne($event,row,$index)"
                       style="width: 22px;height: 22px;"
                       type='checkbox' v-show="true"/>
              </td>
              <td style="text-align:center"><nobr>{{row.sumpreamount}}</nobr></td>
              <td style="text-align:center"><nobr>{{row.sumpregas}}</nobr></td>
              <td style="text-align:center"><nobr>{{row.f_type}}</nobr></td>
            </tr>
          </template>
          <template partial='foot'></template>
        </data-grid>
      </criteria-paged>
    </div>
  </div>


</template>

<script>
  import { PagedList } from 'vue-client'
  import * as Util from '../../../../../Util'

  export default {
    data () {
      return {
        model: new PagedList('rs/sql/getSellinggas', 20, {startDate: 'this.model.startDate', endDate: 'this.model.endDate'}),
        selecteddata: {
          data: []
        },
        showmergeinfo: false,
        f_orgid: this.getorg([this.$login.f.orgid]),
        all: false
      }
    },
    title: '另外的表数据导入t+系统页面',
    ready () {
      this.$refs.paged.$refs.cri.model.startDate = Util.toStandardTimeStringForFirstDay();
      this.$refs.paged.$refs.cri.model.endDate = Util.toStandardTimeStringForLastDay();
    },
    computed: {
      ischecked () {
        return function (row) {
          return this.selecteddata.data.includes(row)
        }
      }
    },
    events: {
      init (val) {
        this.showmergeinfo = false
        this.search()
      }
    },
    methods: {
      search () {
        this.$refs.paged.$refs.cri.search()
      },
      async selfSearch (args) {
        this.selecteddata.data = []
        this.rowsdata = []
        this.all = false
        if (!this.f_orgid) {
          this.getorg([this.$login.f.orgid])
        }
        console.log('查询。。。', this.f_orgid)
        if (this.f_orgid) {
          args.condition = `${args.condition} and f_orgid in ${this.f_orgid}`
          // this.model.params.orgid = this.f_orgid
        }
        await this.model.search(args.condition, args.model)
        this.length = this.model.count
      },

      // 清空
      clear () {
        this.$refs.paged.$refs.cri.model = {}
      },
      getorg (val) {
        this.f_orgid = this.$login.convertToIn(val)
        this.f_filialeids = val[0]
        console.log('yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy', val)
      },
      selectOne (event, row, i) {
        if (this.all) {
          this.all = !this.all
        }
        if (event.target.checked) {
          this.selecteddata.data.push(row)
          console.log('yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy1111', this.selecteddata.data)
          if (this.length === this.selecteddata.data.length) {
            this.all = true
          }
        } else {
          this.all = false
          this.selecteddata.data.splice(this.selecteddata.data.indexOf(row), 1)
        }
      },
      submit () {
        let condition = this.selecteddata.data
        console.log('yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy', condition)
        this.$resetpost('rs/logic/money', {condition: condition})
        this.$showAlert('发送成功','success',2000)
        this.showmergeinfo = true
      }
    }

  }
</script>
