<template>
  <div id="unit" class="flex-row" class="foot_tabset">
    <div class="basic-main">
      <div class="flex">
        <criteria-paged :model="model" v-ref:paged>
          <criteria partial='criteria' v-ref:criteria @condition-changed='$parent.search'>
            <div class="form-horizontal select-overspread container-fluid auto" novalidate partial>
              <div class="row" width="100%">
                <div class="col-sm-2">
                  <label class="font_normal_body">表&nbsp;&emsp;&nbsp;号&nbsp;</label>
                  <input type="text" class="input_search" style="width:60%"
                         placeholder='表钢号'
                         v-model="model.f_meternumber"
                         condition="uf.f_meternumber = '{}'">
                </div>
                <div class="col-sm-2">
                  <label class="font_normal_body">组织机构</label>
                  <right-tree :userid.sync='$parent.$parent.obj.userid' :source.sync='$parent.$parent.obj.source'
                              @re-res="$parent.$parent.getRes"></right-tree>
                </div>
                <div class="col-sm-2">
                  <label class="font_normal_body">客户编号</label>
                  <input type="text" class="input_search" style="width:60%"
                         v-model="model.f_userinfo_code"
                         placeholder='客户编号'
                         condition="uf.f_userinfo_code = '{}'">
                </div>
                <div class="col-sm-2">
                  <label class="font_normal_body">客户名称</label>
                  <input type="text" class="input_search" style="width:60%" v-model="model.f_user_name"
                         placeholder='用户姓名'
                         condition="uf.f_user_name = '{}'">
                </div>
                <div class="span" style="float:right;">
                  <button @click="search()" class="button_search button_spacing">查询</button>
                  <button @click="$parent.$parent.clearmsg" class="button_clear button_spacing">清空</button>
                  <export-excel :data="$parent.$parent.getCondition" class="button_export button_spacing"
                                :field="$parent.$parent.getfield"
                                sqlurl="rs/logic/exportfile" sql-name="iot_balance_analyse"
                                template-name='余额分析'
                                :choose-col="true"></export-excel>
                  <div style="float: right" class="button_spacing"
                       :class="{'button_shrink_top':$parent.$parent.criteriaShow,'button_shrink_bottom':!$parent.$parent.criteriaShow}"
                       @click="$parent.$parent.hidden()"></div>
                </div>
              </div>
              <div class="row" v-show="$parent.$parent.criteriaShow">
                <div class="col-sm-2">
                  <label class="font_normal_body">用户类型</label>
                  <v-select placeholder='用户类型' close-on-select
                            v-model="model.f_user_type"
                            value-single="true"
                            @change="$parent.$parent.userTypeChange"
                            :value.sync="model.f_user_type"
                            :options="$parent.$parent.usertypes"
                            condition="uf.f_user_type = '{}'"
                  >
                  </v-select>
                </div>
                <div class="col-sm-2">
                  <label class="font_normal_body">用气性质</label>
                  <v-select placeholder='用气性质' close-on-select
                            value-single="true"
                            v-model="model.f_gasproperties"
                            :value.sync="model.f_gasproperties"
                            :options="$parent.$parent.gasproperties"
                            condition="uf.f_gasproperties = '{}'">
                  </v-select>
                </div>
                <div class="col-sm-2">
                  <label class="font_normal_body">余额差额</label>
                  <input type="number" class="input_search" style="width:60%"
                         v-model="model.start_f_balance"
                         placeholder='最小差额'
                         condition="abs(meterread.f_jval - hd.f_curbalance) >= {}">
                </div>
                <div class="col-sm-2">
                  <label class="font_normal_body">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;至&nbsp;&nbsp;&nbsp;</label>
                  <input type="number" class="input_search" style="width:60%"
                         v-model="model.end_f_balance"
                         placeholder='最大差额'
                         condition="abs(meterread.f_jval - hd.f_curbalance) <= {}">
                </div>
                <div class="col-sm-2">
                  <label class="font_normal_body">余额状态</label>
                  <v-select placeholder='气价状态' close-on-select
                            v-model="model.blanceState"
                            value-single="true"
                            :search="false"
                            :value.sync="model.blanceState"
                            :options="$parent.$parent.blanceStates"
                            condition=" {} "
                  >
                  </v-select>
                </div>
                <div class="col-sm-2">
                  <label class="font_normal_body">气价状态</label>
                  <v-select placeholder='气价状态' close-on-select
                            v-model="model.priceState"
                            value-single="true"
                            :search="false"
                            :value.sync="model.priceState"
                            :options="$parent.$parent.priceStates"
                            condition=" {} "
                  >
                  </v-select>
                </div>
              </div>
            </div>
          </criteria>
          <data-grid id="webmeterhandplan" :model="model" partial='list' class="list_area table_sy" v-ref:grid>
            <template partial='head'>
              <tr>
                <th style="text-align:center">
                  <nobr>序号</nobr>
                </th>
                <th style="text-align:center">
                  <nobr>用户编号</nobr>
                </th>
                <th style="text-align:center">
                  <nobr>用户名称</nobr>
                </th>
                <th style="text-align:center">
                  <nobr>表号</nobr>
                </th>
                <th style="text-align:center">
                  <nobr>上报时间</nobr>
                </th>
                <th style="text-align:center">
                  <nobr>上期指数</nobr>
                </th>
                <th style="text-align:center">
                  <nobr>本期指数</nobr>
                </th>
                <th style="text-align:center">
                  <nobr>用气量</nobr>
                </th>
                <th style="text-align:center">
                  <nobr>用气金额</nobr>
                </th>
                <th style="text-align:center">
                  <nobr>系统单价</nobr>
                </th>
                <th style="text-align:center">
                  <nobr>表端单价</nobr>
                </th>
                <th style="text-align:center">
                  <nobr>系统余额</nobr>
                </th>
                <th style="text-align:center">
                  <nobr>表端金额</nobr>
                </th>
                <th style="text-align:center">
                  <nobr>差额</nobr>
                </th>
                <th style="text-align:center">
                  <nobr>气价状态</nobr>
                </th>
                <th style="text-align:center">
                  <nobr>余额状态</nobr>
                </th>
                <th style="text-align:center">
                  <nobr>操作</nobr>
                </th>
              </tr>
            </template>
            <template partial='body' partial='list' v-ref:grid>
              <tr>
                <td style="text-align:center">{{ $index + 1 }}</td>
              </tr>
              <tr>
                <td style="text-align:center">{{ row.f_userinfo_code }}</td>
              </tr>
              <tr>
                <td style="text-align:center">{{ row.f_user_name }}</td>
              </tr>
              <tr>
                <td style="text-align:center">{{ row.f_meternumber }}</td>
              </tr>
              <tr>
                <td style="text-align:center">{{ row.f_hand_date }}</td>
              </tr>
              <tr>
                <td style="text-align:center">{{ row.f_tablebase }}</td>
              </tr>
              <tr>
                <td style="text-align:center">{{ row.f_last_tablebase }}</td>
              </tr>
              <tr>
                <td style="text-align:center">{{ row.f_oughtamount }}</td>
              </tr>
              <tr>
                <td style="text-align:center">{{ row.f_oughtfee }}</td>
              </tr>
              <tr>
                <td style="text-align:center">{{ row.h_price }}</td>
              </tr>
              <tr>
                <td style="text-align:center">{{ row.m_price }}</td>
              </tr>
              <tr>
                <td style="text-align:center">{{ row.f_curbalance }}</td>
              </tr>
              <tr>
                <td style="text-align:center">{{ row.f_jval }}</td>
              </tr>
              <tr>
                <td style="text-align:center">{{ row.chae }}</td>
              </tr>
              <tr>
                <td style="text-align:center">{{ row.price_state ? '气价异常' : '气价正常' }}</td>
              </tr>
              <tr>
                <td style="text-align:center">{{ row.balance_state ? '余额异常' : '余额正常' }}</td>
              </tr>
              <tr>
                <td style="text-align:center">
                  <button v-if="row.price_state" class=" btn button_spacing button_new"
                          @click="$parent.$parent.$parent.operation('price',row)">调价指令重发
                  </button>
                  <br v-if="row.price_state"/>
                  <button v-if="row.balance_state" class="btn button_spacing button_new" title="以系统为准"
                          @click="$parent.$parent.$parent.operation('system',row)">
                    生成补/扣费指令
                  </button>
                  <br v-if="row.balance_state"/>
                  <button v-if="row.balance_state" class="btn button_spacing button_new" title="以表端为准"
                          @click="$parent.$parent.$parent.operation('meter',row)">
                    生成补/扣费收费记录
                  </button>
                </td>
              </tr>
            </template>
            <template partial='foot'></template>
          </data-grid>
        </criteria-paged>
      </div>
    </div>
  </div>
