<template>
    <div class="product-sub">
        <el-row :gutter="10" style="margin-bottom: 8px">
            <el-col :span="1.5">
                <el-button plain type="primary" icon="el-icon-plus" size="small" @click="handleAdd" v-hasPermi="['productModbus:gateway:add']">{{ $t('product.product-sub.3843945-0') }}</el-button>
            </el-col>
            <el-col :span="1.5" v-if="!isSet">
                <el-button plain icon="el-icon-edit" size="small" @click="setSubDeviceAddress">{{ $t('product.product-sub.3843945-3') }}</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button plain icon="el-icon-delete" size="small" @click="handleDelete" v-hasPermi="['productModbus:gateway:remove']" :disabled="multiple">
                    {{ $t('product.product-sub.3843945-1') }}
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <span style="font-size: 12px; line-height: 32px; color: #ffb032">{{ $t('product.product-sub.3843945-2') }}</span>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" :search="false" @queryTable="getList">
                <template>
                    <div style="margin-right: 10px" v-if="isSet">
                        <el-button plain type="primary" size="small" @click="saveSetting" v-hasPermi="['productModbus:gateway:edit']">{{ $t('save') }}</el-button>
                        <el-button plain type="info" size="small" @click="cancelSetting">{{ $t('cancel') }}</el-button>
                    </div>
                </template>
            </right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="subProductList" @selection-change="handleSelectionChange" :border="false">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column :label="$t('product.product-sub.3843945-4')" align="left" prop="subProductName" min-width="160" />
            <!-- 子设备地址 -->
            <el-table-column :label="$t('product.product-sub.3843945-5')" align="center" prop="slaveId" width="200px">
                <template slot-scope="scope">
                    <el-input style="width: 100%; text-align: center" :disabled="!isSet" v-model="scope.row.slaveId" size="small" :placeholder="$t('product.product-sub.3843945-6')"></el-input>
                </template>
            </el-table-column>
            <!-- <el-table-column label="在线状态" align="center" prop="status">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.iot_device_status" :value="scope.row.status" />
                </template>
            </el-table-column> -->
            <el-table-column :label="$t('creatTime')" align="center" prop="createTime" width="120">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column fixed="right" :label="$t('product.product-sub.3843945-7')" align="center" width="80">
                <template slot-scope="scope">
                    <el-button size="small" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['productModbus:gateway:remove']">{{ $t('product.product-sub.3843945-8') }}</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" />

        <!-- 子产品列表 -->
        <subProductList ref="subDeviceList" :gateway="gateway" @addSuccess="addSuccess"></subProductList>
    </div>
</template>

<script>
import { gatewayProductList, delProductGateway, editProductGatewayBatch } from '@/api/iot/gateway';

import subProductList from './sub-product-list';

export default {
    name: 'product-sub',
    props: {
        product: {
            type: Object,
            default: null,
        },
    },
    components: {
        subProductList,
    },
    dicts: ['iot_device_status'],
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 产品表格数据
            subProductList: [],
            productInfo: {},
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
            },
            // 表单参数
            form: {},
            gateway: {},
            //是否可设置子设备地址
            isSet: false,
        };
    },

    watch: {
        product: {
            handler(newVal) {
                this.productInfo = newVal;
                if (this.productInfo && this.productInfo.productId != 0) {
                    this.gateway.gwProductId = this.productInfo.productId;
                    this.queryParams.gwProductId = this.productInfo.productId;
                    this.getList();
                }
            },
        },
    },
    mounted() {
        const productId = this.product.productId;
        if (productId) {
            this.queryParams.gwProductId = this.product.productId;
            this.gateway.gwProductId = this.product.productId;
            this.getList();
        }
    },
    methods: {
        /** 查询子产品列表 */
        getList() {
            // this.loading = true;
            gatewayProductList(this.queryParams).then((response) => {
                this.subProductList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                gwDeviceId: null,
                subDeviceId: null,
                slaveId: null,
                createBy: null,
                createTime: null,
                updateBy: null,
                updateTime: null,
                remark: null,
            };
            this.resetForm('form');
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm('queryForm');
            this.getList();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.id);
            this.single = selection.length !== 1;
            this.multiple = !selection.length;
        },

        /** 新增按钮操作 */
        handleAdd() {
            //刷新子组件
            this.$refs.subDeviceList.openDeviceList = true;
            this.$refs.subDeviceList.getList();
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const productIds = row.id || this.ids;
            this.$modal
                .confirm(this.$t('product.product-sub.3843945-9', [productIds]))
                .then(function () {
                    return delProductGateway(productIds);
                })
                .then(() => {
                    this.getList();
                    this.$modal.msgSuccess(this.$t('product.product-sub.3843945-10'));
                })
                .catch(() => {});
        },
        /**设置子设备地址*/
        setSubDeviceAddress() {
            this.$confirm(this.$t('product.product-sub.3843945-11'), this.$t('product.product-sub.3843945-12'), {
                confirmButtonText: this.$t('product.product-sub.3843945-13'),
                cancelButtonText: this.$t('product.product-sub.3843945-14'),
                type: 'warning',
            })
                .then(() => {
                    this.isSet = !this.isSet;
                    //跳转modbus配置页签
                })
                .catch(() => {
                    this.$message({
                        type: 'info',
                        message: this.$t('product.product-sub.3843945-15'),
                    });
                });
        },
        /**保存子设备地址设置*/
        saveSetting() {
            this.isSet = !this.isSet;
            editProductGatewayBatch(this.subProductList).then((response) => {
                this.getList();
                this.$modal.msgSuccess(this.$t('saveSuccess'));
            });
        },
        cancelSetting() {
            this.isSet = !this.isSet;
        },
        /**添加成功 */
        addSuccess() {
            this.getList();
        },
    },
};
</script>

<style lang="scss" scoped>
.product-sub {
    margin-bottom: 20px;
}
</style>
