<template>
  <div class="auto basic-main" style="height:95%">
    <criteria-paged :model="model" v-ref:paged>
      <criteria partial='criteria' @condition-changed='$parent.selfSearch' v-ref:cri>
        <div novalidate class="form-horizontal select-overspread container-fluid auto" partial>
          <div class="row">
            <div class="col-sm-2  form-group">
              <label class="font_normal_body">组织机构</label>
              <res-select restype='organization'
                          @res-select="$parent.$parent.getorg"
                          is-mul="false"
                          :initresid='$parent.$parent.curorgid'>
              </res-select>
            </div>
            <div  class="form-group col-sm-2">
              <label class="font_normal_body">客户编号</label>
              <input  type="text" class="form-control" v-model="model.f_userinfo_code" style="width:60%"
                      condition="f_userinfo_code like '%{}%' " placeholder="客户编号"
              >
            </div>
            <div  class="form-group col-sm-2">
              <label class="font_normal_body">用户姓名</label>
              <input  type="text" class="form-control" v-model="model.f_user_name" style="width:60%"
                      condition="f_user_name = '{}'" placeholder="用户姓名"
              >
            </div>
            <div  class="form-group col-sm-2" >
              <label  class="font_normal_body">票据状态</label>
              <v-select id="f_bill_state"
                        style="width:60%"
                        v-model="model.f_bill_state"
                        placeholder='票据状态'
                        condition="f_bill_state ='{}'"
                        :value.sync="model.f_bill_state"
                        :options='$parent.$parent.eticktypes'
                        close-on-select clear-button>
              </v-select>
            </div>

              <div style="float: right" class="span" >
                <button class="button_search"  @click="search()">查 询</button>
                <button class="button_search"  @click="$parent.$parent.clear()">清空</button>
                <export-excel
                  :data="{condition: $parent.$parent.model.condition}"
                  :field="$parent.$parent.getfield"
                  sqlurl="rs/logic/saleExport"sql-name="eticketRecordList"
                  template-name='电子票管理'
                  :choose-col="true">
                </export-excel>
                <button class="button_search" @click="$parent.$parent.addCust()">自定义开票</button>
                <div
                  :class="{'button_shrink_top':$parent.$parent.criteriaShow,'button_shrink_bottom':!$parent.$parent.criteriaShow}"
                  @click="$parent.$parent.criteriaShow=!$parent.$parent.criteriaShow"
                  class="button_spacing"
                  style="float: right"></div>
              </div>
          </div>
          <div class="row" v-show="$parent.$parent.criteriaShow" >
            <div  class="form-group col-sm-2">
              <label class="font_normal_body">发票号码</label>
              <input  type="text" class="form-control" v-model="model.f_bill_number" style="width:60%"
                      condition="f_bill_number = '{}'" placeholder="发票号码"
              >
            </div>

            <div  class="form-group col-sm-2" >
              <label class="font_normal_body">业务类型</label>
              <v-select id="f_charge_type"
                        style="width:60%"
                        v-model="model.f_charge_type"
                        placeholder='业务类型'
                        condition="f_charge_type ='{}'"
                        :value.sync="model.f_charge_type"
                        :options='$parent.$parent.chargetypes'
                        close-on-select clear-button>
              </v-select>
            </div>
            <div class="form-group col-sm-2">
              <label for="startDate" class="font_normal_body">开票日期</label>
              <datepicker id="startDate" placeholder="开始日期"
                          v-model="model.startDate"
                          :value.sync="model.startDate"
                          :format="'yyyy-MM-dd HH:mm:ss'"
                          condition="f_kprq >='{}'"
                          style="width:60%"
              >
              </datepicker>
            </div>
            <div class="form-group col-sm-2">
              <label  for="endDate"  class="font_normal_body">结束时间</label>
              <datepicker id="endDate" placeholder="结束日期"
                          v-model="model.endDate"
                          :value.sync="model.endDate"
                          :format="'yyyy-MM-dd HH:mm:ss'"
                          condition="f_kprq <='{}'"
                          style="width:60%"
              >
              </datepicker>
            </div>
          </div>
        </div>
      </criteria>
      <data-grid  :model="model" partial='list' class="list_area table_sy" v-ref:grid>
        <template partial='head'>
          <tr>
            <th>序号</th>
            <th>客户编号</th>
            <th>用户姓名</th>
            <th>用户地址</th>
            <th>气量</th>
            <th>税后金额</th>
            <th>合计金额</th>
            <th>收费类型</th>
            <th>收费时间</th>
            <th>发票代码</th>
            <th>是否征税</th>
            <th>票据状态</th>
            <th>发票号码</th>
            <th>开票时间</th>
            <th>开票人</th>
            <th>查看发票</th>
            <th>操作</th>
          </tr>
        </template>
        <template partial='body'  >
          <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_address}}</td>
          <td style="text-align:center">{{row.f_pregas}}</td>
          <td style="text-align:center">{{row.f_tax_money.toFixed(2)}}</td>
          <td style="text-align:center">{{row.f_total_money}}</td>
          <td style="text-align:center">{{row.f_charge_type}}</td>
          <td style="text-align:center">{{row.f_operate_date}}</td>
          <td style="text-align:center">{{row.f_bill_code}}</td>
          <td style="text-align:center">{{row.f_tax_logotype}}</td>
          <td style="text-align:center">{{row.f_bill_state}}</td>
          <td style="text-align:center">{{row.f_bill_number}}</td>
          <td style="text-align:center">{{row.f_kprq}}</td>
          <td style="text-align:center">{{row.f_operator}}</td>
          <td style="text-align:center">
            <button class="btn btn-link" v-if="row.f_sign_url" @click="$parent.$parent.$parent.watchUrl(row)" >查看发票</button>
          </td>
          <td style="text-align:center" >
            <!--             <button class="btn btn-link" v-if="row.f_bill_state=='有效'&&$parent.$parent.$parent.smsMessaage.includes('发票冲红')" @click="$parent.$parent.$parent.fpch(row)" >冲红</button>-->
            <button class="btn btn-link" v-if="row.f_bill_state!='冲正'&&(row.f_bill_state=='开票成功'||row.f_bill_state=='有效')&&row.f_issue_status=='已开具'" @click="$parent.$parent.$parent.fpch(row)" >冲红</button>
            &nbsp;&nbsp;
            <button class="btn btn-link" v-if="(row.f_bill_state=='有效'&&row.f_issue_status=='未开具'&&row.f_invoice_type!='1')||(row.f_bill_state!='冲红'&&row.f_bill_state!='开票成功'&&row.f_bill_state!='冲正')" @click="$parent.$parent.$parent.kp(row)" >重开</button>
            <button class="btn btn-link" v-if="row.f_bill_state=='有效'&&(row.f_invoice_category=='纸质普票'||row.f_invoice_category=='纸质专票')" @click="$parent.$parent.$parent.zf(row)" >作废</button>
            &nbsp;&nbsp;
          </td>
        </template>
        <template partial='foot'></template>
      </data-grid>
    </criteria-paged>
    <table class="table-hover" v-show="false">
      <tr style="position: relative" class="table-bordered">
        <td
          style="display: inline-block;width: auto;border-right: 1px solid #F2F6FA;padding: 0px 10px 0px 10px;color: #5CB95C;font-weight: bold">
          汇总信息
        </td>
        <td style="display: inline-block;width: auto;border-right: 1px solid #F2F6FA;padding: 0px 10px 0px 10px;font-weight: bold">
          气量合计:&emsp;{{sumsmodel.f_pregas}}
        </td>
        <td style="display: inline-block;width: auto;border-right: 1px solid #F2F6FA;padding: 0px 10px 0px 10px;font-weight: bold">
          合计金额:&emsp;{{sumsmodel.f_collection}}
        </td>
        <td style="display: inline-block;width: auto;border-right: 1px solid #F2F6FA;padding: 0px 10px 0px 10px;font-weight: bold">
          税后金额:&emsp;{{sumsmodel.f_tax_money.toFixed(2)}}
        </td>
      </tr>
    </table>
  </div>
  <modal :show.sync="serachshow"  v-ref:modal large backdrop="false" >
    <header slot="modal-header" class="modal-header">
      <button type="button" class="close" @click="cancelsearch"><span>&times;</span></button>
      <h3 class="modal-title">查看发票</h3>
    </header>
    <article slot="modal-body" class="modal-body">
      <iframe :src="f_sign_url" v-if="f_sign_url" width="100%" height="500px">
      </iframe>
    </article>
    <footer slot="modal-footer" class="modal-footer">
      <button type="button" class="btn btn-default" @click='cancelsearch()'>取消</button>
    </footer>
  </modal>
