<template>
  <div class="flex-row">
    <div class="basic-main">
      <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="col-sm-2  form-group">
                <label class="font_normal_body">客户编号</label>
                <input  type="text" class="input_search" style="width:60%" v-model="model.f_invoice_usercode" style="width:60%"
                        condition="f_userinfo_code = '{}'" placeholder="客户编号"
                />
              </div>
              <div class="col-sm-2  form-group">
                <label class="font_normal_body">客户姓名</label>
                <input  type="text" class="input_search" style="width:60%" v-model="model.f_invoice_name" style="width:60%"
                        condition="f_user_name = '{}'" placeholder="客户姓名"
                />
              </div>

              <div style="float: right">
                <button class="button_search button_spacing" @click="search()" >查询</button>
                <button class="button_new button_spacing" title="根据收费记录新增发票" @click="$parent.$parent.addInvoice(true)" >新增1</button>
                <button class="button_new button_spacing" title="根据导入字段新增发票" @click="$parent.$parent.addInvoice(false)" >新增2</button>
                <button class="button_export button_spacing" @click="$parent.$parent.importFile()">导入</button>
                <button  class="button_search" style="width: 90px" @click="$parent.$parent.downloadFiles()">模板下载</button>
                <div style="float: right" class="button_spacing"
                     :class="{'button_shrink_top': criteriaShow,'button_shrink_bottom': !criteriaShow}"
                     @click="criteriaShow = !criteriaShow"></div>
              </div>
            </div>
            <div class="row" v-show="criteriaShow">
              <div class="col-sm-2  form-group">
                <label class="font_normal_body">发票代码</label>
                <input  type="text" class="input_search" style="width:60%" v-model="model.f_invoice_dm" style="width:60%"
                        condition="f_invoice_dm like '%{}%'" placeholder="发票代码"
                />
              </div>
              <div class="col-sm-2  form-group">
                <label class="font_normal_body">发票号码</label>
                <input  type="text" class="input_search" style="width:60%" v-model="model.f_invoice_num" style="width:60%"
                        condition="f_invoice_num like '%{}%'" placeholder="发票号码"
                />
              </div>
              <div class="col-sm-2 form-group">
                <label class="font_normal_body">发票类型</label>
                <v-select :value.sync="model.f_invoice_type" :options='$parent.$parent.invoiceType' :value-single="true" :search="false"
                          placeholder='发票类型' close-on-select v-model='model.f_invoice_type' condition="f_invoice_type = '{}'">
                </v-select>
              </div>
              <div class="col-sm-2  form-group">
                <label class="font_normal_body">操&ensp;作&ensp;员</label>
                <input  type="text" class="input_search" style="width:60%" v-model="model.f_operator" style="width:60%"
                        condition="f_operator like '%{}%'" placeholder="操作员"
                />
              </div>
              <div class="col-sm-2 form-group">
                <label class="font_normal_body" style="word-break:keep-all;">开票日期</label>
                <datepicker :format="'yyyy-MM-dd'" :value.sync="model.f_start_date"
                            class="datepicker"
                            condition="f_invoice_date >= '{}'"
                            placeholder="起始时间"
                            style="width: 60%"
                            v-model="model.f_start_date">
                </datepicker>
              </div>
              <div class="col-sm-2 form-group">
                <label class="font_normal_body">开票日期</label>
                <datepicker :format="'yyyy-MM-dd'" :value.sync="model.f_end_date"
                            class="datepicker"
                            condition="f_invoice_date <= '{}'"
                            placeholder="结束时间"
                            style="width: 60%"
                            v-model="model.f_end_date">
                </datepicker>
              </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><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'>
            <tr>
              <td style="text-align:center"><nobr>{{row.f_userinfo_code?row.f_userinfo_code:row.f_invoice_usercode}}</nobr></td>
              <td style="text-align:center"><nobr>{{row.f_user_name?row.f_user_name:row.f_invoice_name}}</nobr></td>
              <td style="text-align:center"><nobr>{{row.f_invoice_type}}</nobr></td>
              <td style="text-align:center"><nobr>{{row.f_invoice_dm}}</nobr></td>
              <td style="text-align:center"><nobr>{{row.f_invoice_num}}</nobr></td>
              <td style="text-align:center"><nobr>{{row.f_invoice_money}}</nobr></td>
              <td style="text-align:center"><nobr>{{row.f_invoice_date}}</nobr></td>
              <td style="text-align:center"><nobr>{{row.f_operator}}</nobr></td>
              <td style="text-align:center"><nobr>{{row.f_operate_date}}</nobr></td>
              <td style="text-align:center"><nobr>{{row.f_state}}</nobr></td>
              <td style="text-align:center"><nobr>{{row.f_use_date}}</nobr></td>
              <td style="text-align:center"><nobr>{{row.f_use_operator}}</nobr></td>
              <td style="text-align:center">
                <button type="button" name="button" class="button_search button_spacing width-60" v-show="row.f_use_operator === '' ||row.f_use_operator === null "
                        @click.stop="$parent.$parent.$parent.confirmuseinvoice(row)">领用</button></td>
            </tr>
          </template>
          <template partial='foot'></template>
        </data-grid>
      </criteria-paged>
    </div>
  </div>


  <modal :show.sync="fileshow" width="500px" title="文件导入" v-ref:modal large backdrop="false">
    <article slot="modal-body" class="modal-body">
      <file-upload class="my-file-uploader " name="UploadFile" class="btn btn-success" style="border-radius: 2px;"
                   action="rs/file/uploadFile" tagname="导入发票" v-ref:file :headers="headers"></file-upload>
    </article>
    <footer slot="modal-footer" class="modal-footer">
    </footer>
  </modal>

  <modal :show.sync="invoiceEntryShow" v-ref:invoiceEntryShow backdrop="false" id="invoiceEntryShow">
    <article slot="modal-body" class="modal-body">
      <invoice-entry :sellgas="sellgas"></invoice-entry>
    </article>
    <footer slot="modal-footer">
    </footer>
  </modal>

