<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('ziyang_xcj_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>

    <ziyang-xcj-online-modal ref="modalForm" @ok="modalFormOk"></ziyang-xcj-online-modal>
  </a-card>
</template>

<script>

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

export default {
  name: 'ZiyangXcjOnlineList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    ZiyangXcjOnlineModal
  },
  data () {
    return {
      description: 'ziyang_xcj_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: '状态',
          align: 'center',
          dataIndex: 'status'
        },
        {
          title: '报警字段名',
          align: 'center',
          dataIndex: 'statusname'
        },
        {
          title: '操作模式（维护）',
          align: 'center',
          dataIndex: 'data1'
        },
        {
          title: '操作模式（手动）',
          align: 'center',
          dataIndex: 'data2'
        },
        {
          title: '操作模式（自动）',
          align: 'center',
          dataIndex: 'data3'
        },
        {
          title: '端洗选择',
          align: 'center',
          dataIndex: 'data4'
        },
        {
          title: '预冷状态',
          align: 'center',
          dataIndex: 'data5'
        },
        {
          title: '预湿状态',
          align: 'center',
          dataIndex: 'data6'
        },
        {
          title: '左冲洗上升限位一',
          align: 'center',
          dataIndex: 'data7'
        },
        {
          title: '左冲洗上升限位二',
          align: 'center',
          dataIndex: 'data8'
        },
        {
          title: '左冲洗下降限位一',
          align: 'center',
          dataIndex: 'data9'
        },
        {
          title: '左冲洗下降限位二',
          align: 'center',
          dataIndex: 'data10'
        },
        {
          title: '左冲洗前端限位一',
          align: 'center',
          dataIndex: 'data11'
        },
        {
          title: '左冲洗前端限位二',
          align: 'center',
          dataIndex: 'data12'
        },
        {
          title: '左冲洗后端限位一',
          align: 'center',
          dataIndex: 'data13'
        },
        {
          title: '左冲洗后端限位二',
          align: 'center',
          dataIndex: 'data14'
        },
        {
          title: '右冲洗上升限位一',
          align: 'center',
          dataIndex: 'data15'
        },
        {
          title: '右冲洗上升限位二',
          align: 'center',
          dataIndex: 'data16'
        },
        {
          title: '右冲洗下降限位一',
          align: 'center',
          dataIndex: 'data17'
        },
        {
          title: '右冲洗下降限位二',
          align: 'center',
          dataIndex: 'data18'
        },
        {
          title: '右冲洗前端限位一',
          align: 'center',
          dataIndex: 'data19'
        },
        {
          title: '右冲洗前端限位二',
          align: 'center',
          dataIndex: 'data20'
        },
        {
          title: '右冲洗后端限位一',
          align: 'center',
          dataIndex: 'data21'
        },
        {
          title: '右冲洗后端限位二',
          align: 'center',
          dataIndex: 'data22'
        },
        {
          title: '初刷工位弧刷状态（就位、工作）',
          align: 'center',
          dataIndex: 'data23'
        },
        {
          title: '左侧洗初后刷限位',
          align: 'center',
          dataIndex: 'data24'
        },
        {
          title: '左侧洗初前刷限位',
          align: 'center',
          dataIndex: 'data25'
        },
        {
          title: '初刷工位喷水状态',
          align: 'center',
          dataIndex: 'data26'
        },
        {
          title: '次刷工位弧刷工作状态（就位、工作）',
          align: 'center',
          dataIndex: 'data27'
        },
        {
          title: '左侧洗次后刷限位',
          align: 'center',
          dataIndex: 'data28'
        },
        {
          title: '左侧洗次前刷限位',
          align: 'center',
          dataIndex: 'data29'
        },
        {
          title: '次刷工位喷水状态',
          align: 'center',
          dataIndex: 'data30'
        },
        {
          title: '初冲状态',
          align: 'center',
          dataIndex: 'data31'
        },
        {
          title: '精刷工位弧刷工作状态（就位、工作）',
          align: 'center',
          dataIndex: 'data32'
        },
        {
          title: '左侧洗精后刷限位',
          align: 'center',
          dataIndex: 'data33'
        },
        {
          title: '左侧洗精前刷限位',
          align: 'center',
          dataIndex: 'data34'
        },
        {
          title: '精刷工位喷水状态',
          align: 'center',
          dataIndex: 'data35'
        },
        {
          title: '终冲状态',
          align: 'center',
          dataIndex: 'data36'
        },
        {
          title: '吹干状态',
          align: 'center',
          dataIndex: 'data37'
        },
        {
          title: '清洗完毕状态',
          align: 'center',
          dataIndex: 'data38'
        },
        {
          title: '同意洗车状态',
          align: 'center',
          dataIndex: 'data39'
        },
        {
          title: '同意出库状态',
          align: 'center',
          dataIndex: 'data40'
        },
        {
          title: '列车入库状态',
          align: 'center',
          dataIndex: 'data41'
        },
        {
          title: '列车出库状态',
          align: 'center',
          dataIndex: 'data42'
        },
        {
          title: '模拟洗车信号',
          align: 'center',
          dataIndex: 'data43'
        },
        {
          title: '清洗完毕信号',
          align: 'center',
          dataIndex: 'data44'
        },
        {
          title: '预湿泵故障',
          align: 'center',
          dataIndex: 'data45'
        },
        {
          title: '回水泵故障',
          align: 'center',
          dataIndex: 'data46'
        },
        {
          title: '清水泵故障',
          align: 'center',
          dataIndex: 'data47'
        },
        {
          title: '软水泵故障',
          align: 'center',
          dataIndex: 'data48'
        },
        {
          title: '计量泵1故障',
          align: 'center',
          dataIndex: 'data49'
        },
        {
          title: '计量泵2故障',
          align: 'center',
          dataIndex: 'data50'
        },
        {
          title: '侧刷电机1故障',
          align: 'center',
          dataIndex: 'data51'
        },
        {
          title: '侧刷电机2故障',
          align: 'center',
          dataIndex: 'data52'
        },
        {
          title: '侧刷电机3故障',
          align: 'center',
          dataIndex: 'data53'
        },
        {
          title: '侧刷电机4故障',
          align: 'center',
          dataIndex: 'data54'
        },
        {
          title: '侧刷电机5故障',
          align: 'center',
          dataIndex: 'data55'
        },
        {
          title: '侧刷电机6故障',
          align: 'center',
          dataIndex: 'data56'
        },
        {
          title: '侧刷电机7故障',
          align: 'center',
          dataIndex: 'data57'
        },
        {
          title: '侧刷电机8故障',
          align: 'center',
          dataIndex: 'data58'
        },
        {
          title: '侧刷电机9故障',
          align: 'center',
          dataIndex: 'data59'
        },
        {
          title: '运行状态（运行）',
          align: 'center',
          dataIndex: 'data60'
        },
        {
          title: '运行状态（故障）',
          align: 'center',
          dataIndex: 'data61'
        },
        {
          title: '预留1',
          align: 'center',
          dataIndex: 'data62'
        },
        {
          title: '预留2',
          align: 'center',
          dataIndex: 'data63'
        },
        {
          title: '预留3',
          align: 'center',
          dataIndex: 'data64'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/zyxcj/ziyangXcjOnline/list',
        historyList: '/zyxcj/ziyangXcjOnline/historyListByTimeRange',
        delete: '/zyxcj/ziyangXcjOnline/delete',
        deleteBatch: '/zyxcj/ziyangXcjOnline/deleteBatch',
        exportXlsUrl: '/zyxcj/ziyangXcjOnline/exportXls',
        importExcelUrl: 'zyxcj/ziyangXcjOnline/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: '状态' })
      fieldList.push({ type: 'string', value: 'statusname', text: '报警字段名' })
      fieldList.push({ type: 'int', value: 'data1', text: '操作模式（维护）' })
      fieldList.push({ type: 'int', value: 'data2', text: '操作模式（手动）' })
      fieldList.push({ type: 'int', value: 'data3', text: '操作模式（自动）' })
      fieldList.push({ type: 'int', value: 'data4', text: '端洗选择' })
      fieldList.push({ type: 'int', value: 'data5', text: '预冷状态' })
      fieldList.push({ type: 'int', value: 'data6', text: '预湿状态' })
      fieldList.push({ type: 'int', value: 'data7', text: '左冲洗上升限位一' })
      fieldList.push({ type: 'int', value: 'data8', text: '左冲洗上升限位二' })
      fieldList.push({ type: 'int', value: 'data9', text: '左冲洗下降限位一' })
      fieldList.push({ type: 'int', value: 'data10', text: '左冲洗下降限位二' })
      fieldList.push({ type: 'int', value: 'data11', text: '左冲洗前端限位一' })
      fieldList.push({ type: 'int', value: 'data12', text: '左冲洗前端限位二' })
      fieldList.push({ type: 'int', value: 'data13', text: '左冲洗后端限位一' })
      fieldList.push({ type: 'int', value: 'data14', text: '左冲洗后端限位二' })
      fieldList.push({ type: 'int', value: 'data15', text: '右冲洗上升限位一' })
      fieldList.push({ type: 'int', value: 'data16', text: '右冲洗上升限位二' })
      fieldList.push({ type: 'int', value: 'data17', text: '右冲洗下降限位一' })
      fieldList.push({ type: 'int', value: 'data18', text: '右冲洗下降限位二' })
      fieldList.push({ type: 'int', value: 'data19', text: '右冲洗前端限位一' })
      fieldList.push({ type: 'int', value: 'data20', text: '右冲洗前端限位二' })
      fieldList.push({ type: 'int', value: 'data21', text: '右冲洗后端限位一' })
      fieldList.push({ type: 'int', value: 'data22', text: '右冲洗后端限位二' })
      fieldList.push({ type: 'int', value: 'data23', text: '初刷工位弧刷状态（就位、工作）' })
      fieldList.push({ type: 'int', value: 'data24', text: '左侧洗初后刷限位' })
      fieldList.push({ type: 'int', value: 'data25', text: '左侧洗初前刷限位' })
      fieldList.push({ type: 'int', value: 'data26', text: '初刷工位喷水状态' })
      fieldList.push({ type: 'int', value: 'data27', text: '次刷工位弧刷工作状态（就位、工作）' })
      fieldList.push({ type: 'int', value: 'data28', text: '左侧洗次后刷限位' })
      fieldList.push({ type: 'int', value: 'data29', text: '左侧洗次前刷限位' })
      fieldList.push({ type: 'int', value: 'data30', text: '次刷工位喷水状态' })
      fieldList.push({ type: 'int', value: 'data31', text: '初冲状态' })
      fieldList.push({ type: 'int', value: 'data32', text: '精刷工位弧刷工作状态（就位、工作）' })
      fieldList.push({ type: 'int', value: 'data33', text: '左侧洗精后刷限位' })
      fieldList.push({ type: 'int', value: 'data34', text: '左侧洗精前刷限位' })
      fieldList.push({ type: 'int', value: 'data35', text: '精刷工位喷水状态' })
      fieldList.push({ type: 'int', value: 'data36', text: '终冲状态' })
      fieldList.push({ type: 'int', value: 'data37', text: '吹干状态' })
      fieldList.push({ type: 'int', value: 'data38', text: '清洗完毕状态' })
      fieldList.push({ type: 'int', value: 'data39', text: '同意洗车状态' })
      fieldList.push({ type: 'int', value: 'data40', text: '同意出库状态' })
      fieldList.push({ type: 'int', value: 'data41', text: '列车入库状态' })
      fieldList.push({ type: 'int', value: 'data42', text: '列车出库状态' })
      fieldList.push({ type: 'int', value: 'data43', text: '模拟洗车信号' })
      fieldList.push({ type: 'int', value: 'data44', text: '清洗完毕信号' })
      fieldList.push({ type: 'int', value: 'data45', text: '预湿泵故障' })
      fieldList.push({ type: 'int', value: 'data46', text: '回水泵故障' })
      fieldList.push({ type: 'int', value: 'data47', text: '清水泵故障' })
      fieldList.push({ type: 'int', value: 'data48', text: '软水泵故障' })
      fieldList.push({ type: 'int', value: 'data49', text: '计量泵1故障' })
      fieldList.push({ type: 'int', value: 'data50', text: '计量泵2故障' })
      fieldList.push({ type: 'int', value: 'data51', text: '侧刷电机1故障' })
      fieldList.push({ type: 'int', value: 'data52', text: '侧刷电机2故障' })
      fieldList.push({ type: 'int', value: 'data53', text: '侧刷电机3故障' })
      fieldList.push({ type: 'int', value: 'data54', text: '侧刷电机4故障' })
      fieldList.push({ type: 'int', value: 'data55', text: '侧刷电机5故障' })
      fieldList.push({ type: 'int', value: 'data56', text: '侧刷电机6故障' })
      fieldList.push({ type: 'int', value: 'data57', text: '侧刷电机7故障' })
      fieldList.push({ type: 'int', value: 'data58', text: '侧刷电机8故障' })
      fieldList.push({ type: 'int', value: 'data59', text: '侧刷电机9故障' })
      fieldList.push({ type: 'int', value: 'data60', text: '运行状态（运行）' })
      fieldList.push({ type: 'int', value: 'data61', text: '运行状态（故障）' })
      fieldList.push({ type: 'int', value: 'data62', text: '预留1' })
      fieldList.push({ type: 'int', value: 'data63', text: '预留2' })
      fieldList.push({ type: 'int', value: 'data64', text: '预留3' })
      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
        })
      }
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>