<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="30" 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-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-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="boxOnePercent" slot-scope="text, record">{{ record.boxOnePercent ? record.boxOnePercent + '%' : '--' }}</template>
      <template slot="boxTwoPercent" slot-scope="text, record">{{ record.boxTwoPercent ? record.boxTwoPercent + '%' : '--' }}</template>
      <template slot="boxThreePercent" slot-scope="text, record">{{ record.boxThreePercent ? record.boxThreePercent + '%' : '--' }}</template>
      <template slot="boxFourPercent" slot-scope="text, record">{{ record.boxFourPercent ? record.boxFourPercent + '%' : '--' }}</template>
      <template slot="boxFivePercent" slot-scope="text, record">{{ record.boxFivePercent ? record.boxFivePercent + '%' : '--' }}</template>
      <!-- 设备状态 -->
      <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-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-card>
</template>

<script>
import { STable, VSelect } from '@/components'
import equipmentLogModal from './logModal.vue'
import { deviceList } from '@/api/device'
export default {
  name: 'Equipment',
  components: { STable, VSelect, equipmentLogModal },
  data () {
    return {
      formItemLayout: {
        labelCol: { span: 7 },
        wrapperCol: { span: 17 }
      },
      // 查询参数
      queryParam: {
        srcDeviceCode: '', // 设备编号
        stationName: '' //  网点名称
      },
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center' },
        { title: '网点名称', dataIndex: 'stationName', width: 200, align: 'center' },
        { 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: '内置各箱体已投放比例（%）',
          children: [
            { title: '1号箱', scopedSlots: { customRender: 'boxOnePercent' }, width: 100, align: 'center' },
            { title: '2号箱', scopedSlots: { customRender: 'boxTwoPercent' }, width: 100, align: 'center' },
            { title: '3号箱', scopedSlots: { customRender: 'boxThreePercent' }, width: 100, align: 'center' },
            { title: '4号箱', scopedSlots: { customRender: 'boxFourPercent' }, width: 100, align: 'center' },
            { title: '5号箱', scopedSlots: { customRender: 'boxFivePercent' }, width: 100, 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)).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 //  设备编号
    }
  },
  methods: {
    // 重置
    handleReset () {
      this.queryParam.srcDeviceCode = ''
      this.queryParam.stationName = ''
      this.$refs.table.refresh(true)
    },
    //  查看掉线日志
    checkLog (row) {
      this.srcDeviceCode = row ? row.srcDeviceCode : null
      this.openLogModal = true
    },
    closeModal () {
      this.srcDeviceCode = null
      this.openLogModal = false
    }
  }
}
</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;
      }
    }
    .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;
    }
  }
</style>
