<template>
  <div>
    <el-card>
      <div class="search-container" @keyup.enter="searchTable">
        <span class="show-text">任务ID:</span>
        <el-input
          v-model="TaskAID"
          clearable
          placeholder="请输入任务ID"
          style="width: 140px; margin-right: 10px"
        />
        <span class="show-text">生产订单:</span>
        <el-input
          v-model="orderNo"
          clearable
          placeholder="请输入生产订单"
          style="width: 140px; margin-right: 10px"
        />
        <span class="show-text">计划号:</span>
        <el-input
          v-model="planNo"
          clearable
          placeholder="请输入计划号"
          style="width: 140px; margin-right: 10px"
        />
        <span class="show-text">销售订单:</span>
        <el-input
          v-model="saleNo"
          clearable
          placeholder="请输入销售订单"
          style="width: 140px; margin-right: 10px"
        />
        <span class="show-text">物料编码:</span>
        <el-input
          v-model="productCode"
          clearable
          placeholder="请输入物料编码"
          style="width: 150px; margin-right: 10px"
        />
        <span class="show-text">物料名称:</span>
        <el-input
          v-model="productName"
          clearable
          placeholder="请输入物料名称"
          style="width: 150px; margin-right: 10px"
        />
        <span class="show-text">任务状态:</span>
        <el-select
          v-model="productionStatus"
          clearable
          placeholder="请选择状态"
          style="width: 130px; margin-right: 10px"
        >
          <el-option label="全部" value="10" />
          <el-option label="待执行" value="0" />
          <el-option label="可执行" value="1" />
          <el-option label="已开工" value="2" />
          <el-option label="已完成" value="4" />
        </el-select>
        <el-button
          :disabled="loading"
          icon="el-icon-search"
          plain
          type="primary"
          @click="searchTable"
        >查询</el-button>
      </div>

      <el-table
        v-loading="loading"
        :data="tableData"
        :height="'80vh'"
        :row-class-name="tableRowClassName"
        border
        element-loading-background="rgba(0, 0, 0, 0.2)"
        element-loading-spinner="el-icon-loading"
        highlight-current-row
        size="mini"
        style="width: 100%; margin-top: 20px"
        tooltip-effect="dark"
      >
        <el-table-column align="center" fixed label="序号" type="index" width="50" />
        <el-table-column align="center" fixed label="生产订单" prop="订单号" width="90" />
        <el-table-column align="center" label="销售订单" prop="合同号" width="100" />
        <el-table-column align="center" label="计划号" prop="计划号" width="90" />
        <el-table-column align="center" label="物料编码" prop="零件编码" width="170" />
        <el-table-column align="center" label="物料名称" prop="零件名称" width="170" />
        <el-table-column align="center" label="齐套" width="80">
          <template slot-scope="scope">
            <div :style="getStatusStyle(scope.row.齐套)" class="score-cell">
              {{ scope.row.齐套 }}
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" label="发料状态" width="90">
          <template slot-scope="scope">
            <div :style="getStatusStyle(scope.row.发料状态)" class="score-cell">
              {{ scope.row.发料状态 }}
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" label="发料齐套时间" width="150">
          <template slot-scope="scope">{{ formatDate(scope.row.发料齐套时间) }}</template>
        </el-table-column>
        <el-table-column align="center" label="拣配" width="80">
          <template slot-scope="scope">
            <div :style="getStatusStyle(scope.row.拣配状态)" class="score-cell">
              {{ scope.row.拣配状态 }}
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" label="加急数量" prop="加急总数" width="100" />
        <el-table-column align="center" label="分配数" prop="分配数" width="70" />
        <el-table-column align="center" label="送检数" prop="完成数量" width="70" />
        <el-table-column align="center" label="任务状态" width="90">
          <template slot-scope="scope">
            <div :style="getStatusStyle(scope.row.任务状态)" class="score-cell">
              {{ scope.row.任务状态 }}
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" label="指派对象" prop="指派对象" width="90" />
        <el-table-column align="center" label="计划开始" width="120">
          <template slot-scope="scope">{{ formatDate(scope.row.计划开始) }}</template>
        </el-table-column>
        <el-table-column align="center" label="计划完成" width="120">
          <template slot-scope="scope">{{ formatDate(scope.row.计划完成) }}</template>
        </el-table-column>
        <el-table-column align="center" label="二次派工" prop="二次派工名称" width="120" />
        <el-table-column align="center" label="工序名称" prop="工序名称" width="90" />
        <el-table-column align="center" label="上序合格数" prop="上序合格数" width="90" />
        <el-table-column align="center" label="入库数量" prop="入库数量" width="90" />
        <el-table-column align="center" label="手续确认" fixed="right" width="120">
          <template slot-scope="scope">
            <el-button
              :loading="confirmingTaskAID === scope.row.TaskAID"
              size="mini"
              type="primary"
              @click="confirmProcedure(scope.row)"
            >已完成</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'FormalityConfirmReport',
  data() {
    return {
      TaskAID: '',
      orderNo: '',
      planNo: '',
      saleNo: '',
      productCode: '',
      productName: '',
      productionStatus: '10',
      tableData: [],
      loading: false,
      confirmingTaskAID: null
    }
  },
  created() {
    this.searchTable()
  },
  methods: {
    async searchTable() {
      this.loading = true
      this.tableData = []

      const param = []
      param.push(['TaskAID', this.TaskAID || -1])
      param.push(['订单号', this.orderNo || -1])
      param.push(['零件编号', this.productCode])
      param.push(['零件名称', this.productName])
      param.push(['计划开工_Start', ''])
      param.push(['计划开工_End', ''])
      param.push(['生产状态', this.productionStatus || 10])
      param.push(['工位名称', ''])
      param.push(['计划号', this.planNo])
      param.push(['销售订单', this.saleNo])

      const data = this.CreateData('11', '生产管理_手续确认报表_查询', param)
      try {
        const response = await this.ExecDatabase(data)
        this.tableData = response.data || []
      } catch (error) {
        console.error('查询失败:', error)
        this.$message.error('查询失败')
      } finally {
        this.loading = false
      }
    },
    confirmProcedure(row) {
      this.$confirm('确认该任务手续已完成?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        confirmButtonClass: 'confirm-confirm-button',
        cancelButtonClass: 'confirm-cancel-button',
        type: 'warning'
      }).then(async() => {
        this.confirmingTaskAID = row.TaskAID
        const param = []
        param.push(['TaskAID', row.TaskAID])
        param.push(['订单号', row.订单号])
        param.push(['确认人', this.$store.getters.name || ''])
        param.push(['备注', ''])
        const data = this.CreateData('12', '生产管理_手续确认报表_确认', param)
        try {
          const response = await this.ExecDatabase(data)
          const result = response.data && response.data[0]
          if (result && String(result.result) === '1') {
            this.$message.success('确认成功')
            this.searchTable()
          } else {
            this.$message.error((result && (result.msg || result.提示信息)) || '确认失败')
          }
        } catch (error) {
          console.error('确认失败:', error)
          this.$message.error('确认失败')
        } finally {
          this.confirmingTaskAID = null
        }
      }).catch(() => {
        this.$message.info('取消操作')
      })
    },
    tableRowClassName({ row }) {
      if (row.加急 === '加急') {
        return 'priority-row'
      }
      return ''
    },
    getStatusStyle(value) {
      if (value === '可执行' || value === '是' || value === '发料齐套' || value === '已拣配') {
        return {
          backgroundColor: '#f0f9eb',
          color: '#67c23a'
        }
      }
      if (value === '已完成') {
        return {
          backgroundColor: '#ecf5ff',
          color: '#409eff'
        }
      }
      if (value === '已开工' || value === '否' || value === '发料缺件' || value === '未拣配') {
        return {
          backgroundColor: '#fdf6ec',
          color: '#e6a23c'
        }
      }
      return {}
    },
    formatDate(value) {
      if (!value) return ''
      return String(value).split('T')[0].split(' ')[0]
    }
  }
}
</script>

<style scoped>
.search-container {
  margin-bottom: 20px;
}

.show-text {
  margin-right: 5px;
  line-height: 32px;
}

.score-cell {
  border-radius: 3px;
  padding: 2px 4px;
}

::v-deep .priority-row {
  background-color: #f56c6c !important;
  color: #000000 !important;
}

::v-deep .priority-row td {
  background-color: #f56c6c !important;
  color: #000000 !important;
  border: #f56c6c;
}
</style>