</template>

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

export default {
    data () {
      return {
        invoiceType: [{label: '全部', value: ''}, {label: '增值税专用发票', value: '增值税专用发票'}, {label: '增值税普通发票', value: '增值税普通发票'}],
        model: new PagedList('rs/sql/sale_getInvoiceUse', 20),
        fileshow: false,
        invoiceEntryShow: false,
        curorgid: [this.$login.f.orgid],
        sellgas: false
      }
    },
    title: '发票录入',
    ready () {
      this.search()
    },
    computed: {
    },
    events: {
      /* FileUpload: function (file, res) {
        let data = {
          tablename: 't_userinfo',
          filepath: res.path
        }
        this.$resetpost('rs/logic/import', data, {resolveMsg: null, rejectMsg: '上传失败'}).then((res) => {
          console.log('dddddddd', this)
          this.close()
          this.$showAlert(`一共上传${res.data.total}条数据, 成功${res.data.successnum}条`, 'success', 2000)
          this.search()
        })
      }, */
      onFileUpload: function (file, res) {
        let data = {
          tablename: 't_invoiceuse',
          filepath: res.f_downloadpath,
          f_operator: this.$login.f.name,
          f_operatorid: this.$login.f.id,
          f_orgid: this.$login.f.orgid,
          f_orgname: this.$login.f.orgs,
          f_depid: this.$login.f.depids,
          f_depname: this.$login.f.deps,
          f_filialeid: this.f_filialeids
        }
        this.$resetpost('rs/logic/sale_invoiceuseImp', {data: data}, {
          resolveMsg: null,
          rejectMsg: '上传失败'
        }).then((res) => {
          this.fileshow = false
          if (res.data.errornum.length > 0) {
            this.$showMessage(`导入成功${res.data.successnum}条数据, ${res.data.errornum} 因发票号重复未成功导入`)
          } else {
            this.$showAlert(`导入成功${res.data.successnum}条数据`, 'success', 5000)
          }
          this.search()
        })
      }
    },
    methods: {
      downloadFiles () {
        let downurl = 'rs/downloadfile/file?filename=发票导入模板'
        this.$downFile(downurl,'发票导入模板.xlsx')
      },
      search () {
        this.$refs.paged.$refs.cri.search()
      },
      async selfSearch (args) {
        if (!this.f_orgid) {
          this.getorg([this.$login.f.orgid])
        }
        args.condition = `${args.condition} and f_orgid = '${this.f_filialeids}'`
        await this.model.search(args.condition, args.model)
      },
      // 清空
      clear () {
        this.$refs.paged.$refs.cri.model = {}
      },
      getorg (val) {
        this.f_orgid = this.$login.convertToIn(val)
        this.f_filialeids = val[0]
      },
      importFile () {
        this.fileshow = true
      },
      async confirmuseinvoice (val) {
        this.$showMessage(`请确认${val.f_invoice_name}已领走${val.f_invoice_num}发票`, ['confirm', 'cancel']).then((res) => {
          if (res === 'confirm') {
            this.useinvoiceconfirm(val)
          }
        })
      },
      async useinvoiceconfirm (val) {
        let useinvoicedate = Object.assign({}, val)
        useinvoicedate.f_state = '有效'
        useinvoicedate.f_use_date = this.$login.toStandardTimeString()
        useinvoicedate.f_use_operator = this.$login.f.name
        console.log('领用信息', useinvoicedate)
        await this.$resetpost('rs/entity/t_invoiceuse', useinvoicedate, {resolveMsg: null, rejectMsg: '保存失败!!!'})
        this.$showAlert('操作成功', 'success', 2000)
        this.search()
      },
      addInvoice (sellgas) {
        this.invoiceEntryShow = true
        this.sellgas = sellgas
      }
    }

  }
</script>
<style lang="less">
  #invoiceEntryShow {
    .modal-dialog {
      width: 80%;
    }
  }
</style>
