<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 >
            <label>使用公司</label>
            <right-tree :userid.sync='$parent.$parent.userid' :source.sync='$parent.$parent.source'  @re-res="$parent.$parent.getRes" > </right-tree>
          </div>
          <div >
            <v-select id="f_type"
                      v-model="model.f_type"
                      placeholder='收费类型'
                      condition="charge_type ='{}'"
                      :value.sync="model.f_type"
                      :options='$parent.$parent.feetypes'
                      close-on-select clear-button>
            </v-select>
          </div>
          <div >
            <v-select id="ifsuccess"
                      v-model="model.ifsuccess"
                      placeholder='是否开票'
                      condition="(case when f_bill_url IS not null then '是' else '否' end)   ='{}'"
                      :value.sync="model.ifsuccess"
                      :options='$parent.$parent.ifsuccess'
                      close-on-select clear-button>
            </v-select>
          </div>
          <div >
            <input  type="text" class="form-control" v-model="model.f_userinfo_code"
                    condition="f_userinfo_code like '%{}%' " placeholder="客户编号"
                    :size="model.f_userinfo_code ? model.f_userinfo_code.length : 4">
          </div>
          <div >
            <input  type="text" class="form-control" v-model="model.f_user_name"
                    condition="user_name like '%{}%'" placeholder="用户姓名"
                    :size="model.f_user_name ? model.f_user_name.length : 4">
          </div>
          <div >
            <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 >
            <!-- <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 >
            <button class="btn btn-success width-80" @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>
            <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_userinfo_code}}</td>
            <td style="text-align:center">{{row.user_name}}</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.charge_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.user_id}}</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;">合计</td>
          <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>
        </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(row.f_sell_id, 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/eticketQuery', 50,{},{f_total_money:'',f_tax_money:''}),

        filialeNameStr: this.$login.f.f_fengongsi,
        filialeCodeStr: this.$login.f.f_orgids,
        userid:this.$login.f.id,
        source: `tool.getFullTree(this.getRights().where(row.getType() != $zone$).where(row.getType() != $role$))`,
        // 下拉参数获取
        feetypes: [{label: '全部', value: ''}, ...this.$appdata.getParam('收费类型')],
        ifsuccess: [{label: '全部', value: ''}, {label: '是', value: '是'},{label: '否', value: '否'}]
      }
    },
    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.$refs.paged.$refs.cri.model.f_type = ['卡表收费']
      this.search()
    },
    methods: {
      rekingTaxBill(row){
        reopenEticketGen(this, row)
      },
      openUrl(row){
        window.open(row.f_bill_url, '_blank')
      },
      selfSearch (args) {
        args.condition = `${args.condition} and orgstr  like '%${this.filialeCodeStr}%'`
        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>
