<template>
    <fullscreen :fullscreen.sync="fullscreen" class="container">
        <div class="container-box">
            <toolList @iconCli='iconCli' :tooltit='tooltit'></toolList>
            <div class="search">
                <mySearch :searchList="searchList" @searchInfo="searchInfo"></mySearch>
                <el-button type="primary" @click="tempDown" size="medium">下发模板</el-button>
            </div>
            <div class="tabbox">
                <el-table height="calc(100% - 40px)" class="com-table" ref="multipleTable" :data="tableData"
                    tooltip-effect="dark" size="small" border style="width: 100%" v-loading="loading">
                    <el-table-column prop="trainMgrName" label="培训管理人">
                    </el-table-column>
                    <el-table-column prop="demandMgrName" label="培训需求人">
                    </el-table-column>
                    <el-table-column prop="cityName" label="部室/团队">
                    </el-table-column>
                    <el-table-column prop="trainType" label="培训内容分类">
                    </el-table-column>
                    <el-table-column prop="trainContent" label="培训内容名称">
                    </el-table-column>
                    <el-table-column prop="trainObj" label="培训对象">
                    </el-table-column>
                    <el-table-column prop="trainLecturer" label="培训主讲人">
                    </el-table-column>
                    <el-table-column prop="trainPhone" label="联系电话">
                    </el-table-column>
                    <el-table-column prop="trainTime" label="培训日期">
                    </el-table-column>
                    <el-table-column prop="stsDesc" label="状态">
                    </el-table-column>
                    <el-table-column label="操作" width="160px" align="center">
                        <template slot-scope="scope">
                            <el-button size="mini" type="primary" @click="dialogCheck(1,scope.row)">查看</el-button>
                            <el-button v-if='scope.row.demandMgrNo == userInfo.loginNo' size="mini" type="primary" @click="dialogCheck(2,scope.row)">修改</el-button>
                            <!-- <el-button size="mini" type="danger" @click="delLine(scope.row)">删除</el-button> -->
                        </template>
                    </el-table-column>
                </el-table>
                <el-pagination class="pageBox" @current-change="currchange" layout="prev, pager, next" background
                    :total="total">
                </el-pagination>
            </div>
        </div>

        <el-dialog :title="titname" :visible.sync="dialogStatus" width="50%" :destroy-on-close="true"
            :modal-append-to-body="false" :close-on-click-modal="false" :before-close="closedia" :fullscreen="true">
            <div v-loading="loadinged">
                <el-form :model="infolist" ref="infolist" :rules="rules"
                    style="height: calc(100vh - 180px);overflow-y: scroll;">

                    <div class="info-line">
                        <el-form-item>
                            <span>培训内容分类</span>
                            <el-select clearable v-model="infolist.trainType" placeholder="培训内容分类" :disabled="disableStatus">
                                <el-option v-for="items in pxtype" :key="items.name" :label="items.name"
                                    :value="items.name">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item>
                            <span>培训内容名称</span>
                            <el-input v-model="infolist.trainContent" placeholder="培训内容名称" :disabled="disableStatus">
                            </el-input>
                        </el-form-item>
                        <el-form-item>
                            <span>培训对象</span>
                                <el-select clearable v-model="infolist.trainObj" placeholder="培训对象" :disabled="disableStatus">
                                    <el-option v-for="items in trainObjopt" :key="items.name" :label="items.name"
                                        :value="items.name">
                                    </el-option>
                                </el-select>
                            </el-input>
                        </el-form-item>
                        <el-form-item>
                            <span>培训主讲人</span>
                            <el-input v-model="infolist.trainLecturer" placeholder="培训主讲人" :disabled="disableStatus">
                            </el-input>
                        </el-form-item>
                        <el-form-item>
                            <span>联系电话</span>
                            <el-input v-model="infolist.trainPhone" placeholder="联系电话" :disabled="disableStatus">
                            </el-input>
                        </el-form-item>
                        <el-form-item>
                            <span>培训日期</span>
                            <el-date-picker v-model="infolist.trainTime" type="date" placeholder="培训日期"
                                :disabled="disableStatus">
                            </el-date-picker>
                        </el-form-item>
                    </div>

                    <div v-if="disableStatus">
                        <uploadDown :datalist="datalist" :dialogStatus="uploadstatus"></uploadDown>
                    </div>
                    <div v-if="!disableStatus">
                        <myUpload @uploadBack="uploadBack" :fileInfo="fileInfo" :fileList="fileInfo.fileList">
                        </myUpload>
                    </div>

                </el-form>
                <div slot="footer" style="float: right;margin-top: 20px;">
                    <el-button type="primary" @click="dialogCliadd(2)">确 定</el-button>
                    <el-button @click="dialogCliadd(1)">取 消</el-button>
                </div>
            </div>
        </el-dialog>

        <el-dialog title="模板下发" :visible.sync="tempdownSts" width="80%" :destroy-on-close="true"
            :modal-append-to-body="false" :close-on-click-modal="false" :before-close="closedia">
            <div>

                <el-form :model="infolist" ref="infolist" style="height: 52vh;overflow-y: scroll;" :rules="rules">

                    <el-form-item class="info-line online">
                        <span>负责人</span>
                        <div class="tree treeUser" style="width: calc(30% - 10px);margin-right: 1%;">
                            <p v-for="(item,index) in deptList" :key="index" @click="choseDept(item)">{{item.ou}}</p>
                        </div>
                        <div class="tree treeUser" style="width: calc(30% - 10px);margin-right: 1%;">
                            <p v-for="(item,index) in treeListp" :key="index">
                                <span>{{item.ou}}</span>
                                <span class="p-btn" @click="fzperson(item)">负责人</span>
                                <i @click="deletest(item,index)" class="el-icon-error"></i>
                            </p>
                        </div>
                        <div class="tree treeUser" style="width: calc(30% - 10px)">
                            <p v-for="(item,index) in treeListp" :key="index">{{item.loginNameStr}}</p>
                        </div>
                    </el-form-item>

                </el-form>

                <div slot="footer" style="text-align: right;padding: 20px 0;">
                    <el-button type="primary" @click="tempdownadd">确 定</el-button>
                    <el-button @click="tempdownSts = false">取 消</el-button>
                </div>

            </div>
        </el-dialog>

        <el-dialog title="选择负责人" :visible.sync="dialogStatusp" width="50%" :destroy-on-close="true"
            :modal-append-to-body="false" :close-on-click-modal="false">
            <el-form style="height: 50vh;overflow-y: scroll;">
                <div class="treeUserp">
					<p v-for="(item,index) in treeListponly" :key="index" @click="chofz(item)" v-if="item.loginNameStr">
					    {{item.groupName}} -
					    {{item.loginNameStr}}
					</p>
                </div>
            </el-form>
        </el-dialog>


        <myMessage :messTit='messTit' @closeMessage="closeMessage" :centerDialogVisible="centerDialogVisible"
            v-if="centerDialogVisible"></myMessage>
    </fullscreen>

