<template>
  <div class="mod-config yzz">
    <!-- 查询表单 - rqrq -->
    <el-form :inline="true" label-position="top" class="query-form">
      <el-form-item label="工厂编码">
        <el-input v-model="queryData.site" style="width: 120px" placeholder="请输入工厂编码"></el-input>
      </el-form-item>
      <el-form-item label="条码号">
        <el-input v-model="queryData.searchSerialNo" style="width: 150px" placeholder="请输入条码号"></el-input>
      </el-form-item>
      <el-form-item label="操作类型">
        <el-select v-model="queryData.searchOperationType" style="width: 140px" clearable placeholder="请选择">
          <el-option label="全部" value=""></el-option>
          <el-option label="扫入" value="扫入"></el-option>
          <el-option label="扫出" value="扫出"></el-option>
          <el-option label="调用栈板" value="调用栈板"></el-option>
          <el-option label="自动分拣" value="自动分拣"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="操作者">
        <el-input v-model="queryData.searchOperator" style="width: 120px" placeholder="请输入操作者"></el-input>
      </el-form-item>
      <el-form-item label="任务号">
        <el-input v-model="queryData.searchTaskNo" style="width: 150px" placeholder="请输入任务号"></el-input>
      </el-form-item>
      <el-form-item label="开始时间">
        <el-date-picker
          v-model="queryData.startDate"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm:ss"
          style="width: 160px"
          placeholder="请选择开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间">
        <el-date-picker
          v-model="queryData.endDate"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm:ss"
          style="width: 160px"
          placeholder="请选择结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label=" ">
        <el-button type="primary" @click="searchTable()" class="yzzButtonAn">查询</el-button>
        <el-button @click="resetQuery()" class="yzzButtonAn">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 主表格 - rqrq -->
    <el-table
      :height="height"
      :data="dataList"
      border
      highlight-current-row
      v-loading="dataListLoading"
      style="width: 100%;">

      <el-table-column prop="id" header-align="center" align="center" min-width="50" label="ID"></el-table-column>
      <el-table-column prop="serialNo" header-align="center" align="center" min-width="150" label="条码号" show-overflow-tooltip></el-table-column>
      <el-table-column prop="site" header-align="center" align="center" min-width="80" label="站点"></el-table-column>
      <el-table-column prop="operationType" header-align="center" align="center" min-width="100" label="操作类型">
        <template slot-scope="scope">
          <span :style="getOperationTypeStyle(scope.row.operationType)">
            {{ scope.row.operationType || '-' }}
          </span>
        </template>
      </el-table-column>
      <el-table-column prop="operationReason" header-align="center" align="left" min-width="200" label="操作原因" show-overflow-tooltip></el-table-column>
      <el-table-column prop="fieldName" header-align="center" align="center" min-width="120" label="字段名" show-overflow-tooltip></el-table-column>
      <el-table-column prop="oldValue" header-align="center" align="center" min-width="100" label="旧值" show-overflow-tooltip></el-table-column>
      <el-table-column prop="newValue" header-align="center" align="center" min-width="100" label="新值" show-overflow-tooltip></el-table-column>
      <el-table-column prop="operator" header-align="center" align="center" min-width="100" label="操作者"></el-table-column>
      <el-table-column prop="operationTime" header-align="center" align="center" min-width="160" label="操作时间">
        <template slot-scope="scope">
          {{ formatDate(scope.row.operationTime) }}
        </template>
      </el-table-column>
      <el-table-column prop="taskNo" header-align="center" align="center" min-width="150" label="关联任务" show-overflow-tooltip></el-table-column>
      <el-table-column prop="remark" header-align="center" align="center" min-width="150" label="备注" show-overflow-tooltip></el-table-column>
    </el-table>

    <!-- 分页 - rqrq -->
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[20, 50, 100, 1000]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
  </div>
</template>

<script>
import { searchHandlingUnitOperationLogList } from "@/api/warehouse/handlingUnitOperationLog.js"

export default {
  data() {
    return {
      // 页面状态 - rqrq
      height: 500,
      dataListLoading: false,

      // 数据列表 - rqrq
      dataList: [],

      // 查询条件 - rqrq
      queryData: {
        site: this.$store.state.user.site,
        searchSerialNo: '',
        searchOperationType: '',
        searchOperator: '',
        searchTaskNo: '',
        startDate: '',
        endDate: ''
      },

      // 分页信息 - rqrq
      pageIndex: 1,
      pageSize: 20,
      totalPage: 0
    }
  },

  mounted() {
    this.$nextTick(() => {
      this.height = window.innerHeight - 220
    })

    // 加载初始数据 - rqrq
    this.getDataList()
  },

  methods: {
    // 查询表格数据 - rqrq
    searchTable() {
      this.pageIndex = 1
      this.getDataList()
    },

    // 获取数据列表 - rqrq
    getDataList() {
      this.dataListLoading = true

      const params = {
        ...this.queryData,
        page: this.pageIndex,
        limit: this.pageSize
      }

      searchHandlingUnitOperationLogList(params).then(({data}) => {
        this.dataListLoading = false
        if (data && data.code === 0) {
          this.dataList = data.page.list || []
          this.pageIndex = data.page.currPage
          this.totalPage = data.page.totalCount
        } else {
          this.dataList = []
          this.$message.error(data.msg || '查询失败')
        }
      }).catch(() => {
        this.dataListLoading = false
        this.dataList = []
        this.$message.error('查询失败，请稍后重试')
      })
    },

    // 重置查询条件 - rqrq
    resetQuery() {
      this.queryData = {
        site: this.$store.state.user.site,
        searchSerialNo: '',
        searchOperationType: '',
        searchOperator: '',
        searchTaskNo: '',
        startDate: '',
        endDate: ''
      }
      this.searchTable()
    },

    // 每页数变化 - rqrq
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },

    // 当前页变化 - rqrq
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },

    // 获取操作类型样式（加粗+颜色）- rqrq
    getOperationTypeStyle(type) {
      // 扫入是绿色、扫出是蓝色、调用栈板是蓝色 - rqrq
      const colorMap = {
        '扫入': '#67C23A',        // 绿色
        '扫出': '#409EFF',        // 蓝色
        '调用栈板': '#409EFF',    // 蓝色
        '自动分拣': '#E6A23C'     // 橙色
      }
      return {
        fontWeight: 'bold',
        color: colorMap[type] || '#606266'  // 默认灰色
      }
    },

    // 格式化日期 - rqrq
    formatDate(dateStr) {
      if (!dateStr) return ''
      const date = new Date(dateStr)
      const year = date.getFullYear()
      const month = String(date.getMonth() + 1).padStart(2, '0')
      const day = String(date.getDate()).padStart(2, '0')
      const hours = String(date.getHours()).padStart(2, '0')
      const minutes = String(date.getMinutes()).padStart(2, '0')
      const seconds = String(date.getSeconds()).padStart(2, '0')

      if (hours === '00' && minutes === '00' && seconds === '00') {
        return `${year}-${month}-${day}`
      }
      return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
    }
  }
}
</script>

<style scoped>
.query-form {
  margin-top: 1px;
  margin-left: 0px;
}
</style>

