<template>
  <div class="auto basic-main" style="height:95%">
    <criteria-paged :model="model" v-ref:paged @sort="sort">
      <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="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 = '{}' " 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>
              <input type="text" class="form-control" v-model="model.f_buy_address_phone" style="width:60%"
                     condition="f_buy_address_phone like '%{}%'" 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'
                        :search="false"
                        close-on-select clear-button>
              </v-select>
            </div>
            <div class="form-group col-sm-2">
              <label class="font_normal_body">开票人&nbsp;&nbsp;</label>
              <v-select id="f_operator"
                        style="width:60%"
                        v-model="model.f_operator"
                        placeholder='开票人'
                        condition="f_operator ='{}'"
                        :value.sync="model.f_operator"
                        :options='$parent.$parent.drawers'
                        :search="false"
                        close-on-select clear-button>
              </v-select>
            </div>
            <div style="float: right">
              <button class="button_search" @click="search()">查 询</button>
              <button class="button_clear button_spacing" @click="$parent.$parent.clear()">清空</button>
              <export-excel :data="$parent.$parent.getCondition"
                            :field="$parent.$parent.fields"
                            sqlurl="rs/logic/saleExport" sql-name="eticket_query"
                            template-name='发票记录导出'
                            :choose-col="true">
              </export-excel>
              <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>
              <datepicker id="f_operate_datestarthate" placeholder="开始日期"
                          v-model="model.f_operate_datestarthate"
                          :value.sync="model.f_operate_datestarthate"
                          format="yyyy-MM-dd"
                          condition="f_operate_date >= '{} 00:00:00'"
                          style="width:60%">
              </datepicker>
            </div>
            <div class="form-group col-sm-2">
              <label class="font_normal_body">&nbsp;&nbsp;至&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
              <datepicker id="f_operate_dateendDate" placeholder="结束日期"
                          v-model="model.f_operate_dateendDate"
                          :value.sync="model.f_operate_dateendDate"
                          format="yyyy-MM-dd"
                          condition=" f_operate_date <= '{} 23:59:59'"
                          style="width:60%">
              </datepicker>
            </div>
            <div class="col-sm-2 form-group  flex-row" style="white-space: nowrap">
              <label class="font_normal_body">发票金额</label>
              <div class="row">
                <input type="text" style="width:27%" class="input_search" v-model="model.f_collection_small"
                       condition="f_total_money >= {} " placeholder="">
                <label class="font_normal_body"> ≤ x ≤ </label>
                <input type="text" style="width:27%" class="input_search" v-model="model.f_collection_big"
                       condition="f_total_money <= {} " placeholder="">
              </div>
            </div>
            <div class="form-group col-sm-2">
              <label class="font_normal_body">收款人&nbsp;&nbsp;</label>
              <v-select id="sell_operator"
                        style="width:60%"
                        v-model="model.sell_operator"
                        placeholder='收款人'
                        condition="(s.sell_operator = '{}' or other.sell_operator  = '{}') "
                        :value.sync="model.sell_operator"
                        :options='$parent.$parent.recipients'
                        :search="false"
                        close-on-select clear-button>
              </v-select>
            </div>
            <div  class="col-sm-2 form-group">
              <label class="font_normal_body">用户类型</label>
              <v-select :value.sync="model.f_user_type"
                        :options='$parent.$parent.usertypes'
                        placeholder='请选择' v-model="model.f_user_type"
                        condition="(s.f_user_type = '{}' or uf.f_user_type = '{}') "
                        close-on-select></v-select>
            </div>
          </div>
          <div class="row" v-show="$parent.$parent.criteriaShow">
            <div class="form-group col-sm-2">
              <label class="font_normal_body">发票号码&nbsp;&nbsp;</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_invoice_category"
                        style="width:60%"
                        v-model="model.f_invoice_category"
                        placeholder='票据状态'
                        condition="f_invoice_category ='{}'"
                        :value.sync="model.f_invoice_category"
                        :options='$parent.$parent.eticketcategory'
                        :search="false"
                        close-on-select clear-button>
              </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>
              <data-order field="f_total_money" name="实收金额"
                          :order.sync="$parent.$parent.$parent.orderFields.f_collection"></data-order>
            </th>
            <th>
              <data-order field="f_total_money" name="开票金额"
                          :order.sync="$parent.$parent.$parent.orderFields.f_total_money"></data-order>
            </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' :model="model">
          <tr>
            <th style="text-align:center">{{ row.f_userinfo_code }}</th>
            <th style="text-align:center">{{ row.f_user_name }}</th>
            <th style="text-align:center">{{ row.f_buy_address_phone }}</th>
            <th style="text-align:center">{{ row.f_user_type }}</th>
            <th style="text-align:center">{{ row.f_collection }}</th>
            <th style="text-align:center">{{ row.f_total_money }}</th>
            <th style="text-align:center">{{ row.f_bill_number }}</th>
            <th style="text-align:center">{{ row.f_bill_state }}</th>
            <th style="text-align:center">{{ row.f_remessage }}</th>
            <th style="text-align:center">{{ row.f_charge_type }}</th>
            <th style="text-align:center">{{ row.f_sign_result }}</th>
            <th style="text-align:center">{{ row.sell_operate_date }}</th>
            <th style="text-align:center">{{ row.f_operate_date }}</th>
            <th style="text-align:center">{{ row.f_operator }}</th>
            <th style="text-align:center">{{ row.sell_operator }}</th>
            <th style="text-align:center">
              <button class="btn btn-link" v-if="row.f_bill_url"
                      @click="$parent.$parent.$parent.watch_ch(row.f_bill_url)">
                查看发票
              </button>
            </th>
            <th style="text-align:center">
              <template v-if="row.f_invoice_source !== '外部录入'">
                <button class="btn btn-link" v-if="row.f_bill_state==='开票成功' && row.f_invoice_type === '0'"
                        @click="$parent.$parent.$parent.fp_ch(row)">发票冲红
                </button>
                <button class="btn btn-link" v-if="row.f_bill_state==='待开票'"
                        @click="$parent.$parent.$parent.kp(row)">开票
                </button>
                <button class="btn btn-link" v-if="row.f_bill_state==='开票失败'"
                        @click="$parent.$parent.$parent.Rekp(row)">重新开票
                </button>
                <!--                <button class="btn btn-link" v-if="['查询失败','等待查询'].includes(row.f_bill_state)"-->
                <!--                        @click="$parent.$parent.$parent.fp_search(row)">电票查询-->
                <!--                </button>-->
              </template>
            </th>
          </tr>
        </template>
      </data-grid>
    </criteria-paged>
  </div>
  <modal v-if="show" :show.sync="show" v-ref:modal backdrop="false">
    <header slot="modal-header" class="modal-header">
      <h4 class="modal-title">撤销原因</h4>
    </header>
    <article slot="modal-body" class="modal-body">
      <validator name='v'>
        <form class="form-horizontal select-overspread">
          <div class="form-group" :class="[$v.f_cancel_reason.required ? 'has-error' : 'has-success']">
            <label class=" col-sm-3 control-label">冲红备注:</label>
            <div class="col-sm-8">
                    <textarea class="form-control" v-model="bz" rows="3"
                              placeholder="请填写冲红备注" v-validate:bz='{required: true}'></textarea>
            </div>
          </div>
        </form>
      </validator>
    </article>
    <footer slot="modal-footer" class="modal-footer">
      <button v-show="show" type="button" class="button_search" @click='confirm()' :disabled="!$v.valid ">确认</button>
      <button v-show="show" type="button" class="button_clear" @click='close'>取消</button>
    </footer>
  </modal>
  <ticket-print :show="eticket_show" @toggle="eticket_toggle" v-ref:eticketbill></ticket-print>