</template>

<script>
import plugin from 'system-clients/src/plugins/GetLoginInfoService'
import {HttpResetClass, PagedList} from 'vue-client'

let readyGen = async function (self) {
  self.getfield = self.config.excelHeaders
}

export default {
  title: '表端结算分析',
  data () {
    return {
      model: new PagedList('rs/sql/iot_balance_analyse', 50, {
        f_orgid: 'this.model.f_orgid'
      }),
      criteriaShow: false,
      orgCondtionStr: '',
      usertypes: this.$appdata.getParam('用户类型') ? [{label: '全部', value: ''}, ...this.$appdata.getParam('用户类型')] : [],
      isdeal: true,
      condition: '',
      getfield: {},
      priceStates: [{label: '正常', value: ' tc.f_price = meterread.f_price '}, {
        label: '异常',
        value: ' tc.f_price != meterread.f_price '
      }],
      blanceStates: [{label: '正常', value: ' meterread.f_jval = hd.f_curbalance '}, {
        label: '异常',
        value: ' meterread.f_jval != hd.f_curbalance '
      }],
      gasproperties: [],
      config: {
        excelHeaders: {
          'f_userinfo_code': '用户编号',
          'f_user_name': '用户名称',
          'f_meternumber': '表号',
          'f_hand_date': '上报时间',
          'f_tablebase': '上期指数',
          'f_last_tablebase': '本期指数',
          'f_oughtamount': '用气量',
          'f_oughtfee': '用气金额',
          'h_price': '系统单价',
          'm_price': '表端单价',
          'f_curbalance': '系统余额',
          'f_jval': '表端金额',
          'chae': '差额'
        }
      },
      obj: {
        userid: this.$login.f.id,
        source: `tool.getFullTree(this.getRights().where(row.getType() == $organization$))`,
        res: this.$login.f.f_fengongsi,
        resids: this.$login.f.f_orgids
      },
      show: false
    }
  },
  ready () {
    readyGen(this)
  },
  methods: {
    async  operation (type, row) {
      let operinfo = {
        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 msg = '确认要进行操作吗？'
      let resolveMsg = '操作成功'
      if (type === 'price') {
        msg = `当前气价名称：${row.f_price_name}，确认要以用户系统当前气价生成调价指令?`
        resolveMsg = '调价记录已成功生成，请等待指令执行'
      } else if (type === 'system') {
        msg = `确认要以系统余额为准，向表端生成将表端余额改为 ${row.f_curbalance} 的补/扣费指令?`
        resolveMsg = '已成功生成补扣费记录，请等待指令执行'
      } else if (type === 'meter') {
        msg = `确认要以表端余额为准，在系统生成充值/扣费记录，并且修改用户余额为 ${row.f_jval}?`
        resolveMsg = '已成功生成补扣费记录，并处理账户余额'
      }
      this.$showMessage(msg, ['confirm', 'cancel']).then(async (res) => {
        if (res === 'confirm') {
          let http = new HttpResetClass()
          await http.load('POST', 'rs/logic/BalanceAnalyse', {type, row, operinfo}, {
            resolveMsg: resolveMsg,
            rejectMsg: '处理失败！'
          })
        }
      })
    },
    hidden () {
      this.criteriaShow = !this.criteriaShow
    },
    search (args) {
      if (this.orgCondtionStr) {
        this.$refs.paged.$refs.criteria.model.f_orgid = this.orgCondtionStr
        args.condition = `${args.condition}` + 'and uf.f_orgid in ' + this.orgCondtionStr
      } else {
        this.$refs.paged.$refs.criteria.model.f_orgid = '(' + this.$login.orgids + ')'
      }
      this.condition = args.condition
      this.model.search(args.condition, args.model)
    },
    getCondition () {
      var con = `${this.condition} `
      return {
        condition: con,
        f_orgid: this.orgCondtionStr ? this.orgCondtionStr : '(' + this.$login.orgids + ')'
      }
    },
    clearmsg () {
      this.$refs.paged.$refs.cri.model = {}
    },
    getRes (obj) {
      this.orgCondtionStr = plugin.convertToIn(obj.resids) ? plugin.convertToIn(obj.resids) : '(' + this.$login.f.orgid + ')'
    },
    userTypeChange () {
      this.gasproperties = []
      if (this.$refs.paged && this.$refs.paged.$refs.criteria) {
        this.$refs.paged.$refs.criteria.model.f_gasproperties = ''
        this.gasproperties = this.$appdata.getParam(this.$refs.paged.$refs.criteria.model.f_user_type[0])
      } else {
        this.gasproperties = []
      }
    }
  },
  watch: {},
  computed: {}
}
</script>
<style>
</style>
