<template>
  <div class="basic-main" style="height: 100%">
    <criteria-paged :model="model" :pager='false' v-ref:paged>
      <criteria partial='criteria' @condition-changed='$parent.selfSearch' v-ref:criteria>
        <p class="bg-info text-center" style="padding: 8px;">营业厅日报表</p>
        <div class="form-group" v-if="!$parent.$parent.data.f_files_path"></div>
        <div novalidate class="form-horizontal select-overspread container-fluid auto" partial >
          <div class="row">
            <div class="col-sm-2" >
              <label class="font_normal_body" for="startDate">开始日期:</label>
              <datepicker id="startDate" placeholder="开始日期" style="width: 60%"
                          v-model="model.startDate"
                          :value.sync="model.startDate"
                          :disabled-days-of-Week="[]"
                          :format="'yyyy-MM-dd 00:00:00'"
                          :show-reset-button="reset"
                          :disabled="$parent.$parent.noshow">
              </datepicker>
            </div>
            <div class="col-sm-2" >
              <label  class="font_normal_body" for="endDate">结束日期:</label>
              <datepicker id="endDate" placeholder="结束日期" style="width: 60%"
                          v-model="model.endDate"
                          :value.sync="model.endDate"
                          :disabled-days-of-Week="[]"
                          :format="'yyyy-MM-dd 23:59:59'"
                          :show-reset-button="reset">
              </datepicker>
            </div>
            <!--<div class="col-sm-2">-->
              <!--<label class="font_normal_body" title="参数:付款方式查询">付款方式:</label>-->
              <!--<v-select :value.sync="model.f_payment" multiple-->
                        <!--v-model="model.f_payment"-->
                        <!--:options='$parent.$parent.payment' placeholder='请选择'-->
                        <!--condition="f_payment in {}"-->
                        <!--close-on-select></v-select>-->
            <!--</div>-->
            <res-select-group :show-component="$parent.$parent.resshow"
                              :selectin="true" :initres="$parent.$parent.initres"
                              @re-res="$parent.$parent.getRes" v-ref:sel></res-select-group>
          </div>
          <div class="span" style = "float:right;">
            <button class="button_search" @click="$parent.$parent.searchData()">查询</button>
            <!--<tie-an-account v-if="this.$login.r.includes('报表扎帐')"-->
                            <!--:org="$parent.$parent.org"-->
                            <!--@update="$parent.$parent.update"-->
                            <!--:model="$parent.$parent.data"-->
                            <!--:fee-str="$parent.model.data"-->
                            <!--:end-date="model.endDate" ></tie-an-account>-->
            <report-print id='gasprice' top='1cm' left='0' width='100%' height='100%'
                          :style="$parent.$parent.styledata"  :preview="true"
            ></report-print>
            <report-excel id='gasprice'></report-excel>
          </div>
        </div>
      </criteria>
      <div  partial='list' v-el:handcollect id='gasprice' style="overflow-y: scroll">
        <table class='tableprint' style="margin: 0px auto"  v-if="!$parent.data.f_files_path">
          <thead>
          <tr>
            <th :colspan='$parent.tablen+1' style="font-weight: normal; text-align: left;">
              <h3 style="text-align: center">营业厅日结账</h3>
            </th>
          </tr>
          <tr>
            <th :colspan='$parent.tablen+1' style="font-weight: normal; text-align: center;">
              时间区间：{{$refs.criteria.model.startDate}} - {{ $refs.criteria.model.endDate }}<br/>
              打印时间：{{$parent.printTime}}
            </th>
          </tr>
          <tr>
            <th :colspan='$parent.tablen+1' style="font-weight: normal; text-align: center;">
              <div>
                <span v-show="$parent.orgname[0]">公司：{{$parent.orgname}}</span>
                <span v-show="$parent.depname[0]"> 部门：{{$parent.depname}}</span>
                <span v-show="$parent.operatorname[0]">人员：{{$parent.operatorname}}</span>
              </div>
            </th>
          </tr>
          </thead>
          <tr>
            <td :colspan='$parent.tablen+1' class="noborder">
              {{{ $parent.datas }}}
            </td>
          </tr>
          <tfoot>
          <tr style="text-align: left">
            <th :colspan='Math.floor(($parent.tablen+1)/3)'>财务审核：</th>
            <th :colspan='Math.round(($parent.tablen+1)/3)'>收款审核：</th>
            <th :colspan='Math.round(($parent.tablen+1)/3)'>收款员：</th>
          </tr>
          </tfoot>
        </table>
        {{{ $parent.reportStr}}}
      </div>
    </criteria-paged>
    <modal :show.sync="show" v-ref:modal small backdrop="false">
      <header slot="modal-header" class="modal-header">
        <h4 class="modal-title">输入文件名称</h4>
      </header>
      <article slot="modal-body" class="modal-body">
        <div class="form-group">
          <input  type="text" class="form-control" v-model="filename" placeholder='保存的文件名'>
        </div>
      </article>
      <footer slot="modal-footer" class="modal-footer">
        <button v-show="show" type="button" class="btn btn-default" @click='close'>取消</button>
        <button v-show="show" type="button" class="btn btn-success" @click='confirm(filename)'>确认</button>
      </footer>
    </modal>
  </div>
