<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-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="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'
import { deviceTypeListQuery } from '@/api/boxSetting'
export default {
  name: 'Equipment',
  components: { STable, VSelect, equipmentLogModal },
  data () {
    return {
      formItemLayout: {
        labelCol: { span: 7 },
        wrapperCol: { span: 17 }
      },
      // 查询参数
      queryParam: {
        srcDeviceCode: '', // 设备编号
        stationName: '', //  网点名称
        deviceTypeNo: undefined, //  箱体类型
        boxNum: undefined, //  内置箱体数
        onlineFlag: undefined, //  设备状态
        state: undefined //  工作状态
      },
      // 表头
      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: '版本', 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, //  设备编号
      onlineFlagList: [ //  设备状态 下拉数据
        { name: '在线', value: '1' },
        { name: '离线', value: '0' }
      ],
      deviceTypeList: [] //  箱体类型  下拉数据
    }
  },
  methods: {
    // 重置
    handleReset () {
      this.queryParam.srcDeviceCode = ''
      this.queryParam.stationName = ''
      this.queryParam.deviceTypeNo = undefined
      this.queryParam.boxNum = undefined
      this.queryParam.onlineFlag = undefined
      this.queryParam.state = undefined
      this.$refs.table.refresh(true)
    },
    //  查看掉线日志
    checkLog (row) {
      this.srcDeviceCode = row ? row.srcDeviceCode : null
      this.openLogModal = true
    },
    closeModal () {
      this.srcDeviceCode = null
      this.openLogModal = false
    },
    //  获取箱体类型下拉数据
    getDeviceType () {
      deviceTypeListQuery({}).then(res => {
        if (res.status == 200) {
          this.deviceTypeList = res.data
        } else {
          this.deviceTypeList = []
        }
      })
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      vm.getDeviceType()
    })
  }
}
</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>
