<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true" >
            <el-form-item label="记录主键" prop="recordId">
              <el-input v-model="queryParams.recordId" placeholder="请输入记录主键" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="关联评审主表ID" prop="reviewId">
              <el-input v-model="queryParams.reviewId" placeholder="请输入关联评审主表ID" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="检测项编码" prop="itemCode">
              <el-input v-model="queryParams.itemCode" placeholder="请输入检测项编码" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="检测名称" prop="itemName">
              <el-input v-model="queryParams.itemName" placeholder="请输入检测名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="检测位置" prop="inspectionPosition">
              <el-input v-model="queryParams.inspectionPosition" placeholder="请输入检测位置" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="检测项目类别" prop="categoryName">
              <el-input v-model="queryParams.categoryName" placeholder="请输入检测项目类别" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="关联检测类型表" prop="typeId">
              <el-input v-model="queryParams.typeId" placeholder="请输入关联检测类型表" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="检测方法" prop="inspectionMethod">
              <el-select v-model="queryParams.inspectionMethod" placeholder="请选择检测方法" clearable >
                <el-option v-for="dict in qc_methond" :key="dict.value" :label="dict.label" :value="dict.value"/>
              </el-select>
            </el-form-item>
            <el-form-item label="检测方式" prop="detectType">
              <el-select v-model="queryParams.detectType" placeholder="请选择检测方式" clearable >
                <el-option v-for="dict in detect_type" :key="dict.value" :label="dict.label" :value="dict.value"/>
              </el-select>
            </el-form-item>
            <el-form-item label="检测结果" prop="detectResult">
              <el-select v-model="queryParams.detectResult" placeholder="请选择检测结果" clearable >
                <el-option v-for="dict in qc_result" :key="dict.value" :label="dict.label" :value="dict.value"/>
              </el-select>
            </el-form-item>
            <el-form-item label="检测值" prop="detectValue">
              <el-input v-model="queryParams.detectValue" placeholder="请输入检测值" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="控制上限" prop="upperLimit">
              <el-input v-model="queryParams.upperLimit" placeholder="请输入控制上限" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="控制下限" prop="lowerLimit">
              <el-input v-model="queryParams.lowerLimit" placeholder="请输入控制下限" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="规格检测值" prop="specInspectionValue">
              <el-input v-model="queryParams.specInspectionValue" placeholder="请输入规格检测值" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="规格上限" prop="specUpper">
              <el-input v-model="queryParams.specUpper" placeholder="请输入规格上限" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="规格下限" prop="specLower">
              <el-input v-model="queryParams.specLower" placeholder="请输入规格下限" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="检查项说明" prop="description">
              <el-input v-model="queryParams.description" placeholder="请输入检查项说明" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['qms:qcUnqualifiedRecord:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['qms:qcUnqualifiedRecord:edit']">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['qms:qcUnqualifiedRecord:remove']">删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['qms:qcUnqualifiedRecord:export']">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" :columns="columns" :search="true" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="qcUnqualifiedRecordList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="记录主键" align="center" prop="recordId" v-if="columns[0].visible"/>
        <el-table-column label="关联评审主表ID" align="center" prop="reviewId" v-if="columns[1].visible"/>
        <el-table-column label="检测项编码" align="center" prop="itemCode" v-if="columns[3].visible"/>
        <el-table-column label="检测名称" align="center" prop="itemName" v-if="columns[4].visible"/>
        <el-table-column label="检测位置" align="center" prop="inspectionPosition" v-if="columns[5].visible"/>
        <el-table-column label="检测项目类别" align="center" prop="categoryName" v-if="columns[6].visible"/>
        <el-table-column label="关联检测类型表" align="center" prop="typeId" v-if="columns[7].visible"/>
        <el-table-column label="检测方法" align="center" prop="inspectionMethod" v-if="columns[8].visible">
          <template #default="scope">
            <dict-tag :options="qc_methond" :value="scope.row.inspectionMethod"/>
          </template>
        </el-table-column>
        <el-table-column label="检测方式" align="center" prop="detectType" v-if="columns[9].visible">
          <template #default="scope">
            <dict-tag :options="detect_type" :value="scope.row.detectType"/>
          </template>
        </el-table-column>
        <el-table-column label="检测结果" align="center" prop="detectResult" v-if="columns[10].visible">
          <template #default="scope">
            <dict-tag :options="qc_result" :value="scope.row.detectResult"/>
          </template>
        </el-table-column>
        <el-table-column label="检测值" align="center" prop="detectValue" v-if="columns[11].visible"/>
        <el-table-column label="控制上限" align="center" prop="upperLimit" v-if="columns[12].visible"/>
        <el-table-column label="控制下限" align="center" prop="lowerLimit" v-if="columns[13].visible"/>
        <el-table-column label="规格检测值" align="center" prop="specInspectionValue" v-if="columns[14].visible"/>
        <el-table-column label="规格上限" align="center" prop="specUpper" v-if="columns[15].visible"/>
        <el-table-column label="规格下限" align="center" prop="specLower" v-if="columns[16].visible"/>
        <el-table-column label="检查项说明" align="center" prop="description" v-if="columns[17].visible"/>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['qms:qcUnqualifiedRecord:edit']"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['qms:qcUnqualifiedRecord:remove']"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
    <!-- 添加或修改不合格品评审记录对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body>
      <el-form ref="qcUnqualifiedRecordFormRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="记录主键" prop="recordId">
          <el-input v-model="form.recordId" placeholder="请输入记录主键" />
        </el-form-item>
        <el-form-item label="关联评审主表ID" prop="reviewId">
          <el-input v-model="form.reviewId" placeholder="请输入关联评审主表ID" />
        </el-form-item>
        <el-form-item label="检测项编码" prop="itemCode">
          <el-input v-model="form.itemCode" placeholder="请输入检测项编码" />
        </el-form-item>
        <el-form-item label="检测名称" prop="itemName">
          <el-input v-model="form.itemName" placeholder="请输入检测名称" />
        </el-form-item>
        <el-form-item label="检测位置" prop="inspectionPosition">
          <el-input v-model="form.inspectionPosition" placeholder="请输入检测位置" />
        </el-form-item>
        <el-form-item label="检测项目类别" prop="categoryName">
          <el-input v-model="form.categoryName" placeholder="请输入检测项目类别" />
        </el-form-item>
        <el-form-item label="关联检测类型表" prop="typeId">
          <el-input v-model="form.typeId" placeholder="请输入关联检测类型表" />
        </el-form-item>
        <el-form-item label="检测方法" prop="inspectionMethod">
          <el-select v-model="form.inspectionMethod" placeholder="请选择检测方法">
            <el-option
                v-for="dict in qc_methond"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="检测方式" prop="detectType">
          <el-select v-model="form.detectType" placeholder="请选择检测方式">
            <el-option
                v-for="dict in detect_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="检测结果" prop="detectResult">
          <el-select v-model="form.detectResult" placeholder="请选择检测结果">
            <el-option
                v-for="dict in qc_result"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="检测值" prop="detectValue">
          <el-input v-model="form.detectValue" placeholder="请输入检测值" />
        </el-form-item>
        <el-form-item label="控制上限" prop="upperLimit">
          <el-input v-model="form.upperLimit" placeholder="请输入控制上限" />
        </el-form-item>
        <el-form-item label="控制下限" prop="lowerLimit">
          <el-input v-model="form.lowerLimit" placeholder="请输入控制下限" />
        </el-form-item>
        <el-form-item label="规格检测值" prop="specInspectionValue">
          <el-input v-model="form.specInspectionValue" placeholder="请输入规格检测值" />
        </el-form-item>
        <el-form-item label="规格上限" prop="specUpper">
          <el-input v-model="form.specUpper" placeholder="请输入规格上限" />
        </el-form-item>
        <el-form-item label="规格下限" prop="specLower">
          <el-input v-model="form.specLower" placeholder="请输入规格下限" />
        </el-form-item>
        <el-form-item label="检查项说明" prop="description">
          <el-input v-model="form.description" placeholder="请输入检查项说明" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="QcUnqualifiedRecord" lang="ts">
import { listQcUnqualifiedRecord, getQcUnqualifiedRecord, delQcUnqualifiedRecord, addQcUnqualifiedRecord, updateQcUnqualifiedRecord } from '@/api/qms/qcUnqualifiedRecord';
import { QcUnqualifiedRecordVO, QcUnqualifiedRecordQuery, QcUnqualifiedRecordForm } from '@/api/qms/qcUnqualifiedRecord/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { qc_methond, detect_type, qc_result } = toRefs<any>(proxy?.useDict('qc_methond', 'detect_type', 'qc_result'));

const qcUnqualifiedRecordList = ref<QcUnqualifiedRecordVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const qcUnqualifiedRecordFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

// 列显隐信息
const columns = ref<FieldOption[]>([
        { key: 0, label: `记录主键`, visible: true },
        { key: 1, label: `关联评审主表ID`, visible: true },
        { key: 2, label: `租户id`, visible: true },
        { key: 3, label: `检测项编码`, visible: true },
        { key: 4, label: `检测名称`, visible: true },
        { key: 5, label: `检测位置`, visible: true },
        { key: 6, label: `检测项目类别`, visible: true },
        { key: 7, label: `关联检测类型表`, visible: true },
        { key: 8, label: `检测方法`, visible: true },
        { key: 9, label: `检测方式`, visible: true },
        { key: 10, label: `检测结果`, visible: true },
        { key: 11, label: `检测值`, visible: true },
        { key: 12, label: `控制上限`, visible: true },
        { key: 13, label: `控制下限`, visible: true },
        { key: 14, label: `规格检测值`, visible: true },
        { key: 15, label: `规格上限`, visible: true },
        { key: 16, label: `规格下限`, visible: true },
        { key: 17, label: `检查项说明`, visible: true },
        { key: 18, label: `创建部门`, visible: true },
        { key: 19, label: `创建人`, visible: true },
        { key: 20, label: `创建时间`, visible: true },
        { key: 21, label: `修改人`, visible: true },
        { key: 22, label: `修改时间`, visible: true },
        { key: 23, label: `是否删除`, visible: true },
]);

const initFormData: QcUnqualifiedRecordForm = {
  recordId: undefined,
  reviewId: undefined,
  itemCode: undefined,
  itemName: undefined,
  inspectionPosition: undefined,
  categoryName: undefined,
  typeId: undefined,
  inspectionMethod: undefined,
  detectType: undefined,
  detectResult: undefined,
  detectValue: undefined,
  upperLimit: undefined,
  lowerLimit: undefined,
  specInspectionValue: undefined,
  specUpper: undefined,
  specLower: undefined,
  description: undefined,
}
const data = reactive<PageData<QcUnqualifiedRecordForm, QcUnqualifiedRecordQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    recordId: undefined,
    reviewId: undefined,
    itemCode: undefined,
    itemName: undefined,
    inspectionPosition: undefined,
    categoryName: undefined,
    typeId: undefined,
    inspectionMethod: undefined,
    detectType: undefined,
    detectResult: undefined,
    detectValue: undefined,
    upperLimit: undefined,
    lowerLimit: undefined,
    specInspectionValue: undefined,
    specUpper: undefined,
    specLower: undefined,
    description: undefined,
    params: {
    }
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询不合格品评审记录列表 */
const getList = async () => {
  loading.value = true;
  const res = await listQcUnqualifiedRecord(queryParams.value);
  qcUnqualifiedRecordList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
  form.value = {...initFormData};
  qcUnqualifiedRecordFormRef.value?.resetFields();
}

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

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: QcUnqualifiedRecordVO[]) => {
  ids.value = selection.map(item => item.recordId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = "添加不合格品评审记录";
}

/** 修改按钮操作 */
const handleUpdate = async (row?: QcUnqualifiedRecordVO) => {
  reset();
  const _recordId = row?.recordId || ids.value[0]
  const res = await getQcUnqualifiedRecord(_recordId);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = "修改不合格品评审记录";
}

/** 提交按钮 */
const submitForm = () => {
  qcUnqualifiedRecordFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.recordId) {
        await updateQcUnqualifiedRecord(form.value).finally(() =>  buttonLoading.value = false);
      } else {
        await addQcUnqualifiedRecord(form.value).finally(() =>  buttonLoading.value = false);
      }
      proxy?.$modal.msgSuccess("操作成功");
      dialog.visible = false;
      await getList();
    }
  });
}

/** 删除按钮操作 */
const handleDelete = async (row?: QcUnqualifiedRecordVO) => {
  const _recordIds = row?.recordId || ids.value;
  await proxy?.$modal.confirm('是否确认删除不合格品评审记录编号为"' + _recordIds + '"的数据项？').finally(() => loading.value = false);
  await delQcUnqualifiedRecord(_recordIds);
  proxy?.$modal.msgSuccess("删除成功");
  await getList();
}

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('qms/qcUnqualifiedRecord/export', {
    ...queryParams.value
  }, `qcUnqualifiedRecord_${new Date().getTime()}.xlsx`)
}

onMounted(() => {
  getList();
});
</script>
