<template>
    <div style="padding: 6px">
        <el-card v-show="showSearch" style="margin-bottom: 6px">
            <el-form ref="queryForm" :inline="true" :model="queryParams" label-width="68px"
                style="margin-bottom: -20px">
                <el-form-item :label="$t('product.product-edit.473153-45')" prop="templateName">
                    <el-input v-model="queryParams.templateName" clearable placeholder="请输入模板名称" size="small"
                        @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item>
                    <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">{{ $t('search')
                    }}</el-button>
                    <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('reset')
                        }}</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="main-card">
            <div class="card-toolbar mb8">
                <el-button type="primary" size="mini" @click="handleAdd" v-hasPermi="['iot:temp:add']"
                    icon="el-icon-plus">{{ $t('add') }}</el-button>
            </div>
            <el-table class="base-table" v-loading="loading" :data="tempList" @selection-change="handleSelectionChange">
                <el-table-column :label="$t('product.product-edit.473153-45')" align="left" prop="templateName" />
                <el-table-column :label="$t('product.product-edit.473153-52')" align="left" prop="pollingMethod">
                    <template slot-scope="scope">
                        <dict-tag :options="dict.type.data_collect_type" :value="scope.row.pollingMethod" />
                    </template>
                </el-table-column>
                <el-table-column :label="$t('index.VarTemp.20250324-01')" align="left" prop="slaveTotal,pointTotal">
                    <template slot-scope="scope">{{ scope.row.slaveTotal }}/{{ scope.row.pointTotal }}</template>
                </el-table-column>
                <el-table-column :label="$t('updateTime')" align="left" prop="createTime" />
                <el-table-column :label="$t('opation')" align="center" width="320">
                    <template slot-scope="scope">
                        <el-button type="primary" plain style="padding: 5px" size="small" icon="el-icon-cpu"
                            @click="editForm(scope.row)" v-hasPermi="['iot:temp:edit']">{{
                                $t('index.VarTemp.20250324-02') }}</el-button>
                        <el-button type="primary" plain style="padding: 5px" size="small" icon="el-icon-cpu"
                            @click="gatewayEdit(scope.row)" v-hasPermi="['iot:temp:edit']">{{
                                $t('index.VarTemp.20250324-03') }}</el-button>
                        <el-button type="primary" plain style="padding: 5px" size="small" icon="el-icon-edit"
                            @click="handleUpdate(scope.row)" v-hasPermi="['iot:temp:edit']">{{ $t('update')
                            }}</el-button>
                        <el-button type="danger" plain style="padding: 5px" size="small" icon="el-icon-delete"
                            @click="handleDelete(scope.row)" v-hasPermi="['iot:temp:remove']">{{ $t('del')
                            }}</el-button>
                    </template>
                </el-table-column>
            </el-table>

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

            <!-- 添加或修改设备采集变量模板对话框 -->
            <el-dialog :title="title" style="font-size: medium" :visible.sync="open" width="550px" append-to-body>
                <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                    <el-form-item :label="$t('alert.index.236501-28')" prop="templateName" size="small">
                        <el-input v-model="form.templateName" :placeholder="$t('index.VarTemp.20250324-06')" />
                    </el-form-item>
                    <el-form-item :label="$t('product.product-edit.473153-52')" prop="pollingMethod">
                        <el-radio-group v-model="form.pollingMethod" @change="typeChange(form.pollingMethod)">
                            <el-radio v-for="dict in dict.type.data_collect_type" :key="dict.value"
                                :label="dict.value">{{
                                    dict.label }}</el-radio>
                        </el-radio-group>
                    </el-form-item>
                </el-form>
                <div slot="footer" align="center" class="dialog-footer" v-if="form.isAdd">
                    <el-button type="primary" @click="submitForm">{{ $t('index.VarTemp.20250324-07') }}</el-button>
                </div>
                <div slot="footer" class="dialog-footer" v-else>
                    <el-button @click="cancel">{{ $t('cancel') }}</el-button>
                    <el-button type="primary" @click="submitForm">{{ $t('confirm') }}</el-button>
                </div>
            </el-dialog>
        </el-card>
    </div>
