<template>
    <div class="full_page">
        <div class="top adfac">
            <div class="t_l adfac">
                <img src="@/assets/images/agent/arrow_left.png" @click="handleBack">
                <div class="spans">
                    <span>项目管理</span>
                    <span class="last">&nbsp;/&nbsp;{{ topname }}</span>
                </div>
            </div>
            <div class="t_r">
                <div class="steps adfacjc">
                    <div class="s_text adfac" :class="{'active':step<3}">
                        <div class="st_num">1</div>
                        <span>创建新项目</span>
                    </div>
                    <div class="s_line"></div>
                    <div class="s_text adfac" :class="{'active':step===2}">
                        <div class="st_num">2</div>
                        <span>添加团队及其成员</span>
                    </div>
                </div>
            </div>
        </div>
        <div class="content">
            <div class="form">
                <template v-if="step===1">
                    <el-form ref="programRef" :model="programForm" :rules="programRules" label-width="140px" style="width: 90%;margin: 0 auto;">
                        <div class="f_title">创建新项目</div>
                        <el-form-item label="企业名称" prop="aaa">
                            <el-input v-model="programForm.aaa" placeholder="请输入企业名称" />
                        </el-form-item>
                        <el-form-item label="统一社会信用代码" prop="bbb">
                            <el-input v-model="programForm.bbb" placeholder="请输入统一社会信用代码" />
                        </el-form-item>
                        <el-form-item label="所在地区" prop="ccc">
                            <el-select v-model="programForm.ccc" placeholder="请选择所在地区" style="width: 100%;">
                                <el-option label="区域一" value="shanghai"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="所在行业" prop="ddd">
                            <el-select v-model="programForm.ddd" placeholder="请选择所在行业" style="width: 100%;">
                                <el-option label="行业一" value="shanghai"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="人员规模" prop="eee">
                            <el-select v-model="programForm.eee" placeholder="请选择人员规模" style="width: 100%;">
                                <el-option label="规模一" value="shanghai"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="项目教练" prop="eee">
                            <div class="f_s adfacjb" @click="hangleSelectCoach">
                                <div class="fs_text" :class="{'active':coachs!=='请选择'}">{{ coachs }}</div>
                                <i class="el-icon-arrow-down" style="color: #C0C4CC;"></i>
                            </div>
                        </el-form-item>
                    </el-form>
                </template>
                <template v-else>
                    <el-form :model="teamDto" :rules="rules" ref="teamDtoRef" label-width="130px">
                        <div class="f_title">添加团队</div>
                        <el-row style="width: 100%;">
                            <el-col :span="8">
                                <el-form-item label="所属项目" prop="aaa">
                                    <el-select v-model="teamDto.aaa" placeholder="请选择所属项目" style="width: 100%;"></el-select>
                                </el-form-item>
                            </el-col>
                            <el-col :span="8">
                                <el-form-item label="团队名称" prop="bbb">
                                    <el-input v-model="teamDto.bbb" clearable placeholder="请输入团队名称"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="8">
                                <el-form-item label="工作任务" prop="ccc">
                                    <el-input v-model="teamDto.ccc" clearable placeholder="请输入工作任务"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row style="width: 100%;">
                            <el-col :span="24">
                                <el-form-item label="团队介绍" prop="ddd">
                                    <el-input type="textarea" v-model="teamDto.ddd" clearable placeholder="请输入团队介绍"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row style="width: 100%;">
                            <el-col :span="24">
                                <el-form-item label="团队项目情况" prop="eee">
                                    <el-input type="textarea" v-model="teamDto.eee" clearable placeholder="请输入团队项目情况"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <div class="f_title" style="margin-top: 31px;">添加成员</div>
                        <el-row style="width: 100%;">
                            <el-col :span="24">
                                <el-form-item label="团队成员" prop="">
                                    <div class="f_type">
                                        <el-radio-group v-model="addType">
                                            <el-radio :label="1">批量导入</el-radio>
                                            <el-radio :label="2">手动录入</el-radio>
                                        </el-radio-group>
                                    </div>
                                    <template v-if="addType===1">
                                        <div class="f_pldr">
                                            <div class="fp_box">
                                                <div class="fpb_pre">
                                                    <div class="fpbp_btn">下载Excel示例文件</div>
                                                </div>
                                                <img src="@/assets/images/agent/arrow_right_two.png">
                                                <div class="fpb_pre sc">
                                                    <div class="fpbp_btn">上传Excel文件</div>
                                                </div>
                                            </div>
                                            <p>
                                                1、Excel文件后缀名为xls或者xlsx，文件中所含烈数请勿超过20列<br>
                                                2、模版中用户姓名为必填，其他为选填，且不能对已有字段进行删除、修改<br>
                                                3、部门：上下级部门间用“/”符号隔开，且从最上级部门开始，例如“产品部门/设计”。若存在属于多个部门的情况，不同部门之间用 ; 隔开。
                                            </p>
                                        </div>
                                    </template>
                                    <template v-else-if="addType===2">
                                        <div class="f_sdlr">
                                            <div class="fs_item" v-for="(item,index) in teamDto.userList" :key="index">
                                                <el-form :model="teamDto.userList[index]" label-width="100px">
                                                    <el-row style="width: 100%;">
                                                        <el-col :span="8">
                                                            <el-form-item label="姓名" prop="aa">
                                                                <el-input v-model="teamDto.userList[index].aa" clearable placeholder="请输入"></el-input>
                                                            </el-form-item>
                                                        </el-col>
                                                        <el-col :span="8">
                                                            <el-form-item label="性别" prop="bb">
                                                                <el-select v-model="teamDto.userList[index].bb" placeholder="请选择" style="width: 100%;"></el-select>
                                                            </el-form-item>
                                                        </el-col>
                                                        <el-col :span="8">
                                                            <el-form-item label="出生日期" prop="cc">
                                                                <el-select v-model="teamDto.userList[index].cc" placeholder="请选择" style="width: 100%;"></el-select>
                                                            </el-form-item>
                                                        </el-col>
                                                    </el-row>
                                                    <el-row style="width: 100%;">
                                                        <el-col :span="8">
                                                            <el-form-item label="手机号码" prop="dd">
                                                                <el-input v-model="teamDto.userList[index].dd" clearable placeholder="请输入"></el-input>
                                                            </el-form-item>
                                                        </el-col>
                                                        <el-col :span="8">
                                                            <el-form-item label="所属部门" prop="ee">
                                                                <el-input v-model="teamDto.userList[index].ee" clearable placeholder="请输入"></el-input>
                                                            </el-form-item>
                                                        </el-col>
                                                        <el-col :span="8">
                                                            <el-form-item label="职位" prop="ff">
                                                                <el-select v-model="teamDto.userList[index].ff" placeholder="请选择" style="width: 100%;"></el-select>
                                                            </el-form-item>
                                                        </el-col>
                                                    </el-row>
                                                    <el-row style="width: 100%;">
                                                        <el-col :span="8">
                                                            <el-form-item label="分工" prop="gg">
                                                                <el-input v-model="teamDto.userList[index].gg" clearable placeholder="请输入"></el-input>
                                                            </el-form-item>
                                                        </el-col>
                                                        <el-col :span="8">
                                                            <el-form-item label="上级" prop="hh">
                                                                <el-input v-model="teamDto.userList[index].hh" clearable placeholder="请输入"></el-input>
                                                            </el-form-item>
                                                        </el-col>
                                                        <el-col :span="8">
                                                            <el-form-item label="教育程度" prop="ii">
                                                                <el-select v-model="teamDto.userList[index].ii" placeholder="请选择" style="width: 100%;"></el-select>
                                                            </el-form-item>
                                                        </el-col>
                                                    </el-row>
                                                    <el-row style="width: 100%;">
                                                        <el-col :span="8">
                                                            <el-form-item label="级别" prop="jj">
                                                                <el-select v-model="teamDto.userList[index].jj" placeholder="请选择" style="width: 100%;"></el-select>
                                                            </el-form-item>
                                                        </el-col>
                                                        <el-col :span="8">
                                                            <el-form-item label="" prop="">
                                                                <div class="fs_add del" @click="handleDeleteItem(item,index)">x 删除</div>
                                                            </el-form-item>
                                                        </el-col>
                                                    </el-row>
                                                </el-form>
                                            </div>
                                            <div class="fs_add" @click="handleAddItem">+ 添加</div>
                                        </div>
                                    </template>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                </template>
            </div>
            <div class="btns">
                <el-button type="primary" @click="handleNext" v-if="step===1" style="margin-left: 20px;">下一步</el-button>
                <el-button type="primary" @click="handlePrevious" v-if="step===2" style="margin-left: 20px;">上一步</el-button>
                <el-button type="primary" @click="handleConfirm" v-if="step===2" style="margin-left: 20px;">确认</el-button>
                <el-button type="default" @click="handleCancel" style="margin-left: 20px;">取消</el-button>
            </div>
        
            <el-dialog width="790px" :visible.sync="coachShow" title="选择教练" @close="coachShow=false">
                <div class="ed_coach adf">
                    <div class="ec_l">
                        <el-input placeholder="请输入教练名搜索" suffix-icon="el-icon-search" v-model="coachName" style="width: 100%;"></el-input>
                        <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAllChange" style="margin-top: 65px;">全选</el-checkbox>
                        <div class="ecl_cbs">
                            <el-checkbox v-model="coach.checked" v-for="(coach,index) in coachList" :key="coach.id" @change="handleChangeCoach" style="margin-top: 24px;display: block;">{{ coach.name }}</el-checkbox>
                        </div>
                    </div>
                    <div class="ec_r">
                        <div class="ecr_text">已选择（{{ coachSelecteds.length }}/<span>{{ coachList.length }}</span>）</div>
                        <div class="ecr_names">
                            <div class="en_pre adfacjc" v-for="(item,index) in coachSelecteds" :key="index">
                                <span>{{ item.name }}</span>
                                <img src="@/assets/images/agent/delete_mini.png" @click="handleDeleteCoach(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>
    </div>
