<template>
    <div class="page">
        <div class="top adfac">
            <img src="@/assets/images/agent/arrow_left.png" @click="handleBack">
            <div class="spans">
                <span>问卷调研</span>
                <span>&nbsp;/&nbsp;问卷列表</span>
                <span class="last">&nbsp;/&nbsp;发布问卷</span>
            </div>
        </div>
        <div class="content">
            <div class="c_top adfacjb">
                <span>{{ title }}</span>
                <el-button type="primary" @click="reviewWj">预览问卷</el-button>
            </div>
            <div class="c_set">
                <div class="cs_title" style="margin-top: 0;">选择项目成员</div>
                <div class="cs_users adf">
                    <div class="cu_l adfacjb">
                        <img src="@/assets/images/agent/users_mini.png">
                        <div class="cul_add" @click="handleAddUser">+ 添加</div>
                    </div>
                    <div class="cu_r adfac">
                        <div class="cr_item adfacjb" v-for="(item,index) in params.persons" :key="index">
                            <span>{{ item.name }}</span>
                            <img src="@/assets/images/agent/remove_mini.png" @click="handleRemoveUser(item,index)">
                        </div>
                    </div>
                </div>
                <div class="cs_title">问卷回答设置</div>
                <div class="cs_zd adfac">
                    <span>只允许作答</span>
                    <el-input-number v-model="params.answerSetting" :min="1" controls-position="right" style="margin: 0 9px;"></el-input-number>
                    <span>次</span>
                </div>
                <div class="cs_title">问卷作答时间设置</div>
                <div class="cs_time">
                    <div class="ct_pre adfac">
                        <span>开始时间</span>
                        <el-date-picker v-model="params.startTime" type="datetime" placeholder="选择开始时间" style="width:311px;" @change="e=>handleChangeDatetime(e,'startTime')"></el-date-picker>
                    </div>
                    <div class="ct_pre adfac">
                        <span>截止时间</span>
                        <el-date-picker v-model="params.endTime" type="datetime" placeholder="选择截止时间" style="width:311px;" @change="e=>handleChangeDatetime(e,'endTime')"></el-date-picker>
                    </div>
                </div>
                <div class="cs_btn adfac">
                    <el-button :loading="!canSubmit" type="primary" @click="handleConfirm">确认</el-button>
                    <el-button type="default" @click="handleBack">取消</el-button>
                </div>
            </div>
        </div>
        <el-dialog width="790px" :visible.sync="userShow" title="选择成员" @close="userShow=false">
            <div class="ed_coach adf">
                <div class="ec_l">
                    <el-input placeholder="请输入成员名搜索" suffix-icon="el-icon-search" v-model="userName" style="width: 100%;"></el-input>
                    <template v-if="!checkShow">
                        <div class="ecl_gs">
                            <div class="eg_title">按项目选</div>
                            <div class="egt_item adfacjb" v-for="(item,index) in companyList" :key="index">
                                <div class="ei_l adffc" style="cursor: pointer;">
                                    <div class="adfac">
                                        <span @click="handleChooseUser(item)">{{ item.programName }}</span>
                                        <span></span>
                                    </div>
                                    <div><span style="margin-left: 0;">{{ item.enterpriseName }}-{{ item.teamName }}</span></div>
                                </div>
                                <div class="ei_r" @click="handleChooseUser(item)">
                                    <span>成员</span>
                                </div>
                            </div>
                        </div>
                    </template>
                    <template v-else>
                        <div class="ecl_company">
                            <i class="el-icon-arrow-left" @click="handleUserBack"></i>
                            {{ companyName }}
                        </div>
                        <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAllChange" style="margin-top: 33px;">全选</el-checkbox>
                        <div class="ecl_cbs">
                            <el-checkbox v-model="user.checked" v-for="(user,index) in userList" :key="index" @change="handleChangeUser" style="margin-top: 24px;display: block;">{{ user.name }}</el-checkbox>
                        </div>
                    </template>
                </div>
                <div class="ec_r">
                    <div class="ecr_text">已选择（{{ selectedUsers.length }}/<span>{{ userList.length }}</span>）</div>
                    <div class="ecr_names">
                        <div class="en_pre adfacjc" v-for="(item,index) in selectedUsers" :key="index">
                            <span>{{ item.name }}</span>
                            <img src="@/assets/images/agent/delete_mini.png" @click="handleDeleteUser(item,index)">
                        </div>
                    </div>
                </div>
            </div>
            <div class="demo-drawer__footer" style="display: flex;justify-content: end;margin-top: 20px;">
                <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 认</el-button>
                <el-button @click="cancel" style="margin-right: 5%;">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script setup name="">
    import { ref, getCurrentInstance,onMounted } from 'vue'
    const { proxy } = getCurrentInstance();
    import { getProgramList,getCoachList,sendQuestionnaire } from '@/api/agent/index.js'
    
    const questionnaireId = ref('')
    const title = ref('')
    const type = ref('')
    const params = ref({
        questionnaireId:'',
        programId:'',
        teamId:'',
        userIds:[],
        persons:[],
        memberInfos:[],
        answerSetting:1,
        startTime:'',
        endTime:''
    })
    const typecfg = {
        1: '初级',
        2: '中级',
        3: '高级'
    }
    const userShow = ref(false)
    const checkShow = ref(false)
    const companyName = ref('')
    const userName = ref('')
    const companyList = ref([])
    const userList = ref([])
    const selectedUsers = ref([])
    const checkAll = ref(false)
    const isIndeterminate = ref(false)
    const buttonLoading = ref(false)
    const canSubmit = ref(true)


    const handleAddUser = () => {
        userShow.value = true
        getProgramList({page:1,limit:-1}).then(res=>{
            if(res.code !==0) return proxy.$message({type:'warning',message:res.msg});
            companyList.value = res.data.list.map(l=>{
                return {
                    id:l.id,
                    programName:l.programName,
                    enterpriseName:l.enterpriseName,
                    teamName:l.teamName,
                    teamId:l.teamId
                }
            });
        })
    }

    const handleBack = () => {
        proxy.$router.go(-1)
    }

    const handleRemoveUser = (item,index) => {
        params.value.persons.splice(index,1);
        params.value.userIds = params.value.persons.map(l=>l.id);
    }

    const handleConfirm = () => {
        if(!canSubmit.value) return
        canSubmit.value = false;

        if(params.value.userIds.length === 0){
            proxy.$message({type:'warning',message:'请选择项目成员'});
            canSubmit.value = true;
            return;
        }
        if(!params.value.startTime || !params.value.endTime){
            proxy.$message({type:'warning',message:'请设置问卷作答时间'});
            canSubmit.value = true;
            return;
        }

        params.value.memberInfos = params.value.persons.map(l=>{
            return {
                userId:l.id,
                realName:l.name
            }
        })
        let { persons ,userIds ,...paras } = params.value;
        let data = {...paras};
        sendQuestionnaire(data).then(res=>{
            if(res.code!==0){
                canSubmit.value = true;
                return proxy.$message({type:'warning',message:res.msg});
            } 
            proxy.$message({type:'success',message:'问卷已发布！'});
            setTimeout(()=>{proxy.$router.go(-1)},1500)
            canSubmit.value = true;
        })
    }

    const handleChooseUser = (item) => {
        params.value.programId = item.id;
        params.value.teamId = item.teamId;
        getCoachList({teamId:item.teamId,page:1,limit:-1}).then(res=>{
            if(res.code!==0) return proxy.$message({type:'warning',message:res.msg});
            userList.value = res.data.list.map(l=>{
                return {id:l.id,name:l.realName,checked:false}
            })
        })

        checkShow.value = true
        companyName.value = item.programName
    }
    const handleUserBack = () => {
        checkShow.value = false
        companyName.value = ''
    }

    const handleChangeDatetime = (e,key) => {
        params.value[key] = proxy.parseTime(new Date(e), '{yy}-{mm}-{dd} {hh}:{ii}:{ss}');
    }


    const submitForm = () => {
        if(selectedUsers.value.length===0) return proxy.$message.warning('请至少选择一位成员')
        params.value.persons = JSON.parse(JSON.stringify(selectedUsers.value));
        params.value.userIds = params.value.persons.map(l=>l.id);
        checkAll.value = false;
        isIndeterminate.value = false;
        selectedUsers.value = [];
        userList.value = [];
        checkShow.value = false;
        userShow.value = false;
    }
    const cancel = () => {
        userShow.value = false;
    }

    const handleCheckAllChange = (val) => {
        selectedUsers.value = val ? JSON.parse(JSON.stringify(userList.value)) : [];
        isIndeterminate.value = false;
        userList.value.forEach(item => item.checked = val)
    }
    const handleChangeUser = () => {
        let trues = userList.value.filter(item => item.checked).length;
        if(trues>0&&trues<userList.value.length) isIndeterminate.value = true;
        else isIndeterminate.value = false;
        if(trues===userList.value.length) checkAll.value = true;
        else checkAll.value = false;

        selectedUsers.value = userList.value.filter(item => item.checked);
    }
    const handleDeleteUser = (item,index) => {
        let i = userList.value.findIndex(c => c.id === item.id);
        if(i>-1) userList.value[i].checked = false;
        selectedUsers.value.splice(index,1);

        let trues = userList.value.filter(item => item.checked).length;
        if(trues>0&&trues<userList.value.length) isIndeterminate.value = true;
        else isIndeterminate.value = false;
        if(trues===userList.value.length) checkAll.value = true;
        else checkAll.value = false;
    }
    
    const reviewWj = () => {
        proxy.$router.push({path:'/agentQuestionnaireDetail',query:{id:questionnaireId.value,title:title.value,type:typecfg[type.value]}});
    }

    
    onMounted(()=>{
        questionnaireId.value = proxy.$route.query.id;
        params.value.questionnaireId = proxy.$route.query.id;
        title.value = proxy.$route.query.title;
        type.value = proxy.$route.query.type;
    })
