<template>
  <div>
    <criteria-paged :model="model" v-ref:paged>
      <criteria partial='criteria' @condition-changed='$parent.selfSearch' v-ref:cri>
        <div novalidate class="form-inline" partial>
          <div class="form-group">
            <datepicker id="startDate" placeholder="开始日期"
                        v-model="model.startDate"
                        :value.sync="model.startDate"
                        :disabled-days-of-Week="[]"
                        :format="'yyyy-MM-dd 00:00:00'"
                        condition="f_operate_date >= '{}' "
                        :show-reset-button="reset">
            </datepicker>
          </div>
          <div class="form-group">
            <!-- <label for="endDate">结束日期:</label> -->
            <datepicker id="endDate" placeholder="结束日期"
                        v-model="model.endDate"
                        :value.sync="model.endDate"
                        :disabled-days-of-Week="[]"
                        :format="'yyyy-MM-dd 23:59:59'"
                        condition="f_operate_date <= '{}' "
                        :show-reset-button="reset">
            </datepicker>
          </div>
          <div class="form-group">
            <button class="btn btn-success " @click="search()" v-el:cx>查询</button>
          </div>
        </div>
      </criteria>
      <data-grid  :model="model" partial='list' 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>
          </tr>
        </template>
        <template partial='body':model="model">
          <tr>
            <td style="text-align:center">{{$index+1}}</td>
            <td style="text-align:center">{{row.f_total_money}}</td>
            <td style="text-align:center">{{row.f_tax_money}}</td>
            <td style="text-align:center">{{row.f_type}}</td>
            <td style="text-align:center">{{row.f_operator}}</td>
            <td style="text-align:center">{{row.f_bill_state}}</td>
            <td style="text-align:center">{{row.f_operate_date}}</td>
            <td style="text-align:center">{{row.f_bill_number}}</td>
            <td style="text-align:center">{{row.f_bill_code}}</td>
            <td style="text-align:center">
              <div v-if="row.f_bill_state === '有效'">
                <button class="btn btn-link" @click="$parent.$parent.$parent.openUrl(row)" :disabled="!row.f_bill_url">点击查看</button>
              </div>
            </td>
            <td style="text-align:center">
              <button class="btn btn-link" v-if="row.f_bill_state === '有效' && row.f_bill_code && row.f_bill_number" @click="$parent.$parent.$parent.redCancel(row)">冲红</button>
              <button class="btn btn-link" v-if="row.f_bill_state !== '冲正' && !row.f_bill_code && !row.f_bill_number" @click="$parent.$parent.$parent.rekingTaxBill(row)">重开发票</button>
            </td>
          </tr>
        </template>
        <template partial='foot'>
          <td style="text-align: center;">合计</td>
          <td style="text-align: center;">{{model.sums.f_total_money.toFixed(2)}}</td>
          <td style="text-align: center;">{{model.sums.f_tax_money.toFixed(2)}}</td>
          <td style="text-align: center;"></td>
          <td style="text-align: center;"></td>
          <td style="text-align: center;"></td>
          <td style="text-align: center;"></td>
          <td style="text-align: center;"></td>
          <td style="text-align: center;"></td>
          <td style="text-align: center;"></td>
          <td style="text-align: center;"></td>
        </template>
      </data-grid>
    </criteria-paged>
  </div>
</template>

<script>
  /**
   *发票冲红管理
   */
  import { PagedList } from 'vue-client'

  let redCancelGen = async function (self, row) {
    console.log('冲正票据', row)
    let res = await self.$showMessage(`确定要冲红该电子票吗？`, ['confirm', 'cancel'])
    if (res === 'confirm') {
      await self.$CommonService.correctEticket(null, row.f_type)
    }
    self.search()
  }

  let reopenEticketGen = async function (self, row) {
    // 获取分公司配置
    let getConfig = await self.$CommonService.getSelfConfig()
    console.log('获取电子票配置', getConfig)
    await self.$resetpost('rs/logic/reopenEticket', {row: row, eticket: getConfig}, {resolveMsg: '电子发票生成成功', rejectMsg: '电子发票生成失败'})
    self.search()
  }

  export default {
    title: '发票管理',
    data () {
      return {
        model: new PagedList('rs/sql/sale_handEticketQuery', 20,{},{f_total_money:'',f_tax_money:''}),
      }
    },
    props: ['row'],
    ready () {
      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: {
      rekingTaxBill(row){
        reopenEticketGen(this, row)
      },
      openUrl(row){
        window.open(row.f_bill_url, '_blank')
      },
      selfSearch (args) {
        if (this.row) {
          args.condition = `${args.condition} and f_userinfo_id = '${this.row.f_userinfo_id}' and f_filialeids like '%${this.$login.f.f_orgids}%'`
        }
        this.model.search(args.condition, this.model)
      },
      search () {
        this.$refs.paged.$refs.cri.search()
      },
      redCancel (row) {
        redCancelGen(this, row)
      },
      getRes (obj) {
        this.filialeNameStr = obj.res
        this.filialeCodeStr = obj.resids
      },
    }
  }
</script>
