<script>

import {selectCollectRecordList} from "../../../api/cdc/collect";

const tableId = "2000003"
const table1 = tableId + "Table1"
const tableName = "数据采集历史"
export default {
  name: "collectRecord",
  props:{
    height:{
      type:Number,
      default:700
    },
    component:{
      type:Boolean,
      default:false
    }
  },
  data(){
    return{
      // 遮罩层
      loading: false,
      // 导出
      exportData: [],
      exportName: '数据采集历史' + this.dayjs().format('YYYYMMDDHHmmss'),
      exportHeader: [],
      exportFooter: [],
      // 设备信息表格数据
      infoList: [],
      // 弹出层标题
      title: "数据采集历史",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        deviceNo: '',
        deviceDesc: null,
        deviceType: "",
        startDate: "",
        endDate: "",
      },

      collectRecordColumns:[
        {
          userId: this.$store.state.user.name,
          functionId: tableId,
          serialNumber: `${table1}CreateTime`,
          tableId: table1,
          tableName: tableName,
          columnProp: 'createTime',
          headerAlign: 'center',
          align: 'center',
          columnLabel: '录入时间',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 120
        },{
          userId: this.$store.state.user.name,
          functionId: tableId,
          serialNumber: `${table1}DeviceNo`,
          tableId: table1,
          tableName: tableName,
          columnProp: 'deviceNo',
          headerAlign: 'center',
          align: 'left',
          columnLabel: '设备号',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 100
        },{
          userId: this.$store.state.user.name,
          functionId: tableId,
          serialNumber: `${table1}DeviceDesc`,
          tableId: table1,
          tableName: tableName,
          columnProp: 'deviceDesc',
          headerAlign: 'center',
          align: 'left',
          columnLabel: '设备描述',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 120
        },{
          userId: this.$store.state.user.name,
          functionId: tableId,
          serialNumber: `${table1}CollectId`,
          tableId: table1,
          tableName: tableName,
          columnProp: 'collectId',
          headerAlign: 'center',
          align: 'left',
          columnLabel: '数据标识ID',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 100
        },{
          userId: this.$store.state.user.name,
          functionId: tableId,
          serialNumber: `${table1}CollectDesc`,
          tableId: table1,
          tableName: tableName,
          columnProp: 'collectDesc',
          headerAlign: 'center',
          align: 'left',
          columnLabel: '数据标识描述',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 120
        },{
          userId: this.$store.state.user.name,
          functionId: tableId,
          serialNumber: `${table1}ItemValue`,
          tableId: table1,
          tableName: tableName,
          columnProp: 'itemValue',
          headerAlign: 'center',
          align: 'right',
          columnLabel: '数据实测值',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 120
        },
      ],
    }
  },
  computed: {
    tableHeight () {
      const base = Number(this.height) || 0
      if (!base) {
        return 420
      }
      const adjusted = base - 180
      if (adjusted < 320) {
        return 320
      }
      if (adjusted > 420) {
        return 420
      }
      return adjusted
    }
  },
  methods:{
    handleQuery(){
      let params = {
        ...this.queryParams,
        site:this.$store.state.user.site,
      }
      this.loading = true
      this.infoList = []
      selectCollectRecordList(params).then(({data})=>{
        if (data && data.code === 0){
          this.infoList = data.rows
        }else {
          this.$message.warning(data.msg)
        }
        this.loading = false
      }).catch(error=>{
        this.loading = false
        this.$message.error(error)
      })
    },
    async createExportData () {
      if (!this.infoList.length) {
        this.$message.warning('请先查询出数据')
        return []
      }
      const params = {
        ...this.queryParams,
        site: this.$store.state.user.site,
        page: 1,
        limit: -1
      }
      this.exportName = '数据采集历史' + this.dayjs().format('YYYYMMDDHHmmss')
      try {
        const { data } = await selectCollectRecordList(params)
        if (data && data.code === 0) {
          const rows = data.rows || (data.page && data.page.list) || []
          if (!rows.length) {
            this.$message.warning('无可导出的数据')
            return []
          }
          this.exportData = rows
          return this.exportData
        }
        this.$message.error(data ? data.msg : '导出失败')
      } catch (error) {
        this.$message.error(error)
      }
      return []
    },
    startDownload () {},
    finishDownload () {},
    fields () {
      const mapping = {}
      this.collectRecordColumns.forEach(item => {
        if (!item.columnHidden) {
          mapping[item.columnLabel] = item.columnProp
        }
      })
      return mapping
    }
  },
  watch:{
    "queryParams.deviceNo"(newVal, oldVal){
      this.queryParams.deviceNo = newVal.toUpperCase()
    }
  },
  created() {
    //this.handleQuery();
  },
  mounted() {
    this.$nextTick(() => {
      this.height = window.innerHeight - 225;
    })
  },
}
</script>

