<template>
    <el-dialog :title="$t('scene.index.670805-36')" :visible.sync="openDeviceList" width="800px" append-to-body>
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" @submit.native.prevent label-width="48px">
            <el-form-item prop="productName">
                <el-input v-model="queryParams.productName" size="small" placeholder="请输入子产品名称" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">{{ $t('search') }}</el-button>
                <el-button icon="el-icon-refresh" size="small" @click="resetQuery">{{ $t('reset') }}</el-button>
            </el-form-item>
        </el-form>
        <el-table v-loading="loading" :data="gatewayList" size="small" :border="false" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="ID" align="left" prop="productId" width="120" />
            <el-table-column :label="$t('device.device-edit.148398-1')" align="left" prop="productName" />
        </el-table>

        <pagination v-show="total > 0" layout="prev, pager, next" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="handleDeviceSelected">{{ $t('confirm') }}</el-button>
            <el-button @click="closeSelectDeviceList">{{ $t('cancel') }}</el-button>
        </div>
    </el-dialog>
</template>

<script>
import { addProductGatewayBatch } from '@/api/iot/gateway';
import { listProduct } from '@/api/iot/product';

export default {
    name: 'sub-product-list',
    props: {
        gateway: {
            type: Object,
            default: null,
        },
    },
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 网关与子产品关联表格数据
            gatewayList: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            openDeviceList: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                productName: null,
                deviceType: 4,
            },
        };
    },
    created() {},

    watch: {
        gateway: {
            handler() {
                this.queryParams.pageNum = 1;
                this.getList();
            },
            immediate: true,
        },
    },
    methods: {
        /** 查询网关与子产品关联列表 */
        getList() {
            this.loading = true;
            listProduct(this.queryParams).then((response) => {
                this.gatewayList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.resetForm('queryForm');
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.reset();
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.productId);
            this.single = selection.length !== 1;
            this.multiple = !selection.length;
        },
        // 关闭选择产品列表
        closeSelectDeviceList() {
            this.openDeviceList = false;
        },
        // 批量新增子产品
        handleDeviceSelected() {
            this.gateway.subProductIds = this.ids;
            addProductGatewayBatch(this.gateway).then((response) => {
                this.$modal.msgSuccess(this.$t('device.sub-device-list.323213-4'));
                this.openDeviceList = false;
                this.$emit('addSuccess');
            });
        },
    },
};
</script>
