<template>
  <a-card :bordered="false" class="equipment-table-page-search-wrapper">
    <div class="equipment-searchForm">
      <a-form layout="inline" v-bind="formItemLayout" @keyup.enter.native="$refs.table.refresh(true)">
        <a-row :gutter="48">
          <a-col :span="6">
            <a-form-item label="设备编号" :label-col="formItemLayout.labelCol" :wrapper-col="formItemLayout.wrapperCol">
              <a-input id="equipment-srcDeviceCode" allowClear :maxLength="120" v-model="queryParam.srcDeviceCode" placeholder="请输入设备编号" />
            </a-form-item>
          </a-col>
          <a-col :span="6">
            <a-form-item label="网点名称" :label-col="formItemLayout.labelCol" :wrapper-col="formItemLayout.wrapperCol">
              <a-input id="equipment-stationName" allowClear :maxLength="30" v-model="queryParam.stationName" placeholder="请输入网点名称" />
            </a-form-item>
          </a-col>
          <a-col :span="6">
            <a-form-item label="箱体类型" :label-col="formItemLayout.labelCol" :wrapper-col="formItemLayout.wrapperCol">
              <a-select
                id="equipment-deviceTypeNo"
                allowClear
                show-search
                option-filter-prop="children"
                placeholder="请选择箱体类型"
                v-model="queryParam.deviceTypeNo">
                <a-select-option v-for="(item,index) in deviceTypeList" :key="index" :value="item.deviceTypeNo">{{ item.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="6">
            <a-form-item label="内置箱体数" :label-col="formItemLayout.labelCol" :wrapper-col="formItemLayout.wrapperCol">
              <v-select
                v-model="queryParam.boxNum"
                ref="boxNum"
                id="equipment-boxNum"
                code="BOX_NUM"
                placeholder="请选择内置箱体数"
                allowClear></v-select>
            </a-form-item>
          </a-col>
          <a-col :span="6">
            <a-form-item label="设备状态" :label-col="formItemLayout.labelCol" :wrapper-col="formItemLayout.wrapperCol">
              <a-select id="equipment-onlineFlag" allowClear placeholder="请选择工作状态" v-model="queryParam.onlineFlag" >
                <a-select-option v-for="(item,index) in onlineFlagList" :key="index" :value="item.value">{{ item.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="6">
            <a-form-item label="工作状态" :label-col="formItemLayout.labelCol" :wrapper-col="formItemLayout.wrapperCol">
              <v-select
                v-model="queryParam.state"
                ref="state"
                id="equipment-state"
                code="ENABLE_FLAG"
                placeholder="请选择设备状态"
                allowClear></v-select>
            </a-form-item>
          </a-col>
          <a-col :span="6">
            <a-form-item label="网点标签">
              <a-select
                style="max-height: 50px;overflow: auto;margin-top: 3px;"
                id="equipment-labelNoList"
                placeholder="请选择网点标签"
                mode="multiple"
                allowClear
                v-model="queryParam.labelNoList"
                :showSearch="true"
                option-filter-prop="children"
                :filter-option="filterOption"
              >
                <a-select-option v-for="item in lableData" :key="item.labelNo" :value="item.labelNo">{{ item.labelName }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="6">
            <a-button class="handle-btn serach-btn" id="equipment-btn-serach" type="primary" @click="$refs.table.refresh(true)">查询</a-button>
            <a-button class="handle-btn" type="" id="equipment-btn-reset" @click="handleReset">重置</a-button>
            <a-button class="handle-btn export-btn" id="ledouCollectRecord-export" :loading="loading" @click="handleExport" v-hasPermission="'B_equipment_export'">导出</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 合计 -->
    <div class="total">
      <a-icon type="info-circle" class="iconImg" />
      <strong>总计：</strong><span v-model="orderTotal">{{ orderTotal || 0 }} 条</span>
    </div>
    <s-table
      ref="table"
      size="default"
      :rowKey="(record) => record.id"
      :columns="columns"
      :data="loadData"
      bordered>
      <!-- 设备状态 -->
      <template slot="onlineFlag" slot-scope="text, record">
        <span :class="record.onlineFlag==0 ? 'red' : record.onlineFlag==1 ? 'green' : ''">{{ record.onlineFlag == 0 ? '离线' : record.onlineFlag == 1 ? '在线' : '--' }}</span>
      </template>
      <!-- 工作状态 -->
      <template slot="state" slot-scope="text, record">
        <span :class="record.state==0 ? 'red' : record.state==1 ? 'green' : ''">{{ record.state == 0 ? '已禁用' : record.state == 1 ? '已启用' : '--' }}</span>
      </template>
      <!-- 操作 -->
      <template slot="action" slot-scope="text, record">
        <a-icon type="qrcode" title="查看二维码" @click="getQrCode(record)" :style="{ fontSize: '20px', color: '#008000', padding: '0 10px' }" />
        <a-icon type="snippets" title="掉线日志" @click="checkLog(record)" :style="{ fontSize: '20px', color: '#1890ff', padding: '0 10px' }" />
        <!-- <a-button @click="getQrCode(record)" style="background-color: #008000;color: #fff;">二维码</a-button> -->
        <!-- <a-button
          v-if="$hasPermissions('B_equipment_log')"
          type="primary"
          class="btn-edit-s"
          id="equipment-log-btn"
          @click="checkLog(record)">掉线日志</a-button> -->
        <!-- <span v-else>--</span> -->
      </template>
    </s-table>
    <!-- 掉线日志 -->
    <equipment-log-modal :openModal="openLogModal" :srcDeviceCode="srcDeviceCode" @close="closeModal" />
    <!-- 查看二维码 -->
    <a-modal
      class="qrCodeModal"
      title="查看二维码"
      :width="500"
      :footer="null"
      :destroyOnClose="true"
      @cancel="isQrCodeModal = false"
      v-model="isQrCodeModal">
      <img :src="qrCode" width="400" height="400" class="qrCode" />
      <a-button type="primary" class="downQrCode" @click="downloadCode">下载二维码</a-button>
    </a-modal>
  </a-card>
</template>

<script>
import { STable, VSelect } from '@/components'
import equipmentLogModal from './logModal.vue'
import { deviceList } from '@/api/device'
import { deviceTypeListQuery, exportDevice } from '@/api/boxSetting'
import { lableSeleteList } from '@/api/labelSetting.js'
import QRCode from 'qrcode'
import moment from 'moment'
export default {
  name: 'Equipment',
  components: { STable, VSelect, equipmentLogModal },
  data () {
    return {
      loading: false,
      isQrCodeModal: false, //  查看小程序码   弹框展示状态
      qrCode: '',
      formItemLayout: {
        labelCol: { span: 7 },
        wrapperCol: { span: 17 }
      },
      // 查询参数
      queryParam: {
        srcDeviceCode: '', // 设备编号
        stationName: '', //  网点名称
        deviceTypeNo: undefined, //  箱体类型
        boxNum: undefined, //  内置箱体数
        onlineFlag: undefined, //  设备状态
        state: undefined, //  工作状态
        labelNoList: [] // 网点标签
      },
      lableData: [],
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center' },
        { title: '网点名称', dataIndex: 'stationName', width: 200, align: 'center' },
        { title: '网点标签', dataIndex: 'labelName', width: 200, align: 'center', customRender: text => { return text || '--' } },
        { title: '地址', dataIndex: 'stationAddress', width: 200, align: 'center' },
        { title: '设备编号', dataIndex: 'srcDeviceCode', width: 200, align: 'center' },
        { title: '箱体类型', dataIndex: 'deviceTypeName', width: 200, align: 'center' },
        { title: '内置箱体数', dataIndex: 'boxNum', width: 200, align: 'center' },
        { title: '版本', dataIndex: 'boxVersion', width: 200, align: 'center' },
        { title: '设备状态', scopedSlots: { customRender: 'onlineFlag' }, width: 150, align: 'center' },
        { title: '工作状态', scopedSlots: { customRender: 'state' }, width: 150, align: 'center' },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: 200, align: 'center' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return deviceList(Object.assign(parameter, this.queryParam, { deviceType: 'BOX' })).then(res => {
          if (res.status == 200) {
            const no = (res.data.pageNo - 1) * res.data.pageSize
            this.orderTotal = res.data.count
            for (let i = 0; i < res.data.list.length; i++) {
              const _item = res.data.list[i]
              _item.no = no + i + 1
            }
            return res.data
          }
        })
      },
      orderTotal: '', // 合计开单数量
      openLogModal: false, //  掉线日志 弹框
      srcDeviceCode: null, //  设备编号
      onlineFlagList: [ //  设备状态 下拉数据
        { name: '在线', value: '1' },
        { name: '离线', value: '0' }
      ],
      deviceTypeList: [] //  箱体类型  下拉数据
    }
  },
  methods: {
    filterOption (input, option) {
      return (
        option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      )
    },
    // 获取标签数据
    getlableSeleteList () {
      lableSeleteList().then(res => {
        if (res.status == 200) {
          this.lableData = res.data || []
        }
      })
    },
    // 重置
    handleReset () {
      this.queryParam.srcDeviceCode = ''
      this.queryParam.stationName = ''
      this.queryParam.deviceTypeNo = undefined
      this.queryParam.boxNum = undefined
      this.queryParam.onlineFlag = undefined
      this.queryParam.state = undefined
      this.queryParam.labelNoList = []
      this.$refs.table.refresh(true)
    },
    // 导出
    handleExport () {
		  const params = Object.assign(this.queryParam, { deviceType: 'BOX' })
		  this.loading = true
		  exportDevice(params).then(res => {
		    this.loading = false
		    this.download(res)
		  })
    },
    download (data) {
		  if (!data) { return }
		  const url = window.URL.createObjectURL(new Blob([data]))
		  const link = document.createElement('a')
		  link.style.display = 'none'
		  link.href = url
		  const a = moment().format('YYYYMMDDhhmmss')
		  const fname = '回收箱体明细-' + a
		  link.setAttribute('download', fname + '.xlsx')
		  document.body.appendChild(link)
		  link.click()
    },
    //  查看掉线日志
    checkLog (row) {
      this.srcDeviceCode = row ? row.srcDeviceCode : null
      this.openLogModal = true
    },
    // 查看二维码
    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 = 'id=' + item.srcDeviceCode + '&type=dustbin'
      _this.shName = (item.stationName?item.stationName:"")+item.srcDeviceCode.substring(item.srcDeviceCode.length-6)
      if (url) {
        QRCode.toDataURL(url, opts, function (err, url) {
          _this.qrCode = url
          _this.isQrCodeModal = true
        })
      }
    },
    //  下载二维码
    downloadCode (item) {
      const link = document.createElement('a')
      link.href = this.qrCode
      link.download = this.shName + '.jpg'
      link.click()
    },
    closeModal () {
      this.srcDeviceCode = null
      this.openLogModal = false
    },
    //  获取箱体类型下拉数据
    getDeviceType () {
      deviceTypeListQuery({}).then(res => {
        if (res.status == 200) {
          this.deviceTypeList = res.data
        } else {
          this.deviceTypeList = []
        }
      })
    }
  },
  mounted () {
    this.getDeviceType()
    this.getlableSeleteList()
  }
}
</script>
<style lang="less" scoped>
  .equipment-table-page-search-wrapper{
    .equipment-searchForm{
      .ant-form-inline .ant-form-item{
        width: 100%;
      }
      // 搜索框的下间距
      .ant-form-item {
      	margin-bottom: 10px;
      }
      .handle-btn{
        margin-top: 4px;
      }
      .serach-btn{
        margin-right: 10px;
      }
			.export-btn{
			  background-color: #ff9900;
			  border-color: #ff9900;
			  color: #fff;
			  margin-left: 10px;
			}
			.export-btn.ant-btn:focus:not(.ant-btn-primary):not(.ant-btn-danger), .export-btn.ant-btn:hover:not(.ant-btn-primary):not(.ant-btn-danger){
			  color: #fff;
			  border-color: #ff9900;
			}
    }
    .actionBtn {
    	font-size: 20px;
    	padding: 0 10px;
    }
    // 合计
    .total {
    	margin: 15px 0 25px;
    	width: 100%;
    	background-color: #e6f7ff;
    	border: 1px solid #91d5ff;
    	padding: 8px 15px 8px 27px;
    	border-radius: 4px;
    	.iconImg {
    		color: #1890FF;
    		margin-right: 10px;
    	}
    }
    .blue {
    	color: #1890FF;
    }
    .green {
    	color: #16b50e;
    }
    .red {
    	color: red;
    }
  }
  // 查看小程序码  弹框
  	.qrCodeModal {
  		.qrCode {
  			display: block;
  			max-width: 100%;
  			margin: 0 auto;
  		}
  		.downQrCode {
  			display: block;
  			margin: 40px auto 30px;
  		}
  	}
</style>
