<template>
  <div id="boxlist" class="flex" style="flex:1; ">
    <criteria-paged :model="model" v-ref:paged>
      <criteria partial='criteria' @condition-changed='$parent.selfSearch' v-ref:cri>
        <div novalidate class="form-horizontal  select-overspread auto" partial>
          <div class="row">
            <div class="col-sm-2 form-group">
              <label class="font_normal_body">组织机构</label>
              <res-select restype='organization'
                          :is-mul="false"
                          :initresid="$parent.$parent.curorgid"
                          @res-select="$parent.$parent.getorgCur">
              </res-select>
            </div>
            <div class="col-sm-2  form-group">
              <label class="font_normal_body">编&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;号</label>
              <input type="text" class="input_search" style="width:60%" v-model="model.f_adjustable_id"
                     condition="a.f_adjustable_id like '%{}%'" placeholder='调压箱编号'>
            </div>
            <div class="col-sm-2  form-group">
              <label class="font_normal_body">名&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;称</label>
              <input type="text" class="input_search" style="width:60%" v-model="model.f_adjustable_name"
                     condition="f_adjustable_name like '%{}%'" placeholder='调压箱名称'>
            </div>
            <div class="col-sm-2  form-group">
              <label class="font_normal_body">地&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;址</label>
              <input type="text" class="input_search" style="width:60%" v-model="model.f_adjustable_address"
                     condition="f_adjustable_address like '%{}%'" placeholder='调压箱地址'>
            </div>
            <div class="span" style="float:right;">
              <button class="button_search button_spacing" @click="$parent.$parent.search()" v-el:cx>查询</button>
              <button class="button_new button_spacing" @click="$parent.$parent.createNew()" v-el:cx>新建</button>
              <export-excel
                :data="{condition: $parent.$parent.excelCondition}"
                :field="$parent.$parent.fields"
                sqlurl="rs/logic/saleExport" progress="saleGetExportProgress" sql-name="adjustableboxQuery"
                template-name='调压箱导出'
                :choose-col="true">
              </export-excel>
            </div>
          </div>
        </div>
      </criteria>
      <data-grid class="list_area table_sy" :model="model" partial='list' v-ref:grid>
        <template partial='head'>
          <tr>
            <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_adjustable_id }}</td>
          <td style="text-align: center;">{{ row.f_adjustable_name }}</td>
          <td style="text-align: center;">{{ row.f_adjustable_address }}</td>
          <td style="text-align: center;">{{ row.counts }}</td>
          <td style="text-align:center">
            <button class="btn btn-link " v-if="row.counts === 0" @click.stop="$parent.$parent.$parent.del(row.id)">
              删除
            </button>
          </td>
        </template>
      </data-grid>
    </criteria-paged>
  </div>
</template>
<script>
import {PagedList} from 'vue-client'

export default {
  title: '调压箱',
  ready () {
    this.search()
  },
  data () {
    return {
      model: new PagedList('rs/sql/adjustableboxQuery', 20),
      fields: {
        'f_adjustable_id': '调压箱编号',
        'f_adjustable_name': '调压箱名称',
        'f_adjustable_address': '调压箱地址',
        'counts': '用户数'
      },
      excelCondition: '',
      curorgid: [this.$login.f.orgid]
    }
  },
  methods: {
    getorgCur (val) {
      this.f_orgid = this.$login.convertToIn(val)
      this.f_filialeids = val[0]
    },
    search () {
      this.$refs.paged.$refs.cri.search()
    },
    selfSearch (args) {
      if (!this.f_orgid) {
        this.getorgCur([this.$login.f.orgid])
      }
      if (this.f_orgid) {
        args.condition = `${args.condition} and f_orgid in ${this.f_orgid}`
      }
      this.excelCondition = args.condition
      this.model.search(args.condition, args.model)
    },
    createNew () {
      this.$dispatch('add')
    },
    del (id) {
      console.log('删除的id:', 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: {}
}
</script>
