<template>
  <div id="boxlist" class="flex" style="flex:1; ">
    <criteria-paged :model="model" v-ref:paged>
      <criteria partial='criteria' @condition-changed='search' v-ref:cri>
        <div novalidate class="form-inline" partial>
          <div class="form-group">
            <label class = "control-form">设备归属银行:</label>
            <v-select :value.sync="model.f_bank_name" v-model="model.f_bank_name"
                      :options='$parent.$parent.banknames' placeholder='设备归属银行'
                      condition="f_bank_name = '{}'"
                      close-on-select></v-select>
          </div>
          <div class="form-group">
            <label class = "control-form">设备所在地:</label>
            <v-select :value.sync="model.f_outlets" v-model="model.f_outlets"
                      :options='$parent.$parent.outlets' placeholder='设备所在地'
                      condition="f_outlets = '{}'"
                      close-on-select></v-select>
          </div>
          <div class="form-group">
            <label class = "control-form">终端号:</label>
            <input type="text" class="form-control" v-model="model.f_terminal_num"
            condition="f_terminal_num like '%{}%'" placeholder='终端号'>
          </div>
          <div class="form-group">
            <label class = "control-form">终端名称:</label>
            <input type="text" class="form-control" v-model="model.f_terminal_name"
                   condition="f_terminal_name like '%{}%'" placeholder='终端名称'>
          </div>


          <div class="form-group">
            <label class="control-label">设备状态</label>
            <v-select :value.sync="model.f_state"
                      v-model="model.f_state"
                      :options='$parent.$parent.bankStates' placeholder='请选择'
                      condition="f_state = '{}'"
                      close-on-select></v-select>
          </div>

          <div class="form-group">
            <button class="btn btn-success width-80" @click="$parent.$parent.search()" v-el:cx>查询</button>
          </div>
          <div class="form-group">
            <button class="btn btn-success width-80" @click="$parent.$parent.createNew()" v-el:cx>新建</button>
          </div>
        </div>
      </criteria>
      <data-grid :model="model" partial='list' v-ref:grid>
        <template partial='head'>
          <tr>
            <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_bank_name}}</td>
          <td  style="text-align: center;">{{row.f_outlets}}</td>
          <td  style="text-align: center;">{{row.f_terminal_num}}</td>
          <td  style="text-align: center;">{{row.f_terminal_name}}</td>
          <td  style="text-align: center;">{{row.f_link_man}}</td>
          <td  style="text-align: center;">{{row.f_contact_number}}</td>
          <td style="text-align:center">
            <button type="button" name="button" class="btn btn-link" v-if="row.f_state==='停用'"
            @click.stop="$parent.$parent.$parent.modifyState(row, 'startup')">启用</button>
            <button type="button" name="button" class="btn btn-link" v-if="row.f_state==='启用'"
            @click.stop="$parent.$parent.$parent.modifyState(row, 'breakdown')">停用</button>
          </td>
        </template>
      </data-grid>
  </criteria-paged>
 </div>
</template>
<script>
import { PagedList } from 'vue-client'


export default {
  title: '银行对接',
  data () {
    return {
      model: new PagedList('rs/sql/singleTable_OrderBy', 20,{
        items:"'*'",tablename:'"t_bankdocking"',orderitem:'"id desc"'
      })
    }
  },
  props: [],
  ready () {
    this.model.search('1=1')
  },
  methods: {
    search () {
      // this.model.search()
      this.$dispatch('search')
    },
    createNew () {
      // console.log('123123')
      this.$dispatch('add')
    },
    modifyState(row,state){
      if (state==='startup') {
        this.$showMessage('确认启用该终端吗?', ['confirm', 'cancel']).then(
          (response) => {
            if (response === 'confirm') {
              this.$resetpost('rs/logic/changebankstate',{
                data:{state:'启用',data:row}
              })
              .then(()=>{
                this.model.search('1=1')
              })
            }
            })
          }else{
        this.$showMessage('确认停用该终端吗?', ['confirm', 'cancel']).then(
          (response) => {
            if (response === 'confirm') {
              this.$resetpost('rs/logic/changebankstate',{
                data:{state:'停用',data:row}
              })
              .then(()=>{
                this.model.search('1=1')
              })
            }
            })
          }
    },
    del (id) {
      this.$showMessage('确认删除吗?', ['confirm', 'cancel']).then(
        (response) => {
          if (response === 'confirm') {
            this.$resetpost('rs/logic/deleteBox', {data: {id: id}}, {resolveMsg: '删除成功', rejectMsg: '删除失败'})
            .then((res) => { this.$dispatch('success', '调压箱删除', this.data, res) })
            .catch((error) => { this.$dispatch('error', '调压箱删除', this.data, error) })
          } else if (response === 'cancel') {
            this.$dispatch('cancel')
          }
        })
    }
  },
  computed: {
    bankStates () {
      return [{label: '全部', value: ''}, {label: '启用', value: '启用'}, {label: '停用', value: '停用'}]
    },
    banknames () {
      return [{label: '全部', value: ''}, ...this.$appdata.getParam('设备归属银行')]
    },
    outlets () {
      return [{label: '全部', value: ''}, ...this.$appdata.getParam('设备所在地')]
    }
  }
}
</script>
