<template>
  <div class="basic-main" style="height: 98%">
    <criteria-paged :model="model" v-ref:paged>
      <criteria partial='criteria' @condition-changed='$parent.selfSearch' v-ref:cri>
        <div class="form-horizontal select-overspread container-fluid auto"  novalidate partial>
          <div class="row">
            <div class="form-group col-sm-2">
              <label class="font_normal_body">用户编号</label>
              <input type="text" style="width:60%" class="input_search" v-model="model.f_userinfo_code" placeholder='用户编号'
                     condition="f_userinfo_code = '{}'">
            </div>
            <div class="form-group col-sm-2">
              <label class="font_normal_body">处理状态</label>
              <v-select v-model="model.f_hand_state"
                        placeholder='处理状态'
                        :value.sync="model.f_hand_state"
                        :options='$parent.$parent.states'
                        condition= "f_examine_state = {}"
                        close-on-select
                        @change="$parent.$parent.search">
              </v-select>
            </div>

            <div class="span" style="float:right;">
              <button class="button_search button_spacing width-60"  @click="search()" v-el:cx>查询</button>
              <button class="button_search button_spacing width-60"
                      @click="$parent.$parent.allsuccess()" v-el:cx>全部通过</button>
              <button class="button_search button_spacing width-60"
                      @click="$parent.$parent.allfaild()" v-el:cx>全部不通过</button>
              <!--<export-excel :data="{condition: condition}" bean="自助抄表审核导出" sqlurl="rs/logic/exportfile"></export-excel>-->
              <div
                :class="{'button_shrink_top':$parent.$parent.criteriaShow,'button_shrink_bottom':!$parent.$parent.criteriaShow}"
                @click="$parent.$parent.hidden()" class="button_spacing" style="float: right">
              </div>
            </div>
          </div>
          <div class="row" v-show="$parent.$parent.criteriaShow">
            <div class="form-group col-sm-2">
              <label class="font_normal_body">用户姓名</label>
              <input type="text" style="width:60%" class="input_search" v-model="model.f_user_name" placeholder='用户姓名'
                     condition="f_user_name = '{}' ">
            </div>
            <div class="form-group col-sm-2">
              <label class="font_normal_body">用户地址</label>
              <input  type="text" style="width:60%" class="input_search" v-model="model.f_address" placeholder='用户地址'
                      condition="f_address like '%{}%'">
            </div>
            <div class="form-group col-sm-2">
              <label class="font_normal_body">开始时间</label>
              <datepicker id="startDate" placeholder="上传开始时间"
                          v-model="model.startDate"
                          style="width:60%"
                          class="datepicker"
                          :value.sync="model.startDate"
                          :disabled-days-of-Week="[]"
                          :format="'yyyy-MM-dd 00:00:00'"
                          condition= "f_examine_date >= '{}'"
                          :show-reset-button="reset">
              </datepicker>
            </div>
            <div class="form-group col-sm-2">
              <label class="font_normal_body">结束时间</label>
              <datepicker id="endDate" placeholder="上传结束时间"
                          v-model="model.endDate"
                          style="width:60%"
                          class="datepicker"
                          :value.sync="model.endDate"
                          :disabled-days-of-Week="[]"
                          :format="'yyyy-MM-dd 23:59:59'"
                          condition= "f_examine_date <= '{}'"
                          :show-reset-button="reset">
              </datepicker>
            </div>
            <res-select-group :initres="$parent.$parent.initres"  @re-res="$parent.$parent.getRes" v-ref:sel></res-select-group>
          </div>
        </div>
      </criteria>
      <data-grid  :model="model" partial='list' class="list_area table_sy" v-ref:grid>
        <template partial='head'>
          <tr>
            <th>序号</th>
            <th>用户编号</th>
            <th>表编号</th>
            <th>用户姓名</th>
            <th>用户地址</th>
            <th>收款金额</th>
            <th>审核提交时间</th>
            <th>审核状态</th>
            <th>操作</th>
          </tr>
        </template>
        <template partial='body'>
          <td style="text-align:center">{{$index+1}}</td>
          <td style="text-align:center">{{row.f_userinfo_code}}</td>
          <td style="text-align:center">{{row.f_user_id}}</td>
          <td style="text-align:center">{{row.f_user_name}}</td>
          <td style="text-align:center">{{row.f_address}}</td>
          <td style="text-align:center">{{row.f_collection}}</td>
          <td style="text-align:center">{{row.f_examine_date}}</td>
          <td style="text-align:center">{{row.f_examine_state}}</td>
          <td style="text-align: center" v-if="row.f_examine_state == '待审核'">
            <button type="button" class="button_search button_spacing width-60" @click.stop="$parent.$parent.$parent.success(row)">通过</button>
            <br>
            <button type="button" class="button_search button_spacing width-60" @click.stop="$parent.$parent.$parent.faild(row)">不通过</button>
          </td>
          <td v-else></td>
        </template>
      </data-grid>
    </criteria-paged>

    <modal :show.sync="show" v-ref:modal 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="pushdata.cause" placeholder='请填写不通过原因,原因将会推送给用户'>
        </div>
      </article>
      <footer slot="modal-footer" class="modal-footer">
        <button v-show="show" type="button" class="btn btn-default" @click='show = false'>取消</button>
        <button v-show="show" type="button" class="btn btn-success" @click='confirm()'>确认</button>
      </footer>
    </modal>
  </div>
