<template>
|
<view class="account-detail">
|
<PageHeader :title="pageTitle" @back="goBack" />
|
<up-form :model="form" label-width="110">
|
<up-form-item label="设施编号" required>
|
<up-input v-model="form.facilityCode" placeholder="请输入" clearable />
|
</up-form-item>
|
<up-form-item label="设施名称" required>
|
<up-input v-model="form.facilityName" placeholder="请输入" clearable />
|
</up-form-item>
|
<up-form-item label="设施类型" required>
|
<up-input :model-value="form.facilityType" placeholder="请选择" readonly @click="showTypeSheet = true" />
|
<template #right>
|
<up-icon name="arrow-right" @click="showTypeSheet = true"></up-icon>
|
</template>
|
</up-form-item>
|
<up-form-item label="规格型号">
|
<up-input v-model="form.facilitySpec" placeholder="请输入" clearable />
|
</up-form-item>
|
<up-form-item label="安装位置" required>
|
<up-input v-model="form.installLocation" placeholder="请输入" clearable />
|
</up-form-item>
|
<up-form-item label="安装时间">
|
<up-input :model-value="form.installTime" placeholder="请选择" readonly @click="showInstallPicker = true" />
|
<template #right>
|
<up-icon name="arrow-right" @click="showInstallPicker = true"></up-icon>
|
</template>
|
</up-form-item>
|
<up-form-item label="生产日期">
|
<up-input :model-value="form.productionDate" placeholder="请选择" readonly @click="showProductionPicker = true" />
|
<template #right>
|
<up-icon name="arrow-right" @click="showProductionPicker = true"></up-icon>
|
</template>
|
</up-form-item>
|
<up-form-item label="有效期(天)">
|
<up-input v-model="form.validPeriod" type="number" placeholder="请输入" clearable />
|
</up-form-item>
|
<up-form-item label="下次检查时间">
|
<up-input :model-value="nextCheckTime" placeholder="根据安装时间+有效期自动计算" disabled />
|
</up-form-item>
|
<up-form-item label="状态">
|
<up-input :model-value="form.status" placeholder="请选择" readonly @click="showStatusSheet = true" />
|
<template #right>
|
<up-icon name="arrow-right" @click="showStatusSheet = true"></up-icon>
|
</template>
|
</up-form-item>
|
<up-form-item label="备注">
|
<up-textarea v-model="form.remark" placeholder="请输入" auto-height />
|
</up-form-item>
|
</up-form>
|
|
<FooterButtons :loading="loading" confirmText="保存" @cancel="goBack" @confirm="handleSubmit" />
|
|
<up-action-sheet :show="showTypeSheet" title="选择设施类型" :actions="typeActions" @select="onSelectType" @close="showTypeSheet = false" />
|
<up-action-sheet :show="showStatusSheet" title="选择状态" :actions="statusActions" @select="onSelectStatus" @close="showStatusSheet = false" />
|
|
<up-datetime-picker :show="showInstallPicker" mode="date" :value="installValue" @confirm="onInstallConfirm" @cancel="showInstallPicker = false" />
|
<up-datetime-picker :show="showProductionPicker" mode="date" :value="productionValue" @confirm="onProductionConfirm" @cancel="showProductionPicker = false" />
|
</view>
|
</template>
|
|
<script setup>
|
import { computed, ref } from "vue";
|
import { onLoad } from "@dcloudio/uni-app";
|
import dayjs from "dayjs";
|
import PageHeader from "@/components/PageHeader.vue";
|
import FooterButtons from "@/components/FooterButtons.vue";
|
import { addFacilityLedger, updateFacilityLedger } from "@/api/safeProduction/safetyFacility";
|
|
const loading = ref(false);
|
|
const form = ref({
|
id: null,
|
facilityCode: "",
|
facilityName: "",
|
facilityType: "",
|
facilitySpec: "",
|
installLocation: "",
|
installTime: "",
|
productionDate: "",
|
validPeriod: "",
|
nextCheckTime: "",
|
status: "正常",
|
remark: "",
|
});
|
|
const showTypeSheet = ref(false);
|
const showStatusSheet = ref(false);
|
const showInstallPicker = ref(false);
|
const showProductionPicker = ref(false);
|
|
const typeActions = [
|
{ name: "消防器材", value: "消防器材" },
|
{ name: "安全设备", value: "安全设备" },
|
{ name: "防护用品", value: "防护用品" },
|
{ name: "监控设备", value: "监控设备" },
|
];
|
const statusActions = [
|
{ name: "正常", value: "正常" },
|
{ name: "异常", value: "异常" },
|
{ name: "报废", value: "报废" },
|
];
|
|
const pageTitle = computed(() => (form.value.id ? "编辑设施" : "新增设施"));
|
|
const nextCheckTime = computed(() => {
|
const installTime = form.value.installTime;
|
const validPeriod = Number(form.value.validPeriod);
|
if (!installTime || !Number.isFinite(validPeriod) || validPeriod <= 0) return "";
|
const d = dayjs(installTime);
|
if (!d.isValid()) return "";
|
return d.add(validPeriod, "day").format("YYYY-MM-DD");
|
});
|
|
const installValue = computed(() => {
|
if (!form.value.installTime) return Date.now();
|
const ts = dayjs(form.value.installTime).valueOf();
|
return Number.isFinite(ts) ? ts : Date.now();
|
});
|
const productionValue = computed(() => {
|
if (!form.value.productionDate) return Date.now();
|
const ts = dayjs(form.value.productionDate).valueOf();
|
return Number.isFinite(ts) ? ts : Date.now();
|
});
|
|
const goBack = () => {
|
uni.navigateBack();
|
};
|
|
const onSelectType = action => {
|
form.value.facilityType = action.value;
|
showTypeSheet.value = false;
|
};
|
const onSelectStatus = action => {
|
form.value.status = action.value;
|
showStatusSheet.value = false;
|
};
|
|
const onInstallConfirm = e => {
|
form.value.installTime = dayjs(e?.value).format("YYYY-MM-DD");
|
showInstallPicker.value = false;
|
};
|
const onProductionConfirm = e => {
|
form.value.productionDate = dayjs(e?.value).format("YYYY-MM-DD");
|
showProductionPicker.value = false;
|
};
|
|
const handleSubmit = () => {
|
const facilityCode = String(form.value.facilityCode || "").trim();
|
if (!facilityCode) {
|
uni.showToast({ title: "请输入设施编号", icon: "none" });
|
return;
|
}
|
const facilityName = String(form.value.facilityName || "").trim();
|
if (!facilityName) {
|
uni.showToast({ title: "请输入设施名称", icon: "none" });
|
return;
|
}
|
const facilityType = String(form.value.facilityType || "").trim();
|
if (!facilityType) {
|
uni.showToast({ title: "请选择设施类型", icon: "none" });
|
return;
|
}
|
const installLocation = String(form.value.installLocation || "").trim();
|
if (!installLocation) {
|
uni.showToast({ title: "请输入安装位置", icon: "none" });
|
return;
|
}
|
|
const payload = {
|
...form.value,
|
facilityCode,
|
facilityName,
|
facilityType,
|
facilitySpec: String(form.value.facilitySpec || "").trim(),
|
installLocation,
|
validPeriod: form.value.validPeriod === "" ? null : Number(form.value.validPeriod),
|
nextCheckTime: nextCheckTime.value || null,
|
remark: String(form.value.remark || "").trim(),
|
};
|
|
loading.value = true;
|
const api = payload.id ? updateFacilityLedger : addFacilityLedger;
|
api(payload)
|
.then(() => {
|
uni.showToast({ title: "保存成功", icon: "success" });
|
uni.$emit("safetyFacility:refresh");
|
goBack();
|
})
|
.catch(() => {
|
uni.showToast({ title: "保存失败", icon: "none" });
|
})
|
.finally(() => {
|
loading.value = false;
|
});
|
};
|
|
onLoad(options => {
|
if (options?.data) {
|
try {
|
const row = JSON.parse(decodeURIComponent(options.data));
|
form.value = { ...form.value, ...(row || {}) };
|
} catch (e) {}
|
}
|
});
|
</script>
|
|
<style scoped lang="scss">
|
@import "@/static/scss/form-common.scss";
|
</style>
|