</script>

<style scoped lang="scss">
    .page{
        background: #FAFAFA;
        .top{
            width: 100%;
            height: 64px;
            background: #FFFFFF;
            border-bottom: 1px solid #F3F4F6;
            
            img{
                width: 36px;
                height: 36px;
                margin-left: 16px;
                cursor: pointer;
            }
            .spans{
                display: flex;
                align-items: center;
                margin-left: 23px;
                span{
                    font-family: PingFangSC, PingFang SC;
                    font-weight: 400;
                    font-size: 14px;
                    color: #6B7280;
                    line-height: 14px;
                    &.last{
                        color: #111111;
                    }
                }
            }
        }

        .content{
            width: calc(100% - 720px);
            height: calc(100vh - 88px);
            margin: 12px 360px;
            overflow-y: auto;
            .c_top{
                width: 100%;
                padding: 12px 20px;
                box-sizing: border-box;
                background: #FFFFFF;
                border-radius: 6px;
                span{
                    font-family: PingFang-SC, PingFang-SC;
                    font-weight: bold;
                    font-size: 14px;
                    color: #252525;
                    line-height: 16px;
                }
            }
            .c_set{
                width: 100%;
                height: calc(100vh - 152px);
                background: #FFFFFF;
                border-radius: 6px;
                margin-top: 8px;
                padding: 28px 21px;
                box-sizing: border-box;
                .cs_title{
                    font-family: PingFang-SC, PingFang-SC;
                    font-weight: bold;
                    font-size: 14px;
                    color: #252525;
                    line-height: 20px;
                    margin-top: 30px;
                }
                .cs_users{
                    width: 100%;
                    height: 36px;
                    border-radius: 6px;
                    border: 1px solid #E5E7EB;
                    margin-top: 20px;
                    padding: 7px 12px;
                    box-sizing: border-box;
                    .cu_l{
                        width: 104px;
                        padding-right: 15px;
                        box-sizing: border-box;
                        &>img{
                            width: 18px;
                            height: 18px;
                            margin-top: -5px;
                        }
                        .cul_add{
                            width: 54px;
                            height: 24px;
                            border-radius: 4px;
                            border: 1px solid #761E6A;
                            font-family: PingFangSC, PingFang SC;
                            font-weight: 400;
                            font-size: 12px;
                            color: #761E6A;
                            line-height: 22px;
                            text-align: center;
                            margin-top: -2px;
                            cursor: pointer;
                        }
                    }
                    .cu_r{
                        width: calc(100% - 104px);
                        margin-left: -16px;
                        .cr_item{
                            padding: 4px 10px;
                            background: #F9FAFB;
                            border-radius: 6px;
                            margin-left: 16px;
                            span{
                                font-family: PingFangSC, PingFang SC;
                                font-weight: 400;
                                font-size: 14px;
                                color: #252525;
                                line-height: 14px;
                            }
                            img{
                                width: 16px;
                                height: 16px;
                                margin-left: 10px;
                                cursor: pointer;
                            }
                        }
                    }
                }
                .cs_zd{
                    margin-top: 12px;
                    span{
                        font-family: PingFangSC, PingFang SC;
                        font-weight: 400;
                        font-size: 14px;
                        color: #6B7280;
                        line-height: 20px;
                    }
                }
                .cs_time{
                    margin-top: 6px;
                    .ct_pre{
                        margin-top: 30px;
                        span{
                            font-family: PingFangSC, PingFang SC;
                            font-weight: 400;
                            font-size: 14px;
                            color: #6B7280;
                            line-height: 20px;
                            margin-right: 23px;
                        }
                    }
                }
                .cs_btn{
                    margin-top: 70px;
                    padding-left: 79px;
                }
            }
        }

        .ed_coach{
            margin-top: -20px;
            .ec_l{
                width: calc(50% - 1px);
                height: 100%;
                padding-right: 30px;
                box-sizing: border-box;
                border-right: 1px solid #ECEEF5;

                .ecl_gs{
                    margin-top: 19px;
                    .eg_title{
                        font-family: PingFangSC, PingFang SC;
                        font-weight: 400;
                        font-size: 14px;
                        color: #6B7280;
                        line-height: 20px;
                    }
                    .egt_item{
                        margin-top: 24px;
                        .ei_l{
                            span{
                                font-family: PingFang-SC, PingFang-SC;
                                font-weight: bold;
                                font-size: 14px;
                                color: #252525;
                                line-height: 20px;
                                &:last-child{
                                    font-weight: 400;
                                    color: #6B7280;
                                    margin-left: 20px;
                                }
                            }
                        }
                        .ei_r{
                            span{
                                font-family: PingFangSC, PingFang SC;
                                font-weight: 400;
                                font-size: 14px;
                                color: #761E6A;
                                line-height: 20px;
                                position: relative;
                                padding-left: 8px;
                                cursor: pointer;
                                &::before{
                                    content: '';
                                    width: 1px;
                                    height: 16px;
                                    background: #DDE0E6;
                                    position: absolute;
                                    left: 0;
                                    top: 50%;
                                    margin-top: -8px;
                                }
                            }
                        }
                    }
                }

                .ecl_company{
                    margin-top: 19px;
                    position: relative;
                    font-family: PingFang-SC, PingFang-SC;
                    font-weight: bold;
                    font-size: 14px;
                    color: #252525;
                    line-height: 20px;
                    text-align: center;
                    i{
                        position: absolute;
                        left: 0;
                        font-size: 14px;
                        color: #252525;
                        cursor: pointer;
                    }
                }
                .ecl_cbs{
                    width: 100%;
                    // height: calc(100% - 143px);
                    max-height: 376px;
                    overflow-y: auto;
                }
            }
            .ec_r{
                width: 50%;
                padding-left: 30px;
                box-sizing: border-box;
                .ecr_text{
                    font-family: PingFang-SC, PingFang-SC;
                    font-weight: bold;
                    font-size: 16px;
                    color: #252525;
                    line-height: 22px;
                    span{
                        color: #B9B9B9;
                    }
                }
                .ecr_names{
                    margin-left: -16px;
                    overflow: hidden;
                    .en_pre{
                        float: left;
                        padding: 10px;
                        background: #F9FAFB;
                        border-radius: 6px;
                        margin: 19px 0 0 16px;
                        span{
                            font-family: PingFangSC, PingFang SC;
                            font-weight: 400;
                            font-size: 14px;
                            color: #252525;
                            line-height: 14px;
                        }
                        img{
                            width: 16px;
                            height: 16px;
                            margin-left: 10px;
                            cursor: pointer;
                        }
                    }
                }
            }
        }
    }
</style>