<template>
  <div class="app-container">
    <el-card class="topCard">
      <div class="topDiv">
        <span style="">工位号：</span>
        <el-select v-model="stationNumberValue" placeholder="请选择工位号" clearable size="small" style="width: 240px">
          <el-option
            v-for="item in stationNumber"
            :key="item.value"
            :label="item.label"
            :value="item.value"
            size="mini"/>
        </el-select>
        <span style="">模组/PACK：</span>
        <el-input v-model="assemblyNumberValue" style="width:240px;" placeholder="模组/PACK码" size="small" clearable/>
        <span style="">接口名称：</span>
        <el-input v-model="interfaceNameValue" style="width:220px;" placeholder="接口名称" size="small" clearable/>
        <span style="">操作时间：</span>
        <el-date-picker
          v-model="dateTime"
          :clearable="false"
          size="small"
          type="daterange"
          value-format="yyyy-MM-dd"
          format="yyyy-MM-dd"
          range-separator="～"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          style="width:240px;"/>
        <el-button type="primary" icon="el-icon-search" size="small" style="margin-left: 20px;" @click="pageCurrent = 1;getTableData()">查询</el-button>
      </div>
      <div style="margin-top: 5px">
        <el-table
          v-loading="loading"
          ref="multipleTable"
          :data="tableData"
          :header-cell-style="{background:'#eef1f6',color:'#606266'}"
          element-loading-text="数据加载中"
          element-loading-spinner="el-icon-loading"
          element-loading-background="rgba(0, 0, 0, 0.2)"
          class="main"
          tooltip-effect="dark"
          height="590px"
          style="width: 100%"
          border
          size="mini">
          <el-table-column type="index" width="60" label="序号" align="center"/>
          <el-table-column align="center" label="模组/PACK" width="200">
            <template slot-scope="scope">
              {{ scope.row.发动机号 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="工位号" width="100">
            <template slot-scope="scope">
              {{ scope.row.工位号 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="工位名称" width="100">
            <template slot-scope="scope">
              {{ scope.row.工位名称 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="接口名称" width="220">
            <template slot-scope="scope">
              {{ scope.row.接口名称 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="接口说明">
            <template slot-scope="scope">
              {{ scope.row.接口说明 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="启用" width="80">
            <template slot-scope="scope">
              <el-checkbox v-model="scope.row.启用" @change="submitEdit_StationInterface(scope.row)" />
            </template>
          </el-table-column>
          <el-table-column align="center" label="请求内容" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ scope.row.请求内容 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="请求时间" width="90">
            <template slot-scope="scope">
              {{ scope.row.请求时间 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="返回内容" show-overflow-tooltip>
            <template slot-scope="scope">
              <span v-if="scope.row.请求时间">code：{{ scope.row.返回代码 }}，message: {{ scope.row.返回内容 }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="返回时间" width="90">
            <template slot-scope="scope">
              {{ scope.row.返回时间 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="累积用时" width="80">
            <template slot-scope="scope">
              {{ scope.row.累积用时 }}
            </template>
          </el-table-column>
        </el-table>
        <div class="block" style="margin-top: 10px;">
          <el-pagination
            v-if="!loading"
            :current-page="pageCurrent"
            :page-sizes="[15, 30, 40]"
            :page-size="pageSize"
            :total="total"
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"/>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script>
import { getBeforeOrAfterTime, getNowShotTime } from '@/utils/tool'

export default {
  data() {
    return {
      interFaceValue: '',
      interFace: [],
      stationNumberValue: '',
      assemblyNumberValue: '',
      interfaceNameValue: '',
      dateTime: [getBeforeOrAfterTime(-2), getNowShotTime()],
      stationNumber: [],
      loading: false,
      tableData: [],
      tableDataParams: [],
      expandsDetail: [],
      expandedRows: null,
      total: 0, // 总条数
      pageList: 30, // 每页显示条数
      pageSize: 30, // 每页显示条数
      pageCurrent: 1 // 后台获取页
    }
  },
  created() {
    this.getStationNumber()
  },
  methods: {
    getStationNumber() {
      this.stationNumber = []
      var param = []
      var Data = this.CreateData('11', 'MES_计划BOM_工位与名称_查询', param)
      this.ExecDatabase(Data).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.stationNumber.push({
            label: `【${response.data[i].工位号}】${response.data[i].工位名称}`,
            value: response.data[i].工位号
          })
        }
      })
    },

    getTableData() {
      this.loading = true
      var param = this.getParam(this.pageCurrent, this.pageSize)
      var Data = this.CreateData('11', 'MOM_接口_Moby_查询', param)
      this.ExecDatabase(Data).then(response => {
        this.tableData = []

        if (response.data.result.length !== 0) {
          this.tableData = response.data.result
          // 是否启用 0/1 转成bool类型
          this.tableData = this.tableData.map(item => ({
            ...item,
            启用: item['是否启用'] === 1
          }))
        }
        this.total = parseInt(response.data.output[0].ItemCount)
        this.loading = false
      }).catch(error => {
        this.tableData = []
        this.loading = false
        this.$message.error('数据加载失败，请稍后重试！')
        console.error('Error fetching interface data:', error)
      })
    },
    getParam(pageCurrent, pageSize) {
      var param = []
      param[0] = ['工位号', this.stationNumberValue]
      param[1] = ['发动机号', this.assemblyNumberValue]
      param[2] = ['接口名称', this.interfaceNameValue]
      param[3] = ['开始时间', this.dateTime[0] + ' 00:00:00']
      param[4] = ['结束时间', this.dateTime[1] + ' 23:59:59']
      param[5] = ['PageCurrent', pageCurrent]
      param[6] = ['PageSize', pageSize]
      param[7] = ['PageCount', '1111', 'int', '1']
      param[8] = ['ItemCount', '1111', 'int', '1']
      return param
    },

    submitEdit_StationInterface(row) {
      var param = []
      param[0] = ['接口名称', row.接口名称]
      param[1] = ['工位号', row.工位号]
      param[2] = ['发动机号', row.发动机号]
      param[3] = ['是否启用', row.启用 ? 1 : 0]
      var Data = this.CreateData('12', 'MOM_接口_Moby_更新是否启用', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data[0].result === '1') {
          this.$message({
            message: '修改成功',
            type: 'success'
          })
          this.getTableData()
        } else {
          this.$message.error('修改失败！')
        }
      })
    },
    // 分页
    handleSizeChange(val) {
      this.pageCurrent = 1
      this.pageSize = val
      this.getTableData()
    },
    handleCurrentChange(val) {
      this.pageCurrent = val
      this.getTableData()
    }
  }
}
</script>

<style>
.topCard{
  margin: 5px;
}
.topDiv{
  margin-bottom: 10px;
}
</style>