</template>

<script>
import { listTemp, getTemp, delTemp, addTemp, updateTemp } from '@/api/iot/temp';

export default {
    name: 'Temp',
    dicts: ['data_collect_type'],
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 设备采集变量模板表格数据
            tempList: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                templateName: null,
                type: null,
                pollingMethod: null,
                slaveTotal: null,
                pointTotal: null,
                share: null,
                userId: null,
            },
            // 表单参数
            form: {
                pollingMethod: '0',
                //是否新增
                isAdd: true,
            },
            // 表单校验
            rules: {
                templateName: [
                    {
                        required: true,
                        message: '模板名称不能为空',
                        trigger: 'blur',
                    },
                ],
                pollingMethod: [
                    {
                        required: true,
                        message: '采集方式',
                        trigger: 'blur',
                    },
                ],
            },
        };
    },
    created() {
        this.getList();
    },
    methods: {
        /** 查询设备采集变量模板列表 */
        getList() {
            this.loading = true;
            listTemp(this.queryParams).then((response) => {
                this.tempList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                templateId: null,
                templateName: null,
                type: null,
                pollingMethod: '0',
                slaveTotal: null,
                pointTotal: null,
                share: null,
                createTime: null,
                createBy: null,
                updateTime: null,
                updateBy: null,
                userId: null,
            };
            this.resetForm('form');
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm('queryForm');
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.templateId);
            this.single = selection.length !== 1;
            this.multiple = !selection.length;
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = this.$t('index.VarTemp.20250324-04');
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const templateId = row.templateId || this.ids;
            getTemp(templateId).then((response) => {
                this.form = response.data;
                this.form.pollingMethod = this.form.pollingMethod + '';
                this.open = true;
                this.form.isAdd = false;
                this.title = this.$t('index.VarTemp.20250324-05');
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs['form'].validate((valid) => {
                if (valid) {
                    if (this.form.templateId != null) {
                        updateTemp(this.form).then((response) => {
                            this.$modal.msgSuccess('修改成功');
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addTemp(this.form).then((response) => {
                            this.$modal.msgSuccess('新增成功');
                            this.open = false;
                            this.$router.push({
                                path: this.form.pollingMethod == 0 ? '/iot/varTemp-edit/point/' : '/iot/varTemp-edit/mjpoint/',
                                query: {
                                    templateId: response.data,
                                    templateName: this.form.templateName,
                                    pollingMethod: this.form.pollingMethod,
                                    share: this.form.share,
                                    isOpen: 1,
                                },
                            });
                        });
                    }
                }
            });
        },

        /** 编辑按钮 */
        editForm(row) {
            this.$router.push({
                path: row.pollingMethod === 0 ? '/iotdev/template/point/' : '/iotdev/template/mjpoint/',
                query: {
                    templateId: row.templateId,
                    templateName: row.templateName,
                    pollingMethod: row.pollingMethod,
                    share: row.share,
                    isOpen: 2,
                },
            });
        },
        //网关配置
        gatewayEdit(row) {
            this.$router.push({
                path: '/iotdev/template/gateway-things-model/',
                query: {
                    templateId: row.templateId,
                    templateName: row.templateName,
                    pollingMethod: row.pollingMethod,
                    share: row.share,
                    isOpen: 2,
                },
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const templateIds = row.templateId || this.ids;
            this.$modal
                .confirm('是否确认删除变量模板编号为"' + templateIds + '"的数据项？')
                .then(function () {
                    return delTemp(templateIds);
                })
                .then(() => {
                    this.getList();
                    this.$modal.msgSuccess('删除成功');
                })
                .catch(() => { });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download(
                'iot/temp/export',
                {
                    ...this.queryParams,
                },
                `temp_${new Date().getTime()}.xlsx`
            );
        },
        typeChange(type) {
            this.form.pollingMethod = type;
        },
    },
};
</script>