</template>

<script>
  import { DataModel } from 'vue-client'
  import co from 'co'
  let saveFile = function * (self) {
    // 线验证文件是否重名
    let count = yield self.$resetpost('api/af-revenue/sql/manageSingleTable',
      {data: {tablename: 't_report_record', condition: `f_files_name = '${self.filename}'`}},
      {resolveMsg: null, rejectMsg: null})
    if (count.data.length > 0) {
      self.$showAlert('无法保存，文件名重名', 'warning', 3000)
      return
    }
    let saveBack = yield self.$resetpost('api/af-revenue/logic/saveReport', {f_files_content: self.$refs.paged.$els.handcollect.innerHTML,
      f_files_name: self.filename, f_files_type: self.data.f_report_type, f_component_name: 'manage-bus-summary',
      f_operator: this.$login.f.name, f_query_month: `${self.model.model.startDate}-${self.model.model.endDate}`,
      f_outlets: this.$login.f.depname})
    if (saveBack.data.status === 'succeed') {
      self.show = false
      self.filename = ''
      // 后台保存完成，通知刷新
      self.$dispatch('save-success', self.filename)
    }
  }
  export default {
    title: '营业厅日结账',
    props: ['data'],
    data () {
      return {
        printTime: this.$login.toStandardTimeString(),
        depresid: [],
        userresid: [],
        f_orgid: this.$login.f.orgid,
        f_depid: this.$login.f.depids,
        f_operatorid: this.$login.f.id,
        operatorid: [],
        styledata: 'td {border-width: 1px;padding: 0.3px;border-style: solid; border-color:#000000;font-size: 0.9em;}  \n' +
          'table{border-collapse:collapse; border}\n' +
          '.noborder{border-width: 0px;}\n' +
          '.tdleft{text-align: left;}\n' +
          '.tdright{text-align: right;}  \n' +
          '.tdcenter{text-align: center;}\n' +
          '.fs10{font-size: 0.4em;}\n' +
          '.fs11{font-size: 0.5em;}\n' +
          '.fs12{font-size: 0.6em;}\n' +
          '.fs13{font-size: 0.7em;}\n' +
          '.fs15{font-size: 0.9em;}\n' +
          '.fs17{font-size: 1.1em;}\n' +
          '.fs19{font-size: 1.2em;}\n' +
          '.fs24{font-size: 1.5em;}\n' +
          '.fs26{font-size: 1.6em;}\n' +
          '.b{font-weight: bold;}\n' +
          '.td-border-right{BORDER-RIGHT: #000000 1px solid; }\n' +
          '.td-border-top{BORDER-TOP: #000000 1px solid; }\n' +
          '.td-border-left{BORDER-LEFT: #000000 1px solid;}\n' +
          '.td-border-bottom{BORDER-BOTTOM: #000000 1px solid;}',
        depid: [],
        org: {
          f_orgname: '',
          f_depname: '',
          f_operator: ''
        },
        tablen: 0,
        noshow: false,
        datas: '',
        orgname: '',
        depname: '',
        operatorname: '',
        orgCondtionStr: '1=1',
        model: new DataModel('api/af-revenue/report/sellDepReport', {startDate: 'this.model.startDate', endDate: 'this.model.endDate',
          f_orgid: 'this.model.f_orgid'}),
        reportStr: null,
        resshow: ['company', 'department', 'operator'],
        show: false,
        spans: 0,
        initres: {
          org: [this.$login.f.orgid],
          dep: [],
          user: []
        }
      }
    },
    ready () {
      this.$refs.paged.$refs.criteria.model.startDate = this.$login.toStandardDateString() + ' 00:00:00'
      this.$refs.paged.$refs.criteria.model.endDate = this.$login.toStandardDateString() + ' 23:59:59'
      this.styledata =
         'td {border-width: 1px;padding: 0.3px;border-style: solid; border-color:#000000;font-size: 0.9em;}  \n' +
          'table{border-collapse:collapse; border}\n' +
          '.noborder{border-width: 0px;}\n' +
          '.tdleft{text-align: left;}\n' +
          '.tdright{text-align: right;}  \n' +
          '.tdcenter{text-align: center;}\n' +
          '.fs10{font-size: 0.4em;}\n' +
          '.fs11{font-size: 0.5em;}\n' +
          '.fs12{font-size: 0.6em;}\n' +
          '.fs13{font-size: 0.7em;}\n' +
          '.fs15{font-size: 0.9em;}\n' +
          '.fs17{font-size: 1.1em;}\n' +
          '.fs19{font-size: 1.2em;}\n' +
          '.fs24{font-size: 1.5em;}\n' +
          '.fs26{font-size: 1.6em;}\n' +
          '.b{font-weight: bold;}\n' +
          '.td-border-right{BORDER-RIGHT: #000000 1px solid; }\n' +
          '.td-border-top{BORDER-TOP: #000000 1px solid; }\n' +
          '.td-border-left{BORDER-LEFT: #000000 1px solid;}\n' +
          '.td-border-bottom{BORDER-BOTTOM: #000000 1px solid;}'
      console.log(this.$login.f)
      console.log('styledata', this.styledata)
    },
    methods: {
      searchData () {
        this.$refs.paged.$refs.criteria.search()
      },
      selfSearch (args) {
        this.printTime = this.$login.toStandardTimeString()
        let orgcondition = '1=1'
        let orgstr = this.orgCondtionStr
        this.org.f_depname = this.depname
        this.org.f_operator = this.operatorname
        orgcondition = orgcondition + orgstr
        this.$refs.paged.$refs.criteria.model.f_orgid = orgcondition + ' and ' + this.$refs.paged.$refs.criteria.condition
        this.$refs.paged.search(args)
      },
      update (val) {
        if (val !== ''){
          this.$refs.paged.$refs.criteria.model.startDate = val
          this.noshow = true
        } else {
          this.$refs.paged.$refs.criteria.model.startDate = this.$login.toStandardDateString() + ' 00:00:00'
          this.noshow = false
        }
      },
      getRes (condition, obj) {
        this.orgCondtionStr = condition
        this.org.f_orgname = obj.orgnames[0]
        this.orgname = obj.orgnames[0]
        this.depname = obj.depnames[0]
        this.operatorname = obj.operatornames[0]
      },
      getdep (obj, val) {
        this.org.f_depname = this.depname = val[0]
        this.userresid = obj
        this.f_depid = obj
      },
      getuser ( obj, val) {
        this.org.f_operator = this.operatorname = val[0]
        this.f_operatorid = obj
      },
      // 根据文件路径获取储存的报表内容
      getFileContent (path) {
        this.$resetpost('api/af-revenue/logic/getReportFileContent', {f_files_path: path}, {resolveMsg: null, rejectMsg: null}).then((res) => {
          this.reportStr = res.data.filecontent
          this.model.state = '正确'
        })
      },
      confirm () {
        if (!this.filename || this.filename === '') {
          this.$showAlert('无法保存，文件名不能为空', 'warning', 3000)
        }
        let saveGen = saveFile(this)
        co(saveGen)
      },
      close () {
        this.show = false
      },
      // 将报表保存成文件
      confirmReport () {
        this.show = true
        this.filename = this.data.f_report_name + this.$login.toStandardDateString()
      }
    },
    watch: {
      'data' (val) {
        if (val.f_files_path) {
          this.getFileContent(val.f_files_path)
        } else {
          this.reportStr = null
        }
      },
      'model.data' (val) {
        this.datas = val.replace(/#\(|\)|\^(.+?)\)/g, '').toString().substring(26, val.length - 8)
        let len=0
        let a=val.split('</tr>')
        for(let i=0;i<a.length;i++){
          if(a[i].split('</td>').length-1>len){
            len=a[i].split('</td>').length-1
          }
        }
        this.tablen=len
      }
    },
    computed: {
      payment() {
        return [...this.$appdata.getParam('付款方式查询')]
      }
    }
  }
</script>
<style scoped>
  .noborder{
    border: none;
  }
</style>
