<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-3">
              <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 HH:mm:ss'"
                          :show-reset-button="reset">
              </datepicker>
            </div>
            <div class="col-sm-3">
              <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 HH:mm:ss'"
                          :show-reset-button="reset">
              </datepicker>
            </div>
            <div class="col-sm-3">
              <label class="font_normal_body">&nbsp;&nbsp;&nbsp;公司&nbsp;&nbsp;&nbsp;</label>
              <right-tree @re-res="$parent.$parent.getRes"></right-tree>
            </div>
            <div class="col-sm-3">
              <label class="font_normal_body">用户所属公司</label>
              <res-select restype='organization'
                          @res-select="$parent.$parent.getRess"
                          :initresid="$parent.$parent.curorgid">
              </res-select>
            </div>
            <div class="col-sm-3 ">
              <label class="font_normal_body">用户分类</label>
              <v-select :value.sync="model.f_user_type"
                        @change="$parent.$parent.userTypeChange"
                        :options='$parent.$parent.usertypes' placeholder='请选择' v-model="model.f_user_type"
                        condition="uf.f_user_type = '{}'"
                        close-on-select></v-select>
            </div>
            <div class="col-sm-3 ">
              <label class="font_normal_body">用户类型</label>
              <v-select :value.sync="model.f_user_mold"
                        @change="$parent.$parent.userMoldChange"
                        :options='$parent.$parent.usermold' placeholder='请选择' v-model="model.f_user_mold"
                        condition="uf.f_user_mold = '{}'"
                        close-on-select></v-select>
            </div>
            <div class="col-sm-3 ">
              <label class="font_normal_body">用气性质</label>
              <v-select :value.sync="model.f_gasproperties" v-model="model.f_gasproperties"
                        :options='$parent.$parent.gasproperties' placeholder='请选择'
                        condition="uf.f_gasproperties = '{}'"
                        close-on-select></v-select>
            </div>
          </div>
          <div class="row" style="text-align: right">
            <div class="span col-sm-3"></div>
            <div class="span col-sm-3"></div>
            <div class="span col-sm-3"></div>
            <div class="span col-sm-3" style="text-align: right;">
              <button class="button_search" @click="$parent.$parent.searchData()">查询</button>
              <report-print id='gasprice' top='3cm' left='0' width='100%' height='100%' :preview="true"></report-print>
              <report-excel id='gasprice'
                            v-if="$parent.$parent.jurisdiction.includes('excel导出')"
              ></report-excel>
            </div>
          </div>
        </div>
      </criteria>
      <div partial='list' v-el:handcollect id='gasprice' style="overflow-y: scroll">
        <table class='tableprint' style="margin: 0px auto">
          <thead>
          <tr>
            <th colspan='7' style="font-weight: normal; text-align: left;">
              <h3 style="text-align: center">咸阳财务收费报表(新)</h3>
            </th>
          </tr>
          <tr>
            <th colspan='8' style="font-weight: normal; text-align: center;">
              开始时间：{{ model.model.startDate }}&nbsp;&nbsp;&nbsp;
              结束时间：{{ model.model.endDate }}
            </th>
          </tr>
          <tr>
            <th colspan='8' style="font-weight: normal; text-align: center;">
              打印时间：{{{ $parent.printTime }}}
            </th>
          </tr>
          <tr>
            <th colspan='8' style="font-weight: normal; text-align: center;">
              <div>
                <span v-show="$parent.orgname.trim()!=''">公司：{{ $parent.orgname }}</span>
                <span v-show="$parent.depname.trim()!=''"> 部门：{{ $parent.depname }}</span>
                <span v-show="$parent.operatorname.trim()!=''">人员：{{ $parent.operatorname }}</span>
              </div>
            </th>
          </tr>
          </thead>
          <tr>
            <td colspan='12'>
              {{{ model.data.substring(26, model.data.length - 8) }}}
            </td>
          </tr>
          <tfoot>
          <tr style="text-align: left">
            <th colspan='3'>财务审核：</th>
            <th colspan='3'>收款审核：</th>
            <th colspan='1'>收款员：</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'
import * as Util from "../../Util";