</template>

<script setup name="">
    import { ref, getCurrentInstance } from 'vue'
    const { proxy } = getCurrentInstance();
    
    const topname = ref('创建项目')
    const step = ref(1)
    const coachs = ref('请选择')
    const coachName = ref('')
    const coachShow = ref(false)
    const coachSelecteds = ref([])
    const coachList = ref([
        {id:1,name:'张三',checked:false},
        {id:2,name:'李四',checked:false},
        {id:3,name:'王五',checked:false},
        {id:4,name:'赵六',checked:false},
        {id:5,name:'田七',checked:false}
    ])
    const isIndeterminate = ref(false)
    const checkAll = ref(false)

    const programForm = ref({
        aaa: '',
        bbb: '',
        ccc: '',
        ddd: '',
        eee: ''
    });
    const programRules = ref({
        aaa: [
            { required: true, message: '请输入企业名称', trigger: 'blur' }
        ],
        bbb: [
            { required: true, message: '请输入统一社会信用代码', trigger: 'blur' }
        ],
        ccc: [
            { required: true, message: '请选择所在地区', trigger: 'change' }
        ],
        ddd: [
            { required: true, message: '请选择所在行业', trigger: 'change' }
        ],
        eee: [
            { required: true, message: '请选择人员规模', trigger: 'change' }
        ]
    });
    const teamDto = ref({
        aaa: '',
        bbb: '',
        ccc: '',
        ddd: '',
        eee: '',
        userList:[]
    })
    const addType = ref(1)
    const programRef = ref(null)
    const teamDtoRef = ref(null)
    const rules = ref({
        aaa: [
            { required: true, message: '请选择所属项目', trigger: 'change' }
        ],
        bbb: [
            { required: true, message: '请输入团队名称', trigger: 'blur' }
        ],
        ccc: [
            { required: true, message: '请输入工作任务', trigger: 'blur' }
        ],
        ddd: [
            { required: true, message: '请输入团队介绍', trigger: 'blur' }
        ],
        eee: [
            { required: true, message: '请输入团队项目情况', trigger: 'blur' }
        ]
    })

    const handleBack = () => {
        proxy.$router.back()
    }

    const handleAddItem = () => {
        teamDto.value.userList.push({
            aa: '',
            bb: '',
            cc: '',
            dd: '',
            ee: '',
            ff: '',
            gg: '',
            hh: '',
            ii: '',
            jj: ''
        })
    }

    const handleDeleteItem = (item,index) => {
        teamDto.value.userList.splice(index,1)
    }

    const handleNext = () => {
        proxy.$refs.programRef.validate((valid) => {
            if (valid) {
                step.value = 2;
                topname.value = '添加团队及其成员';
            } else {
                return false;
            }
        });
    }
    const handlePrevious = () => {
        step.value = 1;
        topname.value = '创建项目';
    }
    const handleConfirm = () => {
        proxy.$refs.teamDtoRef.validate((valid) => {
            if (valid) {
                console.log(programForm.value);
                console.log(teamDto.value);
            } else {
                return false;
            }
        });
    }
    const handleCancel = () => {
        proxy.$router.back()
    }

    const hangleSelectCoach = () => {
        coachShow.value = true;
    }

    const submitForm = () => {
        coachShow.value = false;
    }
    const cancel = () => {
        coachShow.value = false;
    }

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

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

        let trues = coachList.value.filter(item => item.checked).length;
        if(trues>0&&trues<coachList.value.length) isIndeterminate.value = true;
        else isIndeterminate.value = false;
        if(trues===coachList.value.length) checkAll.value = true;
        else checkAll.value = false;
    }