</template>

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

  let readyGen = async function (self) {
    await self.$LoadParams.loadMeterBook(self.f_filialeid)
    self.loadMeterBooks()
    self.getfield = self.config.excelHeaders
    self.reflash()
  }

  export default {
    title: '  抄表审核',
    data () {
      return {
        initres: {
          org: [this.$login.f.orgid],
          dep: [],
          user: []
        },
        orgCondtionStr: '',
        criteriaShow: false,
        show: false,
        pushdata: {openid: '', cause: ''},
        row: null,
        model: new PagedList('rs/sql/sellReadAudit', 20),
        states: [{label: '待审核', value: `'待审核'`}, {label: '已审核', value: `'已审核'`}, {label: '未通过', value: `'未通过'`}],
        meterbooks: [{label: '全部', value: ''}]
      }
    },
    ready () {
      readyGen(this)
      this.$refs.paged.$refs.cri.model.f_hand_state[0] =`'待审核'`
    },
    methods: {
      loadMeterBooks () {
        this.meterbooks = [...this.meterbooks, ...this.$GetSaleParam.getMeterBooks()]
      },
      getRes (obj) {
        this.orgCondtionStr = obj
      },
      hidden () {
        this.criteriaShow = !this.criteriaShow
      },
      search () {
        this.$refs.paged.$refs.cri.search()
      },
      selfSearch (args) {
        if (!this.orgCondtionStr) {
          args.condition = `${args.condition}` + ' and f_orgid = ' + this.$login.f.orgid
        } else {
          args.condition = `${args.condition}` + this.orgCondtionStr
        }
        this.model.search(args.condition, args.model)
      },
      async allsuccess () {
        let res = await this.$showMessage('确定通过全部抄表吗?操作后将不可取消', ['confirm', 'cancel'])
        let condition = []
        if (res === 'confirm') {
          if (this.model.rows.length >= 0) {
            if (this.model.model.f_hand_state[0] == `'待审核'`) {
              for (let i = 0; i < this.model.rows.length; i++) {
                condition += `'` + this.model.rows[i].id + `',`
              }
              condition = condition.substring(0, condition.length - 1)
              condition += ')'
              let sql = `update t_sellinggas set f_examine_state = '已审核' WHERE id in ${condition}`
              await this.$resetpost('rs/logic/runSQL', {sql: sql}, {resolveMsg: '操作成功', rejectMsg: '操作失败'})
              this.search()
            } else {
              this.$showMessage('此为非待审核数据，无法修改')
            }
          } else {
            this.$showMessage('暂无数据，无法通过当前所有抄表')
          }
        }
      },
      async success (row) {
        let res = await this.$showMessage('确定通过退费审核吗?操作后将不可取消', ['confirm', 'cancel'])
        if (res === 'confirm') {
          let sql = `update t_sellinggas set f_examine_state = '已审核' where id = '${row.id}'`
          let data = {
            sql: sql
          }
          await this.$resetpost('rs/logic/runSQL',data)
          this.search()
        }
      },
      async allfaild () {
        let res = await this.$showMessage('确定作废全部抄表吗?操作后将不可取消', ['confirm', 'cancel'])
        let condition = '('
        if (res === 'confirm') {
          if (this.model.rows.length > 0) {
            if (this.model.model.f_hand_state[0] === `'待审核'`) {
              for (let i = 0; i < this.model.rows.length; i++) {
                condition += `'` + this.model.rows[i].id + `',`
              }
              condition = condition.substring(0, condition.length - 1)
              condition += ')'
              let sql = `update t_sellinggas set f_examine_state = '未通过' WHERE id in ${condition}`
              await this.$resetpost('rs/logic/runSQL', {sql: sql}, {resolveMsg: '操作成功', rejectMsg: '操作失败'})
              this.search()
            } else {
              this.$showMessage('此为非待审核数据，无法修改')
            }
          } else {
            this.$showMessage('暂无数据，无法作废当前所有抄表')
          }
        }
      },
      async faild (row) {
        let res = await this.$showMessage('确定作废该条抄表吗?操作后将不可取消', ['confirm', 'cancel'])
        if (res === 'confirm') {
          let sql = `update t_sellinggas set f_examine_state = '未通过' where id = '${row.id}'`
          let data = {
            sql: sql
          }
          await this.$resetpost('rs/logic/runSQL',data)
          this.search()
        }
      },
      async confirm () {
        await this.$resetpost('rs/logic/weixinHanPlanAudit', this.row, {resolveMsg: '操作成功', rejectMsg: '操作失败'})
        await this.$resetpost('http://www.hunanhongdaranqi.com/weixin/rs/logic/meterReadFailed', this.pushdata, {resolveMsg: null, rejectMsg: null})
        this.show = false
        this.search()
      }
    },
    computed: {
      inputtores () {
        // 获取抄表员
        console.log('获取抄表员', this.$login.f)
        let rs = []
        if (this.$login.f.f_gasman.length > 0) {
          for (let i = 0; i < this.$login.f.f_gasman.length; i++) {
            let temp = {
              label: this.$login.f.f_gasman[i].name,
              value: this.$login.f.f_gasman[i].name
            }
            rs.push(temp)
          }
        }
        this.inputtores2 = rs
        return [...rs]
      }
    }
  }
</script>
