<template style="position: absolute;">
  <div :class="{'basic-main':!showItem,'binary-left':showItem}" style="height:98%;">
    <criteria-paged :model="model" v-ref:paged>
      <criteria partial='criteria' v-ref:criteria @condition-changed='$parent.selfSearch'>
        <div novalidate class="form-horizontal select-overspread container-fluid auto" partial>
          <div class="row" width="100%">
            <div class="row">
              <div class="col-sm-2 form-group">
                <label class="font_normal_body">组织机构</label>
                <res-select
                  :initresid='$parent.$parent.curorgid'
                  @res-select="$parent.$parent.getorgCur"
                  is-mul="false"
                  v-model="$parent.$parent.curorgid"
                  :value.sync="$parent.$parent.curorgid"
                  restype='organization'
                >
                </res-select>
              </div>
              <div class="col-sm-2 form-group">
                <label class="font_normal_body" for="usercode">用户编号</label>
                <input id="usercode" class="input_search" condition="f_userinfo_code = '{}'"
                       placeholder="请输入要查询的用户编号" style="width:60%" type="text"
                       v-el:qjmc v-model="model.f_userinfo_code" v-next-el="cx"
                >
              </div>
              <div class="col-sm-2 form-group">
                <label class="font_normal_body" for="username">用户姓名</label>
                <input id="username" class="input_search" condition="f_user_name = '{}'"
                       placeholder="请输入要查询的用户姓名" style="width:60%" type="text"
                       v-el:qjmc v-model="model.f_user_name" v-next-el="cx"
                >
              </div>

              <div class="span" style="float:right;">
                <button class="button_search" @click="search()" v-el:cba>查询</button>
                <button class="button_export" @click="$parent.$parent.importFile">导入</button>
                <button class="button_search" @click="$parent.$parent.clear">清空</button>
                <button @click="$parent.$parent.delete()" v-show="$parent.$parent.deleteShow" class="button_delete">
                  批量删除
                </button>
                <div class="button_spacing" style="float:right;"
                     :class="{'button_shrink_top':$parent.$parent.criteriaShow,'button_shrink_bottom':!$parent.$parent.criteriaShow}"
                     @click="$parent.$parent.hidden()"></div>
              </div>
            </div>
          </div>
          <div class="row" v-show="$parent.$parent.criteriaShow">
            <div class="col-sm-2 form-group">
              <label class="font_normal_body">开始时间</label>
              <datepicker :format="'yyyy-MM-dd'"
                          :value.sync="model.f_start_date"
                          condition="f_start_date >= '{} 00:00:00'"
                          v-model="model.f_start_date" style="width: 55%">
              </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"
                          condition="f_end_date <= '{} 23:59:59'"
                          v-model="model.f_end_date" style="width: 55%">
              </datepicker>
            </div>
            <div class="col-sm-2 form-group">
              <label class="font_normal_body" title="参数名称:补差状态">补差状态</label>
              <v-select id="f_adjust_state"
                        v-model="model.f_state"
                        placeholder='状态'
                        condition="f_state ='{}'"
                        :value.sync="model.f_state"
                        :options='$parent.$parent.states'
                        close-on-select clear-button>
              </v-select>
            </div>
            <div class="col-sm-2 form-group">
              <label class="font_normal_body" title="参数名称:是否缴费">是否缴费</label>
              <v-select id="f_whether_pay"
                        v-model="model.f_whether_pay"
                        placeholder='是否缴费'
                        condition="f_whether_pay ='{}'"
                        :value.sync="model.f_whether_pay"
                        :options='$parent.$parent.whetherPay'
                        close-on-select clear-button>
              </v-select>
            </div>
            <div class="col-sm-2 form-group">
              <label class="font_normal_body" for="operator">操作人</label>
              <input id="operator" class="input_search" condition="f_operator like '%{}%'"
                     placeholder="请输入要查询的操作人" style="width:60%" type="text"
                     v-el:qjmc v-model="model.f_operator" v-next-el="cx"
              >
            </div>
          </div>
        </div>
      </criteria>
      <data-grid id="webmeterinfo" :model="model" class="list_area table_sy" partial='list' v-ref:grid>
        <template partial='head'>
          <tr>
            <th>
              <input type="checkbox" v-show="$parent.$parent.$parent.deleteShow"
                     @click="$parent.$parent.$parent.isAll(model.pageIndex)">
            </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">
            <input type="checkbox" v-show="$parent.$parent.$parent.deleteShow" v-model="$parent.$parent.$parent.ids"
                   :value="row.id">
          </td>
          <td style="text-align: center;">{{ $index + 1 }}</td>
          <td style="text-align: center;">{{ row.f_userinfo_code }}</td>
          <td style="text-align: center">{{ row.f_user_name }}</td>
          <td style="text-align: center">{{ row.f_change_price }}</td>
          <td style="text-align: center">{{ row.f_old_price }}</td>
          <td style="text-align: center">{{ row.f_new_price }}</td>
          <!--            <td style="text-align: center" >{{row.f_device_id}}</td>-->
          <td style="text-align: center">{{ row.f_real_amount }}</td>
          <td style="text-align: center">{{ row.f_surplus_fee }}</td>
          <td style="text-align: center">{{ row.f_start_date }}</td>
          <td style="text-align: center">{{ row.f_end_date }}</td>
          <td style="text-align: center">{{ row.f_type }}</td>
          <!--            <td style="text-align: center" >{{row.f_vender}}</td>-->
          <td style="text-align: center">{{ row.f_whether_pay }}</td>
          <td style="text-align: center">{{ row.f_operator }}</td>
          <td style="text-align: center">{{ row.f_state }}</td>
        </template>
      </data-grid>
    </criteria-paged>
  </div>
  <modal :show.sync="show" width="500px" title="文件导入" v-ref:modal large backdrop="false">
    <article slot="modal-body" class="modal-body">
      <file-upload class="my-file-uploader " name="UploadFile" class="btn btn-success" style="border-radius: 2px;"
                   action="rs/file/uploadFile" tagname="调价导入" v-ref:file :headers="headers" multiple></file-upload>
    </article>
    <footer slot="modal-footer" class="modal-footer">
    </footer>
  </modal>