</template>
<script>
    import mySearch from "../../../components/search.vue";
    import myMessage from "../../../components/myMessage.vue"
    import toolList from '../../../components/toolList'
    import myUpload from '../../../components/upload'
    import uploadDown from '../../../components/uploadDown.vue'
    import deptTreeOnly from "../../../components/deptTreeOnly.vue"

    export default {
        components: {
            mySearch,
            myMessage,
            toolList,
            myUpload,
            uploadDown,
            deptTreeOnly
        },
        data() {
            const coopName = (rule, value, callback) => {
                if (!this.infolist.coopName) {
                    callback(new Error('不能为空'))
                } else {
                    callback()
                }
            }
            return {
                rules: {
                    coopName: [{
                        required: true,
                        trigger: 'blur',
                        validator: coopName
                    }],
                },
                searchList: [{
                        type: 'sel',
                        tit: '培训内容分类',
                        value: '',
                        width: '49%',
                        options:[
                            {
                                dataCode:'业务类',
                                dataName:'业务类',
                            },{
                                dataCode:'产品类',
                                dataName:'产品类',
                            },{
                                dataCode:'营销类',
                                dataName:'营销类',
                            }
                        ]
                    },
                    {
                        type: 'sel',
                        tit: '培训对象',
                        value: '',
                        width: '49%',
                        options:[
                            {
                                dataCode:'自有营业人员',
                                dataName:'自有营业人员',
                            },{
                                dataCode:'社会渠道营业人员',
                                dataName:'社会渠道营业人员',
                            },{
                                dataCode:'自有及社会渠道营业人员',
                                dataName:'自有及社会渠道营业人员',
                            }
                        ]
                    },
                ],
                tooltit: '培训计划',
                fullscreen: false,
                total: 0,
                pageSize: 1,
                tableData: [{}],
                dialogStatus: false,
                disableStatus: false,
                titname: '',
                infolist: {
                    signTime:'',
                },
                userInfo: {},
                params: {},
                centerDialogVisible: false,
                messTit: '',
                delid: '',
                loading: false,
                loadinged: false,
                docType:[],
                contentType:[],
                infodata:[{}],
                infodatat:[{}],
                datalist: {
                    url: '/market/compatt/downfile',
                    type: 1
                },
                uploadstatus: false,
                attList: [],
                fileInfo: {
                    limit: 5,
                    url: '/market/cStoreScheTrain/upload',
                    fileList: [],
                    uploadType:'train'
                },

                tempdownSts:false,
                deptList: [],
                treeListp: [],
                treeListponly: [],
                dialogStatusp: false,
                companyFlag: '0',
                groupId: '',
                gjsts: false,
                gjlist: [],

                pxtype:[
                    {name:'业务类'},
                    {name:'产品类'},
                    {name:'营销类'},
                ],
                trainObjopt:[
                    {name:'自有营业人员'},
                    {name:'社会渠道营业人员'},
                    {name:'自有及社会渠道营业人员'},
                ],
                relId:'',
            }
        },
        methods: {
            chofz(v) {
                this.dialogStatusp = false;
                for (let i = 0; i < this.treeListp.length; i++) {
                    if (this.treeListp[i].o == this.fadept.o) {
                        this.treeListp[i].loginNameStr = v.loginNameStr;
                        this.treeListp[i].loginNoStr = v.loginNoStr;
                    }
                }
            },
            tempdownadd() {
                let param = [];
                let _this = this;
                for (let i = 0; i < this.treeListp.length; i++) {
                    if (this.treeListp[i].loginNoStr) {
                        param.push({
                            demandMgrNo: this.treeListp[i].loginNoStr,
                            demandMgrName: this.treeListp[i].loginNameStr,
                            cityId: this.treeListp[i].o,
                            cityName: this.treeListp[i].ou,
                        })
                    }
                }
                if (param.length == 0) {
                    _this.$message({
                        message: '请选择地市及负责人',
                        type: 'error'
                    });
                    return
                }
                this.$http({
                    url: "/market/cStoreScheTrain/add",
                    method: "post",
                    headers: {
                        "Content-Type": "application/json",
                    },
                    data: param,
                }).then((res) => {
                    if (res.data.result === 1) {
                        _this.$message({
                            message: res.data.desc,
                            type: 'error'
                        });
                    } else {
                        _this.$message({
                            message: '成功',
                            type: 'success'
                        });
                        _this.tempdownSts = false;
                        _this.getList(this.params, this.pageSize);
                    }
                });
            },
            fzperson(v) {
                this.fadept= v;
                this.dialogStatusp = true;
                this.$http({
                    url: "/sysmgr/sysuserinfo/queryList",
                    method: "post",
                    headers: {
                        "Content-Type": "application/json",
                    },
                    data: {
                        groupId: v.o,
                    },
                }).then((res) => {
                   this.treeListponly = res.data;
                });
                this.$http({
                    url: "/sysmgr/csysdept/queryRegionDeptList",
                    method: "post",
                    headers: {
                        "Content-Type": "application/json",
                    },
                    data: {
                        parentorgid: v.o,
                    },
                }).then((res) => {
                   res.data.forEach(item=>{
                       this.$http({
                           url: "/sysmgr/sysuserinfo/queryList",
                           method: "post",
                           headers: {
                               "Content-Type": "application/json",
                           },
                           data: {
                               groupId: item.o,
                           },
                       }).then((ress) => {
                          this.treeListponly.push({bumen:item.ou})
                          this.treeListponly=this.treeListponly.concat(ress.data);
                       });
                   })
                });
            },
            deletest(val, index) {
                this.treeListp.splice(index, 1);
            },
            choseDept(v) {
                let x = 'no';
                for (let i = 0; i < this.treeListp.length; i++) {
                    if (this.treeListp[i].o == v.o) {
                        x = i;
                    }
                }
                if (x != 'no') {
                    this.treeListp.splice(x, 1)
                } else {
                    this.treeListp.push(v);
                }
            },
            tempDown() {
                this.tempdownSts = true;
                if (this.companyFlag == '0') {
                    this.$http({
                        url: "/sysmgr/csysdept/queryRegionDeptList",
                        method: "post",
                        headers: {
                            "Content-Type": "application/json",
                        },
                        data: {},
                    }).then((res) => {
                        this.deptList = res.data[0].children;
                    });
                } else {
                    this.$http({
                        url: "/sysmgr/csysdept/queryListByParent",
                        method: "post",
                        headers: {
                            "Content-Type": "application/json",
                        },
                        data: {
                            parentorgid: this.groupId
                        },
                    }).then((res) => {
                        this.deptList = res.data;
                    });
                }
            },


            uploadBack(v) {
                this.attList = v;
            },
            //搜索数据
            searchInfo(v) {
                this.params = {};
                v[0] ? this.params.trainType = v[0] : '';
                v[1] ? this.params.trainObj = v[1] : '';
                this.getList(this.params, this.pageSize);
            },
            closedia() {
                this.infolist = {
                    signTime:'',
                };
                this.infodata = [{}];
                this.infodatat = [{}];
                this.attList = [];
                this.fileInfo.fileList = [];
                this.dialogStatus = false;
            },
            //获取列表
            getList(v, n) {
                this.pageSize = n;
                let _this = this;
                this.loading = true;
                this.$http({
                    url: "/market/cStoreScheTrain/queryPage",
                    method: "post",
                    headers: {
                        "Content-Type": "application/json",
                        "page": '{"pageNo":"' + n + '","pageSize":"10"}'
                    },
                    data: v,
                }).then((res) => {
                    this.tableData = res.data.data;
                    this.total = res.data.totalRecord;
                    this.loading = false;
                });
            },
            // 分页
            currchange(v) {
                this.pageSize = v;
                this.getList(this.params, this.pageSize);
            },
            //申请
            dialogCheck(v, n) {
                this.dialogStatus = true;
                let infolist = Object.assign({}, n);
                if (v === 1) {
                    this.titname = '查看';
                    this.disableStatus = true;
                } else if (v === 2) {
                    this.titname = '修改';
                    this.disableStatus = false;
                } else if (v === 3) {
                    this.titname = '添加';
                    this.disableStatus = false;
                    return
                }
                this.infolist = infolist;
                this.fileInfo.relId = n.id;
                this.$http({
                    url: "/market/cStoreScheTrain/query",
                    method: "post",
                    headers: {
                        "Content-Type": "application/json",
                    },
                    data: {
                        id: n.id
                    },
                }).then((res) => {
                    if(res.data.fileName){
                        this.fileInfo.fileList.push({
                            name: res.data.fileName,
                            url: '',
                            id: res.data.fileId,
                            fileCode: res.data.fileId,
                            fileName: res.data.fileName
                        });
                        this.attList.push({
                            name: res.data.fileName,
                            url: '',
                            id: res.data.fileId,
                            fileCode: res.data.fileId,
                            fileName: res.data.fileName
                        });
                    }
                    this.datalist.attList = [];
                    this.datalist.attList = this.attList;
                    this.infolist.attList = [];
                    this.infolist.attList = this.attList;
                    this.uploadstatus = !this.uploadstatus;
                });
            },
            //添加
            dialogCliadd(v) {
                if (v == 1 || this.titname == '查看') {
                    this.infolist = {
                        signTime:'',
                    };
                    this.infodata = [{}];
                    this.infodatat = [{}];
                    this.attList = [];
                    this.fileInfo.fileList = [];
                    this.dialogStatus = false;
                    return
                }
                let _this = this;
                let info = this.infolist;
                info.trainTime = _this.$formatDate(this.infolist.trainTime, "YYYY-MM-DD");

                info.fileId = this.attList[0].id;
                info.fileName = this.attList[0].fileName;

                if (this.titname == '修改') {
                    this.submitInfo("/market/cStoreScheTrain/update", info);
                }
            },

            submitInfo(u, v) {
                let _this = this;
                this.$refs.infolist.validate(valid => {
                    if (valid) {
                        this.$http({
                            url: u,
                            method: "post",
                            headers: {
                                "Content-Type": "application/json",
                            },
                            data: v,
                        }).then((res) => {
                            if (res.data.result === 1) {
                                _this.$message({
                                    message: res.data.desc,
                                    type: 'error'
                                });
                            } else {
                                _this.$message({
                                    message: '成功',
                                    type: 'success'
                                });
                                _this.closedia();
                                _this.infolist = {
                                    signTime:'',
                                };
                                _this.infodata = [{}];
                                _this.infodatat = [{}];
                                _this.getList(this.params, this.pageSize);
                            }

                        });
                    }
                })
            },
            closeMessage(v) {
                this.centerDialogVisible = false;
                let _this = this;
                if (v === 1) {
                    _this.$http({
                        url: "/market/cStoreOutNorm/del",
                        method: "post",
                        headers: {
                            "Content-Type": "application/json",
                        },
                        data: {
                            id: this.delid
                        },
                    }).then((res) => {
                        if (res.data.result === 1) {
                            _this.$message({
                                message: res.data.desc,
                                type: 'error'
                            });
                        } else {
                            _this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            _this.getList(this.params, this.pageSize);
                        }
                    });
                }
            },
            //删除
            delLine(v) {
                this.centerDialogVisible = true;
                this.messTit = '即将删除此条数据, 是否删除?';
                this.delid = v.id;
            },
            //功能栏
            iconCli(v) {
                if (v === 1) {
                    this.getList(this.params, this.pageSize);
                }
                if (v === 2) {
                    this.fullscreen = !this.fullscreen
                }
            },
            getUser() {
                this.userInfo = JSON.parse(window.sessionStorage.userInfo);
                // this.searchList[1].options = []
                // this.$http({
                //     url: "/sysmgr/csysdept/queryRegionDeptList",
                //     method: "post",
                //     headers: {
                //         "Content-Type": "application/json",
                //     },
                //     data: {},
                // }).then((res) => {
                //     this.searchList[1].options.push({
                //         dataName:'省公司',
                //         dataCode:'省公司'
                //     })
                //     res.data[0].children.forEach(item=>{
                //         this.searchList[1].options.push({
                //             dataName:item.ou,
                //             dataCode:item.ou
                //         })
                //     })
                // });
            },
        },
        mounted() {
            this.getList({}, 1);
            this.getUser();
        },
        created() {

        }
    }
