<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">

      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input v-model="query.deviceSn"  size="small" placeholder="输入设备号" style="width: 180px;" class="filter-item" @keyup.enter.native="crud.toQuery"/>
        <el-input v-model="query.model"  size="small" placeholder="输入型号" style="width: 180px;" class="filter-item" @keyup.enter.native="crud.toQuery"/>
        <el-input v-model="query.brand"  size="small" placeholder="输入品牌" style="width: 180px;" class="filter-item" @keyup.enter.native="crud.toQuery"/>
        <el-select v-model="query.type"  size="small" placeholder="选择类型" class="filter-item" style="width: 180px" @change="crud.toQuery">
          <el-option key="" label="全部" value=""/>
          <el-option v-for="item in typeList" :key="item.value" :label="item.label" :value="item.value"/>
        </el-select>
        <rrOperation/>
      </div>

      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="设备号" prop="deviceSn">
            <el-input v-model="form.deviceSn" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="型号" prop="model">
            <el-input v-model="form.model" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="品牌">
            <el-input v-model="form.brand" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="固件版本">
            <el-input v-model="form.firmwareVersion" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="放置位置">
            <el-input v-model="form.location" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="类型">
            <el-select v-model="form.type"  size="small" placeholder="设备类型" class="filter-item" style="width: 370px">
              <el-option v-for="item in typeList" :key="item.value" :label="item.label" :value="item.value"/>
            </el-select>
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="form.remark" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>

      <el-dialog :visible.sync="qrCodeVisible" width="500px">
        <div style="text-align: center"><img :src="baseApi + '/file/image/' + qrCodeUrl" /></div>
      </el-dialog>

      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="deviceCode" label="设备码" />
        <el-table-column prop="deviceSn" label="设备序列号" />
        <el-table-column prop="model" label="型号" />
        <el-table-column prop="brand" label="品牌" />
        <el-table-column prop="firmwareVersion" label="固件版本" />
        <el-table-column prop="location" label="放置位置" />
        <el-table-column prop="status" label="设备状态">
          <template slot-scope="scope">
            <span v-if="scope.row.status==1">在线</span>
            <span v-else-if="scope.row.status==2">离线</span>
            <span v-else>故障</span>
          </template>
        </el-table-column>
        <el-table-column prop="type" label="类型">
          <template slot-scope="scope">
            <span v-if="scope.row.type==1">打印机</span>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" />
        <el-table-column v-if="checkPer(['admin','busDevice:edit','busDevice:del'])" label="操作" width="250px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
<!--            <el-button type="success" size="mini" title="设备指令" @click="openCommandData(scope.row.id)"><svg-icon icon-class="chain" /></el-button>-->
            <el-button type="warning" size="mini" title="生成二维码" @click="createQrCode(scope.row.id)"><svg-icon icon-class="exit-fullscreen" /></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />

      <!-- 子窗口（模态框组件） -->
      <commandPage :visible.sync="showCommandData" :data="deviceId" @close="closeCommandPage" />

    </div>
  </div>
</template>

<script>
import crudBusDevice, { getQrCode } from '@/api/bus/busDevice/busDevice'
import commandPage from '@/views/bus/busDeviceCommand/index'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import {mapGetters} from "vuex";

const defaultForm = { id: null, deviceSn: null, model: null, brand: null, firmwareVersion: null, location: null, status: null, onlineTime: null, offlineTime: null, totalPrintTime: null, remark: null, type: null, createBy: null, updateBy: null, createTime: null, updateTime: null }
export default {
  name: 'BusDevice',
  components: { pagination, crudOperation, rrOperation, udOperation, commandPage },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '', url: 'api/busDevice', idField: 'id', sort: 'id,desc', crudMethod: { ...crudBusDevice }})
  },
  data() {
    return {
      qrCodeUrl: '',
      qrCodeVisible: false,
      permission: {
        add: ['admin', 'busDevice:add'],
        edit: ['admin', 'busDevice:edit'],
        del: ['admin', 'busDevice:del']
      },
      typeList: [
        { 'label': '打印机', 'value': 1 },
      ],
      rules: {
        deviceSn: [
          { required: true, message: '设备序列号不能为空', trigger: 'blur' }
        ],
        model: [
          { required: true, message: '型号不能为空', trigger: 'blur' }
        ]
      },
      showCommandData: false,
      commandData: [],
      commandPage: { page: 1, size: 8, total: 0 },
      deviceId: 0
    }
  },
  created() {
    this.crud.optShow.download = false
  },
  computed: {
    ...mapGetters([
      'baseApi'
    ])
  },
  methods: {
    async createQrCode(id) {
      await getQrCode(id).then(res => {
        this.qrCodeVisible = true
        this.qrCodeUrl = res
      })
    },
    openCommandData(id) {
      this.showCommandData = true
      this.deviceId = id
    },
    closeCommandPage() {
      this.showCommandData = false
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    }
  }
}
</script>

<style scoped>

</style>
