<template>
    <el-dialog v-model="visible" :title="titleMap[mode]" :width="480" :close-on-click-modal="false" @closed="$emit('closed')">
        <el-form ref="formRef" :model="form" :rules="rules" label-width="120">
            <template v-if="props.projectId != 1">
                <el-form-item label="所属项目" prop="projectId">
                    <el-select v-model="form.projectId" filterable placeholder="请选择所属项目" @change="form.mountedId = null">
                        <el-option v-for="item in $TOOL.data.get('PROJECT')" :key="item.fpiId" :label="item.projectName" :value="item.fpiId"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="设备安装点" prop="mountedId">
                    <el-select v-model="form.mountedId" filterable placeholder="请选择设备安装点">
                        <el-option v-for="item in filterMounteds" :key="item.id" :label="item.mountedName" :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
            </template>
            <el-form-item label="监测类型" prop="vtype">
                <el-select v-model="form.vtype" placeholder="请选择监测类型" @change="form.value = null">
                    <el-option v-for="(label, key) in typeDic" :key="key" :label="label" :value="key"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="监测数值" prop="value">
                <el-input-number v-model="form.value" :readonly="!form.vtype" :precision="2" :controls="false" placeholder="请输入监测数值">
                    <template v-if="form.vtype" #suffix>{{ XEUtils.get(unitDic, form.vtype) }}</template>
                </el-input-number>
            </el-form-item>
            <el-form-item label="监测时间" prop="createTime">
                <el-date-picker v-model="form.createTime" type="datetime" :clearable="false" value-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择监测时间"></el-date-picker>
            </el-form-item>
        </el-form>

        <template #footer>
            <el-button :loading="isSaving" type="primary" auto-insert-space @click="submit">保存</el-button>
            <el-button auto-insert-space @click="visible = false">取消</el-button>
        </template>
    </el-dialog>
</template>

<script setup>
import XEUtils from "xe-utils";
import API from "@/api";
import TOOL from "@/utils/tool";
import { typeDic, unitDic } from "@/views/dataMock/scc/main";

const $emit = defineEmits(["success", "closed"]);
const props = defineProps({
    projectId: { type: Number, default: TOOL.data.get("PROJECT_ID") }
});

const visible = ref(false);
const isSaving = ref(false);

const mode = ref("add");
const titleMap = reactive({
    add: "数据录入",
    edit: "修改"
});

const form = ref({
    id: null,
    projectId: props.projectId,
    mountedId: null,
    vtype: null,
    value: null,
    createTime: null,
    features: "{}"
});
const rules = reactive({
    projectId: [{ required: true, message: "请选择所属项目" }],
    mountedId: [{ required: true, message: "请选择设备安装点" }],
    createTime: [{ required: true, message: "请选择监测时间" }],
    vtype: [{ required: true, message: "请选择监测类型" }],
    value: [{ required: true, message: "请输入监测数值" }]
})

const mounteds = ref([]);
const filterMounteds = computed(() => form.value.projectId ? XEUtils.filter(mounteds.value, item => item.projectId == form.value.projectId) : []);
const fetchMounted = async () => {
    const res = await API.scc.mounted.get();
    mounteds.value = res || [];
    if (props.projectId == 1) form.value.mountedId = XEUtils.get(XEUtils.find(res, item => item.projectId == 1), "id");
}

const open = () => {
    visible.value = true;
    fetchMounted();
}
const setData = data => {
    open();
    mode.value = "edit";
    XEUtils.objectEach(form.value, (_, key) => {
        if (key == "value") XEUtils.set(form.value, key, XEUtils.divide(XEUtils.get(data, key), 100));
        else XEUtils.set(form.value, key, XEUtils.get(data, key));
    });
}

const formRef = ref();
const submit = () => {
    formRef.value.validate(valid => {
        if (valid) {
            const data = XEUtils.omit(form.value, "value");
            XEUtils.set(data, "value", XEUtils.multiply(XEUtils.get(form.value, "value"), 100));

            isSaving.value = true;
            API.scc.record[mode.value](data).then(() => {
                isSaving.value = false;
                ElMessage.success("操作成功");
                visible.value = false;
                $emit("success", mode.value);
            }).catch(() => isSaving.value = false);
        } else {
            return false;
        }
    });
}

defineExpose({
    open,
    setData
})
</script>

<style lang="scss" scoped>
.el-form {
    padding-right: calc(var(--el-dialog-padding-primary) + var(--el-message-close-size, 16px));

    .el-input-number {width: 100%;}
    .el-input-number :deep(.el-input__inner) {text-align: unset;}
    .el-input-number :deep(.el-input__suffix) {font-size: 12px;}
}
</style>