<template>
  <div id="unit" class="flex-row">
    <div class="basic-main">
      <div 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">
                  <label for="startDate" class="font_normal_body">开始日期</label>
                  <datepicker id="startDate" placeholder="开始日期" style="width:60%"
                              v-model="model.startDate"
                              :value.sync="model.startDate"
                              :format="'yyyy-MM-dd'"
                              :show-reset-button="true"
                              condition="f_operator_date >= '{} 00:00:00'">
                  </datepicker>
                </div>
                <div class="col-sm-2 form-group">
                  <label for="endDate" class="font_normal_body">结束日期</label>
                  <datepicker id="endDate" placeholder="结束日期" style="width:60%"
                              v-model="model.endDate"
                              :value.sync="model.endDate"
                              :format="'yyyy-MM-dd'"
                              :show-reset-button="true"
                              condition="f_operator_date <= '{} 23:59:59'">
                  </datepicker>
                </div>
                <div class="form-group col-sm-2" >
                  <label class="font_normal_body">错误类型</label>
                  <v-select :value.sync="model.f_operator_type" :value-single="true"
                            class="select_list select"
                            title="参数:抄表错误类型"
                            style="width: 60%"
                            v-model="model.f_operator_type"
                            :options='$parent.$parent.operatortype' placeholder='错误类型'
                            condition = "f_operator_type = '{}'"
                            close-on-select search="true">
                  </v-select>
                </div>
                <div class="form-group col-sm-2" >
                  <label class="font_normal_body">错误原因</label>
                  <v-select :value.sync="model.f_error_type" :value-single="true"
                            class="select_list select"
                            title="参数:错误原因"
                            style="width: 60%"
                            v-model="model.f_error_type"
                            :options='$parent.$parent.errortype' placeholder='错误原因'
                            condition = "f_error_type = '{}'"
                            close-on-select search="true">
                  </v-select>
                </div>
                <div class="span" style="float:right;">
                  <button class="button_search button_spacing" @click="search">查询</button>
                  <button class="button_clear button_spacing" @click="$parent.$parent.clear()">清空</button>
                  <button class="button_clear button_spacing"  @click="$parent.$parent.oneClikeImport()">一键导入</button>
                  <export-excel :data="$parent.$parent.getCondition"
                                :field="$parent.$parent.getfield"
                                sqlurl="api/af-revenue/logic/openapi/exportfile" sql-name="changebattcherror" template-name='报错查询'
                                :choose-col="true"></export-excel>
                  <div style="float: right"  class="button_spacing" :class="{'button_shrink_top':$parent.$parent.criteriaShow,'button_shrink_bottom':!$parent.$parent.criteriaShow}"    @click="$parent.$parent.hidden()">
                  </div>
                </div>
              </div>

              <div class="row" v-show="$parent.$parent.criteriaShow">
                <div class="form-group col-sm-2" >
                  <label class="font_normal_body">银行名称</label>
                  <v-select :value.sync="model.f_bankname" :value-single="true"
                            class="select_list select"
                            title="参数:抄表错误类型"
                            style="width: 60%"
                            v-model="model.f_bankname"
                            :options='$parent.$parent.bankname' placeholder='银行名称'
                            condition = "f_bankname = '{}'"
                            close-on-select search="true">
                  </v-select>
                </div>


                <!--用户名称-->
                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">用户名称</label>
                  <input type="text" style="width:60%" class="input_search" v-model="model.f_user_name"
                         condition="f_user_name like '%{}%'" placeholder='用户名称'>
                </div>
                <!--缴费编号-->
                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">缴费编号</label>
                  <input type="text" style="width:60%" class="input_search" v-model="model.f_bank_pay_number"
                         condition="f_bank_pay_number like '%{}%'" placeholder='缴费编号'>
                </div>
                <div class="col-sm-2 form-group" v-if="false">
                  <label class="font_normal_body">公&ensp;&ensp;司</label>
                  <res-select :initresid="$parent.$parent.orgid"
                              style="width:60%"
                              @res-select="$parent.$parent.getRes"

                              restype='organization'>
                  </res-select>
                </div>
                <div class="form-group col-sm-2" v-if="false">
                  <label class="font_normal_body">选择小区</label>
                  <v-select :value.sync="model.f_residential_area" :value-single="true"
                            class="select_list select"
                            style="width: 60%"
                            v-model="model.f_residential_area"
                            :options='$parent.$parent.residentialArea' placeholder='选择小区'
                            condition = "f_residential_area = '{}'"
                            close-on-select search="true">
                  </v-select>
                </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>
              </tr>
            </template>
            <template partial='body'>
              <td style="text-align:center">
                <nobr>{{$index+1}}</nobr>
              </td>
              <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_meternumber}}</nobr>
              </td>

              <td style="text-align: center;">
                <nobr>{{row.f_bank_pay_number}}</nobr>
              </td>

              <td style="text-align: center;">
                <nobr>{{row.f_operator_date}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_operator_type}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_error_type}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_bankname}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_msg}}</nobr>
              </td>
              <td style="text-align: center;" >
                <button class="button_search" v-if="row.f_error_type==='系统错误'" @click="$parent.$parent.$parent.cxfps(row)">重新反盘</button>
              </td>
            </template>
            <template partial='foot'></template>
          </data-grid>
        </criteria-paged>
      </div>
    </div>
  </div>