<template>
<div class="app-container">
  <el-form :model="queryParams" label-position="top" ref="queryForm" :inline="true" label-width="68px">
    <el-form-item label="设备类型" prop="deviceType">
      <el-select v-model="queryParams.deviceType" placeholder="请选择设备类型"  >
        <el-option label="全部" value=""/>
        <el-option label="涂布机器-大线" value="涂布机器-大线"/>
        <el-option label="涂布机器-小线" value="涂布机器-小线"/>
        <el-option label="涂布机器-浓度-大线" value="涂布机器-浓度-大线"/>
        <el-option label="涂布机器-浓度-小线" value="涂布机器-浓度-小线"/>
        <el-option label="配胶间设备" value="配胶间设备"/>
      </el-select>
    </el-form-item>
    <el-form-item label="设备号" prop="deviceNo">
      <el-input
        v-model="queryParams.deviceNo"
        placeholder=" "
        clearable
      />
    </el-form-item>
    <el-form-item label="设备描述" prop="deviceDesc">
      <el-input
        v-model="queryParams.deviceDesc"
        style="width: 200px"
        placeholder=" "
        clearable
      />
    </el-form-item>
    <el-form-item label="采集日期" prop="startDate">
      <el-date-picker
        type="date"
        style="width: 100%"
        value-format="yyyy-MM-dd"
        v-model="queryParams.startDate"
        placeholder=" "
      />
    </el-form-item>
    <el-form-item label=" ">
      <i class="el-icon-right"></i>
    </el-form-item>
    <el-form-item label=" " prop="endDate">
      <el-date-picker
        type="date"
        style="width: 100%"
        v-model="queryParams.endDate"
        value-format="yyyy-MM-dd"
        placeholder=" "
      />
    </el-form-item>
    <el-form-item label=" ">
      <el-button type="primary" @click="handleQuery" plain>查询</el-button>
      <download-excel
        class="el-button el-button--primary el-button--medium"
        :fields="fields()"
        :data="exportData"
        type="xls"
        :name="exportName"
        :header="exportHeader"
        :footer="exportFooter"
        :fetch="createExportData"
        :before-generate="startDownload"
        :before-finish="finishDownload">
        导出
      </download-excel>
    </el-form-item>
  </el-form>
  <el-table v-loading="loading" border :data="infoList">
    <el-table-column
      v-for="(item,index) in collectRecordColumns" :key="index"
      :sortable="item.columnSortable"
      :height="height"
      :prop="item.columnProp"
      :header-align="item.headerAlign"
      :show-overflow-tooltip="item.showOverflowTooltip"
      :align="item.align"
      :fixed="item.fixed === ''?false:item.fixed"
      :min-width="item.columnWidth"
      :label="item.columnLabel">
      <template slot-scope="scope">
        <span v-if="!item.columnHidden">{{ scope.row[item.columnProp] }}</span>
        <span v-if="item.columnImage"><img :src="scope.row[item.columnProp]" style="width: 100px; height: 80px"/></span>
      </template>
    </el-table-column>
  </el-table>
</div>
</template>

<style scoped>

</style>
