<template>
  <partial-view v-ref:pv>
    <criteria-paged :model="model" :pager="false" v-ref:paged>
      <div partial="list" style="overflow-y: scroll;height: 90%">
        <accordion :one-at-atime="true" type="info">
          <panel :is-click="false" :is-open="true" type="info" v-for="row in model.rows">
            <span @click="$parent.$parent.$parent.selected(row)" class="panel-title row" slot="header"
                  style="cursor: pointer;">
              <div class="col-sm-11">
                <div class="row">
                  <strong :title="row.f_userinfo_code" class="col-sm-4"
                          show-overflow-tooltip="true">{{ row.f_userinfo_code.length > 9 ? '' + row.f_userinfo_code.substring(0, 9) + '...' : row.f_userinfo_code }}</strong>
                  <!--<span class="col-sm-3" title="用户名称">{{row.f_user_name}}</span>-->
                  <span class="col-sm-5" title="抄表日期">{{ row.f_input_date.substr(0, 10) }}</span>
                  <!--<span class="col-sm-3" title="用气金额">{{row.f_oughtfee}}</span>-->
                  <span class="col-sm-3" title="是否欠费">{{ row.f_whether_pay === '是' ? '已缴' : '欠费' }}</span>
                </div>
              </div>
<!--               <div class="col-sm-1">-->
<!--                <dropdown class="auto">-->
<!--                  <button data-toggle="dropdown" style="border: 0px;background: none;" type="button">-->
<!--                    <span class="glyphicon glyphicon-th-list"></span>-->
<!--                  </button>-->
<!--                  <ul class="dropdown-menu dropdown-menu-right" slot="dropdown-menu" style=" min-width: 60px;">-->
<!--                    <li>-->
<!--                      <a @click.stop="$parent.$parent.$parent.operate('票据补打', row)" href="#">票据补打</a>-->
<!--                    </li>-->
<!--                    <li>-->
<!--                      <a @click.stop="$parent.$parent.$parent.operate('电子票补打', row)" href="#">电子票补打</a>-->
<!--                    </li>-->
<!--                  </ul>-->
<!--                </dropdown>-->
<!--              </div>-->

            </span>
            <div class="panel-body" v-if="$parent.$parent.$parent.isSelected(row)">
              <div class="auto">
                <div class="row">
                  <label class="col-sm-4">客户名称:</label>
                  <span class="col-sm-8" style="text-align:left">{{ row.f_user_name }}</span>
                </div>
                <div class="row">
                  <label class="col-sm-4">上期底数:</label>
                  <span class="col-sm-2" style="text-align:left">{{ row.f_last_tablebase }}</span>
                  <label class="col-sm-4">本期底数:</label>
                  <span class="col-sm-2" style="text-align:left">{{ row.f_tablebase }}</span>
                </div>
                <!--<div class="row">-->
                <!--<label class = "col-sm-4">本期底数:</label>-->
                <!--<pre style="padding:0" class = "col-sm-8">{{row.f_tablebase}}</pre>-->
                <!--</div>-->
                <div class="row">
                  <label class="col-sm-4">用气量:</label>
                  <pre style="text-align:left" class="col-sm-2">{{ row.f_oughtamount }}</pre>
                  <label class="col-sm-4">用气金额:</label>
                  <span class="col-sm-2" style="text-align:left">{{ row.f_oughtfee }}</span>
                </div>
                <!--<div class="row">-->
                <!--<label class = "col-sm-4">用气金额:</label>-->
                <!--<span class = "col-sm-8" style="text-align:left">{{row.f_oughtfee}}</span>-->
                <!--</div>-->
                <div class="row">
                  <label class="col-sm-4">抄表员:</label>
                  <span style="text-align:left" class="col-sm-2">{{ row.f_inputtor }}</span>
                  <label class="col-sm-4">是否缴费:</label>
                  <span class="col-sm-2" style="text-align:left">{{ row.f_whether_pay }}</span>
                </div>
                <div class="row">
                  <label class="col-sm-4" title="所属月份">下发日期:</label>
                  <span class="col-sm-8" style="text-align:left">{{ row.f_hand_date }}</span>
                </div>
                <div class="row">
                  <label class="col-sm-4">抄表时间:</label>
                  <span class="col-sm-8" style="text-align:left">{{ row.f_input_date }}</span>
                </div>
                <div class="row">
                  <label class="col-sm-4">录入时间:</label>
                  <span class="col-sm-8" style="text-align:left">{{ row.f_operate_date }}</span>
                </div>
                <div class="row">
                  <label class="col-sm-4">缴费时间:</label>
                  <span class="col-sm-8" style="text-align:left">{{ row.f_sellinggas_date }}</span>
                </div>
              </div>
            </div>
            <div style="margin-top: 8px;background: #FFF;" v-if="!$parent.$parent.$parent.isSelected(row)">
              <span>上期底数:{{ row.f_last_tablebase }}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;本期指数:{{ row.f_tablebase }}</span>
            </div>
          </panel>
        </accordion>
      </div>

    </criteria-paged>
  </partial-view>
  <reissue-bill :data="reissue_data" :show.sync="reissue_show" @cancel="reissueOper()" @reissue-success="reissueSucc()"
                v-if="reissue_show"></reissue-bill>
  <ticket-print :show="eticket_show" @toggle="eticket_toggle" v-ref:eticketbill></ticket-print>
  <modal :show.sync="eticket_msg" v-ref:modal large backdrop="false" :width="900">
    <header slot="modal-header" class="modal-header">
      <button type="button" class="close" @click="eticket_msg = false"><span>&times;</span></button>
      <h4 class="modal-title">发票信息</h4>
    </header>
    <article slot="modal-body" class="modal-body">
      <div class="auto select-overspread form-horizontal">
        <div class="row auto">
          <div class="col-sm-6 form-group form-input-group" :class="[!row.f_paper_name ? 'has-error' : '']">
            <label class="control-label">开票名称:</label>
            <input class="form-control col-sm-2"
                   v-model="row.f_paper_name" placeholder="开票名称">
          </div>
          <div class="col-sm-6 form-group form-input-group">
            <nobr class="col-sm-4"><label class="control-label">是否征税:</label></nobr>
            <v-select v-model="invoice_is_pax"
                      placeholder='请选择'
                      :value.sync="invoice_is_pax"
                      close-on-select
                      :width="60"
                      :value-single="true"
                      :search="false"
                      :options='is_pax' clear-button>
            </v-select>
          </div>
          <div class="col-sm-12 form-group form-input-group">
            <label class="control-label">开户行及账号:</label>
            <input class="form-control col-sm-2"
                   v-model="row.f_paper_account" placeholder="购方开户行及账号">
          </div>
          <div class="col-sm-12 form-group form-input-group" >
            <label class="control-label">纳税人识别号:</label>
            <input class="form-control col-sm-2"
                   v-model="row.f_taxpayer_id" placeholder="纳税人识别号">
          </div>
          <div class="col-sm-12 form-group form-input-group" :class="[!row.f_address_phone ? 'has-error' : '']">
            <label class="control-label">开票地址电话:</label>
            <input class="form-control col-sm-2"
                   v-model="row.f_address_phone" placeholder="开票地址">
          </div>
          <div class="col-sm-12 form-group form-input-group">
            <label class="control-label">邮箱推送:</label>
            <input class="form-control col-sm-2"
                   v-model="row.f_email" placeholder="邮箱推送">
          </div>
        </div>
      </div>
    </article>
    <footer slot="modal-footer" class="modal-footer">
      <button type="button" class="btn button_search"
              :disabled="!(row.f_paper_name&&row.f_address_phone)"
              @click='confirm'>确认收费
      </button>
    </footer>
  </modal>