</template>

<script>
  import {PagedList,  HttpResetClass} from 'vue-client'
  import defaultPrint from './config/DefaultPrint'
  import exportConfig from "./config/exportConfig";
  //import plugins from "../../../plugins/GetLoginInfoService";
  import plugins from "../../plugins/GetLoginInfoService";

  let readySomething = async function (self) {
    self.$refs.paged.$refs.cri.model.startDate = self.$login.toStandardDateString()
    self.$refs.paged.$refs.cri.model.endDate = self.$login.toStandardDateString()
    // await self.$refs.paged.$refs.cri.search()
    await self.$MagLoadParams.loadParam()
    self.initParams()

    self.sumsmodel = self.$refs.paged.$refs.grid.model.sums
  }

  export default {
    title: '错误查询',
    data() {
      return {
        data: {},
        model: new PagedList('api/af-revenue/sql/changebattcherror', 20),
        criteriaShow: false,
        f_filialeid: this.$login.f.orgid,
        orgid:[this.$login.f.orgid],
        //小区
        residentialArea: [],
        orgCondtionStr: '',
        operatortype:[{label: '全部', value: ''}, ...this.$appdata.getParam('抄表错误类型')],
        errortype:[{label: '全部', value: ''}, ...this.$appdata.getParam('错误原因')],
        bankname:[{label: '全部', value: ''}, ...this.$appdata.getParam('银行名称')],
        showClick:false,
        resdata:[],
      }
    },
    ready() {

      readySomething(this).then(() => {
        this.$emit('ready')
      }).catch((error) => {
        this.$emit('error', error)
      })
    },
    watch:{
    },
    methods: {
     async oneClikeImport(){
        console.log('看看，',this.resdata,this.model.f_error_type)
        if(this.resdata.length>0 && this.$refs.paged.$refs.cri.model.f_error_type==='系统错误'){
          let total=0
          let  totalerror=0
          this.$showAlert(`数据正在处理请稍后`, 'warning')
          for(let i=0;i<this.resdata.length;i++){
            console.log(this.resdata[i])
            let param=this.cxfpsParam(this.resdata[i])
          try {
                let http = new HttpResetClass()
                 await http.load('post', 'rs/logic/machineChargePay', {data: param}, {
                  resolveMsg: '',
                  rejectMsg: '收费失败'
                }).then((res)=>{
                   if (res.data) {
                     let param = {
                       id: this.resdata[i].id,
                       f_error_type: '扣款成功',
                       f_operator_type: `反盘导入`,
                       f_operator_info: `${this.$login.f.name}进行重新反盘,收费成功`,
                       f_msg: `反盘收费,${this.resdata[i].f_userinfo_code}收费成功`
                     }
                     this.$resetpost('rs/logic/updateHanderror', {data: param}, {
                       resolveMsg: '',
                       rejectMsg: '反盘收费成功,修改错误状态失败'
                     })
                     total = total + 1
                   }
                 })
              }catch (e) {
                totalerror=totalerror+1
              }
            }
          this.$closeAlert()
          this.$refs.paged.$refs.cri.search()
          this.$showMessage(`导入成功! 累计导入:${this.resdata.length}, 成功:${total}条,失败${totalerror}条`);
        }else{
          this.$showMessage(`查询不到系统错误数据或请重新选择错误原因，进行查询`);
        }
      },
      cxfpsParam(row){
        let param = {
          f_userinfo_id: row.f_userinfo_id,
          f_collection: row.f_collection ,
          f_print: '普通收据',
          f_payment:'银行转账',
          isBreakOverdue:false,
          f_bank_name: row.f_bankname,
          floor:true,
          operInfo: {
            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,
          }
        }
        return param
      },
    async cxfps(row){
        console.log("登录人",this.$login.f)
          //组织数据
        let param=this.cxfpsParam(row)
        //查询本月是否已缴费
        let queryFee=  await this.$resetpost('rs/logic/feeChargeQuery', {data: param}, {resolveMsg: '', rejectMsg: '查询本月收费失败'})
        if(queryFee.data){
            this.$showAlert('此用户已反盘，请在收费查询确认此用户是否已缴费','warning','3000')
            let data={
              id:row.id,
              f_error_type:'扣款成功',
              f_operator_type: `反盘导入`,
              f_operator_info:`${this.$login.f.name}进行反盘导入,收费成功`,
              f_msg:`反盘收费,${row.f_userinfo_code}收费成功`
            }
             await this.$resetpost('rs/entity/t_handerror',data,{resolveMsg: '', rejectMsg: '更新错误查询记录失败'})
            this.$refs.paged.$refs.cri.search()
        } else {
            let http=new HttpResetClass()
            let res = await http.load('post', 'rs/logic/machineChargePay', {data: param}, {
              resolveMsg: '',
              rejectMsg: '反盘收费失败，请确认系统信息'
            })
            let data={
              id:row.id,
              f_error_type:'扣款成功',
              f_operator_type: `反盘导入`,
              f_operator_info:`${this.$login.f.name}进行重新反盘,收费成功`,
              f_msg:`反盘收费,${row.f_userinfo_code}收费成功`
            }
            if(res.data){
              await this.$resetpost('rs/logic/updateHanderror', {data:data},{resolveMsg: '反盘收费成功', rejectMsg: '反盘收费成功,修改错误状态失败'})
              this.$refs.paged.$refs.cri.search()
            }
      }

    },
    async selfSearch (args){

        args.condition += `and f_orgid = '${this.f_filialeid}'`
        await  this.model.search(args.condition, args.model)
          console.log("rses",this.model)
          this.resdata=[]
          this.resdata=this.model.rows
      },
      getRes (obj) {
        this.orgCondtionStr = plugins.convertToIn(obj)
      },
      clear() {
        Object.keys(this.$refs.paged.$refs.cri.model).forEach((key) => {
          this.$refs.paged.$refs.cri.model[key] = []
        })
      },
      hidden() {
        this.criteriaShow = !this.criteriaShow
      }
    },
    computed: {
      getCondition () {
        var con = this.$refs.paged.$refs.cri.condition+` and f_orgid= '${this.f_filialeid}'`
        // if (this.orgCondtionStr) {
        //   con = `${this.$refs.paged.$refs.cri.condition} and f_orgid in  (${this.orgCondtionStr})`
        // }
        // else {
        //   con = `${this.$refs.paged.$refs.cri.condition}and f_orgid in ('${this.$login.f.orgid}' )`
        // }
        return {
          condition: con
        }
      },
      getfield() {
        return exportConfig.Wrongquery
      }
    }

  }
</script>