</template>
<script>
import {PagedList} from 'vue-client'
import Vue from 'vue'

export default {
  title: '调价导入',
  data() {
    return {
      model: new PagedList('rs/sql/getsurplus', 20, {items: '"*"', tablename: '"t_meterinfo"', orderitem: '"id"'}),
      criteriaShow: false,
      curorgid: [this.$login.f.orgid],
      headers: {'username': Vue.$login.f.name},
      ids: [],
      f_state: '',
      f_whether_pay: '',
      deleteShow: false,
      f_filialeid: this.$login.f.orgid,
      pages: [],
      whetherPay: this.$appdata.getParam('是否缴费') ? [{
        label: '全部',
        value: ''
      }, ...this.$appdata.getParam('是否缴费')] : [],
      states: this.$appdata.getParam('补差状态') ? [{
        label: '全部',
        value: ''
      }, ...this.$appdata.getParam('补差状态')] : [],
      show: false
    }
  },
  ready() {
    this.search()
  },

  methods: {
    clear() {
      Object.keys(this.$refs.paged.$refs.criteria.model).forEach((key) => {
        this.$refs.paged.$refs.criteria.model[key] = []
      })
      this.deleteShow = false
    },
    delete() {
      if (this.ids.length !== 0) {
        this.$refs.paged.$refs.criteria.condition = this.$refs.paged.$refs.criteria.condition + 'and id in (' + this.ids + ')'
      }
      this.$showMessage('删除后不可恢复，确认删除？', ['confirm', 'cancel']).then((res) => {
        if (res === 'confirm') {
          this.deleteAdjustImport(this.$refs.paged.$refs.criteria.condition).then((res) => {
            this.$dispatch('success')
            this.search()
            this.ids = []
          }).catch((error) => {
            this.$dispatch('error', error)
          })
        }
      })
    },
    deleteAdjustImport(condition) {
      console.log(condition)
      return this.$resetpost('rs/logic/deleteAdjustImport', {condition: condition}, {
        resolveMsg: '删除成功',
        rejectMsg: '删除失败'
      })
    },
    isAll(page) {
      // console.log(this.model.ids)
      if (this.pages.includes(page)) { // 已添加,取消动作
        // 从ids中删除本页所有数据的ids
        this.addOrRemove(false)
        this.pages = this.pages.filter((item) => {
          return item !== page
        })
      } else { // 未添加
        this.pages.push(page) // 添加id，添加动作
        this.addOrRemove(true)
      }
    },
    addOrRemove(is) {
      let that = this
      if (is) { // 添加本页所有数据到ids中
        this.ids = []
        this.model.rows.forEach((row) => {
          if (!that.ids.includes(row.id)) {
            that.ids.push(row.id)
          }
        })
      } else { // 从ids中取消所有的数据
        this.ids = []
        this.model.rows.forEach((row) => {
          that.ids = that.ids.filter((item) => {
            return item !== row.id
          })
        })
      }
    },
    hidden() {
      this.criteriaShow = !this.criteriaShow
    },
    search() {
      this.$refs.paged.$refs.criteria.search()
    },
    selfSearch(args) {
      if (!this.f_orgid) {
        this.getorgCur([this.$login.f.orgid])
      }
      if (this.f_orgid) {
        args.condition = `${args.condition} and f_orgid  in ${this.f_orgid}`
        this.model.search(args.condition, args.model)
      }
      if (args.condition.includes('f_state =\'有效\' and f_whether_pay =\'否\'')) {
        this.deleteShow = true
      } else {
        this.deleteShow = false
      }
    },

    getorgCur(val) {
      console.log('组织事件返回。。', val[0])
      this.f_orgid = this.$login.convertToIn(val)
      this.f_filialeids = val[0]
      this.$parent.f_filialeid = val[0]
      this.search()
    },
    importFile() {
      this.show = true
    }

  },
  events: {
    onFileUpload: function (file, res) {
      console.log(this.$login.f, '======')
      console.log(res)
      let batchParam = {
        filepath: res.f_downloadpath,
        f_operator: this.$login.f.name,
        f_operatorid: this.$login.f.id,
        f_orgid: this.$login.f.orgid,
        f_orgname: this.$login.f.orgs,
        f_depid: this.$login.f.depids,
        f_depname: this.$login.f.deps
      }
      let jobParam = {
        f_operatorid: this.$login.f.id,
        total: 0,
        operator: this.$login.f.name,
        jobType: '调价补差'
      }
      let param = {
        jobParam: jobParam,
        batchParam: batchParam
      }
      console.log(param, '++++++++++++++++++++++++++')
      this.$resetpost('rs/business/batchRunExcelDefault/thirdSurplusSave/thirdSurplus', {data: param}, {
        resolveMsg: null,
        rejectMsg: null
      }).then((res) => {
        // window.location.href = '/excel/表具导入异常信息.txt';
        this.show = false
        this.search()
      })
      this.$showAlert('已生成批量导入任务', 'success', 2000)
      this.show = false
    }
  }
}
</script>
<style>
.form-input-group label {
  text-align: right;
  width: auto;
}

.datapanel {
  color: #333;
  background-color: white;
  box-shadow: darkgrey 0.5px 0.5px 0.5px 0.5px;
  padding: 10px 30px 10px 30px;
  border-radius: 15px;
}
</style>

