<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper" style="background: #f0f2f5; padding: 16px; margin-bottom: 16px; border-radius: 4px;">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="6" :sm="24">
            <a-form-item label="查询类型">
              <a-select v-model="queryType" placeholder="请选择查询类型" @change="handleQueryTypeChange">
                <a-select-option value="online">在线数据</a-select-option>
                <a-select-option value="history">历史数据</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <template v-if="queryType === 'history'">
            <a-col :md="6" :sm="24">
              <a-form-item label="开始时间">
                <a-date-picker v-model="startTime" placeholder="请选择开始时间" show-time valueFormat="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="结束时间">
                <a-date-picker v-model="endTime" placeholder="请选择结束时间" show-time valueFormat="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="6" :sm="24">
            <div style="margin-top: 4px;">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button v-if="queryType === 'history'" type="primary" @click="switchToOnline" style="margin-left: 8px">返回在线数据</a-button>
              <a-button v-else type="primary" @click="switchToHistory" style="margin-left: 8px">查询历史数据</a-button>
            </div>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('bu_zy_jcw_online')">导出</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <!-- 高级查询区域 -->
      <j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;" v-if="selectedRowKeys.length > 0">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text,record">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" :preview="record.id" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>

          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>

    <bu-zy-jcw-online-modal ref="modalForm" @ok="modalFormOk" :queryType="queryType" :queryMonth="queryMonth"></bu-zy-jcw-online-modal>
  </a-card>
</template>

<script>

import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import BuZyJcwOnlineModal from './modules/BuZyJcwOnlineModal'

