<template>
  <div style="flex:1;" class="flex">
    <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 form-input-group" >
            <label class="control-label">客户类型</label>
            <v-select :value.sync="model.f_user_type"
                      :options='$parent.$parent.usertypes' placeholder='请选择'
                      v-model="model.f_user_type"
                      condition="f_user_type = '{}'"
                      close-on-select></v-select>
          </div>
            <div class="col-sm-2 form-group form-input-group" >
              <label class="control-label">银行名称</label>
              <v-select :value.sync="model.f_bank_name"
                        v-model="model.f_bank_name"
                        :options='$parent.$parent.f_bankname' placeholder='请选择'
                        condition="f_bank_name = '{}'"
                        close-on-select
              ></v-select>
            </div>
            <div class="col-sm-2 form-group form-input-group" >
              <label class="control-label">组织机构</label>
              <right-tree :userid.sync='$parent.$parent.userid' :source.sync='$parent.$parent.source'  @re-res="$parent.$parent.getRes"></right-tree>
            </div>

            <div class="span" style = "float:right">
              <button class="btn btn-success" @click="search()">查询</button>
              <button class="btn btn-success" @click="$parent.$parent.clear()">清空</button>
              <export-excel :data="$parent.$parent.getCondition"
                            :field="$parent.$parent.getfield"
                            sqlurl="rs/logic/saleExport" progress="saleGetExportProgress" sql-name="getAllOverdueUser" template-name='银行送盘导出'  :choose-col="true"></export-excel>
              <button class="btn btn-success" @click="$parent.$parent.bankoffercommit()">送盘</button>
            </div>

          </div>
        </div>
      </criteria>

      <data-grid  :model="model" partial='list' v-ref:grid :classname="$parent.classname" @dblclick="$parent.dblclick">
        <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>
            <th><nobr>银行证件号</nobr></th>
            <th><nobr>营业网点</nobr></th>
            <th><nobr>分公司</nobr></th>
          </tr>
        </template>
        <template partial='body'>
          <td style="text-align: center;"><nobr>{{row.f_userinfo_code}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_user_name}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_user_phone}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_user_type}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_address}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_balance}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.overdue}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_oughtamount}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_oughtfee}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.qianfei}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_bank_name}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_bank_accopen}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_bank_account}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_bank_idnumber}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_outlets}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_filiale}}</nobr></td>
        </template>
        <template partial='foot'></template>
      </data-grid>

    </criteria-paged>


  </div>
</template>

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

  export default {
    title: '银行送盘',
    data () {
      return {
        showinfo: false,

        data: {},
        model: new PagedList('rs/sql/getAllOverdueUser', 20),

        filialeNameStr: null,
        filialeCodeStr: this.$login.f.f_orgids,
        userid: this.$login.f.id,
        source: `tool.getFullTree(this.getRights().where(row.getType() != $zone$).where(row.getType() != $role$))`,

        modelval: [],
        printshow: false,
        all: false,
        fields: {},
        thead: '',
        tfoot: '',

        usertypes: this.$appdata.getParam('用户类型') ? [{label: '全部', value: ''}, ...this.$appdata.getParam('用户类型')] : [],
        f_bankname: this.$appdata.getParam('银行名称') ? [{label: '全部', value: ''}, ...this.$appdata.getParam('银行名称')] : []
      }
    },
    ready () {
    },
    methods: {
      selfSearch (args) {
        args.condition = `${args.condition} and f_cost_type = '银行代扣' and f_filialeids like '${this.filialeCodeStr}%'`
        this.model.search(args.condition, args.model)
      },
      clear () {
        Object.keys(this.$refs.paged.$refs.cri.model).forEach((key) => {
          this.$refs.paged.$refs.cri.model[key] = []
        })
      },
      getRes (obj) {
        this.filialeNameStr = obj.res
        this.filialeCodeStr = obj.resids
      },
      stamp () {
        this.all = false
        this.fields = this.getfield
        console.log('所有打印字段', this.fields)
        this.printshow = true
        this.put()
      },
      put () {
        // 对Modelval进行排序
        this.sortModelval()
        this.thead = `<tr><th colspan=${this.modelval.length}>收费查询统计报表</th></tr><tr>`
        for (let key of this.modelval) {
          this.thead += '<th>' + this.fields[key] + '</th>'
        }
        this.thead += '</tr>'
      },
      print () {
        this.$refs.print.PrintAsFile()
        this.printshow = false
      },
      close () {
        this.printshow = false
        this.all = false
      },
      // 对选择的列进行排序
      sortModelval () {
        let sortModel = []
        Object.keys(this.fields).forEach((key) => {
          if (this.modelval.includes(key)) {
            sortModel.push(key)
          }
        })
        this.modelval = sortModel
        console.log('选择的打印的字段', this.modelval)
      },
      bankoffercommit () {
        // console.log(this.model.condition)
        this.$resetpost('rs/logic/bankOfferCommit', { data: {condition: this.model.condition} }, {resolveMsg: null, rejectMsg: '送盘出现错误'}).then((res) => {
          console.log(res.data)
          this.$showAlert(`成功送盘${res.data.userinfo}户（${res.data.userfiles}块表）`, 'success', 5000)
        })
      }
    },
    watch: {
      'all' (val) {
        if (val) {
          this.modelval = this.bodyData
        } else {
          this.modelval = []
          this.put()
        }
      },
      'modelval.length' () {
        this.put()
      }
    },
    computed: {
      getCondition () {
        return {condition: `${this.$refs.paged.$refs.cri.condition} and f_cost_type = '银行代扣' and f_filialeids like '${this.filialeCodeStr}%'`}
      },
      getfield () {
        return {'f_userinfo_id': '用户号', 'f_userinfo_code': '客户编号', 'f_user_id': '表编号', 'f_user_name': '用户姓名', 'f_user_phone': '用户电话', 'f_user_type': '用户类型', 'f_address': '用户地址', 'f_balance': '账户结余', 'overdue': '违约金', 'f_oughtamount': '应交气量', 'f_oughtfee': '应交气费', 'qianfei': '应交总额', 'f_bank_name': '银行名称', 'f_bank_accopen': '银行账户', 'f_bank_account': '银行账号', 'f_bank_idnumber': '银行证件号', 'f_outlets': '营业网点'}
      }
    }
  }
</script>
