<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_xdcgcc_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;">
        <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-xdcgcc-online-modal ref="modalForm" @ok="modalFormOk"></bu-zy-xdcgcc-online-modal>
  </a-card>
</template>

<script>

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

  export default {
    name: 'BuZyXdcgccOnlineList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      BuZyXdcgccOnlineModal
    },
    data () {
      return {
        description: 'bu_zy_xdcgcc_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:'传感器ID',
            align:"center",
            dataIndex: 'sensorId'
          },
          {
            title:'状态',
            align:"center",
            dataIndex: 'status'
          },
          {
            title:'报警字段名',
            align:"center",
            dataIndex: 'statusname'
          },
          {
            title:'采集时间',
            align:"center",
            dataIndex: 'acqTime'
          },
          {
            title:'设备编号',
            align:"center",
            dataIndex: 'emCode'
          },
          {
            title:'运行状态',
            align:"center",
            dataIndex: 'emRunState'
          },
          {
            title:'故障信息',
            align:"center",
            dataIndex: 'emErrorCode'
          },
          {
            title:'接触网电压',
            align:"center",
            dataIndex: 'catenaryvoltage'
          },
          {
            title:'牵引蓄电池电压',
            align:"center",
            dataIndex: 'tractionbatteryvoltage'
          },
          {
            title:'牵引力',
            align:"center",
            dataIndex: 'traction'
          },
          {
            title:'制动力',
            align:"center",
            dataIndex: 'brakingforce'
          },
          {
            title:'时速',
            align:"center",
            dataIndex: 'speedperhour'
          },
          {
            title:'均衡风缸风压',
            align:"center",
            dataIndex: 'equalizingreservoirairpressure'
          },
          {
            title:'里程数',
            align:"center",
            dataIndex: 'mileage'
          },
          {
            title:'转向架1中间直流—电压',
            align:"center",
            dataIndex: 'bogieintermediatevoltage1'
          },
          {
            title:'转向架1中间直流—电流',
            align:"center",
            dataIndex: 'bogieintermediatecurrent1'
          },
          {
            title:'转向架2中间直流—电压',
            align:"center",
            dataIndex: 'bogieintermediatevoltage2'
          },
          {
            title:'转向架2中间直流—电流',
            align:"center",
            dataIndex: 'bogieintermediatecurrent2'
          },
          {
            title:'牵引蓄电池充电机—电压',
            align:"center",
            dataIndex: 'tractionbatterymotorvoltage'
          },
          {
            title:'牵引蓄电池充电机—电流',
            align:"center",
            dataIndex: 'tractionbatterymotorcurrent'
          },
          {
            title:'牵引电机1温度',
            align:"center",
            dataIndex: 'temperatureoftractionmotor1'
          },
          {
            title:'牵引电机1转速',
            align:"center",
            dataIndex: 'speedoftractionmotor1'
          },
          {
            title:'牵引电机2温度',
            align:"center",
            dataIndex: 'temperatureoftractionmotor2'
          },
          {
            title:'牵引电机2转速',
            align:"center",
            dataIndex: 'speedoftractionmotor2'
          },
          {
            title:'牵引电机3温度',
            align:"center",
            dataIndex: 'temperatureoftractionmotor3'
          },
          {
            title:'牵引电机3转速',
            align:"center",
            dataIndex: 'speedoftractionmotor3'
          },
          {
            title:'牵引电机4温度',
            align:"center",
            dataIndex: 'temperatureoftractionmotor4'
          },
          {
            title:'牵引电机4转速',
            align:"center",
            dataIndex: 'speedoftractionmotor4'
          },
          {
            title:'牵引蓄电池温度',
            align:"center",
            dataIndex: 'tractionbatterytemperature'
          },
          {
            title:'转向架1实际力',
            align:"center",
            dataIndex: 'actualforceofbogie1'
          },
          {
            title:'转向架2实际力',
            align:"center",
            dataIndex: 'actualforceofbogie2'
          },
          {
            title:'充电状态',
            align:"center",
            dataIndex: 'chargestate'
          },
          {
            title:'蓄电池输入电压',
            align:"center",
            dataIndex: 'batteryinputvoltage'
          },
          {
            title:'蓄电池输出电压',
            align:"center",
            dataIndex: 'batteryoutputvoltage'
          },
          {
            title:'蓄电池输出总电流',
            align:"center",
            dataIndex: 'totaloutputcurrentofbattery'
          },
          {
            title:'蓄电池充电电流',
            align:"center",
            dataIndex: 'batterychargecurrent'
          },
          {
            title:'蓄电池温度1',
            align:"center",
            dataIndex: 'temperatureofbattery1'
          },
          {
            title:'蓄电池温度2',
            align:"center",
            dataIndex: 'temperatureofbattery2'
          },
          {
            title:'时间戳',
            align:"center",
            dataIndex: 'ts',
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/xudiancgcc/buZyXdcgccOnline/list",
          historyList: "/xudiancgcc/buZyXdcgccOnline/historyListByTimeRange",
          delete: "/xudiancgcc/buZyXdcgccOnline/delete",
          deleteBatch: "/xudiancgcc/buZyXdcgccOnline/deleteBatch",
          exportXlsUrl: "/xudiancgcc/buZyXdcgccOnline/exportXls",
          importExcelUrl: "xudiancgcc/buZyXdcgccOnline/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:'number',value:'status',text:'状态'})
        fieldList.push({type:'string',value:'statusname',text:'报警字段名'})
        fieldList.push({type:'string',value:'acqTime',text:'采集时间'})
        fieldList.push({type:'string',value:'emCode',text:'设备编号'})
        fieldList.push({type:'number',value:'emRunState',text:'运行状态'})
        fieldList.push({type:'string',value:'emErrorCode',text:'故障信息'})
        fieldList.push({type:'number',value:'catenaryvoltage',text:'接触网电压'})
        fieldList.push({type:'number',value:'tractionbatteryvoltage',text:'牵引蓄电池电压'})
        fieldList.push({type:'number',value:'traction',text:'牵引力'})
        fieldList.push({type:'number',value:'brakingforce',text:'制动力'})
        fieldList.push({type:'number',value:'speedperhour',text:'时速'})
        fieldList.push({type:'number',value:'equalizingreservoirairpressure',text:'均衡风缸风压'})
        fieldList.push({type:'number',value:'mileage',text:'里程数'})
        fieldList.push({type:'number',value:'bogieintermediatevoltage1',text:'转向架1中间直流—电压'})
        fieldList.push({type:'number',value:'bogieintermediatecurrent1',text:'转向架1中间直流—电流'})
        fieldList.push({type:'number',value:'bogieintermediatevoltage2',text:'转向架2中间直流—电压'})
        fieldList.push({type:'number',value:'bogieintermediatecurrent2',text:'转向架2中间直流—电流'})
        fieldList.push({type:'number',value:'tractionbatterymotorvoltage',text:'牵引蓄电池充电机—电压'})
        fieldList.push({type:'number',value:'tractionbatterymotorcurrent',text:'牵引蓄电池充电机—电流'})
        fieldList.push({type:'number',value:'temperatureoftractionmotor1',text:'牵引电机1温度'})
        fieldList.push({type:'number',value:'speedoftractionmotor1',text:'牵引电机1转速'})
        fieldList.push({type:'number',value:'temperatureoftractionmotor2',text:'牵引电机2温度'})
        fieldList.push({type:'number',value:'speedoftractionmotor2',text:'牵引电机2转速'})
        fieldList.push({type:'number',value:'temperatureoftractionmotor3',text:'牵引电机3温度'})
        fieldList.push({type:'number',value:'speedoftractionmotor3',text:'牵引电机3转速'})
        fieldList.push({type:'number',value:'temperatureoftractionmotor4',text:'牵引电机4温度'})
        fieldList.push({type:'number',value:'speedoftractionmotor4',text:'牵引电机4转速'})
        fieldList.push({type:'number',value:'tractionbatterytemperature',text:'牵引蓄电池温度'})
        fieldList.push({type:'number',value:'actualforceofbogie1',text:'转向架1实际力'})
        fieldList.push({type:'number',value:'actualforceofbogie2',text:'转向架2实际力'})
        fieldList.push({type:'number',value:'chargestate',text:'充电状态'})
        fieldList.push({type:'number',value:'batteryinputvoltage',text:'蓄电池输入电压'})
        fieldList.push({type:'number',value:'batteryoutputvoltage',text:'蓄电池输出电压'})
        fieldList.push({type:'number',value:'totaloutputcurrentofbattery',text:'蓄电池输出总电流'})
        fieldList.push({type:'number',value:'batterychargecurrent',text:'蓄电池充电电流'})
        fieldList.push({type:'number',value:'temperatureofbattery1',text:'蓄电池温度1'})
        fieldList.push({type:'number',value:'temperatureofbattery2',text:'蓄电池温度2'})
        fieldList.push({type:'date',value:'ts',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.startTime, this.endTime);
      },
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>