<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_exp_data_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-exp-data-online-modal ref="modalForm" @ok="modalFormOk" :queryType="queryType" :queryMonth="queryMonth"></bu-zy-exp-data-online-modal>
  </a-card>
</template>

<script>

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

  export default {
    name: 'BuZyExpDataOnlineList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      BuZyExpDataOnlineModal
    },
    data () {
      return {
        description: 'bu_zy_exp_data_online管理页面',
        // 查询类型: online/history
        queryType: 'online',
        // 历史数据查询月份
        queryMonth: undefined,
        // 历史数据查询开始时间
        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: 'createTime',
            customRender: function (text) {
              if (!text) {
                return '';
              }
              // 格式化日期时间显示
              return text.length > 10 ? text : text + ' 00:00:00';
            }
          },
          {
            title:'协议版本',
            align:"center",
            dataIndex: 'data01'
          },
          {
            title:'备注符号',
            align:"center",
            dataIndex: 'data02'
          },
          {
            title:'ANSII/binär',
            align:"center",
            dataIndex: 'data03'
          },
          {
            title:'数据隔离符号',
            align:"center",
            dataIndex: 'data05'
          },
          {
            title:'部件数据隔离符号',
            align:"center",
            dataIndex: 'data06'
          },
          {
            title:'机床类型',
            align:"center",
            dataIndex: 'data12'
          },
          {
            title:'机床号码',
            align:"center",
            dataIndex: 'data13'
          },
          {
            title:'当前日期',
            align:"center",
            dataIndex: 'data19'
          },
          {
            title:'当前时间',
            align:"center",
            dataIndex: 'data110'
          },
          {
            title:'用户名称',
            align:"center",
            dataIndex: 'data21'
          },
          {
            title:'操作者姓名',
            align:"center",
            dataIndex: 'data24'
          },
          {
            title:'当前轮对识别号',
            align:"center",
            dataIndex: 'data311'
          },
          {
            title:'车辆编码',
            align:"center",
            dataIndex: 'data313'
          },
          {
            title:'加工原因',
            align:"center",
            dataIndex: 'data314n'
          },
          {
            title:'车辆方向',
            align:"center",
            dataIndex: 'data34'
          },
          {
            title:'当前车辆车轴配置',
            align:"center",
            dataIndex: 'data35'
          },
          {
            title:'车辆号码',
            align:"center",
            dataIndex: 'data38'
          },
          {
            title:'当前轮对号/车轴号码',
            align:"center",
            dataIndex: 'data39'
          },
          {
            title:'轮廓号码',
            align:"center",
            dataIndex: 'data43sn'
          },
          {
            title:'轮廓变量',
            align:"center",
            dataIndex: 'data44sn'
          },
          {
            title:'公里数',
            align:"center",
            dataIndex: 'data55'
          },
          {
            title:'内侧距',
            align:"center",
            dataIndex: 'data58n'
          },
          {
            title:'预测量左侧径向跳动',
            align:"center",
            dataIndex: 'data611sn'
          },
          {
            title:'预测量右侧径向跳动',
            align:"center",
            dataIndex: 'data612sn'
          },
          {
            title:'data661',
            align:"center",
            dataIndex: 'data661'
          },
          {
            title:'data662',
            align:"center",
            dataIndex: 'data662'
          },
          {
            title:'data68',
            align:"center",
            dataIndex: 'data68'
          },
          {
            title:'后测量左侧径向跳动',
            align:"center",
            dataIndex: 'data6111sn'
          },
          {
            title:'后测量右侧径向跳动',
            align:"center",
            dataIndex: 'data6112sn'
          },
          {
            title:'后测量左侧轴向跳动',
            align:"center",
            dataIndex: 'data6121sn'
          },
          {
            title:'后测量右侧轴向跳动',
            align:"center",
            dataIndex: 'data6122sn'
          },
          {
            title:'后测量左侧直径',
            align:"center",
            dataIndex: 'data6131sn'
          },
          {
            title:'后测量右侧直径',
            align:"center",
            dataIndex: 'data6132sn'
          },
          {
            title:'预测量左侧轴向跳动',
            align:"center",
            dataIndex: 'data621sn'
          },
          {
            title:'预测量右侧轴向跳动',
            align:"center",
            dataIndex: 'data622sn'
          },
          {
            title:'data731',
            align:"center",
            dataIndex: 'data731'
          },
          {
            title:'data732',
            align:"center",
            dataIndex: 'data732'
          },
          {
            title:'data741',
            align:"center",
            dataIndex: 'data741'
          },
          {
            title:'data742',
            align:"center",
            dataIndex: 'data742'
          },
          {
            title:'后测量左侧轮缘厚度',
            align:"center",
            dataIndex: 'data7121sn'
          },
          {
            title:'后测量右侧轮缘厚度',
            align:"center",
            dataIndex: 'data7122sn'
          },
          {
            title:'后测量左侧轮缘高度',
            align:"center",
            dataIndex: 'data7131sn'
          },
          {
            title:'后测量右侧轮缘高度',
            align:"center",
            dataIndex: 'data7132sn'
          },
          {
            title:'左侧名义轮缘厚度',
            align:"center",
            dataIndex: 'data7211sn'
          },
          {
            title:'右侧名义轮缘厚度',
            align:"center",
            dataIndex: 'data7212sn'
          },
          {
            title:'左侧轮廓测量点坐标',
            align:"center",
            dataIndex: 'data7221sn'
          },
          {
            title:'右侧轮廓测量点坐标',
            align:"center",
            dataIndex: 'data7222sn'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/ZyExpDataOnline/buZyExpDataOnline/list",
          historyList: "/ZyExpDataOnline/buZyExpDataOnline/historyListByTimeRange",
          delete: "/ZyExpDataOnline/buZyExpDataOnline/delete",
          deleteBatch: "/ZyExpDataOnline/buZyExpDataOnline/deleteBatch",
          exportXlsUrl: "/ZyExpDataOnline/buZyExpDataOnline/exportXls",
          importExcelUrl: "ZyExpDataOnline/buZyExpDataOnline/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:'string',value:'createTime',text:'采集时间'})
        fieldList.push({type:'string',value:'data01',text:'协议版本 protocol revision 协议版本'})
        fieldList.push({type:'string',value:'data02',text:'备注符号 sign of comments 备注符号'})
        fieldList.push({type:'string',value:'data03',text:'ANSII / binär'})
        fieldList.push({type:'string',value:'data05',text:'数据隔离符号 data set seperator 数据隔离符号'})
        fieldList.push({type:'string',value:'data06',text:'部件数据隔离符号 part data seperator 部件数据隔离符号'})
        fieldList.push({type:'string',value:'data12',text:'机床类型 machine typ 机床类型'})
        fieldList.push({type:'string',value:'data13',text:'机床号码 machine number 机床号码'})
        fieldList.push({type:'string',value:'data19',text:'当前日期 current date 当前日期'})
        fieldList.push({type:'string',value:'data110',text:'当前时间 current time 当前时间'})
        fieldList.push({type:'string',value:'data21',text:'用户名称 customer name 用户名称'})
        fieldList.push({type:'string',value:'data24',text:'操作者姓名 operator name 操作者姓名'})
        fieldList.push({type:'string',value:'data311',text:'当前轮对识别号 current wheel set identification 当前轮对识别号'})
        fieldList.push({type:'string',value:'data313',text:'车辆编码 vehicle number 车辆编码'})
        fieldList.push({type:'string',value:'data314n',text:'加工原因 reason of maching 加工原因'})
        fieldList.push({type:'string',value:'data34',text:'车辆方向 direction of vehicle (0-1)车辆方向'})
        fieldList.push({type:'string',value:'data35',text:'当前车辆车轴配置 current vehicle axis configuration 当前车辆车轴配置'})
        fieldList.push({type:'string',value:'data38',text:'车辆号码 vehicle number 车辆号码'})
        fieldList.push({type:'string',value:'data39',text:'当前轮对号 current wheel set number // axis number 当前轮对号//车轴号码'})
        fieldList.push({type:'string',value:'data43sn',text:'轮廓号码 profile number 轮廓号码'})
        fieldList.push({type:'string',value:'data44sn',text:'轮廓变量 profile variances 轮廓变量'})
        fieldList.push({type:'string',value:'data55',text:'公里数 Milages 公里数'})
        fieldList.push({type:'string',value:'data58n',text:'内侧距 back to back dimension 内侧距'})
        fieldList.push({type:'string',value:'data611sn',text:'预测量左侧径向跳动 radial run out left pre measuring 预测量左侧径向跳动'})
        fieldList.push({type:'string',value:'data612sn',text:'预测量右侧径向跳动 radial run out right pre measuring 预测量右侧径向跳动'})
        fieldList.push({type:'string',value:'data661',text:'data661'})
        fieldList.push({type:'string',value:'data662',text:'data662'})
        fieldList.push({type:'string',value:'data68',text:'data68'})
        fieldList.push({type:'string',value:'data6111SN',text:'后测量左侧径向跳动 radial run out left post measuring 后测量左侧径向跳动'})
        fieldList.push({type:'string',value:'data6112SN',text:'后测量右侧径向跳动 radial run out right post measuring 后测量右侧径向跳动'})
        fieldList.push({type:'string',value:'data6121SN',text:'后测量左侧轴向跳动 axial run out left post measuring 后测量左侧轴向跳动'})
        fieldList.push({type:'string',value:'data6122SN',text:'后测量右侧轴向跳动 axial run out right post measuring 后测量右侧轴向跳动'})
        fieldList.push({type:'string',value:'data6131SN',text:'后测量左侧直径 diameter post measuring left 后测量左侧直径'})
        fieldList.push({type:'string',value:'data6132SN',text:'后测量右侧直径 diameter post measuring right 后测量右侧直径'})
        fieldList.push({type:'string',value:'data621SN',text:'预测量左侧轴向跳动 axial run out left pre measuring 预测量左侧轴向跳动'})
        fieldList.push({type:'string',value:'data622SN',text:'预测量右侧轴向跳动 axial run out right pre measuring 预测量右侧轴向跳动'})
        fieldList.push({type:'string',value:'data731',text:'data731'})
        fieldList.push({type:'string',value:'data732',text:'data732'})
        fieldList.push({type:'string',value:'data741',text:'data741'})
        fieldList.push({type:'string',value:'data742',text:'data742'})
        fieldList.push({type:'string',value:'data7121SN',text:'后测量左侧轮缘厚度 flange thickness left post measuring 后测量左侧轮缘厚度'})
        fieldList.push({type:'string',value:'data7122SN',text:'后测量右侧轮缘厚度 flange thickness right post measuring 后测量右侧轮缘厚度'})
        fieldList.push({type:'string',value:'data7131SN',text:'后测量左侧轮缘高度 flange hight left post measuring 后测量左侧轮缘高度'})
        fieldList.push({type:'string',value:'data7132SN',text:'后测量右侧轮缘高度 flange hight right post measuring 后测量右侧轮缘高度'})
        fieldList.push({type:'string',value:'data7211SN',text:'左侧名义轮缘厚度 flang thickness nominal left 左侧名义轮缘厚度'})
        fieldList.push({type:'string',value:'data7212SN',text:'右侧名义轮缘厚度 flange thickness nominal right 右侧名义轮缘厚度'})
        fieldList.push({type:'string',value:'data7221SN',text:'左侧轮廓测量点坐标 X datas profile measuring points left 左侧轮廓测量点坐标'})
        fieldList.push({type:'string',value:'data7222SN',text:'右侧轮廓测量点坐标 X datas profile measuring points right 右侧轮廓测量点坐标'})
        this.superFieldList = fieldList
      },
      // 查询类型变更
      handleQueryTypeChange(value) {
        if (value === 'online') {
          // 切换到在线数据查询
          this.loadData(1)
        }
      },
      // 切换到历史数据查询
      switchToHistory() {
        this.queryType = 'history'
      },
      // 切换到在线数据查询
      switchToOnline() {
        this.queryType = 'online'
        this.queryMonth = undefined
        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>