<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="批次号" prop="batchNo">
        <el-input
          v-model="queryParams.batchNo"
          placeholder="请输入批次号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="工序名称" prop="processName">
        <el-input
          v-model="queryParams.processName"
          placeholder="请输入工序名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['mes:process-quality:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
      v-loading="loading"
      :data="list"
      :stripe="true"
      :show-overflow-tooltip="true"
    >
      <el-table-column type="index" label="序号" width="80" align="center" />
      <el-table-column label="编号" prop="code" min-width="180" align="center" />
      <el-table-column label="批次号" prop="batchNo" min-width="150" align="center" />
      <el-table-column label="产品名称" prop="productName" min-width="120" align="center" />
      <el-table-column label="工序路线" prop="processRouteName" min-width="120" align="center" />
      <el-table-column label="工序名称" prop="processName" min-width="120" align="center" />
      <el-table-column label="加工前重量" prop="beforeWeight" min-width="100" align="center" />
      <el-table-column label="加工后重量" prop="afterWeight" min-width="100" align="center" />
      <el-table-column label="颜色" prop="color" min-width="80" align="center" />
      <el-table-column label="损耗率" prop="lossRate" min-width="80" align="center">
        <template #default="scope">
          {{ scope.row.lossRate }}%
        </template>
      </el-table-column>
      <el-table-column label="粗细度" prop="thickness" min-width="80" align="center" />
      <el-table-column label="数量" prop="quantity" min-width="80" align="center" />
      <el-table-column label="是否合格" prop="qualified" min-width="100" align="center">
        <template #default="scope">
          <el-tag :type="scope.row.qualified ? 'success' : 'danger'">
            {{ scope.row.qualified ? '合格' : '不合格' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="配料比例" prop="ratio" min-width="100" align="center" />
      <el-table-column label="开始时间" prop="startTime" min-width="160" align="center">
        <template #default="scope">
          {{ formatDate(scope.row.startTime) }}
        </template>
      </el-table-column>
      <el-table-column label="结束时间" prop="endTime" min-width="160" align="center">
        <template #default="scope">
          {{ formatDate(scope.row.endTime) }}
        </template>
      </el-table-column>
      <el-table-column label="备注" prop="data1" min-width="100" align="center" />
      <el-table-column label="说明" prop="data2" min-width="100" align="center" />
      <el-table-column label="扩展" prop="data3" min-width="100" align="center" />
      <el-table-column label="操作" width="160" align="center">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row)"
            v-hasPermi="['mes:process-quality:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mes:process-quality:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      v-if="total > 0"
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗 -->
  <ProcessQualityForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox, FormInstance } from 'element-plus'
import { formatDate } from '@/utils/formatTime'
import { getProcessQualityPage, deleteProcessQuality } from '@/api/mes/quality/process-quality'
import ProcessQualityForm from './ProcessQualityForm.vue'
import { Icon } from '@/components/Icon'
import { ContentWrap } from '@/components/ContentWrap'

/** 工序质检列表 */
defineOptions({ name: 'MesProcessQuality' })

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryFormRef = ref<FormInstance>()
const formRef = ref()

// 查询参数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  batchNo: '',
  productName: '',
  processName: ''
})

// 获取列表数据
const getList = async () => {
  loading.value = true
  try {
    const res = await getProcessQualityPage(queryParams)
    list.value = res.list
    total.value = res.total
  } catch (error) {
    console.error('获取列表失败', error)
  } finally {
    loading.value = false
  }
}

// 查询操作
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

// 重置查询
const resetQuery = () => {
  queryFormRef.value?.resetFields()
  handleQuery()
}

// 打开表单
const openForm = (type: string, row?: any) => {
  formRef.value?.open(type, row?.id)
}

// 删除操作
const handleDelete = async (row: any) => {
  try {
    await ElMessageBox.confirm('是否确认删除该工序质检记录?', '警告', {
      type: 'warning'
    })
    await deleteProcessQuality(row.id)
    ElMessage.success('删除成功')
    await getList()
  } catch (error) {
    console.error('删除失败', error)
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>