let saveFile = function* (self) {
  // 线验证文件是否重名
  let count = yield self.$resetpost('rs/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('rs/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: [],
      gasproperties: [],
      usermold: [],
      curorgid: [this.$login.f.orgid],
      f_orgid: this.$login.f.orgid,
      f_orgid_other: this.$login.f.orgid,
      f_depid: this.$login.f.depids,
      f_operatorid: this.$login.f.id,
      jurisdiction: this.$login.r,
      operatorid: [],
      depid: [],
      orgname: '',
      depname: '',
      operatorname: '',
      f_inputtor: [],
      f_user_type: '',
      orgCondtionStr: '1=1',
      model: new DataModel('rs/report/咸阳财务收费新报表(总计)', {
        startDate: 'this.model.startDate', endDate: 'this.model.endDate',
        f_orgid: 'this.model.f_orgid',
        f_orgid_other: 'this.model.f_orgid_other',
        userCondition: 'this.model.userCondition'
      }),
      reportStr: null,
      show: false,
      userCondition: ' 1 = 1 '
    }
  },
  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.$refs.paged.$refs.criteria.search()
    console.log(this.$login.f)
  },
  methods: {
    userTypeChange() {
      this.usermold = []
      this.f_gasproperties = []
      if (this.$refs.paged.$refs.criteria.model !== null) {
        this.$refs.paged.$refs.criteria.model.f_gasproperties = ''
        this.$refs.paged.$refs.criteria.model.f_user_mold = ''
        this.usermold = this.$appdata.getParam(this.$refs.paged.$refs.criteria.model.f_user_type[0])
      } else {
        this.usermold = [{label: '全部', value: ''}]
      }
    },
    userMoldChange() {
      this.gasproperties = []
      if (this.$refs.paged.$refs.criteria.model !== null) {
        this.$refs.paged.$refs.criteria.model.f_gasproperties = ''
        if (this.$refs.paged.$refs.criteria.model.f_user_mold[0] === '居民') {
          this.gasproperties = this.$appdata.getParam(this.$refs.paged.$refs.criteria.model.f_user_type[0] + this.$refs.paged.$refs.criteria.model.f_user_mold[0])
          return
        }
        this.gasproperties = this.$appdata.getParam(this.$refs.paged.$refs.criteria.model.f_user_mold[0])
      } else {
        this.gasproperties = [{label: '全部', value: ''}]
      }
    },
    getRes(obj) {
      this.orgname = obj.res[0]
      this.depresid = obj.resids
      this.f_orgid = obj.resids
      this.f_orgid_other = obj.resids
    },
    getdep(obj, val) {
      // this.depname = val[0]
      this.userresid = obj
      this.f_depid = obj
    },
    getuser(obj, val) {
      this.operatorname = val[0]
      this.f_operatorid = obj
    },
    getRess(obj) {
      if (obj.length > 0) {
        this.userCondition = ` info.f_orgid in ${Util.convertToIn(obj)} `
      } else {
        this.userCondition = ' 1 = 1 '
      }
    },
    searchData() {
      this.$refs.paged.$refs.criteria.search()
    },
    selfSearch(args) {
      this.printTime = this.$login.toStandardTimeString()
      let orgcondition = '1=1'
      let orgConditionOther = '1=1'
      if (this.f_orgid[0]) {
        var id = this.f_orgid.toString()
        var ids = id.split(',')
        console.log('几个公司:' + ids.length)
        var orgids = ''
        var f_orgids = ''
        if (ids.length == 1) {
          orgcondition += " and s.F_PAYMENT != '银行'"
          f_orgids += "'" + ids[0] + "'"
        } else {
          for (var i = 0; i < ids.length; i++) {
            console.log('id = ' + ids[i])
            orgids += "'" + ids[i] + "',"
          }
          f_orgids = orgids.substring(0, orgids.length - 1)
        }

        orgcondition += " and s.f_orgid in ( " + f_orgids + " ) "
        console.log('f_orgids:' + f_orgids)
      }
      if (this.f_orgid_other[0]) {
        var id = this.f_orgid.toString()
        var ids = id.split(',')
        console.log('几个公司:' + ids.length)
        var orgids = ''
        var f_orgids = ''
        if (ids.length == 1) {
          orgConditionOther += " and 1= 1"
          f_orgids += "'" + ids[0] + "'"
        } else {
          for (var i = 0; i < ids.length; i++) {
            console.log('id = ' + ids[i])
            orgids += "'" + ids[i] + "',"
          }
          f_orgids = orgids.substring(0, orgids.length - 1)
        }
        orgConditionOther += " and f_orgid in ( " + f_orgids + " ) "
        console.log('f_orgids:' + f_orgids)
      }
      this.$refs.paged.$refs.criteria.model.f_orgid = orgcondition
      this.$refs.paged.$refs.criteria.model.f_orgid_other = orgConditionOther
      this.$refs.paged.$refs.criteria.model.userCondition = this.userCondition
      this.$refs.paged.search(args)
    },
    // 根据文件路径获取储存的报表内容
    getFileContent(path) {
      this.$resetpost('rs/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
      }
    }
  },
  computed: {
    usertypes() {
      return [{label: ' 全部 ', value: ''}, ...this.$appdata.getParam('用户类型')]
    },
    inputtor() {
      return [{label: '全部', value: ''}, ...this.$appdata.getParam('抄表员')]
    },
  }
}
</script>
<style scoped>
.noborder {
  border: none;
}
</style>