export default {
  name: 'BuZyJcwOnlineList',
  mixins:[JeecgListMixin, mixinDevice],
  components: {
    BuZyJcwOnlineModal
  },
  data () {
    return {
      description: 'bu_zy_jcw_online管理页面',
      // 查询类型: online/history
      queryType: 'online',
      // 历史数据查询开始时间
      startTime: undefined,
      // 历史数据查询结束时间
      endTime: undefined,
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key:'rowIndex',
          width:60,
          align:"center",
          customRender:function (t,r,index) {
            return parseInt(index)+1;
          }
        },
        {
          title:'上传服务器名',
          align:"center",
          dataIndex: 'serverName'
        },
        {
          title:'传感器',
          align:"center",
          dataIndex: 'sensorId_dictText'
        },
        {
          title:'状态 (0正常 1预警 2报警)',
          align:"center",
          dataIndex: 'status'
        },
        {
          title:'报警字段名 逗号分隔',
          align:"center",
          dataIndex: 'statusname'
        },
        {
          title:'消息类型',
          align:"center",
          dataIndex: 'dataType'
        },
        {
          title:'消息ID',
          align:"center",
          dataIndex: 'dataId'
        },
        {
          title:'通信协议类型',
          align:"center",
          dataIndex: 'protocolType'
        },
        {
          title:'通信协议版本号',
          align:"center",
          dataIndex: 'protocolVersion'
        },
        {
          title:'城市ID',
          align:"center",
          dataIndex: 'cityId'
        },
        {
          title:'线路ID',
          align:"center",
          dataIndex: 'lineId'
        },
        {
          title:'设备内部ID',
          align:"center",
          dataIndex: 'inId'
        },
        {
          title:'子设备ID',
          align:"center",
          dataIndex: 'subId'
        },
        {
          title:'上传时间',
          align:"center",
          dataIndex: 'upTime',
          customRender:function (text) {
            return !text?"":(text.length>10?text.substr(0,10):text)
          }
        },
        {
          title:'一位端前牵',
          align:"center",
          dataIndex: 'data1',
          customRender: function(text) {
            return text ? '是' : '否';
          }
        },
        {
          title:'一位端后退',
          align:"center",
          dataIndex: 'data2',
          customRender: function(text) {
            return text ? '是' : '否';
          }
        },
        {
          title:'二位端前牵',
          align:"center",
          dataIndex: 'data3',
          customRender: function(text) {
            return text ? '是' : '否';
          }
        },
        {
          title:'二位端后退',
          align:"center",
          dataIndex: 'data4',
          customRender: function(text) {
            return text ? '是' : '否';
          }
        },
        {
          title:'机车空档位信号',
          align:"center",
          dataIndex: 'data5',
          customRender: function(text) {
            return text ? '是' : '否';
          }
        },
        {
          title:'机车自动一档信号',
          align:"center",
          dataIndex: 'data6',
          customRender: function(text) {
            return text ? '是' : '否';
          }
        },
        {
          title:'机车自动二档信号',
          align:"center",
          dataIndex: 'data7',
          customRender: function(text) {
            return text ? '是' : '否';
          }
        },
        {
          title:'机车自动三档信号',
          align:"center",
          dataIndex: 'data8',
          customRender: function(text) {
            return text ? '是' : '否';
          }
        },
        {
          title:'机车手动一档信号',
          align:"center",
          dataIndex: 'data9',
          customRender: function(text) {
            return text ? '是' : '否';
          }
        },
        {
          title:'机车手动二档信号',
          align:"center",
          dataIndex: 'data10',
          customRender: function(text) {
            return text ? '是' : '否';
          }
        },
        {
          title:'机车手动三档信号',
          align:"center",
          dataIndex: 'data11',
          customRender: function(text) {
            return text ? '是' : '否';
          }
        },
        {
          title:'机车速度报警信号',
          align:"center",
          dataIndex: 'data12',
          customRender: function(text) {
            return text ? '是' : '否';
          }
        },
        {
          title:'紧急制动缓解开关信号',
          align:"center",
          dataIndex: 'data13',
          customRender: function(text) {
            return text ? '是' : '否';
          }
        },
        {
          title:'停车制动信号',
          align:"center",
          dataIndex: 'data14',
          customRender: function(text) {
            return text ? '是' : '否';
          }
        },
        {
          title:'蓄电池电压',
          align:"center",
          dataIndex: 'rdata1'
        },
        {
          title:'蓄电池电流',
          align:"center",
          dataIndex: 'rdata2'
        },
        {
          title:'机车速度',
          align:"center",
          dataIndex: 'rdata3'
        },
        {
          title:'发动机转速',
          align:"center",
          dataIndex: 'rdata4'
        },
        {
          title:'发动机机油压力信号',
          align:"center",
          dataIndex: 'rdata5'
        },
        {
          title:'发动机水温信号',
          align:"center",
          dataIndex: 'rdata6'
        },
        {
          title:'传动箱转速信号',
          align:"center",
          dataIndex: 'rdata7'
        },
        {
          title:'变扭器传动油压力信号',
          align:"center",
          dataIndex: 'rdata8'
        },
        {
          title:'变扭器传动油温度信号',
          align:"center",
          dataIndex: 'rdata9'
        },
        {
          title:'柴油油位信号',
          align:"center",
          dataIndex: 'rdata10'
        },
        {
          title:'公里数信息',
          align:"center",
          dataIndex: 'rdata11'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align:"center",
          fixed:"right",
          width:147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: "/bu_zy_jcw/buZyJcwOnline/list",
        historyList: "/bu_zy_jcw/buZyJcwOnline/historyListByTimeRange",
        delete: "/bu_zy_jcw/buZyJcwOnline/delete",
        deleteBatch: "/bu_zy_jcw/buZyJcwOnline/deleteBatch",
        exportXlsUrl: "/bu_zy_jcw/buZyJcwOnline/exportXls",
        importExcelUrl: "bu_zy_jcw/buZyJcwOnline/importExcel",

      },
      dictOptions:{},
      superFieldList:[],
    }
  },
  created() {
    this.getSuperFieldList();
  },
  computed: {
    importExcelUrl: function(){
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
    },
  },
  methods: {
    initDictConfig(){
    },
    getSuperFieldList(){
      let fieldList=[];
      fieldList.push({type:'string',value:'serverName',text:'上传服务器名'})
      fieldList.push({type:'string',value:'sensorId',text:'传感器ID'})
      fieldList.push({type:'int',value:'status',text:'状态 (0正常 1预警 2报警)'})
      fieldList.push({type:'string',value:'statusname',text:'报警字段名 逗号分隔'})
      fieldList.push({type:'int',value:'dataType',text:'消息类型'})
      fieldList.push({type:'int',value:'dataId',text:'消息ID'})
      fieldList.push({type:'int',value:'protocolType',text:'通信协议类型'})
      fieldList.push({type:'int',value:'protocolVersion',text:'通信协议版本号'})
      fieldList.push({type:'int',value:'cityId',text:'城市ID'})
      fieldList.push({type:'int',value:'lineId',text:'线路ID'})
      fieldList.push({type:'int',value:'inId',text:'设备内部ID'})
      fieldList.push({type:'int',value:'subId',text:'子设备ID'})
      fieldList.push({type:'date',value:'upTime',text:'上传时间'})
      fieldList.push({type:'boolean',value:'data1',text:'一位端前牵'})
      fieldList.push({type:'boolean',value:'data2',text:'一位端后退'})
      fieldList.push({type:'boolean',value:'data3',text:'二位端前牵'})
      fieldList.push({type:'boolean',value:'data4',text:'二位端后退'})
      fieldList.push({type:'boolean',value:'data5',text:'机车空档位信号'})
      fieldList.push({type:'boolean',value:'data6',text:'机车自动一档信号'})
      fieldList.push({type:'boolean',value:'data7',text:'机车自动二档信号'})
      fieldList.push({type:'boolean',value:'data8',text:'机车自动三档信号'})
      fieldList.push({type:'boolean',value:'data9',text:'机车手动一档信号'})
      fieldList.push({type:'boolean',value:'data10',text:'机车手动二档信号'})
      fieldList.push({type:'boolean',value:'data11',text:'机车手动三档信号'})
      fieldList.push({type:'boolean',value:'data12',text:'机车速度报警信号'})
      fieldList.push({type:'boolean',value:'data13',text:'紧急制动缓解开关信号'})
      fieldList.push({type:'boolean',value:'data14',text:'停车制动信号'})
      fieldList.push({type:'number',value:'rdata1',text:'蓄电池电压'})
      fieldList.push({type:'number',value:'rdata2',text:'蓄电池电流'})
      fieldList.push({type:'number',value:'rdata3',text:'机车速度'})
      fieldList.push({type:'number',value:'rdata4',text:'发动机转速'})
      fieldList.push({type:'number',value:'rdata5',text:'发动机机油压力信号'})
      fieldList.push({type:'number',value:'rdata6',text:'发动机水温信号'})
      fieldList.push({type:'number',value:'rdata7',text:'传动箱转速信号'})
      fieldList.push({type:'number',value:'rdata8',text:'变扭器传动油压力信号'})
      fieldList.push({type:'number',value:'rdata9',text:'变扭器传动油温度信号'})
      fieldList.push({type:'number',value:'rdata10',text:'柴油油位信号'})
      fieldList.push({type:'number',value:'rdata11',text:'公里数信息'})
      this.superFieldList = fieldList
    },
    // 查询类型变更
    handleQueryTypeChange(value) {
      if (value === 'online') {
        // 切换到在线数据查询
        this.loadData(1)
      }
    },
    // 切换到历史数据查询
    switchToHistory() {
      this.queryType = 'history'
    },
    // 切换到在线数据查询
    switchToOnline() {
      this.queryType = 'online'
      // 清空历史查询条件
      this.startTime = undefined
      this.endTime = undefined
      this.loadData(1)
    },
    // 重写加载数据方法
    loadData(arg) {
      if (!this.url.list) {
        this.$message.error('请设置url.list属性!')
        return
      }
      // 加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1
      }
      let params = this.getQueryParams()// 查询条件
      if (this.queryType === 'history') {
        // 历史数据查询
        if (!this.startTime || !this.endTime) {
          this.$message.warning('请选择完整的起止时间')
          return
        }
        
        if (this.startTime >= this.endTime) {
          this.$message.warning('开始时间必须小于结束时间')
          return
        }
        
        params.startTime = this.startTime
        params.endTime = this.endTime
        
        this.loading = true
        this.$http({
          url: this.url.historyList,
          method: 'get',
          params: params
        }).then((res) => {
          if (res.success) {
            this.dataSource = res.result.records
            this.ipagination.total = res.result.total
          } else {
            // 处理后端返回的错误信息
            this.$message.error(res.message)
            // 清空表格数据
            this.dataSource = []
            this.ipagination.total = 0
          }
          this.loading = false
        })
      } else {
        // 在线数据查询
        this.loading = true
        this.$http({
          url: this.url.list,
          method: 'get',
          params: params
        }).then((res) => {
          if (res.success) {
            this.dataSource = res.result.records
            this.ipagination.total = res.result.total
          } else {
            // 处理后端返回的错误信息
            this.$message.error(res.message)
            // 清空表格数据
            this.dataSource = []
            this.ipagination.total = 0
          }
          this.loading = false
        })
      }
    },
    handleEdit(record) {
      // 将查询类型和月份信息传递给编辑模态框
      this.$refs.modalForm.edit(record, this.queryType, this.queryMonth);
    },
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>