</script>

<style scoped lang="scss">
    :v-deep .el-dialog__wrapper{
        background: rgba(0,0,0,.3) !important;
    }
    ::v-deep .el-dialog__title{
        font-weight: bold !important;
    }
    ::v-deep .el-dialog{
        margin-top: 15vh !important;
    }

    .full_page{
        background: #FAFAFA;
        .top{
            width: 100%;
            height: 64px;
            background: #FFFFFF;
            border-bottom: 1px solid #F3F4F6;
            .t_l{
                width: 310px;
                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;
                        }
                    }
                }
            }
            .t_r{
                width: calc(100% - 310px);
                .steps{
                    .s_text{
                        .st_num{
                            width: 24px;
                            height: 24px;
                            border-radius: 50%;
                            background: #DDE0E6;
                            font-family: PingFang-SC, PingFang-SC;
                            font-weight: bold;
                            font-size: 14px;
                            color: #FFFFFF;
                            line-height: 24px;
                            text-align: center;
                        }
                        span{
                            margin-left: 10px;
                            font-family: PingFangSC, PingFang SC;
                            font-weight: 400;
                            font-size: 14px;
                            color: #6B7280;
                            line-height: 14px;
                        }
                        &.active{
                            .st_num{
                                background: #761E6A;
                            }
                            span{
                                color: #761E6A;
                            }
                        }
                    }
                    .s_line{
                        width: 496px;
                        height: 1px;
                        border: 1px solid #E5E7EB;
                        margin: 0 21px;
                    }
                }
            }
        }

        .content{
            width: calc(100% - 24px);
            height: calc(100vh - 88px);
            margin: 12px;
            background: #FFFFFF;
            border-radius: 6px 6px 0px 0px;
            border: 1px solid #F3F4F6;
            position: relative;
            .form{
                width: 100%;
                height: calc(100% - 81px);
                overflow-y: auto;
                padding: 20px;
                box-sizing: border-box;
            }
            .btns{
                width: 100%;
                height: 80px;
                border-top: 1px solid #E5E7EB;
                display: flex;
                align-items: center;
                position: absolute;
                left: 0;
                bottom: 0;
            }
        }

        .f_title{
            font-family: PingFangSC, PingFang SC;
            font-weight: 600;
            font-size: 16px;
            color: #0D162A;
            line-height: 16px;
            margin-bottom: 20px;
        }

        .f_pldr{
            margin-top: 20px;
            .fp_box{
                display: flex;
                width: 526px;
                height: 125px;
                border-radius: 4px;
                border: 1px solid #EDEEF0;
                position: relative;
                .fpb_pre{
                    width: calc(50% - 1px);
                    height: 100%;
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    .fpbp_btn{
                        width: 140px;
                        height: 36px;
                        border-radius: 6px;
                        border: 1px solid #E5E7EB;
                        font-family: PingFangSC, PingFang SC;
                        font-weight: 400;
                        font-size: 12px;
                        color: #833478;
                        line-height: 32px;
                        text-align: center;
                        cursor: pointer;
                    }
                    &.sc{
                        border-left: 1px solid #EDEEF0;
                        .fpbp_btn{
                            background: #833478;
                            border: 1px solid #833478;
                            color: #FFFFFF;
                        }
                    }
                }
                img{
                    width: 50px;
                    height: 20px;
                    position: absolute;
                    left: 50%;
                    margin-left: -25px;
                    top: 50%;
                    margin-top: -10px;
                }
            }
            p{
                margin-top: 11px;
                font-family: PingFangSC, PingFang SC;
                font-weight: 400;
                font-size: 12px;
                color: #C1C7D2;
                line-height: 20px;
                text-align: justify;
            }
        }

        .f_sdlr{
            padding-top: 10px;

            .fs_add{
                width: 80px;
                height: 32px;
                background: #833478;
                border-radius: 6px;
                font-family: PingFangSC, PingFang SC;
                font-weight: 400;
                font-size: 14px;
                color: #FFFFFF;
                line-height: 32px;
                text-align: center;
                margin-top: 20px;
                cursor: pointer;
                &.del{
                    background: rgb(212, 96, 96);
                    margin: 0;
                    margin-top: 5px;
                }
            }
        }

        .f_s{
            width: 100%;
            height: 42px;
            border-radius: 6px;
            border: 1px solid #dcdfe6;
            padding: 11px 11px 11px 16px;
            box-sizing: border-box;
            cursor: pointer;
            .fs_text{
                font-family: PingFangSC, PingFang SC;
                font-weight: 400;
                font-size: 14px;
                color: #C0C4CC;
                line-height: 20px;
                &.active{
                    color: #606266;
                }
            }
        }

        .ed_coach{
            .ec_l{
                width: calc(50% - 1px);
                height: 100%;
                padding-right: 30px;
                box-sizing: border-box;
                border-right: 1px solid #ECEEF5;
                .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;
                        }
                    }
                }
            }
        }
    }

    ::v-deep .el-form-item{
        margin-bottom: 20px !important;
    }
</style>