<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="90px">
            <el-form-item label="焚烧炉名称" prop="incineratorName">
                <el-input v-model="queryParams.incineratorName" 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-button type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
				<el-button type="warning" plain icon="Download" @click="handleExport">导出</el-button>
            </el-form-item>
			
        </el-form>
        <el-card shadow="never" :body-style="{padding: '0px'}">
            <el-table v-loading="loading" :data="incineList" 
				:header-cell-style="{ color: 'var(--white)',background: '#30B08E' }">
                <el-table-column label="序号" align="center" type="index" width="50" />
                <el-table-column label="船舶编号" align="center" prop="sn" show-overflow-tooltip />
				<el-table-column label="焚烧炉编号" align="center" prop="incineratorSn" show-overflow-tooltip />
				<el-table-column label="焚烧炉名称" align="center" prop="incineratorName"></el-table-column>
                <el-table-column label="初始地址" align="center" prop="incineratorLocation"/>
				<el-table-column label="运行时间" align="center" key="incineratorRuntime">
					<template #default="scope">
						{{scope.row.incineratorRuntime}} <span v-if="scope.row.incineratorRuntime">h</span>
					</template>
				</el-table-column>	
				<el-table-column label="状态" align="center" key="incineratorStatus">
					<template #default="scope">
						<span class="text-theme" v-if="scope.row.incineratorStatus == 10">运行中</span>
						<span class="text-red"  v-if="scope.row.incineratorStatus == 20">停机中</span>
					</template>
				</el-table-column>	
                <el-table-column label="备注" align="center" prop="remark" />
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200"
                    fixed="right">
                    <template #default="scope">
                        <el-button link type="success" icon="Edit" @click="handleUpdate(scope.row)">
							修改
                        </el-button>
                        <el-button link type="danger" icon="Delete" @click="handleDelete(scope.row)">
							删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>

        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改证书对话框 -->
        <el-dialog :title="title" v-model="open" width="500px" append-to-body>
            <el-form ref="incinerRef" :model="form" :rules="rules" label-width="120px">
                <el-form-item label="船舶编号" prop="sn">
                    <el-input v-model="form.sn" placeholder="请输入船舶编号" />
                </el-form-item>
				<el-form-item label="焚烧炉编号" prop="incineratorSn">
				    <el-input v-model="form.incineratorSn" placeholder="请输入焚烧炉编号" />
				</el-form-item>
				<el-form-item label="焚烧炉名称" prop="incineratorName">
				    <el-input v-model="form.incineratorName" placeholder="请输入焚烧炉名称" />
				</el-form-item>
				<el-form-item label="初始运行时间" prop="incineratorRuntime">
				    <el-input v-model="form.incineratorRuntime" placeholder="请输入初始运行时间" />
				</el-form-item>
				<el-form-item label="初始地址" prop="incineratorLocation">
				    <el-input v-model="form.incineratorLocation" placeholder="请输入初始地址" />
				</el-form-item>
				<el-form-item label="状态">
					<el-radio-group v-model="form.incineratorStatus">
						<el-radio v-for="dict in incinerator_state	" :key="dict.value" :label="dict.value">
							{{dict.label}}
						</el-radio>
					</el-radio-group>
				</el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="form.remark" type="textarea" :rows="3" placeholder="请输入备注" />
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script setup name="Certificate">
import { listIncinerator, getIncinerator, addIncinerator, updateIncinerator, delIncinerator, downloadIncinerator } from "@/api/Incinerator/incineratorApi";
const { proxy } = getCurrentInstance();
const { incinerator_state } = proxy.useDict("incinerator_state");
const incineList = ref([]);
const open = ref(false);
const loading = ref(true);
const total = ref(0);
const title = ref("");
const data = reactive({
    form: {},
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        incineratorName: null
    },
    rules: {
        incineratorSn: [{
            required: true, message: "焚烧炉不能为空", trigger: "blur"
         }],
        incineratorName: [{
            required: true, message: "传感器名称不能为空", trigger: "blur"
        }],
        sensorPath: [{
            required: true, message: "传感器路径不能为空", trigger: "blur"
        }],
    }
})
    ;

const { queryParams, form, rules } = toRefs(data);
const baseUrl = import.meta.env.VITE_APP_BASE_API;

/** 查询证书列表 */
function getList() {
    loading.value = true;
    listIncinerator(queryParams.value).then(response => {
        incineList.value = response.rows;
        total.value = response.total;
        loading.value = false;
    });
}

function addList() {
    loading.value = true;
    listIncinerator(queryParams.value)
        .then(response => {
            incineList.value.push(...response.rows);
            total.value = response.total;
            if (incineList.value.length >= total.value) finished.value = true;
            loading.value = false;
        })
        .catch(response => {
            loading.value = false;
        });
}

// 取消按钮
function cancel() {
    open.value = false;
    reset();
}

// 表单重置
function reset() {
    form.value = {
		id: undefined,
		sn: null,
		incineratorSn: null,
		incineratorName: null,
		incineratorRuntime: null,
		incineratorLocation: null,
		incineratorStatus: "10",
		remark: null
    };
    proxy.resetForm("incinerRef");
}
/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
}

/** 重置按钮操作 */
function resetQuery() {
    proxy.resetForm("queryRef");
    handleQuery();
}
/** 新增按钮操作 */
function handleAdd() {
    reset();
    open.value = true;
    title.value = "添加传感器";
}

/** 修改按钮操作 */
function handleUpdate(row) {
    reset();
    const ids = row.id || ids.value
    getIncinerator(ids).then(response => {
        form.value = response.data;
        open.value = true;
        title.value = "修改传感器";
    });
}

/** 提交按钮 */
function submitForm() {
    proxy.$refs["incinerRef"].validate(valid => {
        if (valid) {
            if (form.value.id != null) {
                updateIncinerator(form.value).then(response => {
                    proxy.$modal.msgSuccess("修改成功");
                    open.value = false;
                    getList();
                });
            } else {
                addIncinerator(form.value).then(response => {
                    proxy.$modal.msgSuccess("新增成功");
                    open.value = false;
                    getList();
                });
            }
        }
    });
}

/** 删除按钮操作 */
function handleDelete(row) {
    const incineIds = row.id || ids.value;
    proxy.$modal.confirm('是否确认删除传感器为"' + row.incineratorName + '"的数据项？').then(function () {
        return delIncinerator(incineIds);
    }).then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
    }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
    downloadIncinerator(queryParams.value)
}

getList();
</script>

