<template>
    <el-dialog :title="$t('sip.user-list.558539-0')" :visible.sync="open" width="800px">
        <div style="margin-top:-50px;">
            <el-divider></el-divider>
        </div>
        <!--用户数据-->
        <el-form :model="queryParams" ref="queryForm" :rules="rules" :inline="true" label-width="80px">
            <el-form-item :label="$t('sip.user-list.558539-1')" prop="phonenumber">
                <el-input type="text" :placeholder="$t('sip.user-list.558539-2')" v-model="queryParams.phonenumber"
                    minlength="10" clearable size="small" show-word-limit style="width: 240px"
                    @keyup.enter.native="handleQuery"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini"
                    @click="handleQuery">{{ $t('query') }}</el-button>
            </el-form-item>
        </el-form>

        <el-table v-loading="loading" :data="userList" highlight-current-row size="mini"
            @current-change="handleCurrentChange" border>
            <el-table-column :label="$t('select')" width="50" align="center">
                <template slot-scope="scope">
                    <input type="radio" :checked="scope.row.isSelect" name="user" />
                </template>
            </el-table-column>
            <el-table-column :label="$t('sip.user-list.558539-2')" align="center" key="userId" prop="userId"
                width="120" />
            <el-table-column :label="$t('sip.user-list.558539-3')" align="center" key="userName" prop="userName" />
            <el-table-column :label="$t('sip.user-list.558539-4')" align="center" key="nickName" prop="nickName" />
            <el-table-column :label="$t('sip.user-list.558539-1')" align="center" key="phonenumber" prop="phonenumber"
                width="120" />
            <el-table-column :label="$t('sip.user-list.558539-5')" align="center" prop="createTime" width="160">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.createTime) }}</span>
                </template>
            </el-table-column>

        </el-table>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="confirmSelectUser">{{ $t('confirm') }}</el-button>
            <el-button @click="closeSelectUser">{{ $t('close') }}</el-button>
        </div>
    </el-dialog>
</template>

<script>
import {
    listUser
} from "@/api/iot/tool";

export default {
    name: "user-list",
    props: {
        device: {
            type: Array,
            default: null
        }
    },
    watch: {
        // 获取到父组件传递的device
        device: function (newVal, oldVal) {
            this.deviceInfo = newVal;
        }
    },
    data() {
        return {
            // 遮罩层
            loading: false,
            // 选中数组
            ids: [],
            // 弹出层标题
            title: "",
            // 用户列表
            userList: [],
            // 选中的用户
            user: {},
            // 设备信息
            deviceInfo: {},
            // 是否显示选择用户弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                userName: undefined,
                phonenumber: undefined,
                status: 0,
                deptId: undefined
            },
            // 表单校验
            rules: {
                phonenumber: [{
                    required: true,
                    message: this.$t('sip.user-list.558539-6'),
                    trigger: "blur"
                }, {
                    min: 11,
                    max: 11,
                    message: this.$t('sip.user-list.558539-7'),
                    trigger: 'blur'
                }],
            },
        };
    },
    created() { },
    methods: {
        /** 查询用户列表 */
        getList() {
            this.loading = true;
            listUser(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
                this.userList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.$refs["queryForm"].validate(valid => {
                if (valid) {
                    this.queryParams.pageNum = 1;
                    this.getList();
                }
            });

        },
        // 重置查询
        resetQuery() {
            this.$refs["queryForm"].resetFields();
            this.userList = [];
        },
        //设置单选按钮选中
        setRadioSelected(userId) {
            for (let i = 0; i < this.userList.length; i++) {
                if (this.userList[i].userId === userId) {
                    this.userList[i].isSelect = true;
                    this.user = this.userList[i];
                } else {
                    this.userList[i].isSelect = false;
                }
            }
        },
        // 单选数据
        handleCurrentChange(user) {
            if (user != null) {
                this.setRadioSelected(user.userId);
                this.user = user;
            }
        },
        confirmSelectUser() {
            this.$emit('userEvent', this.user);
            this.open = false;
        },
        // 关闭选择用户
        closeSelectUser() {
            this.open = false;
            this.resetQuery();
        }
    }
};
</script>
