<template>
  <!-- 列表 -->
  <ContentWrap>
    <el-button
      type="primary"
      plain
      @click="openForm('create')"
      v-hasPermi="['mes:record-template:create']"
    >
      <Icon icon="ep:plus" class="mr-5px" /> 新增
    </el-button>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
        sortable
      />
      <el-table-column label="创建者" align="center" prop="creator" />
       <el-table-column
        label="开机时间"
        align="center"
        prop="startTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="停机时间"
        align="center"
        prop="endTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="碎浆时间"
        align="center"
        prop="suijiangTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="碎浆时间" align="center" prop="suijiangCostTime" />
      <el-table-column label="原料配比id" align="center" prop="ratioId" />
      <el-table-column label="原料配比" align="center" prop="ratio" />
      <el-table-column label="用水量kg" align="center" prop="waterUsage" />
      <el-table-column label="放浆浓度%" align="center" prop="densityOut" />
      <el-table-column label="加药量kg" align="center" prop="addItemUsage" />
      <el-table-column label="出渣率%" align="center" prop="slagYield" />
      <el-table-column label="浓度%" align="center" prop="density" />
      <el-table-column label="备注" align="center" prop="remark" />

      <el-table-column label="操作" align="center" fixed="right" width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['mes:record-template:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['mes:record-template:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
    <!-- 表单弹窗：添加/修改 -->
    <RecordSuijiangForm ref="formRef" @success="getList" />
</template>
<script setup lang="ts">
import { DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import { RecordTemplateApi } from '@/api/mes/record'
import RecordSuijiangForm from './RecordSuijiangForm.vue'

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const props = defineProps<{
  recordId?: number // 组织id（主表的关联字段）
}>()
const loading = ref(false) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  recordId: undefined as unknown
})

/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
  () => props.recordId,
  (val: number) => {
    if (!val) {
      return
    }
    queryParams.recordId = val
    handleQuery()
  },
    { immediate: true, deep: true }
)

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await RecordTemplateApi.getRecordSuijiangPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  if (!props.recordId) {
    message.error('请选择一个记录表')
    return
  }
  formRef.value.open(type, id, props.recordId)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await RecordTemplateApi.deleteRecordSuijiang(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}
</script>