</script>
<style scoped lang="scss">
    .onetab {
        margin-bottom: 20px;
        padding: 0 20px;
    }

    .titbox {
        div {
            float: right;

            i {
                font-size: 22px;
                margin-left: 20px;
                cursor: pointer;
            }
        }
    }

    .tabbox {
        margin-top: 15px;
    }

    .pageBox {
        text-align: right;
        margin-top: 10px;
    }

    .info-line {
        width: 100%;
        display: block;
        padding-left: 20px;

        div {
            width: 50%;
            display: inline-block;
        }

        span {
            width: 100px;
            display: inline-block;
            text-align: left;

            i {
                color: red;
                display: inline-block;
                padding-right: 5px;
            }
        }

        .el-select,
        .el-input {
            width: calc(100% - 120px);
        }
    }

    .online {
        width: 100%;

        .el-select {
            width: calc(100% - 100px);
        }

        span {
            vertical-align: top;
        }

        .el-textarea {
            width: calc(100% - 120px);
        }

        .tree {
            width: calc(50% - 60px);
            display: inline-block;
            margin-right: 20px;
            height: 300px;
            overflow-y: scroll;

            p {
                overflow: hidden;
                display: flex;
                justify-content: space-between;
            }

            .p-btn {
                width: 50px !important;
                cursor: pointer;
                color: #0074DA;
            }

            span {
                display: inline-block;
                width: calc(100% - 100px);
            }

            .el-icon-error {
                float: right;
                font-size: 20px;
                margin-top: 9px;
                cursor: pointer;
            }
        }

        .treeUser {
            margin: 0;
            border: 1px solid #ddd;

            p {
                background: #f4f4f4;
                padding: 0 20px;
                margin-bottom: 5px;
            }
        }

        .treeUserb {
            width: calc(100% - 100px);
            border: 1px solid #ddd;
            background: #f4f4f4;
            border-radius: 3px;
            height: auto;
            overflow: hidden;

            p {
                display: inline-block;
                padding: 0 20px;
                margin-bottom: 5px;
            }
        }
    }

    .adv-type {
        margin-top: 20px;
        display: flex;
        justify-content: space-between;
        flex-wrap: wrap;
        // border: 1px solid #ddd;
        border-radius: 5px;
        padding: 20px;
        box-shadow: 0 2px 12px 0 rgba(0, 0, 0, .1);

        div {
            width: 33%;
            text-align: center;
            height: 80px;
            overflow: hidden;
            min-width: 100px;
            cursor: pointer;
            padding-top: 10px;
            margin: 10px 0;
        }

        div:hover {
            background: #CFE8FC;
            border-radius: 5px;
        }

        span {
            width: 100%;
            display: inline-block;
            height: 40px;

            // line-height: 40px;
            i {
                color: #0074D9;
                font-size: 36px;
            }
        }
    }
    .treeUserp {
        p {
            padding: 5px;
        }

        p:hover {
            background: #01BEF0;
            color: #fff;
            cursor: pointer;
        }
    }
</style>
