<template>
  <div id="unit" class="flex-row">
    <div class="basic-main">
      <div class="flex">
        <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">
                <div class="form-group 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="f_userinfo_code = '{}'" >
                </div>
                <div class="form-group 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="f_user_name = '{}'">
                </div>
                <div class="form-group col-sm-2">
                  <label class="font_normal_body">客户地址</label>
                  <input type="text" class="input_search" style="width:60%" style="width:60%" v-model="model.f_address" placeholder='客户地址'
                         condition="u3.f_address like '%{}%'">
                </div>
                <div class="span" style="float:right;">&nbsp;&nbsp;
                  <button class="button_search" @click="search()" v-el:cx>查询</button>
                  <button class="button_clear" @click="$parent.$parent.clear()">清空</button>
<!--                  <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" >
                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">归属起始</label>
                  <datepicker placeholder="开始日期"
                              style="width:60%"
                              class="datepicker"
                              v-model="model.startDate"
                              :value.sync="model.startDate"
                              :format="'yyyy-MM-dd'"
                  ></datepicker>
                </div>
                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">归属结束</label>
                  <datepicker placeholder="结束日期"
                              style="width:60%"
                              v-model="model.endDate"
                              :value.sync="model.endDate"
                              :format="'yyyy-MM-dd'"
                  ></datepicker>
                </div>
                <div class="form-group col-sm-2">
                  <label class="font_normal_body">电&emsp;&emsp;话</label>
                  <input type="text" class="input_search" style="width:60%"  v-model="model.f_user_phone" placeholder='电话'
                         condition="f_user_phone like '%{}%'">
                </div>
                <div class="form-group col-sm-2">
                  <label class="font_normal_body">表&emsp;&emsp;号</label>
                  <input type="text" class="input_search" style="width:60%" style="width:60%" v-model="model.f_meternumber" placeholder='表号'
                         condition="f_meternumber like '%{}%'">
                </div>
                <div class="form-group col-sm-2">
                  <label class="font_normal_body">状&emsp;&emsp;态</label>
                  <v-select id="price_states"
                            v-model="model.price_states"
                            placeholder='未调价'
                            style="width: 60%"
                            :value.sync="model.price_states"
                            :options='$parent.$parent.price_states'
                            condition="f_new_price {}"
                            close-on-select></v-select>
                </div>
                <div class="form-group col-sm-2">
                  <label class="font_normal_body">气表类型</label>
                  <v-select id="f_source"
                            v-model="model.f_source"
                            placeholder='气表类型'
                            style="width: 60%"
                            :value.sync="model.f_source"
                            :options='$parent.$parent.source'
                            condition="f_source='{}'"
                            close-on-select></v-select>
                </div>
                <div class="form-group col-sm-2">
                  <label class="font_normal_body">用户类型</label>
                  <v-select id="f_user_tye"
                            v-model="model.f_user_type"
                            placeholder='用户类型'
                            style="width: 60%"
                            :value.sync="model.f_user_type"
                            :options='$parent.$parent.userTypes'
                            condition="u2.f_user_type = '{}'"
                            close-on-select></v-select>
                </div>
                <div class="form-group col-sm-2">
                  <label class="font_normal_body">气&emsp;&emsp;价</label>
                  <v-select id="f_price_name"
                            v-model="model.price_name"
                            placeholder='气价名称'
                            style="width: 60%"
                            :value.sync="model.price_name"
                            :options='$parent.$parent.price_name'
                            condition="sp.id = '{}'"
                            close-on-select></v-select>
                </div>
              </div>
            </div>
          </criteria>
          <data-grid :model="model" partial='list' class="list_area table_sy" v-ref:grid >
            <template partial='head'>
              <tr>
                <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>
                <th><nobr>差价来源</nobr></th>
                <th><nobr>抄表日期</nobr></th>
                <th><nobr>操作</nobr></th>
              </tr>
            </template>
            <template partial='body'>
              <td style="text-align: center;">{{$index+1}}</td>
              <th style="text-align: center;">{{row.f_userinfo_code}}</th>
              <th style="text-align: center;">{{row.f_meternumber}}</th>
              <th style="text-align: center;">{{row.f_user_name}}</th>
              <th style="text-align: center;">{{row.f_user_type}}</th>
              <th style="text-align: center;">{{row.f_table_remaining}}</th>
              <th style="text-align: center;">{{row.f_meter_type}}</th>
              <th style="text-align: center;">{{row.f_price_name}}</th>
              <!--              <th style="text-align: center;">{{row.f_address}}</th>-->
              <th style="text-align: center;">{{row.f_old_price?row.f_old_price:row.f_price}}</th>
              <th style="text-align: center;">
                <div class="form-group" style="margin-bottom:0px;"
                     v-if="!row.f_new_price>0">
                  <input type="text" class="form-control" v-model="row.newprice"  @blur="$parent.$parent.$parent.enter(row)"
                         v-next-el="{id: $index+1}"  :id="$index" style="width: 100px;padding: 6px;" :disabled="row.state === '开始'">
                </div>
                <div v-else>{{row.f_new_price}}</div>
              </th>
              <th style="text-align: center;">{{row.f_change_price}}</th>
              <th style="text-align: center;">{{row.f_last_tablebase}}</th>
              <th style="text-align: center;">{{row.f_tablebase}}</th>
              <th style="text-align: center;">{{row.pregas}}</th>
              <th style="text-align: center;">{{row.f_surplus_fee}}</th>
              <th style="text-align: center;">{{row.f_source}}</th>
              <th style="text-align: center;">{{row.f_input_date}}</th>
              <th style="text-align: center;">
                <button type="button" name="button" class="btn btn-link" v-if="row.f_new_price"
                        @click.stop="$parent.$parent.$parent.delectHand(row)  && row.states === 1">冲正
                </button>
              </th>
            </template>
          </data-grid>
        </criteria-paged>
      </div>
    </div>
  </div>
