<template>
  <div class="auto">
    <div class="flex">
      <criteria-paged :model="model" v-ref:paged>
        <criteria partial='criteria' @condition-changed='$parent.selfSearch' v-ref:cri>
          <div novalidate class="form-inline" partial>
          </div>
        </criteria>
        <data-grid :model="model" partial='list' v-ref:grid>
          <template partial='head'>
            <tr>
              <th>
                <input type="checkbox"  @click="$parent.$parent.$parent.addOrRemove($parent.$parent.$parent.selectAll)" :checked="$parent.$parent.$parent.selectAll">
              </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'>
            <tr>
              <td style="text-align:center">
                <input type="checkbox" v-model="$parent.$parent.$parent.selecteds" :value="row">
              </td>
              <td style="text-align:center">{{row.f_last_tablebase}}</td>
              <td style="text-align:center">{{row.f_tablebase}}</td>
              <td style="text-align:center">{{row.totalgas}}</td>
              <td style="text-align:center">{{row.totalfee}}</td>
              <td style="text-align:center">{{row.f_hand_month}}</td>
            </tr>
          </template>
          <template partial='foot'></template>
        </data-grid>
      </criteria-paged>
    </div>
    <div class="span" v-if="selecteds.length > 0">
      <form class="form-horizontal">
        <div class="row">
          <div class="form-group form-input-group col-sm-4">
            <label class="control-label col-sm-2">总气量:</label>
            <input type="text" class="form-control col-sm-2" v-model="eticket.totalGas" placeholder="总气量" readonly>
          </div>
          <div class="form-group form-input-group col-sm-4">
            <label class="control-label col-sm-2">总金额:</label>
            <input type="text" class="form-control col-sm-2" v-model="eticket.totalFee" placeholder="总金额" readonly>
          </div>
        </div>
      </form>
      <div class="auto" style="float: right">
        <button class="btn btn-success" @click="openEticket">开票</button>
      </div>
    </div>
  </div>
</template>

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

  export default {
    title: '电子票开具',
    data () {
      return {
        model: new PagedList('rs/sql/sale_getHandplan', 20),
        eticket: {
          totalGas: 0,
          totalFee: 0,
          hand_months: []
        },
        selecteds: [],
        selectAll: false
      }
    },
    props: ['row'],
    ready () {
      console.log('给起止时间赋值', this.$refs.paged.$refs.cri)
      this.search()
    },
    methods: {
      search () {
        this.$refs.paged.$refs.cri.search()
      },
      selfSearch (args) {
        if (this.row) {
          args.condition = `${args.condition} and m.f_userinfo_id = '${this.row.f_userinfo_id}'`
        }
        this.model.search(args.condition, args.model)
      },

      addOrRemove (is) {
        this.selecteds = []
        if (!is) { // 添加本页所有数据到ids中
          this.model.rows.forEach((row) => {
            this.selecteds.push(row)
          })
          this.selectAll = true
        } else { // 从ids中取消所有的数据
          this.selecteds = []
          this.selectAll = false
        }
      },

      async openEticket () {
        let param = {
          totalgas: this.eticket.totalGas,
          totalfee: this.eticket.totalFee,
          f_userinfo_id: this.selecteds[0].f_userinfo_id,
          f_operator: this.$login.f.name,
          f_hand_month: this.eticket.hand_months,
          eticket: {}
        }

        // 获取电子票配置
        // let conf = await this.$CommonService.getSelfConfig()
        try {
          let conf = null
          param.eticket = conf

          // 组织发票数据
          let getEticket = await this.$resetpost('rs/logic/handCombineData', {data: param}, {resolveMsg: null, rejectMsg: '组织发票数据出错！！'})
          console.log('开票数据', getEticket)

          // 开局发票
          await this.$resetpost('rs/logic/openEticket', {id: getEticket.data, eticket: conf}, {resolveMsg: '开票成功', rejectMsg: '开具电子票出错, 请到已开发票进行重新开具'})
        } catch (error) {
          this.selecteds = []
          this.search()
        }
      }
    },
    watch: {
      'selecteds.length' (val) {
        console.log('观察选中的数据', val, this.selecteds)
        this.selectAll = val === this.model.rows.length
        if (val > 0) {
          this.eticket.totalGas = 0
          this.eticket.totalFee = 0
          this.eticket.hand_months = []

          for (let i = 0; i < this.selecteds.length; i++) {
            this.eticket.totalGas = (this.eticket.totalGas - 0) + (this.selecteds[i].totalgas - 0)
            this.eticket.totalFee = (this.eticket.totalFee - 0) + (this.selecteds[i].totalfee - 0)
            this.eticket.hand_months.push(this.selecteds[i].f_hand_month)
          }
        }
      }
    },
    computed: {

    },
    events: {

    }

  }
</script>