</template>

<script>
  /**
   *发票冲红管理
   */
  import {PagedList} from 'vue-client'
  import Vue from 'vue'
  export default {
    title: '电子票管理',
    data () {
      return {
        criteriaShow: false,
        f_filialeids: Vue.$login.f.orgids,
        model: new PagedList('rs/sql/eticketRecordList', 50, {},{f_pregas:0,f_tax_money:0,}),
        // orgCondtionStr: '',
        f_outlets: Vue.$login.f.f_parentname,
        f_filiale: Vue.$login.f.f_fengongsi,
        f_operator: Vue.$login.f.name,
        f_orgstr: Vue.$login.f.orgpathstr,
        jurisdiction: this.$login.r,
        f_filialeid: Vue.$login.f.orgid,
        curorgid: [this.$login.f.orgid],
        sumsmodel:{},
        show:false,
        f_sign_url:'',
        serachshow:false,
        getfield: {
          'f_userinfo_code': '户编号',
          'f_user_name': '用户姓名',
          'f_address': '用户地址',
          'f_pregas': '气量',
          'f_tax_money': '税后金额',
          'f_total_money': '合计金额',
          'f_type': '收费类型',
          'f_operate_date': '收费时间',
          'f_bill_code': '发票代码',
          'f_bill_number': '发票号码',
          'f_tax_logotype': '是否征税',
          'f_bill_state': '票据状态',
          'f_kprq': '开票时间'
        },
      }
    },
    ready () {
      console.log('this.$login.f',this.$login.f, Vue.$login,this.jurisdiction.includes('发票冲红'))

      this.$refs.paged.$refs.cri.model.startDate = this.$login.toStandardDateString() + ' 00:00:00'
      this.$refs.paged.$refs.cri.model.endDate = this.$login.toStandardDateString() + ' 23:59:59'
      this.search()
    },
    methods: {
      addCust(){
        this.$dispatch('addCust')
      },
       zf (row) {
          let param = {
            id: row.f_eticket_id,
            f_bill_number: row.f_bill_number,
            f_bill_code: row.f_bill_code
          }
          this.$InvoiceService.voidEticket(param).then((res)=>{
            if (res.data == 200) {
              this.$showAlert('发票作废成功', 'success', 3000)
            } else {
              this.$showAlert('发票作废失败', 'danger', 3000)
            }
            this.$refs.paged.$refs.cri.search()
          })
      },
      watchUrl(row){
        this.f_sign_url=row.f_sign_url
        this.serachshow=true
      },
      cancelsearch(){
        this.serachshow=false
      },
      clear () {
        Object.keys(this.$refs.paged.$refs.cri.model).forEach((key) => {
          this.$refs.paged.$refs.cri.model[key] = []
        })
      },
      //发票重开
      kp (row) {

        try {
          let param={
            id:row.id,
            bz:row.f_comments,
            f_eticket_id:row.f_eticket_id
          }
          if(row.f_charge_type === '物联网结算'||row.f_charge_type === '卡表结算'){
            return this.$showAlert(`结算票据请在结算页面重新开票`,"warning",3000)
          }
          if (row.f_charge_type === '补卡费') {
            this.$InvoiceService.openEticketGen(param, '补卡费').then((res)=>{

              this.$refs.paged.$refs.cri.search()
            })
          }else  if (row.f_charge_type === '其他收费') {
            this.$InvoiceService.openEticketGen(param, '其他收费').then((res)=>{

              this.$refs.paged.$refs.cri.search()
            })
          }else if (row.f_charge_type === '换表收费') {
            this.$InvoiceService.openEticketGen(param, '换表收费').then((res)=>{

              this.$refs.paged.$refs.cri.search()
            })
          }else if (row.f_charge_type === '超用收费') {
            this.$InvoiceService.openEticketGen(param, '超用收费').then((res)=>{

              this.$refs.paged.$refs.cri.search()
            })
          }else if (row.f_charge_type === '其他补气') {
            this.$InvoiceService.openEticketGen(param, '其他补气').then((res)=>{

              this.$refs.paged.$refs.cri.search()
            })
          }else if(row.f_charge_type === '物联网结算') {
            this.$InvoiceService.openEticketGen(param, '物联网结算')
          } else if(row.f_charge_type === '卡表结算') {
            this.$InvoiceService.openEticketGen(param, '卡表结算')
          }else {
            this.$InvoiceService.openEticketGen(param, '售气收费').then((res)=>{

              this.$refs.paged.$refs.cri.search()
            })
          }
        } catch (e) {
          this.$showAlert('电子发票重开失败！！请到发票界面重新开票!!', 'danger', 2000)
        }
      },
      async fpch (row) {
        row.f_operator=this.$login.f.name,
          row.f_filiale= this.$login.f.f_fengongsi,
          row.f_outlets= this.$login.f.f_parentname,
          row.f_orgstr= this.$login.f.orgpathstr,
          row.f_filialeids= this.$login.f.orgid
        this.$InvoiceService.redOpenEticket(row).then((res)=>{
          this.$refs.paged.$refs.cri.search()
        })
      },
      getorg (val) {
        this.f_orgid = this.$login.convertToIn(val)
        this.f_filialeids = val[0]
      },
      async selfSearch (args) {
        if (!this.f_orgid) {
          this.getorg([this.$login.f.orgid])
        }
        args.condition=` f_filialeids='${this.f_filialeids}' and ${args.condition}`
        await this.model.search(args.condition, this.model)
        this.sumsmodel=this.model.sums
        console.log("res",this.model)
      },
      search () {
        this.$refs.paged.$refs.cri.search()
      }
    },
    computed: {
      eticktypes(){
        return [{label: '全部', value: ''},...this.$appdata.getParam('票据状态')]
      },
      chargetypes(){
        return [{label: '全部', value: ''},...this.$appdata.getParam('收费类型')]
      },
    }
  }
</script>