</template>

<script>
  import { HttpResetClass } from 'vue-client'
  import { PagedList } from 'vue-client'
  import Vue from 'vue'
  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.$resetpost('rs/sql/getpriceid', {data: {f_filialeids: self.$login.f.orgid}}, {resolveMsg: null, rejectMsg: null}).then((res) => {
      console.log(res.data)
      let rs = []
      for (let i = 0; i < res.data.length; i++) {
        if (res.data[i].f_price_type === '固定气价') {
          let temp = {
            label: res.data[i].f_price_name,
            flage: res.data[i].f_user_type,
            value: res.data[i].id
          }
          rs.push(temp)
        }
      }
      console.log(rs)
      self.pricers = [{label: '全部', value: '', flage: ''}, ...rs]
      self.$refs.paged.$refs.criteria.search()
    })
  }

  export default {
    title: '余量补差',
    data () {
      return {
        criteriaShow: false,
        pricers: [{label: '全部', value: '', flage: ''}],
        price_name: [{label: '全部', value: ''}],
        price_states: [{label: '全部', value: ''}, {label: '已调价', value: ' >0'}, {label: '未调价', value: ' <=0'}],
        source: [{label: '卡表抄表', value: '卡表抄表'}, {label: '机表抄表', value: '机表抄表'}],
        userTypes: this.$appdata.getParam('用户类型') ? [{label: '全部', value: ''}, ...this.$appdata.getParam('用户类型')] : [],
        condition: '',
        model: new PagedList('rs/sql/remainingAdjuest', 20, {startDate: 'this.model.startDate', endDate: 'this.model.endDate'}),
        f_new_price: 0.0,
      }
    },
    ready () {
      readySomething(this)
    },
    methods: {
      hidden () {
        this.criteriaShow = !this.criteriaShow
      },
      close () {
        this.show = false
        this.f_new_price = 0.0
      },
      async enter (row) {
        let http = new HttpResetClass()
        let param = {
          f_state: '有效',
          f_new_price: parseFloat(row.newprice),
          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,
          f_start_date: this.model.model.startDate + ' 00:00:00',
          f_end_date: this.model.model.endDate + ' 23:59:59',
          row: row
        }
        await http.load('POST', 'rs/logic/remaininga', {data: param}, {resolveMsg: null, rejectMsg: null})
        this.selfSearch(this.model)
      },
      search () {
        this.$refs.paged.$refs.criteria.$refs.partial.search()
      },
      selfSearch (args) {
        this.condition = `${args.condition} and u2.f_orgid = '${this.$login.f.orgid}'`
        this.model.search(this.condition, args.model)
      },
      clear () {
        Object.keys(this.$refs.paged.$refs.criteria.model).forEach((key) => {
          this.$refs.paged.$refs.criteria.model[key] = ''
        })
      },
      // 冲正
      delectHand (row) {
        this.$showMessage('冲正后不可恢复，确定冲正吗？', ['confirm', 'cancel']).then((res) => {
          if (res === 'confirm') {
            row.f_operator = this.$login.f.name
            row.f_operatorid = this.$login.f.id
            row.f_orgid = this.$login.f.orgid
            row.f_orgname = this.$login.f.orgs
            row.f_depid = this.$login.f.depids
            row.f_depname = this.$login.f.deps
            this.$resetpost('rs/logic/remainingAdjustRush', {data: row}).then(() => {
              // this.$dispatch('refresh', '正在处理', row)
              this.selfSearch(this.model)
            })
          }
        })
      },
    },
    computed: {
    },
    watch: {
    }
  }
</script>

<style >
</style>
