<template>
  <div class="mod-config">
    <el-form :inline="true" label-position="top" class="query-form">
      <el-form-item label="单号">
        <el-input v-model="searchData.orderNo" clearable placeholder="请输入单号" style="width: 180px"></el-input>
      </el-form-item>
      <el-form-item label="订单类型">
        <el-select v-model="searchData.orderType" clearable placeholder="全部" style="width: 150px">
          <el-option label="家用电梯" value="HOME_LIFT"></el-option>
          <el-option label="线缆/COP" value="CABLE_COP"></el-option>
          <el-option label="改造项目" value="RENOVATION"></el-option>
          <el-option label="机加工生产" value="MACHINING"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="报工节点">
        <el-input v-model="searchData.nodeName" clearable placeholder="请输入节点名称" style="width: 160px"></el-input>
      </el-form-item>
      <el-form-item label=" " style="margin-top: -11px">
        <el-button @click="getDataList('Y')" plain class="search-btn">查询</el-button>
        <el-button @click="resetQuery()" plain class="reset-btn">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table
      class="data-table"
      :data="dataList"
      :height="tableHeight"
      border
      v-loading="dataListLoading"
      style="width: 100%">
      <el-table-column type="index" label="#" width="50" align="center"></el-table-column>
      <el-table-column prop="logTime" label="报工时间" min-width="160" align="center"></el-table-column>
      <el-table-column prop="orderTypeName" label="订单类型" min-width="120" align="center">
        <template slot-scope="scope">
          <el-tag :type="getTypeTag(scope.row.orderType)" size="small">{{ scope.row.orderTypeName }}</el-tag>
        </template>
      </el-table-column>
<!--      <el-table-column prop="orderNo" label="单号" width="190" align="center"></el-table-column>-->
      <el-table-column prop="projectNo" label="项目/任务标识" min-width="150" align="center">
        <template slot-scope="scope">{{ scope.row.projectNo || '-' }}</template>
      </el-table-column>
      <el-table-column prop="nodeName" label="报工节点" min-width="140" align="center"></el-table-column>
      <el-table-column prop="reportQty" label="报工数量" min-width="100" align="center">
        <template slot-scope="scope">{{ scope.row.reportQty || '-' }}</template>
      </el-table-column>
      <el-table-column prop="comment" label="备注" min-width="180" show-overflow-tooltip></el-table-column>
      <el-table-column label="操作" width="120" align="center">
        <template slot-scope="scope">
          <a type="text" style="color:#F56C6C" @click="cancelLog(scope.row)">取消报工</a>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
      style="margin-top: 20px; text-align: right">
    </el-pagination>
  </div>
</template>

<script>
import { cancelReportLog, getMyReportLogList } from '@/api/longchuang/productionPlan'

export default {
  name: 'ProductionReportCancel',
  data() {
    return {
      searchData: { orderNo: '', orderType: '', nodeName: '', page: 1, limit: 20 },
      dataList: [],
      pageIndex: 1,
      pageSize: 20,
      totalPage: 0,
      dataListLoading: false,
      tableHeight: window.innerHeight - 260
    }
  },
  activated() {
    this.getDataList('Y')
  },
  methods: {
    getDataList(flag) {
      if (flag === 'Y') this.pageIndex = 1
      this.searchData.page = this.pageIndex
      this.searchData.limit = this.pageSize
      this.dataListLoading = true
      getMyReportLogList(this.searchData).then(({ data }) => {
        this.dataListLoading = false
        if (data && data.code === 0) {
          const page = data.page || {}
          const list = page.list || []
          this.dataList = list.map(this.normalizeRow)
          this.totalPage = page.totalCount || 0
        } else {
          this.dataList = []
          this.totalPage = 0
          this.$message.error((data && data.msg) || '查询失败')
        }
      }).catch(() => {
        this.dataListLoading = false
        this.dataList = []
        this.totalPage = 0
        this.$message.error('查询失败')
      })
    },
    normalizeRow(row) {
      return {
        ...row,
        orderTypeName: this.getOrderTypeName(row.orderType)
      }
    },
    getOrderTypeName(orderType) {
      const map = {
        HOME_LIFT: '家用电梯',
        CABLE_COP: '线缆/COP',
        RENOVATION: '改造项目',
        MACHINING: '机加工生产'
      }
      return map[orderType] || orderType || '-'
    },
    getTypeTag(orderType) {
      const map = {
        HOME_LIFT: 'primary',
        CABLE_COP: 'warning',
        RENOVATION: 'success',
        MACHINING: 'danger'
      }
      return map[orderType] || 'info'
    },
    resetQuery() {
      this.searchData = { orderNo: '', orderType: '', nodeName: '', page: 1, limit: 20 }
      this.getDataList('Y')
    },
    cancelLog(row) {
      if (!row || !row.logNo) return
      this.$confirm('确定取消该条报工记录吗？取消后需要重新报工。', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        cancelReportLog({ logNo: row.logNo }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message.success(data.msg || '取消报工成功')
            this.getDataList()
          } else {
            this.$message.error((data && data.msg) || '取消报工失败')
          }
        }).catch(() => {
          this.$message.error('取消报工失败')
        })
      }).catch(() => {})
    },
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    }
  }
}
</script>