</template>

<script>
import {HttpResetClass, PagedList} from 'vue-client'
import co from 'co'

let reprintGen = function* (self, row) {
  try {
    row.f_bill_type = row.type
    row.f_bill_style = '普通收据'
    row.f_operator = self.$login.f.name
    row.operator = self.$login.f.name
    row.f_operatorid = self.$login.f.id
    row.f_orgid = self.$login.f.orgid
    row.f_orgname = self.$login.f.orgs
    row.f_depid = self.$login.f.depids
    row.f_depname = self.$login.f.deps
    row.billUrl = 'rs/report/hand_bill'
    self.reissue_data = row
    self.reissue_show = true
  } catch (error) {
    if (error.status) {
      self.$warn(`加载数据出错, ${JSON.stringify(error)}`, 'CardList')
    }
    throw error
  }
}
export default {
  title: '历史抄表',
  props: {
    'f_userfiles_id': {},
    isOperate: {
      type: Boolean,
      default: true
    }
  },
  data () {
    return {
      model: new PagedList('rs/sql/gethandlist', 9999),
      row: null,
      invoice_show_gas: false,
      reissue_data: null,
      reissue_show: false,
      eticket_show: false,
      eticket_msg: false,
      invoice_is_pax: '征税',
      is_pax: [{label: '征税', value: '征税'}, {label: '不征税', value: '不征税'}],
    }
  },
  ready () {
    let condition = `f_userfiles_id = '${this.f_userfiles_id}' `
    this.model.search(condition)
  },
  methods: {
    eticket_toggle () {
      this.eticket_show = false
      let condition = `f_userfiles_id = '${this.f_userfiles_id}' `
      this.model.search(condition)
    },
    reissueOper () {
      this.reissue_data = null
      this.reissue_show = false
    },
    reissueSucc () {
      this.reissue_show = false
      this.$dispatch('refresh')
    },
    async operate (type, row) {
      if (type === '票据补打') {
        let reprint = reprintGen(this, row, type)
        return co(reprint)
      } else {
        let HttpReset = new HttpResetClass()
        let handE = await HttpReset.load('POST', 'rs/sql/getHandETicket', {
          data: {
            hand_id: row.id
          }
        }, {resolveMsg: null, rejectMsg: '获取信息失败!'})
        if (handE.data.length > 0) {
          return this.$showAlert('已结算的记录不能再次结算开票', 'warning', 3000)
        }
        let userInfos = await HttpReset.load('POST', 'rs/sql/sale_getUser', {
          data: {
            orderitem: 'f_userinfo_code',
            condition: `f_userinfo_code = '${row.f_userinfo_code}'`
          }
        }, {resolveMsg: null, rejectMsg: '获取地址失败!'})
        let userInfo = userInfos.data[0]
        this.row = row
        this.invoice_show_gas = userInfo.f_collection_type === '按气量' ? '是' : '否'
        this.row.f_taxpayer_id = userInfo.f_idnumber
        this.row.f_paper_name = userInfo.f_user_name
        this.row.f_address_phone = userInfo.f_address + ' ' + userInfo.f_user_phone
        this.row.f_email = userInfo.f_email
        this.eticket_msg = true
      }
    },
    confirm () {
      this.eticket_msg = false
      this.eticket_show = true
      console.log('this.invoice_show_gas', this.invoice_show_gas)
      this.$refs.eticketbill.openETicket([this.row.id], this.row, '是', this.invoice_is_pax, '机表结算')
    },
    selected (row) {
      if (this.row === row) {
        this.row = null
      } else {
        this.row = row
      }
    },
    isSelected (row) {
      return this.row === row
    }
  },
  watch: {
    'f_userfiles_id' () {
      let condition = `f_userfiles_id = '${this.f_userfiles_id}' `
      this.model.search(condition)
    }
  }
}
</script>