</template>
<script>

import {PagedList} from 'vue-client'

export default {
  title: '发票管理',
  data () {
    return {
      model: new PagedList('rs/sql/eticket_query', 20, {orderitem: this.orderitem ? '"' + this.orderitem + ' "' : `'f_operate_date desc'`}),
      criteriaShow: false,
      eticket_show: false,
      orgCondtionStr: '',
      showsum_show: false,
      invoice_types: this.$appdata.getParam('发票种类'),
      eticktypes: [{label: '开票成功', value: '开票成功'}, {label: '等待查询', value: '等待查询'}, {
        label: '开票失败',
        value: '开票失败'
      }, {
        label: '冲红失败',
        value: '冲红失败'
      }, {label: '冲红', value: '冲红'}, {label: '撤销', value: '撤销'}],
      eticketcategory: [{label: '数电普票', value: '数电普票'}, {label: '数电专票', value: '数电专票'}, {label: '电子发票', value: '电子发票'},
        {label: '普通收据', value: '普通收据'}, {label: '专用发票', value: '专用发票'}],
      show: false,
      bz: '',
      row: {},
      orderFields: {
        f_operate_date: 'desc',
        f_collection: 'no',
        f_total_money: 'no'
      },
      // 排序
      orderitem: ' f_operate_date desc ',
      fields: {
        'f_userinfo_code': '客户编号',
        'f_user_name': '客户姓名',
        'f_buy_address_phone': '用户地址',
        'f_user_type': '用户类型',
        'f_collection': '实收金额',
        'f_total_money': '开票金额',
        'f_tax_money': '不含税金额',
        'f_invoice_category': '发票种类',
        'f_invoice_type': '发票类型',
        'f_fpqqlsh': '发票流水号',
        'f_bill_number': '发票号码',
        'f_bill_state': '发票状态',
        'f_remessage': '失败原因',
        'f_charge_type': '业务类型',
        'sell_operate_date': '收费时间',
        'f_operate_date': '开票时间',
        'f_operator': '开票人',
        'sell_operator': '收款人'
      },
      install_show: false
    }
  },
  ready () {
  },
  methods: {
    async Rekp (row) {
      console.log('重开参数' + JSON.stringify(row))
      this.eticket_show = true
      row.f_paper_name = row.f_user_name
      row.f_email = row.f_mail_push
      row.f_taxpayer_id = row.f_pay_id
      row.f_address_phone = row.f_buy_address_phone
      row.f_paper_accoun = row.f_buy_openbank
      await this.$refs.eticketbill.openETicket(row.f_sell_id, row, row.f_charge_type, row.f_invoice_category, true)
    },
    search () {
      this.$refs.paged.$refs.cri.search()
    },
    selfSearch (args) {
      this.model.search(args.condition, args.model)
    },
    sort (field, rule) {
      // 将所有排序方式设为不排序，实现相互排斥
      for (let key in this.orderFields) {
        if (key === field) {
          this.orderFields[key] = rule
        } else {
          this.orderFields[key] = 'no'
        }
      }
      // 如果新规则不排序，还原为默认排序
      if (rule === 'no') {
        this.model.paramSource.orderitem = `'${this.orderitem}'`
      } else {
        this.model.paramSource.orderitem = `'${field} ${rule}'`
      }
      this.search()
    },
    async confirm () {
      this.eticket_show = true
      await this.$refs.eticketbill.rushRedInvoice({id: this.row.f_sell_id, eticket_id: this.row.id, bz: this.bz})
    },
    async fp_search (row) {
      this.eticket_show = true
      await this.$refs.eticketbill.fp_search(row, row.id)
    },
    watch_ch (src) {
      this.eticket_show = true
      this.$refs.eticketbill.watch_ch(src)
    },
    fp_ch (row) {
      this.row = row
      this.show = true
      this.bz = `对应正数发票号码:${row.f_bill_number}。`
    },
    close () {
      this.show = false
      this.install_show = false
    },
    kp (row) {
      this.eticket_show = true
      this.$refs.eticketbill.openETicket(row.f_sell_id, row, row.f_charge_type)
    },
    eticket_toggle () {
      this.eticket_show = false
      this.show = false
      this.row = {}
      this.bz = ''
    }
  },
  computed: {
    usertypes () {
      return [{label: ' 全部 ', value: ''}, ...this.$appdata.getParam('用户类型')]
    },
    drawers () {
      return [{label: ' 全部 ', value: ''}, ...this.$appdata.getParam('开票人')]
    },
    recipients () {
      return [{label: ' 全部 ', value: ''}, ...this.$appdata.getParam('收款人')]
    },
    getCondition () {
      return {
        condition: `${this.$refs.paged.$refs.cri.condition}` + this.orgCondtionStr,
        orderitem: this.orderitem ? `${this.orderitem}` : ' f_operate_date desc'
      }
    }
  },
  events: {
    'search' () {
      this.search()
    }
  },
  watch: {}
}
</script>
<style scoped>
th {
  text-align: center;
  padding: 2px;
}

td {
  text-align: center;
  padding: 2px;
}
</style>
