<template>
  <a-card class="electronicScale-container" :bordered="false">
    <!-- 搜索框 -->
    <div class="electronicScale-container-searchForm">
      <a-form-model :model="searchForm" layout="inline" ref="searchForm" @keyup.enter.native="$refs.table.refresh(true)">
        <a-row :gutter="24">
          <a-col :xs="24" :sm="12" :md="8" :lg="6">
            <a-form-model-item label="设备MAC地址" :label-col="{ span: 7 }" :wrapper-col="{ span: 17 }">
              <a-input v-model="searchForm.macAddress" placeholder="请输入设备MAC地址" :maxLength="12" allowClear id="electronicScale-macAddress"/>
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12" :md="8" :lg="6">
            <a-button class="handle-btn serach-btn" type="primary" @click="$refs.table.refresh(true)" id="electronicScale-refresh">查询</a-button>
            <a-button class="handle-btn" type="" @click="handleReset" id="electronicScale-handleReset">重置</a-button>
          </a-col>
        </a-row>
      </a-form-model>
    </div>
    <!-- 新增 -->
    <div class="electronicScale-container-add"><a-button type="primary" icon="plus" @click="openModal" id="electronicScale-openModal" v-hasPermission="'B_electronicScale_add'">新增</a-button></div>
    <!-- 合计 -->
    <a-alert type="info" showIcon style="margin-bottom:15px">
      <div class="ftext" slot="message">
        总计
        <span>{{ totalNum }} 个</span>
      </div>
    </a-alert>
    <!-- 列表 -->
    <s-table ref="table" :rowKey="record => record.id" :columns="columns" :data="loadData" bordered>
      <span slot="action" slot-scope="text, record">
        <a-icon type="qrcode" title="二维码" class="actionBtn icon-green" @click="getQrCode(record)" />
        <a-icon type="edit" title="编辑" class="actionBtn icon-blues" @click="handleEdit(record)" v-hasPermission="'B_electronicScale_edit'"/>
        <a-icon type="delete" title="删除" class="actionBtn icon-red" @click="delect(record)" v-hasPermission="'B_electronicScale_del'"/>
      </span>
      <span v-if="!($hasPermissions('B_electronicScale_edit') && $hasPermissions('B_electronicScale_del'))">--</span>
    </s-table>
    <!-- 新增/编辑弹窗 -->
    <addElectronicScale :visible="isOpenModal" :id="itemId" :deviceCode="itemDeviceCode" @refresh="refreshTable" @close="isOpenModal = false"></addElectronicScale>
    <!-- 查看小程序码 -->
    <a-modal
      class="qrCodeModal"
      title="查看MAC地址二维码"
      :width="400"
      :footer="null"
      :shName="shName"
      :destroyOnClose="true"
      @cancel="isQrCodeModal = false"
      v-model="isQrCodeModal">
      <img :src="qrCode" width="300" height="300" class="qrCode" />
      <a-button type="primary" class="downQrCode" @click="downloadCode">下载二维码</a-button>
    </a-modal>
  </a-card>
</template>

<script>
import { STable } from '@/components'
import addElectronicScale from './addElectronicScale.vue'
import { getDeviceList, delectDevice } from '@/api/electronicScale.js'
import QRCode from 'qrcode'
export default {
  components: { STable, addElectronicScale },
  data () {
    return {
      searchForm: {
        macAddress: '' // 设备MAC地址
      },
      isOpenModal: false, // 默认弹窗关闭
      itemDeviceCode: '', // 编辑行设备编号
      itemId: '', // 编辑行id
      totalNum: '', // 总计条数
      isQrCodeModal: false,
      shName: '',
      qrCode: '',
      // 列表表头
      columns: [
        { title: '序号', dataIndex: 'no', width: 60, align: 'center' },
        { title: '创建时间', dataIndex: 'createDate', width: 100, align: 'center' },
        { title: '设备MAC地址', dataIndex: 'macAddress', width: 100, align: 'center' },
        { title: '设备名称', dataIndex: 'deviceName', width: 100, align: 'center', ellipsis: true, customRender: text => { return text || '--' } },
        { title: '设备编号', dataIndex: 'deviceCode', width: 100, align: 'center', ellipsis: true, customRender: text => { return text || '--' } },
        { title: '设备型号', dataIndex: 'deviceModel', width: 100, align: 'center', ellipsis: true, customRender: text => { return text || '--' } },
        { title: '生产厂家', dataIndex: 'manufacturer', width: 100, align: 'center', ellipsis: true, customRender: text => { return text || '--' } },
        { title: '操作', dataIndex: 'action', width: 100, align: 'center', scopedSlots: { customRender: 'action' } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return getDeviceList(Object.assign(parameter, this.searchForm, { deviceType: 'STEELYARD' })).then(res => {
          if (res.status == 200) {
            const no = (res.data.pageNo - 1) * res.data.pageSize
            for (let i = 0; i < res.data.list.length; i++) {
              const _item = res.data.list[i]
              _item.no = no + i + 1
            }
            this.totalNum = res.data.count
            return res.data
          } else {
            this.totalNum = 0
          }
        })
      }
    }
  },
  methods: {
    //  下载二维码
    downloadCode (item) {
      const link = document.createElement('a')
      link.href = this.qrCode
      if (this.shName) {
        link.download = this.shName + '.png'
      } else {
        link.download = 'MAC地址二维码' + '.png'
      }

      link.click()
    },
    // 查看二维码
    getQrCode (item) {
      const _this = this
      const opts = {
        errorCorrectionLevel: 'H',
        type: 'image/jpeg',
        quality: 0.3,
        margin: 1.5,
        width: 240,
        color: {
          dark: '#000000',
          light: '#ffffff'
        }
      }
      const url = item.macAddress
      _this.shName = item.deviceName
      if (url) {
        QRCode.toDataURL(url, opts, function (err, url) {
          _this.qrCode = url
          _this.isQrCodeModal = true
        })
      }
    },
    // 重置
    handleReset () {
      this.searchForm = {
        macAddress: ''
      }
      this.$refs.table.refresh(true)
    },
    // 新增
    openModal () {
      this.itemId = ''
      this.itemDeviceCode = ''
      this.isOpenModal = true
    },
    // 编辑
    handleEdit (record) {
      this.itemId = record.id
      this.itemDeviceCode = record.srcDeviceCode
      this.isOpenModal = true
    },
    // 删除
    delect (record) {
      const _this = this
      _this.$confirm({
        title: '警告',
        centered: true,
        content: '删除后无法恢复，确认删除?',
        okText: '确定',
        cancelText: '取消',
        onOk () {
          delectDevice({
            id: record.id
          }).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$refs.table.refresh()
            }
          })
        }
      })
    },
    // 保存或编辑后刷新列表
    refreshTable () {
      // 编辑
      if (this.itemId) {
        this.$refs.table.refresh()
      } else {
        // 保存
        this.$refs.table.refresh(true)
      }
    }
  }
}
</script>

<style lang="less" scoped>
.electronicScale-container {
  .electronicScale-container-searchForm {
    .ant-form-inline .ant-form-item {
      width: 100%;
    }
  }

  // 查询按钮
  .handle-btn {
    margin-top: 4px;
  }

  .serach-btn {
    margin-right: 10px;
  }

  .electronicScale-container-add {
    margin: 15px 0;
  }
}
 // 查看小程序码  弹框
  .qrCodeModal {
    .qrCode {
      display: block;
      max-width: 100%;
      margin: 0 auto;
    }
    .downQrCode {
      display: block;
      margin: 30px auto;
    }
  }
</style>