<style scoped>
.data-table {
  background-color: #fff;
  border-radius: 4px;
}
.data-table >>> .cell {
  line-height: 20px;
  height: 20px;
}
.data-table >>> .el-table__header-wrapper th,
.data-table >>> .el-table__fixed-header-wrapper th {
  background-color: #f5f7fa !important;
  color: #333;
  font-weight: 600;
  border-color: #ebeef5;
  padding: 8px 0;
}

.data-table >>> .el-table__header-wrapper .cell,
.data-table >>> .el-table__fixed-header-wrapper .cell,
.data-table >>> .el-table__body-wrapper .cell,
.data-table >>> .el-table__fixed-body-wrapper .cell {
  padding: 0 10px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 13px !important;
}

.data-table >>> .el-table__body tr:hover > td {
  background-color: #f5f7fa !important;
}

.data-table >>> .el-table__body tr.current-row > td {
  background-color: #ecf5ff !important;
}

.query-form {
  background-color: #fff;
  padding: 15px 15px 5px 15px;
  border-radius: 4px;
  margin-bottom: 12px;
}

.query-form >>> .el-form-item__label {
  color: #333;
  font-size: 13px;
  padding-bottom: 5px;
}

.query-form >>> .el-input__inner {
  height: 32px;
  line-height: 32px;
  border-radius: 4px;
  font-size: 13px;
}

.query-form >>> .el-button {
  height: 32px;
  padding: 0 15px;
  font-size: 13px;
  border-radius: 4px;
}

.search-btn {
  background-color: #ecf5ff;
  border-color: #b3d8ff;
  color: #409eff;
}

.search-btn:hover {
  background-color: #409eff;
  border-color: #409eff;
  color: #fff;
}

.reset-btn {
  background-color: #f5f7fa;
  border-color: #d3d4d6;
  color: #606266;
}

.reset-btn:hover {
  background-color: #909399;
  border-color: #909399;
  color: #fff;
}

.add-btn {
  background-color: #f0f9eb;
  border-color: #c2e7b0;
  color: #67c23a;
}

.add-btn:hover {
  background-color: #67c23a;
  border-color: #67c23a;
  color: #fff;
}

.dialog-footer {
  text-align: right;
}

.edit-form {
  margin-left: 5px;
  margin-top: -5px;
}

.detail-tabs-wrap {
  margin-top: 12px;
  background-color: #fff;
  border-radius: 4px;
}

.detail-table {
  width: 100%;
}

.detail-table >>> .el-table__header-wrapper th,
.detail-table >>> .el-table__fixed-header-wrapper th {
  background-color: #f5f7fa !important;
  color: #333;
  font-weight: 600;
  border-color: #ebeef5;
}

.two-column-layout {
  display: flex;
  gap: 12px;
}

.stages-column {
  width: 38%;
  min-width: 320px;
  border: 1px solid #ebeef5;
  border-radius: 4px;
  background: #fff;
}

.logs-column {
  flex: 1;
  border: 1px solid #ebeef5;
  border-radius: 4px;
  background: #fff;
}

.column-header {
  height: 24px;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 0 12px;
  border-bottom: 1px solid #ebeef5;
  font-weight: 600;
  color: #303133;
}

.progress-badge,
.logs-count {
  margin-left: auto;
  color: #409eff;
  font-size: 12px;
  font-weight: 500;
}

.stages-list {
  max-height: 295px;
  overflow-y: auto;
  padding: 10px;
}

.stage-item {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 3px 8px;
  border-radius: 4px;
}

.stage-item + .stage-item {
  margin-top: 2px;
}

.stage-item.stage-done {
  background: #f0f9eb;
}

.stage-item.stage-processing {
  background: #fdf6ec;
}

.stage-item.stage-pending {
  background: #f5f7fa;
}

.stage-icon {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  background: #fff;
  border: 1px solid #dcdfe6;
  color: #606266;
}

.stage-content {
  flex: 1;
}

.stage-name {
  font-size: 13px;
  color: #303133;
  line-height: 20px;
}

.stage-meta {
  margin-top: 6px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.stage-owner {
  color: #606266;
  font-size: 12px;
}

.logs-table-wrapper {
  padding: 8px;
}

.el-icon-check {
  color: #67c23a;
  font-weight: 1000;
}
</style>
