<template>
    <div>
        <el-transfer
                class="user"
                filterable
                :filter-method="filterMethod"
                filter-placeholder="请输入用户名称拼音"
                v-model="value"
                :titles="['系统人员列表', '通知人员列表']"
                :data="userdata"
                v-if="!this.showtable">
        </el-transfer>

        <NoticeUserInfo v-if="this.showtable" ref="userinfo" :data="this.value" :noticeuuid="this.uuid"/>
    </div>
</template>

<script>
    import NoticeApi from "@/api/base/sysm/NoticeApi";
    import PageTable from "@/libs/PageTable";
    import NoticeUserInfo from "@/module/sysm/NoticeUserInfo";

    export default {
        name: "NoticeUserMgr",
        components: {NoticeApi,NoticeUserInfo},
        props: {
            uuid:"",
            uuids:[]
        },
        data() {
            return {
                pageTable: new PageTable({
                    autoLoad: true,
                    doLoad: NoticeApi.doQuery,
                    param: {},
                    log: false
                }),
                userdata:[],
                value: [],
                showtable: false,
            };
        },
        async mounted() {
            if(this.uuid){
                await this.getNoticeUsers();
            }
            await this.generateUserData();
        },
        watch: {},
        methods: {
            // 用户检索条件
            filterMethod(query, item) {
                return item.pinyin.indexOf(query) > -1;
            },
            // 获取所有系统用户
            async generateUserData(){
                var data_ = [];
                var users = [];
                await NoticeApi.getUserList().then(resp=>{
                    users = resp.datarows;
                });
                users.forEach((user, index) => {
                    data_.push({
                        label: user.userid,
                        key: user.uuid,
                        pinyin: users[index].userid
                    });
                });
                this.userdata = data_;
            },
            // 获取该通知下的用户
            async getNoticeUsers(){
                await NoticeApi.getNoticeUsers(this.uuid).then(resp=>{
                    this.value = resp.data;
                });
            },
            // 提交保存通知人员
            async submitNoticeUser(){
                if(this.value.length<1){
                    this.$message({
                        showClose: true,
                        message: '通知人员列表不可为空，请设置！',
                        type: 'warning'
                    });
                }else{
                    await this.confirm(null,"将默认使用系统用户管理设置通知方式，是否确认保存？");
                    var params = {};
                    params.uuid = this.uuid;
                    params.users = this.value;
                    await NoticeApi.doSaveNoticeUser(params);
                    this.$message({
                        showClose: true,
                        message: '保存成功',
                        type: 'success'
                    });
                }
            },
            // 批量设置通知用户
            async submitNoticesUser(){
                if(this.value.length<1){
                    this.$message({
                        showClose: true,
                        message: '通知人员列表不可为空，请设置！',
                        type: 'warning'
                    });
                }else{
                    await this.confirm(null,"此操作会修改所有通知相关人员，默认使用系统用户管理设置通知方式，您确定这样做？");
                    var params = {};
                    params.uuids = this.uuids;
                    params.users = this.value;
                    await NoticeApi.doSeleNoticeUser(params);
                    this.$message({
                        showClose: true,
                        message: '保存成功',
                        type: 'success'
                    });
                }
            },
            showNotyiceUserInfo(){
                if(this.value.length<1){
                    this.$message({
                        showClose: true,
                        message: '通知人员列表不可为空，请设置！',
                        type: 'warning'
                    });
                    return false;
                }else{
                    this.showtable = true;
                    return true;
                }
            },
            // 关闭人员信息设置页面后 重新查询穿梭框
            async closeNotyiceUserInfoAndQuery(){
                this.showtable = false;
                if(this.uuid){
                    await this.getNoticeUsers();
                }
                await this.generateUserData();
            },
            // 单纯切换显示
            async closeNotyiceUserInfo(){
                this.showtable = false;
            },
            // 保存单个通知人员信息
            async submitNoticeUserInfo(){
                // 保存
                const param = {};
                var data = this.$refs.userinfo.pageTable.data;
                data.forEach(d=>{
                    d.userId = d.userid;
                    d.userName = d.username;
                    d.openEmail = d.openemail;
                    d.openMobl = d.openmobl;
                    d.openPhon = d.openphon;
                })
                param.users = data;
                let arrayUuid = [];
                arrayUuid.push(this.uuid);
                param.uuids = arrayUuid;
                await NoticeApi.submitNoticeUserInfo(param);
                // 关闭人员信息列表 显示穿梭框
                await this.closeNotyiceUserInfoAndQuery();
            },
            // 保存批量通知人员信息
            async submitNoticesUsersInfo(){
                // 保存
                const param = {};
                var data = this.$refs.userinfo.pageTable.data;
                data.forEach(d=>{
                    d.userId = d.userid;
                    d.userName = d.username;
                    d.openEmail = d.openemail;
                    d.openMobl = d.openmobl;
                    d.openPhon = d.openphon;
                })
                param.users = data;
                param.uuids = this.uuids;
                await NoticeApi.submitNoticeUserInfo(param);
                // 保存后就直接关闭页面了 这个再次查询没有意义 也查询不出结果集
                this.showtable = false;
            }

        }
    };
</script>
<style lang="scss">
    .user .el-input{
        width: 80% !important;
    }
</style>

