<style>
    .addchannelcode .ant-card-head {
        min-height: 48px;
        line-height: 48px;
        margin-bottom: -1px;
        /* padding: 0 24px; */
        color: rgba(0, 0, 0, .85);
        font-weight: 500;
        font-size: 16px;
        background: transparent;
        border-bottom: 1px solid #e8e8e8;
        border-radius: 2px 2px 0 0;
        zoom: 1;
    }

    .addchannelcode .tag-item {
        padding-top: 10px;
        display: -webkit-box;
        display: -ms-flexbox;
        display: flex;
        -ms-flex-wrap: wrap;
        flex-wrap: wrap;
    }

    .addchannelcode .tag-content {
        flex: 1;
        margin-left: 20px;
        line-height: 32px;
    }

    .addchannelcode .tag-content .ant-tag {
        display: inline-block;
        height: auto;
        margin-right: 8px;
        padding: 0 7px;
        font-size: 12px;
        line-height: 20px;
        white-space: nowrap;
        background: #fafafa;
        border: 1px solid #d9d9d9;
        border-radius: 2px;
        cursor: pointer;
        opacity: 1;

    }

    .addchannelcode .ant-tag-has-color {
        color: #FFF;
        border: 1px solid #1890ff;
    }

    .addchannelcode .ant-alert {
        width: 100%;
        background-color: #fff1f0;
        color: #000;
        font-size: 14px;
        padding: 10px 24px;
        display: flex;
        align-items: center;
        margin-top: 15px;
    }

    .addchannelcode .weekstyle {
        background: #fafafa;
        border-bottom: 1px solid #e8e8e8;
        width: 14.28%;
        height: 50px;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .addchannelcode .weekstyle2 {
        width: 14.28%;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
    }
    .addchannelcode .weekstyle2 .weekstyle2_item div{
        height: 40px;
        line-height: 40px;
    }

    .addchannelcode .tsbox .el-date-editor.el-input,
    .addchannelcode .tsbox .el-date-editor.el-input__inner {
        width: 140px;
        margin-right: 10px;
    }
    .addchannelcode .ant-tag {
        display: inline-block;
        height: auto;
        margin-right: 8px;
        padding: 0 7px;
        font-size: 12px;
        line-height: 20px;
        white-space: nowrap;
        background: #fafafa;
        border: 1px solid #d9d9d9;
        border-radius: 2px;
        cursor: pointer;
        opacity: 1;

    }

    .addchannelcode .ant-tag-has-color {
        color: #FFF;
        border: 1px solid #1890ff;
    }
</style>

<template>
    <div class="addchannelcode">
        <div class="page-body">
            <div class="ant-card-head">
                基础设置
            </div>
            <el-form ref="addMsg" :model="addMsg" label-width="120px" style="padding: 15px 0 ;width: 60%;">
                <el-form-item label="选择分组：">
                    <q-select filled stack-label option-value="Id" option-label="Name" v-model="addMsg.ChannelGroupId"
                        ref="Id" :options="GroupList" label="分组" :dense="false" emit-value map-options />
                </el-form-item>
                <el-form-item label="活码名称：">
                    <q-input v-model="addMsg.Name" filled clearable label="活码名称" :rules="[val => !!val || '请输入活码名称']" :disable='Nameforbid' />
                    <div style="color: red;">(一旦创建，不可修改)</div>
                </el-form-item>
                <el-form-item label="自动添加好友：">
                    <div class="q-gutter-sm" style="align-items: center;display: flex;width: 160%;">
                        <q-radio v-model="addMsg.SkipVerify" :val="x.Id" :label="x.Name" v-for="(x,y) in radiolist2" :key='y' />
                        <span style="color: red;">（开启时，客户添加时无需企业成员确认，自动成为好友）</span>
                    </div>
                </el-form-item>
                <el-form-item label="客户标签：">
                    <div class="q-gutter-sm" style="width: 120%;padding: 15px;border: 1px solid #e8e8e8;">
                        <ul v-if="Lableshow == false">
                            <li class="tag-item" v-for="(x,y) in LableList" :key='y'>
                                <el-tag effect="plain" style="min-width: 70px;text-align: center;overflow: hidden;"> {{ x.Name }} </el-tag>
                                <div class="tag-content" >
                                    <span class="ant-tag" :class="{'ant-tag-has-color':item.chek}"
                                        v-for="(item,index) in x.ChildList" :key='index'
                                        @click="LableList[y].ChildList[index].chek = !LableList[y].ChildList[index].chek,$forceUpdate()"
                                        :style="{background:item.chek?'#1890ff':'#FFF'}">{{item.Name}}</span>
                                </div>
                            </li>
                        </ul>
                        <ul v-if="Lableshow == true">
                            <li class="tag-item" v-for="(x,y) in LableList" :key='y' v-if="y<5">
                                <el-tag effect="plain" style="min-width: 70px;text-align: center;overflow: hidden;"> {{ x.Name }} </el-tag>
                                <div class="tag-content">
                                    <span class="ant-tag" :class="{'ant-tag-has-color':item.chek}"
                                        v-for="(item,index) in x.ChildList" :key='index'
                                        @click="LableList[y].ChildList[index].chek = !LableList[y].ChildList[index].chek,$forceUpdate()"
                                        :style="{background:item.chek?'#1890ff':'#FFF'}">{{item.Name}}</span>
                                </div>
                            </li>
                        </ul>
                        <div v-if="LableList.length>5">
                            <q-btn flat color="primary" label="展开" v-if="Lableshow == true"
                                @click="Lableshow = !Lableshow" />
                            <q-btn flat color="primary" label="收起" v-if="Lableshow == false"
                                @click="Lableshow = !Lableshow" />
                        </div>

                    </div>
                </el-form-item>


            </el-form>
        </div>
        <div class="page-body" v-show="!loading">
            <div class="ant-card-head">
                引流成员设置
            </div>
            <div class="ant-alert">
                扫码添加成员规则：在周期和特殊时期共存情况下，扫码优先添加特殊时期的成员
            </div>
            <el-form label-width="120px" style="padding: 15px 0 ;">
                <el-form-item label="类型：">
                    <div class="q-gutter-sm" style="align-items: center;display: flex;">
                        <q-radio :disable = 'Nameforbid' v-model="addMsg.Type" :val="x.Id" :label="x.Name" v-for="(x,y) in radiolist3" :key='y'  @input="btnType" />
                        <span style="color: red;">(一旦创建,不可修改)</span>
                    </div>
                </el-form-item>
                <el-form-item label="企业成员：">
                    <q-btn color="white" text-color="black" icon='add' label="添加成员" @click="addmember" />
                    <div style="width: 80%;display: flex;">
                        <div v-for="(x,y) in lsweeklist" :key='y' class="weekstyle">
                            <div style="display: flex;align-items: center;">{{x.Name}}</div>
                            <div v-if="x.chek == true">
                                <i class="iconfont icon-xiugai1"
                                    style="margin-left: 8px;font-size: 14px;color: #3FC4FF;" @click="goeditry(x)"></i>
                            </div>
                        </div>
                    </div>
                    <div style="width: 80%;display: flex;align-items: flex-start;" v-if="addMsg.Type==1">
                        <div v-for="(x,y) in lsweeklist" :key="y+'b'" class="weekstyle2">
                            <div v-for="(item,index) in x.lsMsg" :key="index+'a1'" class="weekstyle2_item"
                                style="width: 100%;;display: flex;flex-direction: column;align-items: center;border-bottom: 1px solid #e8e8e8;">
                                <div>{{item.StartHours}}~{{item.EndHours}}</div>
                                <div>
                                    <q-btn v-if="item.EmpId!=0"  outline color="primary" disable :label="item.EmpName" size="sm" />
                                    <q-btn v-if="item.EmpId==0" outline color="#FF0080" disable :label="item.EmpName" size="sm" />
                                </div>
                            </div>
                        </div>
                    </div>
                    <div style="width: 80%;display: flex;align-items: flex-start;" v-if="addMsg.Type==2">
                        <div v-for="(x,y) in lsweeklist" :key="y+'b'" class="weekstyle2">
                            <div v-for="(item,index) in x.lsMsg" :key="index+'a1'" class="weekstyle2_item"
                                style="width: 100%;;display: flex;flex-direction: column;align-items: center;border-bottom: 1px solid #e8e8e8;">
                                <div>{{item.StartHours}}~{{item.EndHours}}</div>
                                <el-tooltip class="item" effect="dark" :content="item.EmpName" placement="top">
                                    <div
                                        style="width: 100%; overflow: hidden; white-space: nowrap;text-overflow: ellipsis;cursor: pointer;text-align: center;">
                                        <span>{{item.EmpName}}</span>
                                    </div>
                                </el-tooltip>

                                <div v-for="(b,m) in item.DeptList" :key="m+'a1'">
                                    <q-btn color="primary" disable :label="b.Name" size="sm" />
                                </div>
                            </div>
                        </div>
                    </div>




                </el-form-item>
                <el-form-item label="特殊时期：">
                    <div class="q-gutter-sm" style="align-items: center;display: flex;">
                        <q-radio v-model="addMsg.SpecialDateEnable" :val="x.Id" :label="x.Name" v-for="(x,y) in radiolist" :key='y' />
                        <span style="color: red;">(如遇到国家节假日或是企业自身重要日子，在特殊时期内，需要更换活码成员)</span>
                    </div>
                    <div v-if="addMsg.SpecialDateEnable==1">
                        <div style="width:70%;background: #f7f5f5;padding: 10px;border-bottom: 1px dashed #e8e8e8;"
                            v-for="(x,y) in spweeklist" :key='y'>
                            <el-form label-width="120px" style="padding: 15px 0 ;">
                                <el-form-item :label="'特殊时期'+(y+1)+'：'">
                                    <el-date-picker v-model="x.value" type="daterange" value-format="yyyy-MM-dd"
                                        @change="getdatepicker(y)" size="small" range-separator="至"
                                        start-placeholder="开始日期" end-placeholder="结束日期">
                                    </el-date-picker>
                                    <div class="tsbox" v-for="(item,index) in x.list" :key='index'>
                                        <div style="display: flex;align-items: flex-start;margin-top: 10px;">
                                            <el-time-select :disabled="item.IsDefault==1" placeholder="起始时间" @change="changeTime(y,index,1)"
                                                v-model="item.StartHours" size="small"
                                                :picker-options="{ start: '00:00', step: '01:00', end: '23:00', }">
                                            </el-time-select>
                                            <el-time-select :disabled="item.IsDefault==1" placeholder="结束时间" @change="changeTime(y,index,1)"
                                                v-model="item.EndHours" size="small"
                                                :picker-options="{ start: '00:00', step: '01:00', end: '23:00',  minTime: item.StartHours}">
                                            </el-time-select>
                                            <el-select v-model="item.EmpList[0].Id" placeholder="请选择" size="small"
                                                v-if="addMsg.Type==1" @change="selectPersonnel(y,index)">
                                                <el-option filterable v-for="item in Employeelist" :key="item.Id"
                                                    :label="item.EmployeeName" :value="item.Id">
                                                </el-option>
                                            </el-select>
                                            <div style="flex:1;width: 1px;margin-top: 3px;" v-if="addMsg.Type==2">
                                                <div v-if="item.EmpList.length>0" style="line-height:20px" >
                                                    <span v-for="(c,v) in item.EmpList "
                                                        :key="v">{{c.DeptName}}{{item.EmpList.length-1
                                                        !=v?'、':''}}</span>
                                                </div>
                                                <div style="display: flex;align-items: center;">
                                                    <el-button
                                                        @click="outerVisible=true,outerindex0=y,outerindex=index,byval=item.EmpList" 
                                                        style="margin-top: 0;"
                                                        size="small">选择企业成员</el-button>
                                                    <q-btn flat color="red" v-if="item.IsDefault!=1" label="删除"
                                                        @click="spweeklist[y].list.splice(index,1),getalluser()" />
                                                </div>
                                            </div>
                                            <q-btn flat color="red" v-if="item.IsDefault!=1 && addMsg.Type==1" label="删除"
                                                @click="spweeklist[y].list.splice(index,1),getalluser()" />
                                        </div>
                                        <div v-if="addMsg.Type==2">
                                            <selectTree class="q-pb-lg q-pr-lg" :treeData='deptl'
                                                style="margin-top: 10px;" :defaultArray="item.returnString"
                                                nodeKey="DeptId" :multiple="true" :defaultExpandAll="true"
                                                labelKey="DeptName" childrenKey="ChildList" tipText="选择部门"
                                                @getChild="getChild($event,y,index)" classStr=" q-pr-lg q-pb-lg">
                                            </selectTree>
                                        </div>




                                    </div>
                                    <div style="display: flex;align-items: center;margin-top: 5px;">
                                        <q-btn flat color="primary" label="添加" @click="adddata(y)" />
                                        <q-btn flat size='sm' icon='delete' color="negative" label=""
                                            v-if="spweeklist.length>1" @click="spweeklist.splice(y,1),getalluser()" />
                                    </div>

                                </el-form-item>
                            </el-form>
                        </div>
                        <q-btn color="primary" label="添加时期" icon='add' size='sm' style="margin-top: 10px;" @click="addalldata()" />
                    </div>
                </el-form-item>

                <el-form-item label="员工添加上限：">
                    <div class="q-gutter-sm" style="align-items: center;display: flex;">
                        <q-radio v-model="addMsg.EmpAddLimit" :val="x.Id" :label="x.Name" v-for="(x,y) in radiolist" :key='y' @input="addMsg.EmpAddLimit==1?getalluser():'' "/>
                        <span style="color: red;">(因受官方限制，无法对动态部门的员工设置添加好友上限，只可针对指定活码成员进行设置)</span>
                    </div>
                    <div style="width: 70%;" v-if="addMsg.EmpAddLimit==1">
                        <el-table :data="addMsg.LimitList" stripe header-align='center' style="width: 100%">
                            <el-table-column prop="EmpName" label="名称" align="center"> </el-table-column>
                            <el-table-column prop="LimitNum" label="上限" align="center">
                                <template slot-scope="scope">
                                    <el-input style="width: 150px;" type='number' v-model="scope.row.LimitNum" :min='0' @input="empAddLimittext(scope)"
                                        size="mini" placeholder="" />
                                </template>
                            </el-table-column>
                        </el-table>

                    </div>
                </el-form-item>
                <el-form-item label="备用员工：" v-if="addMsg.EmpAddLimit==1">
                    <q-select  v-if="addMsg.Type==1"  style="width: 60%;" filled stack-label option-value="Id" option-label="EmployeeName" v-model="addMsg.EmpBackUp"
                        ref="Id" :options="Employeelist" label="备用员工" :dense="false" emit-value map-options />
                    <div style="width: 60%;">
                        <div style="width: 100%;display: flex;flex-wrap: wrap;line-height: 20px;" v-if="EmpBackUpList.length>0">
                            <span v-for="(c,v) in EmpBackUpList "
                            :key="v">{{c.DeptName}}{{EmpBackUpList.length-1
                            !=v?'、':''}}</span>
                        </div>
                        <q-btn color="white" v-if="addMsg.Type==2" text-color="black" icon='add' label="选择企业成员" @click="outerVisible=true,byval=EmpBackUpList,choiceapprovaltype='2'" />

                    </div>
                    <div style="color: red;height: 40px;line-height: 40px;">(备用员工的选择最好不要和员工上限模块里的人员重复)</div>


                </el-form-item>
            </el-form>
        </div>

        <div class="page-body">
            <div class="ant-card-head">
                欢迎语设置
            </div>
            <div class="ant-alert">
                欢迎语推送规则：在通用、周期及特殊时期欢迎语共存情况下，推送优先顺序为特殊时期的欢迎语 > 按周期的欢迎语 > 通用欢迎语。若企业微信官方后台已配置了欢迎语，则在第三方系统配置的欢迎语均失效，客户收到的依然是官方推送的。
            </div>
            <el-form label-width="120px" style="padding: 15px 0 ;">
                <el-form-item label="扫码推送：">
                    <div class="q-gutter-sm" style="align-items: center;display: flex;">
                        <q-radio v-model="addMsg.WelcomeEnable" :val="x.Id" :label="x.Name" v-for="(x,y) in radiolist" :key='y' />
                        <span style="color: red;">(关闭后，客户扫该渠道活码，依然可对该客户自动打标签，但仅收到系统的【欢迎语】消息)</span>
                    </div>
                </el-form-item>
            </el-form>
            <div v-if="addMsg.WelcomeEnable == 1">
                <q-splitter v-model="splitterModel" >
                  <template v-slot:before>
                    <q-tabs
                      v-model="tab"
                      vertical
                    >
                      <q-tab name="mails"  label="通用欢迎语" />
                      <q-tab name="alarms"  label="周期欢迎语" />
                      <q-tab name="movies"  label="特殊时期欢迎语" />
                    </q-tabs>
                  </template>
            
                  <template v-slot:after>
                    <div v-if="tab=='mails'" style="padding: 0 20px;">
                        <div  :key="index"  v-for="(item, index) in addMsg.WelcomeList">
                            <welcomefrom :welcomeList='item' :welcomeindex='index'  @success="welcometrigger" welcomename='WelcomeList'></welcomefrom>

                        </div>
                    </div>
                    <div v-if="tab=='alarms'" style="padding: 0 20px;">
                        <el-form label-width="120px" style="padding: 15px 0 ;">
                            <el-form-item label="状态：">
                                <div class="q-gutter-sm" style="align-items: center;display: flex;">
                                    <q-radio v-model="addMsg.WelcomeWeekEnable" :val="x.Id" :label="x.Name" v-for="(x,y) in radiolist" :key='y' />

                                </div>
                            </el-form-item>
                        </el-form>
                        <div style="width: 100%;text-align: right;">
                            <q-btn color="accent" class="q-mr-md" size="sm" label="新增" @click="handleTabsEdit()"  v-if="addMsg.WelcomeWeekEnable==1"/>
                        </div>
                        <div style="width: 100%;display: flex;align-items: center;justify-content: space-between;" v-if="addMsg.WelcomeWeekEnable==1">
                            
                            <el-tabs v-model="editableTabsValue" type="card"  style="width: 1px;flex:1">
                                <el-tab-pane
                                  :key="index"
                                  v-for="(item, index) in wwlist"
                                  :name="index.toString()"
                                >
                                <span slot="label"> {{'欢迎语'+(index+1)}}<i class="el-icon-circle-close" v-if="wwlist.length>1" style="margin-left: 10px;font-size: 16px;" @click='handleTabsclose(index)'></i></span>
                                <el-form label-width="120px" style="padding-top: 15px 0 ;">
                                    <el-form-item label="选择周期：">
                                        <span class="ant-tag" :class="{'ant-tag-has-color':x.chek}"
                                            v-for="(x,y) in item.weeklist" :key='y'
                                            @click="wwlist[index].weeklist[y].chek = !wwlist[index].weeklist[y].chek,$forceUpdate()"
                                            :style="{background:x.chek?'#1890ff':'#FFF'}">{{x.Name}}</span>
                                    </el-form-item>
                                </el-form>
                                <el-form label-width="120px" >
                                    <el-form-item label="时间段：" v-for="(x,y) in item.childrenlist" :key="y+'a1'" style="border-bottom: 1px solid #e8e8e8;padding: 15px 0 ;">
                                        <div >
                                            <div style="display: flex;align-items: flex-start;" class="tsbox">
                                                <el-time-select  placeholder="起始时间" @change="changeTime(index,y,2)"
                                                    v-model="x.StartHours" size="small"
                                                    :picker-options="{ start: '00:00', step: '01:00', end: '23:00', }">
                                                </el-time-select>
                                                <el-time-select  placeholder="结束时间"
                                                    v-model="x.EndHours" size="small" @change="changeTime(index,y,2)"
                                                    :picker-options="{ start: '00:00', step: '01:00', end: '23:00',  minTime: x.StartHours}">
                                                </el-time-select>
                                                <q-btn flat color="red" v-if="item.childrenlist.length>1" label="删除"
                                                    @click="abv(index,y,x)" />

                                            </div>
                                            <div style="padding: 15px 0 ;">
                                                <welcomefrom :welcomeList='x' :welcomeindex='index' :childrenindex="y" @success="welcometrigger" welcomename='WelcomeWeekList' ></welcomefrom>

                                            </div>

                                        </div>
                                        
                                    </el-form-item>
                                </el-form>
                                <div style="padding: 10px;">
                                    <q-btn color="white" text-color="black" icon='add' label="添加时间段" @click="addwelcomesjd(index)" />
                                </div>
                                </el-tab-pane>
                              </el-tabs>
                        </div>
                       
                    </div>
                    <div v-if="tab=='movies'" style="padding: 0 20px;">
                        <el-form label-width="120px" style="padding: 15px 0 ;">
                            <el-form-item label="状态：">
                                <div class="q-gutter-sm" style="align-items: center;display: flex;">
                                    <q-radio v-model="addMsg.WelcomeSpecialEnable" :val="x.Id" :label="x.Name" v-for="(x,y) in radiolist" :key='y' />
                                </div>
                            </el-form-item>
                        </el-form>
                        <div style="width: 100%;text-align: right;">
                            <q-btn color="accent" class="q-mr-md" size="sm" label="新增" @click="handleTabsEdit2()"  v-if="addMsg.WelcomeSpecialEnable==1"/>
                        </div>
                        <div style="width: 100%;display: flex;align-items: center;justify-content: space-between;" v-if="addMsg.WelcomeSpecialEnable==1">
                            <el-tabs v-model="editableTabsValue2" type="card" style="width: 1px;flex:1">
                                <el-tab-pane
                                  :key="index"
                                  v-for="(item, index) in wsplist"
                                  :name="index.toString()"
                                >
                                <span slot="label"> {{'欢迎语'+(index+1)}}<i class="el-icon-circle-close" v-if="wsplist.length>1" style="margin-left: 10px;font-size: 16px;" @click='handleTabsclose(index,2)'></i></span>
                                <el-form label-width="120px" style="padding-top: 15px 0 ;">
                                    <el-form-item label="选择日期：">
                                        <el-date-picker v-model="item.value" type="daterange" value-format="yyyy-MM-dd"
                                            @change="getdatepicker(index,2)" size="small" range-separator="至"
                                            start-placeholder="开始日期" end-placeholder="结束日期">
                                        </el-date-picker>
                                    </el-form-item>
                                </el-form>
                                <el-form label-width="120px" >
                                    <el-form-item label="时间段：" v-for="(x,y) in item.childrenlist" :key="y+'a1'" style="border-bottom: 1px solid #e8e8e8;padding: 15px 0 ;">
                                        <div >
                                            <div style="display: flex;align-items: flex-start;" class="tsbox">
                                                <el-time-select  placeholder="起始时间"  @change="changeTime(index,y,3)"
                                                    v-model="x.StartHours" size="small"
                                                    :picker-options="{ start: '00:00', step: '01:00', end: '23:00', }">
                                                </el-time-select>
                                                <el-time-select  placeholder="结束时间"  @change="changeTime(index,y,3)"
                                                    v-model="x.EndHours" size="small"
                                                    :picker-options="{ start: '00:00', step: '01:00', end: '23:00',  minTime: x.StartHours}">
                                                </el-time-select>
                                                <q-btn flat color="red" v-if="item.childrenlist.length>1" label="删除"
                                                    @click="abv(index,y,x,2)" />
                                            </div>
                                            <div style="padding: 15px 0 ;">
                                                <welcomefrom :welcomeList='x' :welcomeindex='index' :childrenindex="y" @success="welcometrigger" welcomename='WelcomeSpecialList' ></welcomefrom>

                                            </div>

                                        </div>
                                        
                                    </el-form-item>
                                </el-form>
                                <div style="padding: 10px;">
                                    <q-btn color="white" text-color="black" icon='add' label="添加时间段" @click="addwelcomesjd(index,2)" />
                                </div>
                                </el-tab-pane>
                              </el-tabs>
                        </div>
                    </div>
                  </template>
            
                </q-splitter>
              </div>
        </div>
        <div class="page-body">
            <q-btn color="accent" class="q-mr-md" label="保存" @click="savemove()" style="width: 100px;"
                :loading="loading2" />
            <q-btn class="q-mr-md" label="返回" @click="goblck()" style="width: 60px;" />
        </div>
        <chaneelcodeform v-if="ismember" @close="getclose()" :peopletype='addMsg.Type' :lsday="lsday"
            :lsweeklist="lsweeklist" @success="getsuccess"></chaneelcodeform>
        <choiceapproval v-if="outerVisible" @close="getclose()" @success="getsuccess2" :byval='byval' :cptype="choiceapprovaltype"></choiceapproval>

    </div>

</template>

<script>
    import {
        getWeChatChannelGroupList,
        getWeChatLableList,//获取标签的列表
        setWeChatChannelInfo,//渠道活码的新增和修改
        getWeChatChannelInfo,//获取详情
    } from '../../api/system/wechat';
    import {
        getDeptTree
    } from '../../api/system/dept.js'
    import {
        queryEmployee
    } from '../../api/users/user'
    import chaneelcodeform from '../../components/enterprise/chaneelcode-form'
    import choiceapproval from '../../components/enterprise/choiceapproval'
    import  welcomefrom from '../../components/enterprise/welcome-from'
   
    import selectTree from '../../components/common/select-tree'


    export default {
        meta: {
            title: "渠道码编辑"
        },
        components: {
            chaneelcodeform,//所有列表
            choiceapproval,
            selectTree,
            welcomefrom
        },
        name: "addchannelcode",
        data() {
            return {
                loading: false,
                loading2:false,
                radiolist:[{Id:1,Name:"开启"},{Id:2,Name:'关闭'}],
                radiolist2:[{Id:1,Name:"需验证"},{Id:2,Name:'自动添加'}],
                radiolist3:[{Id:1,Name:"单人"},{Id:2,Name:'多人'}],
                addMsg: {
                    Id: 0,
                    ChannelGroupId: '',//分组Id
                    Name: '',//渠道活码名称
                    SkipVerify: 1,//自动添加好友 1需验证 2自动添加
                    LabelIdList: [],//标签
                    Type: 1,//类型 1单人 2多人
                    SpecialDateEnable: 2,//特殊时期 1开启 2未开启
                    EmpAddLimit: 2,//员工添加上限 1开启 2未开启
                    WeekList: [],//人员周期
                    SpecialWeekList: [],//特殊周期
                    LimitList: [],//员工添加上限
                    EmpBackUp:'',//备用员工 多人的话 英文逗号分隔	
                    WelcomeEnable:2,//是否启用欢迎语 1是
                    WelcomeList:[{Id:0,Content:'',MediumIds:'',MediumModel:[]}],//通用欢迎语
                    WelcomeWeekEnable:2,//周期欢迎语 1启用
                    WelcomeWeekList:[],
                    WelcomeSpecialEnable:2,//特殊时期欢迎语 1启用
                    WelcomeSpecialList:[],
                },
                GroupList: [],//分组数据
                LableList: [],//标签数据
                Lableshow: false,
                ismember: false,//成员弹窗
                lsMsg: [],//人员临时的msg
                lsweeklist: [{ Id: 1, Name: '周一', chek: false ,lsMsg:[]}, { Id: 2, Name: '周二', chek: false ,lsMsg:[]}, { Id: 3, Name: '周三', chek: false ,lsMsg:[]}, { Id: 4, Name: '周四', chek: false ,lsMsg:[]}, { Id: 5, Name: '周五', chek: false ,lsMsg:[]}, { Id: 6, Name: '周六', chek: false ,lsMsg:[]}, { Id: 0, Name: '周日', chek: false ,lsMsg:[]}],//周末
                editType: '',//选中修改
                lsday: '',//修改的时候记录是哪天的
                spweeklist: [
                    { Id: 0, StartDate: '', EndDate: '', value: '', list: [{ IsDefault: 1, StartHours: '00:00', EndHours: '00:00', EmpList: [{ Id: '', Name: '' }], DeptList: [], returnString: [] }] }
                ],
                Employeelist: [],//人员单选数据
                deptl: [],//部门下拉
                outerVisible: false,
                outerindex: 0,
                outerindex0: 0,
                byval: [],
                EmpBackUpList:[],//备用员工多人的时候临时数组
                choiceapprovaltype:'1',
                tab: 'mails',
                splitterModel: 13,
                welcomeindex:'0',
                wwlist:[{weeklist:[{ Id: 1, Name: '周一', chek: false }, { Id: 2, Name: '周二', chek: false }, { Id: 3, Name: '周三', chek: false }, { Id: 4, Name: '周四', chek: false }, { Id: 5, Name: '周五', chek: false }, { Id: 6, Name: '周六', chek: false }, { Id: 0, Name: '周日', chek: false }],
                    childrenlist:[{StartHours:'',EndHours:'',Id:0,Content:'',MediumIds:'',MediumModel:[]}]
                    }],//周期欢迎语 
                editableTabsValue:'0',
                editableTabsValue2:'0',
                wsplist:[{StartDate:'',EndDate:'', value:[],childrenlist:[{StartHours:'',EndHours:'',Id:0,Content:'',MediumIds:'',MediumModel:[]}] }],//特殊欢迎语 
                isall:false,//验证通过
                Nameforbid:false,//渠道名称 修改时不能改变
            }
        },
        mounted(){
            if(this.$route.query && this.$route.query.Id){
                    this.getqudaodetails()//获取详情
                    this.Nameforbid=true
            }
        },
        created() {
            this.getChannelGroupList()//分租列表
            this.getLableList()//获取标签列表
            this.queryEmployee()//单选
            this.getDepartList()//部门下拉
        },
        methods: {
            getqudaodetails(){//渠道详情
                getWeChatChannelInfo({ChannelId:this.$route.query.Id}).then(res => {
                    if (res.Code == 1) {
                        this.loading = true
                        this.addMsg = res.Data
                        if(this.addMsg.LabelIdList && this.addMsg.LabelIdList.length>0){
                            this.addMsg.LabelIdList.map(z=>{//处理标签
                                this.LableList.map(x => {
                                    let isexist = false
                                    x.ChildList.map(j => {
                                        if(z == j.Id){
                                            j.chek = true
                                            isexist=true
                                        }
                                    })
                                    if(isexist == true){
                                        return
                                    }
                                })
                            })
                        }
                        
                        if(this.addMsg.Type ==1){
                            this.addMsg.WeekList.map(x=>{//单人模式处理 人员
                                this.lsweeklist.map(j=>{
                                    if(x.Week == j.Id){
                                        j.chek = true;
                                        // j.lsMsg= []
                                        if(x.EmpList && x.EmpList.length>0){
                                            x.EmpList.forEach(z=>{
                                                let obj={
                                                    EmpId: z.Id,
                                                    EmpName: z.Name,
                                                    Id: x.Id,
                                                    IsDefault: x.IsDefault,
                                                    StartHours:x.StartHours,
                                                    EndHours:x.EndHours,
                                                }
                                                j.lsMsg.push(obj)
                                            })
                                        }else{
                                            let obj={
                                                    EmpId: 0,
                                                    EmpName: '离职',
                                                    Id: x.Id,
                                                    IsDefault: x.IsDefault,
                                                    StartHours:x.StartHours,
                                                    EndHours:x.EndHours,
                                                }
                                                j.lsMsg.push(obj)
                                        }
                                       
                                        return
                                    }
                                })
                            })
                        }else{
                            this.lsweeklist.map(x=>{//多人模式
                                this.addMsg.WeekList.map(j=>{
                                    if(x.Id == j.Week){
                                        x.chek = true;
                                        // x.lsMsg = []
                                        let eplist = [];
                                        let EmpName = '';
                                        j.EmpList.forEach(z=>{
                                            let obj = {
                                                DeptId:z.Id,
                                                DeptName:z.Name,
                                            }
                                            eplist.push(obj)
                                            EmpName = EmpName + z.Name + ','
                                        })
                                        let obj = {
                                            Id:j.Id,
                                            IsDefault:j.IsDefault,
                                            StartHours:j.StartHours,
                                            EndHours:j.EndHours,
                                            EmpList:eplist,
                                            DeptList:j.DeptList,
                                            EmpName:EmpName,
                                            returnString:[]
                                        }
                                        x.lsMsg.push(obj)
                                    }
                                })
                            })

                            //多人的时候 备用员工处理处理
                            if(this.addMsg.EmpBackUp !='' ){
                                let data = this.addMsg.EmpBackUp.split(',') 
                                this.EmpBackUpList =[]
                                data.forEach(x=>{
                                    this.Employeelist.map(j=>{
                                        if(x == j.Id){
                                            let obj = {
                                                DeptId:j.Id,
                                                DeptName:j.EmployeeName
                                            }
                                            this.EmpBackUpList.push(obj)
                                            return
                                        }
                                    })
                                })
                            }
                         
                        }
                         //特殊时期 数据处理
                        if(this.addMsg.SpecialDateEnable==1  &&  this.addMsg.SpecialWeekList.length>0){
                           this.spweeklist= [];
                           this.addMsg.SpecialWeekList.forEach((x,y)=>{
                               let list = JSON.parse(JSON.stringify(x.ChlidList))
                               
                            
                               if(this.addMsg.Type==2){
                                setTimeout(()=>{
                                    list = [];
                                    x.ChlidList.forEach(j=>{
                                        let returnString = []
                                        let a = []
                                        j.EmpList.forEach(z=>{
                                            let epobj={
                                                DeptId:z.Id,
                                                DeptName:z.Name,
                                            }
                                            a.push(epobj)
                                        })
                                        j.DeptList.forEach(z=>{
                                            returnString.push(z.Id)
                                        })
                                        let obj = {
                                            DeptList:j.DeptList,
                                            EmpList:a,
                                            EndHours:j.EndHours,
                                            IsDefault:j.IsDefault,
                                            StartHours:j.StartHours,
                                            returnString:returnString,
                                        } 
                                        list.push(obj)
                                    })
                                    this.spweeklist[y].list = JSON.parse(JSON.stringify(list))
                                },10)
                               }
                               
                                let obj= {
                                    StartDate:x.StartDate,
                                    EndDate:x.EndDate,
                                    value:[x.StartDate,x.EndDate],
                                    list:list
                                }
                                this.spweeklist.push(obj)
                           })
                        }
                        if(this.addMsg.EmpAddLimit==1 ){
                            this.addMsg.EmpBackUp = Number(this.addMsg.EmpBackUp)
                        }
                        //欢迎语通用处理
                        if(this.addMsg.WelcomeList==null || this.addMsg.WelcomeList.length==0){
                            this.addMsg.WelcomeList=[{Id:0,Content:'',MediumIds:'',MediumModel:[]}]//通用欢迎语
                        }else if(this.addMsg.WelcomeList.length>0){
                            this.addMsg.WelcomeList[0].MediumModel = this.addMsg.WelcomeList[0].MediumList?this.addMsg.WelcomeList[0].MediumList:[]
                        }                        
                        //周期欢迎语的处理
                        if(this.addMsg.WelcomeWeekList && this.addMsg.WelcomeWeekList.length>0){
                            let data = this.addMsg.WelcomeWeekList
                            data.forEach(x=>{
                                x.weeklist = [{ Id: 1, Name: '周一', chek: false }, { Id: 2, Name: '周二', chek: false }, { Id: 3, Name: '周三', chek: false }, { Id: 4, Name: '周四', chek: false }, { Id: 5, Name: '周五', chek: false }, { Id: 6, Name: '周六', chek: false }, { Id: 0, Name: '周日', chek: false }]
                                x.childrenlist = x.ChildList
                                if(x.weeklist && x.weeklist.length>0){
                                    x.weeklist.forEach(j=>{
                                        if(x.Weeks.indexOf(j.Id) !=-1){
                                            j.chek = true
                                        }
                                    })

                                }
                                if(x.childrenlist && x.childrenlist.length>0){
                                    x.childrenlist.forEach(j=>{
                                        j.MediumModel = j.MediumList?j.MediumList:[]
                                    })
                                }
                            })
                            this.wwlist = data
                            
                        }
                        // wsplist:[{StartDate:'',EndDate:'', value:[],childrenlist:[{StartHours:'',EndHours:'',Id:0,Content:'',MediumIds:'',MediumModel:[]}] }],//特殊欢迎语 
                        //特殊欢迎语的处理
                        if(this.addMsg.WelcomeSpecialList && this.addMsg.WelcomeSpecialList.length>0){
                            let data = this.addMsg.WelcomeSpecialList
                            data.forEach(x=>{
                                x.value = [x.StartDate,x.EndDate]
                                x.childrenlist = x.ChildList
                                if(x.childrenlist && x.childrenlist.length>0){
                                    x.childrenlist.forEach(j=>{
                                        j.MediumModel = j.MediumList?j.MediumList:[]
                                    })
                                }
                            })
                            this.wsplist = data
                            
                        }
                        
                        this.loading = false
                    }
                }).catch(() => {

                })
            },
            queryEmployee() {//下拉人员列表
                queryEmployee({IsLeave:1}).then(res => {
                    if (res.Code == 1) {
                        this.Employeelist = res.Data
                    }
                }).catch(() => {

                })
            },
            //获取部门下拉数据
            getDepartList(id) {
                let msg = {
                    School_Id: 0
                }
                getDeptTree(msg).then(res => {
                    if (res.Code == 1) {
                        this.deptl = res.Data;
                    }
                })
               
            },
            getChannelGroupList() {
                getWeChatChannelGroupList({}).then(res => {
                    if (res.Code == 1) {
                        this.GroupList = res.Data;
                    }
                }).catch(() => {

                })
            },

            getLableList() {
                getWeChatLableList({}).then(res => {
                    if (res.Code == 1) {
                        this.LableList = res.Data;
                        if (this.LableList.length > 5) {
                            this.Lableshow = true
                        }
                        this.LableList.forEach(x => {
                            x.ChildList.forEach(j => {
                                j.chek = false
                            })
                        })
                    }
                }).catch(() => {

                })

            },
            addmember() {
                this.ismember = true
                this.lsday = ''
            },
            getclose() {// 企业关闭
                this.ismember = false
                this.outerVisible = false
            },
            getsuccess(msg, lsweeklist) {//人员选择返回数据
                this.lsMsg = JSON.parse(JSON.stringify(msg))
                this.lsweeklist = lsweeklist;
                if (this.addMsg.Type == 2) {//如果选择多人的时候
                    this.lsweeklist.forEach(x => {
                        if (x.chek == true) {
                            x.lsMsg.forEach(j => {
                                j.EmpName = '';
                                j.EmpList.forEach(z => {
                                    j.EmpName = j.EmpName + z.DeptName + ','
                                })
                            })
                        }
                    })
                }
                this.getclose()
                this.getalluser()

            },
            getsuccess2(list,type) {
                if(type==1){
                    this.spweeklist[this.outerindex0].list[this.outerindex].EmpList = JSON.parse(JSON.stringify(list))
                    this.outerVisible = false;
                    this.getalluser()
                }else{
                    this.EmpBackUpList = list
                    this.outerVisible = false;

                }
                
            },
            goeditry(x) {
                this.lsday = (x.Id).toString();
                this.lsMsg = x.lsMsg
                this.ismember = true;
            },
            btnType() {//人员选择清除数据 //数据的初始值
                this.lsMsg = [];
                this.lsday = '';
                this.lsweeklist = [{ Id: 1, Name: '周一', chek: false }, { Id: 2, Name: '周二', chek: false }, { Id: 3, Name: '周三', chek: false }, { Id: 4, Name: '周四', chek: false }, { Id: 5, Name: '周五', chek: false }, { Id: 6, Name: '周六', chek: false }, { Id: 0, Name: '周日', chek: false }]

                if (this.addMsg.Type == 1) {//单人模式
                    this.spweeklist = [{ Id: 0, StartDate: '', EndDate: '', value: '', list: [{ IsDefault: 1, StartHours: '00:00', EndHours: '00:00', EmpList: [{ Id: '', Name: '' }], DeptList: [], returnString: [] }] }]
                } else {
                    this.spweeklist = [{ Id: 0, StartDate: '', EndDate: '', value: '', list: [{ IsDefault: 1, StartHours: '00:00', EndHours: '00:00', EmpList: [], DeptList: [], returnString: [] }] }]
                }
                this.addMsg.LimitList=[];
                this.addMsg.EmpBackUp = '';
                this.EmpBackUpList= [];

            },
            goblck() {//返回上一个页面
                this.$router.go(-1);//返回上一层
            },
            savemove() {
                this.isall = false//初始化
                if(this.addMsg.ChannelGroupId==''){
                    this.$q.notify({
                        type: 'negative',
                        message: `请选择分组`,
                        position: 'top'
                    })
                    return                         
                }
                if(this.addMsg.Name==''){
                    this.$q.notify({
                        type: 'negative',
                        message: `请选择渠道活码名称`,
                        position: 'top'
                    })
                    return
                         
                }
                this.addMsg.LabelIdList = []
                this.LableList.forEach(x => {//标签数据处理
                    x.ChildList.forEach(x => {
                        if (x.chek == true) {
                            this.addMsg.LabelIdList.push(x.Id)
                        }
                    })
                })
                this.addMsg.WeekList = [];
                this.getWeekListdata()//处理企业人员的数据 给予后台
                if (this.addMsg.SpecialDateEnable == 1 && this.isall == false) {
                    this.getSpecialWeekListdata()//特殊时期的数据
                }

                if(this.addMsg.Type==2 && this.addMsg.EmpAddLimit==1 && this.isall == false){//多人 备用开启 处理备用人员数据
                    if(this.EmpBackUpList.length>0){
                        let data = []
                        this.EmpBackUpList.forEach(x=>{
                            data.push(x.DeptId)
                        })
                        this.addMsg.EmpBackUp = data.join(',')
                    }
                }
                //验证欢迎语

                if(this.addMsg.WelcomeEnable==1 && this.isall == false){
                    this.welcometverification()
                }
                if(this.isall == false){
                    //调用接口
                    this.loading2 = true
                    setWeChatChannelInfo(this.addMsg).then(res => {
                    if (res.Code == 1) {
                        this.$q.notify({
                            icon: 'iconfont icon-chenggong',
                            color: 'accent',
                            timeout: 2000,
                            message: res.Message,
                            position: 'top'
                        })
                        this.loading2 = false
                        this.goblck()
                        }
                    }).catch(() => {
                        this.loading2 = false

                    })
                }
            },
            welcometverification(){//欢迎语的验证
                //1 通用验证
                if(this.addMsg.WelcomeList[0].Content=='' || this.addMsg.WelcomeList[0].Content==null){
                    this.$q.notify({
                        type: 'negative',
                        message: `请输入通用欢迎语的内容`,
                        position: 'top'
                    })
                    this.isall = true
                    return
                }
                if(this.addMsg.WelcomeList[0].MediumModel && this.addMsg.WelcomeList[0].MediumModel .length>0){
                    let ids= []
                    this.addMsg.WelcomeList[0].MediumModel.forEach(z=>{
                        ids.push(z.Id)
                    })
                    this.addMsg.WelcomeList[0].MediumIds = ids.join(',');
                        
                }
                //2 周期欢迎语
                if(this.addMsg.WelcomeWeekEnable==1){
                    let isweek =  false;
                    let isContent =  false;
                    let isHours =  false;
                    let data=[]
                
                    this.wwlist.map(x=>{
                        isweek =  false;
                        let obj={}
                        let weekl = []
                        x.weeklist.map(j=>{
                            if(j.chek == true){
                                isweek = true
                                weekl.push(j.Id)
                            }
                        })
                        obj.Weeks = weekl.join(',')
                        if(isweek == false){
                            this.$q.notify({
                                type: 'negative',
                                message: `请选择欢迎语周期`,
                                position: 'top'
                            })
                            this.isall = true
                            return
                        }
                        x.childrenlist.map(j=>{
                            if (j.StartHours == '' || j.EndHours == '') {
                                isHours = true;
                                return
                            }
                            if(j.Content == ''){
                                isContent = true
                                return
                            }
                            obj.StartHours = j.StartHours;
                            obj.EndHours = j.EndHours;
                            obj.Id = j.Id;
                            obj.Content = j.Content;
                            let ids= []
                            if(j.MediumModel.length>0){
                                j.MediumModel.forEach(z=>{
                                    ids.push(z.Id)
                                })
                            }
                            obj.MediumIds = ids.join(',');
                            data.push(JSON.parse(JSON.stringify(obj)) )
                            
                        })
                        if(isHours == true){
                            this.$q.notify({
                                type: 'negative',
                                message: `请输入周期欢迎语的时间段`,
                                position: 'top'
                            })
                            this.isall = true
                            return
                        }
                        if(isContent == true){
                            this.$q.notify({
                                type: 'negative',
                                message: `周期欢迎语的内容不能为空`,
                                position: 'top'
                            })
                            this.isall = true
                            return
                        }
                        this.addMsg.WelcomeWeekList = data

                    })
                }
                if(this.addMsg.WelcomeSpecialEnable==1){
                    let datevalue =  false;
                    
                    let isContent =  false;
                    let isHours =  false;
                    let data=[]
                    this.wsplist.map(x=>{
                        let obj={}
                        if (!x.value || x.value == [] || x.value == '' || x.value.length==0) {
                            this.$q.notify({
                                type: 'negative',
                                message: `请选择特殊时期的欢迎语日期范围`,
                                position: 'top'
                            })
                            this.isall = true
                            return
                        }
                       
                        obj.StartDate = x.value[0]
                        obj.EndDate = x.value[1]
                        x.childrenlist.map(j=>{
                            if (j.StartHours == '' || j.EndHours == '') {
                                isHours = true;
                                return
                            }
                            if(j.Content == ''){
                                isContent = true
                                return
                            }
                            obj.StartHours = j.StartHours;
                            obj.EndHours = j.EndHours;
                            obj.Id = j.Id;
                            obj.Content = j.Content;
                            let ids= []
                            if(j.MediumModel.length>0){
                                j.MediumModel.forEach(z=>{
                                    ids.push(z.Id)
                                })
                            }
                            obj.MediumIds = ids.join(',');
                            data.push(JSON.parse(JSON.stringify(obj)) )
                            
                        })
                        if(isHours == true){
                            this.$q.notify({
                                type: 'negative',
                                message: `请输入周期欢迎语的时间段`,
                                position: 'top'
                            })
                            this.isall = true
                            return
                        }
                        if(isContent == true){
                            this.$q.notify({
                                type: 'negative',
                                message: `周期欢迎语的内容不能为空`,
                                position: 'top'
                            })
                            this.isall = true
                            return
                        }
                        this.addMsg.WelcomeSpecialList = data

                    })
                }

            },
            getSpecialWeekListdata() {
                let isEmpId = false;
                let isHours = false;
                let datevalue = false;
                let datalength = this.spweeklist.length;//获取特殊时期的长度
                let spdata = []
                this.spweeklist.map((x, i) => {
                    if (!x.value || x.value == [] || x.value == '') {
                        datevalue = true;
                        return
                    }
                    x.list.map((j, ji) => {
                        if (isEmpId) return
                        if (ji > 0) {
                            if (j.StartHours == '' || j.EndHours == '') {
                                isHours = true;
                                return
                            }
                            if (this.addMsg.Type == 1) {
                                j.EmpList.map(c => {
                                    if (c.Id == '') {
                                        isEmpId = true;
                                        return
                                    }
                                })
                            } else {
                                if (j.EmpList.length == 0 && j.DeptList.length == 0) {
                                    isEmpId = true;
                                    return
                                }
                            }


                        } else {
                            if (this.addMsg.Type == 1) {
                                j.EmpList.map(c => {
                                    if (c.Id == '') {
                                        isEmpId = true;
                                        return
                                    }
                                })
                            } else {
                                if (j.EmpList.length == 0 && j.DeptList.length == 0) {
                                    isEmpId = true;
                                    return
                                }
                            }
                        }
                        if (isEmpId) return
                        if (isHours) return
                        let dlist = [];
                        let eplist=j.EmpList
                        if (this.addMsg.Type == 2) {//保存时部门赋值
                            dlist = j.DeptList;
                            let list = []
                            j.EmpList.forEach(m=>{
                                let obj={
                                    Id:m.DeptId,
                                    Name:m.DeptName,
                                }
                                list.push(obj)
                            })
                            eplist= list
                        }
                        
                        let obj = {
                            Id: 0,
                            StartDate: x.value[0],
                            EndDate: x.value[1],
                            IsDefault: j.IsDefault,
                            StartHours: j.StartHours,
                            EndHours: j.EndHours,
                            EmpList: eplist,
                            DeptList: dlist,
                        }
                        spdata.push(obj)
                    })
                })

                if (datevalue == true) {
                    this.$q.notify({
                        type: 'negative',
                        message: `请选择特殊时期时间段`,
                        position: 'top'
                    })
                    this.isall = true
                    return
                }
                if (isHours == true) {
                    this.$q.notify({
                        type: 'negative',
                        message: `请选择特殊时期时间`,
                        position: 'top'
                    })
                    this.isall = true
                    return
                }
                if (isEmpId == true) {
                    this.$q.notify({
                        type: 'negative',
                        message: `请选择特殊时期成员`,
                        position: 'top'
                    })
                    this.isall = true
                    return
                }
                this.addMsg.SpecialWeekList = spdata

            },
            getWeekListdata() {
                let show = false
                let show2 = false
                this.lsweeklist.map(x => {
                    
                    if(show == true){
                        return
                    }
                    if(show2 == true){
                        return
                    }
                    if (x.chek == true) {
                        if (this.addMsg.Type == 1) {
                            x.lsMsg.map(j => {
                                if(j.EmpId==0){//判断是否是离职的员工
                                    this.isall = true
                                    show2 = true
                                    this.$q.notify({
                                        type: 'negative',
                                        message: `周期人员有人离职,请重新选择`,
                                        position: 'top'
                                    })
                                    return
                                }
                                if(show2== true){
                                    return
                                }
                                let obj = {
                                    Id: j.Id,
                                    Week: x.Id,
                                    IsDefault: j.IsDefault,
                                    StartHours: j.StartHours,
                                    EndHours: j.EndHours,
                                    EmpList: [
                                        { Id: j.EmpId, Name: j.EmpName }
                                    ],
                                    DeptList: [],
                                };
                                this.addMsg.WeekList.push(obj)
                            })
                        } else {
                            
                            x.lsMsg.forEach(j => {
                                let eplist = []
                                j.EmpList.forEach(z=>{
                                    let epobj={
                                        Id:z.DeptId,
                                        Name:z.DeptName,
                                    }
                                    eplist.push(epobj)
                                })
                                let obj = {
                                    Id: j.Id,
                                    Week: x.Id,
                                    IsDefault: j.IsDefault,
                                    StartHours: j.StartHours,
                                    EndHours: j.EndHours,
                                    EmpList: eplist,
                                    DeptList: j.DeptList,
                                };
                                this.addMsg.WeekList.push(obj)
                            })
                        }

                    }else{

                        this.$q.notify({
                            type: 'negative',
                            message: `请完善周期数据`,
                            position: 'top'
                        })
                        this.isall = true
                        show = true
                        return
                        
                    }
                   
                })
            },
            adddata(y) {//特殊时期的内部添加
                let obj
                if (this.addMsg.Type == 1) {//单人模式
                    obj = { IsDefault: 2, StartHours: '', EndHours: '', EmpList: [{ Id: '', Name: '' }], DeptList: [], returnString: [] }
                } else {
                    obj = { IsDefault: 2, StartHours: '', EndHours: '', EmpList: [], DeptList: [], returnString: [] }
                }
                this.spweeklist[y].list.push(obj)

            },
            addalldata() {//特殊时期的外部添加
                let obj
                if (this.addMsg.Type == 1) {//单人模式
                    obj = { Id: 0, StartDate: '', EndDate: '', value: '', list: [{ IsDefault: 1, StartHours: '00:00', EndHours: '00:00', EmpList: [{ Id: '', Name: '' }], DeptList: [], returnString: [] }] }
                } else {
                    obj = { Id: 0, StartDate: '', EndDate: '', value: '', list: [{ IsDefault: 1, StartHours: '00:00', EndHours: '00:00', EmpList: [], DeptList: [], returnString: [] }] }
                }
                this.spweeklist.push(obj)

            },
            selectPersonnel(y, index) {
                this.Employeelist.map(x => {//部门id带过去
                    if (x.Id == this.spweeklist[y].list[index].EmpList[0].Id) {
                        this.spweeklist[y].list[index].EmpList[0].Name = x.EmployeeName;
                        return
                    }
                })
                this.getalluser()
            },
            getdatepicker(index,type=1) {//特殊时期日期范围判断
                let value 
                let data 
                if(type==2){
                    value = JSON.parse(JSON.stringify(this.wsplist[index].value))
                    data  = this.wsplist
                }else{
                     value = JSON.parse(JSON.stringify(this.spweeklist[index].value))
                    data = this.spweeklist
                }
                let iscorrect = false
                if (data.length > 1) {
                    data.map((x, i) => {
                        if (x.value && x.value.length == 2 && i != index) {
                            if ((new Date(value[0]).getTime() >= new Date(x.value[0]).getTime() && new Date(value[0]).getTime() <= new Date(x.value[1]).getTime())
                                || (new Date(value[0]).getTime() >= new Date(x.value[0]).getTime() && new Date(value[1]).getTime() <= new Date(x.value[1]).getTime())
                                || (new Date(value[0]).getTime() <= new Date(x.value[0]).getTime() && new Date(value[1]).getTime() >= new Date(x.value[0]).getTime())
                                || (new Date(value[0]).getTime() <= new Date(x.value[0]).getTime() && new Date(value[1]).getTime() >= new Date(x.value[1]).getTime())
                            ) {
                                //开始时间在范围其他时间段内
                                iscorrect = true;
                                return
                            }
                        }
                    })
                }

                if (iscorrect == true) {
                    this.$q.notify({
                        type: 'negative',
                        message: `时间段存在冲突`,
                        position: 'top'
                    })
                    if(type==2){
                        this.wsplist[index].value = ''
                    }else{
                        this.spweeklist[index].value = ''

                    }
                    return
                }
            },
            ChildList(x, list, y, index) {
                list.map(j => {
                    if (x == j.DeptId) {
                        let obj = {
                            Id: j.DeptId,
                            Name: j.DeptName,
                        }
                        this.spweeklist[y].list[index].DeptList.push(obj)
                        return
                    } else {
                        if (j.ChildList && j.ChildList.length > 0) {
                            this.ChildList(x, j.ChildList, y, index)
                        }
                    }
                })
            },
            getChild(deptArray, y, index) {//特殊时期选择部门的时候的处理
                var tempStr = [];
                if (deptArray && deptArray != '') {
                    tempStr = deptArray.split(',');
                }
                this.spweeklist[y].list[index].DeptList = []
                tempStr.forEach(x => {
                    this.deptl.map(j => {
                        if (x == j.DeptId) {
                            let obj = {
                                Id: j.DeptId,
                                Name: j.DeptName,
                            }
                            this.spweeklist[y].list[index].DeptList.push(obj)
                            return
                        } else {
                            if (j.ChildList && j.ChildList.length > 0) {
                                this.ChildList(x, j.ChildList, y, index)
                            }
                        }
                    })
                })
                this.getalluser()
            },
            getalluser() {//获取所以选择的成员的 上限处理           
                let temporaryList = JSON.parse(JSON.stringify(this.addMsg.LimitList))
                let newList = []
                this.lsweeklist.forEach(x=>{//企业成员
                    if(x.chek == true){
                        x.lsMsg.forEach(j=>{
                           
                            if(this.addMsg.Type==1){
                                let obj = { Id: 0, EmpId: j.EmpId, EmpName: j.EmpName, LimitNum: '0' }
                                newList.push(obj)
                            }else{
                                j.EmpList.forEach(z=>{
                                    let obj = { Id: 0, EmpId: z.DeptId, EmpName: z.DeptName, LimitNum: '0' }
                                    newList.push(obj)
                                })
                                
                            }
                            if(j.DeptList && j.DeptList.length>0){
                                j.DeptList.forEach(item=>{

                                    this.Employeelist.map(z=>{
                                        if(item.Id == z.Dept_Id){
                                            let obj = { Id: 0, EmpId: z.Id, EmpName: z.EmployeeName, LimitNum: '0' }
                                            newList.push(obj)
                                        }
                                    })
                                })
                            }
                        })
                    }
                })

                if(this.addMsg.SpecialDateEnable==1){
                    this.spweeklist.forEach(x => {//特殊时期 人员处理
                        x.list.forEach(j => {
                            j.EmpList.forEach(item => {
                                let obj
                                if(this.addMsg.Type==1){
                                    if(item.Id!=''){
                                        obj = { Id: 0, EmpId: item.Id, EmpName: item.Name, LimitNum: '0' }
                                        newList.push(obj)
                                    }
                                }else{
                                    obj = { Id: 0, EmpId: item.DeptId, EmpName: item.DeptName, LimitNum: '0' }
                                    newList.push(obj)

                                }
                            })
                            if(j.DeptList.length>0){
                                j.DeptList.forEach(item=>{

                                    this.Employeelist.map(z=>{
                                        if(item.Id == z.Dept_Id){
                                            let obj = { Id: 0, EmpId: z.Id, EmpName: z.EmployeeName, LimitNum: '0' }
                                            newList.push(obj)
                                        }
                                    })
                                })
                            }

                        })
                    })
                }
                
                newList = this.filterArr(newList,'EmpId')//去重
                this.addMsg.LimitList = JSON.parse(JSON.stringify(newList))
                newList.forEach((x,y)=>{//如果人员没变动不修改输入值
                    temporaryList.map(j=>{
                        if(x.EmpId == j.EmpId){
                            this.addMsg.LimitList[y]  = JSON.parse(JSON.stringify(j))
                            return
                        }
                    })
                })
            },
            filterArr(Arr, data) {//根据参数去重
                let newarr = {};
                return Arr.reduce((ok, item) => {
                    newarr[item[data]] ? '' : newarr[item[data]] = true && ok.push(item);
                    return ok;
                }, []);
            },
            welcometrigger(list,index,childrenindex,name,){//欢迎语的触发事件
                if(name=='WelcomeList'){//通用欢迎语
                    this.addMsg.WelcomeList[index] = list
                }else if(name=="WelcomeWeekList"){
                    this.wwlist[index].childrenlist[childrenindex] = list
                }else if(name=='WelcomeSpecialList'){
                    this.wsplist[index].childrenlist[childrenindex] = list

                }
                this.$forceUpdate()
            },
            handleTabsEdit(targetName, action){
                
                    this.wwlist.push({weeklist:[{ Id: 1, Name: '周一', chek: false }, { Id: 2, Name: '周二', chek: false }, { Id: 3, Name: '周三', chek: false }, { Id: 4, Name: '周四', chek: false }, { Id: 5, Name: '周五', chek: false }, { Id: 6, Name: '周六', chek: false }, { Id: 0, Name: '周日', chek: false }],
                        childrenlist:[{StartHours:'',EndHours:'',Id:0,Content:'',MediumIds:'',MediumModel:[]}]
                        })
                    this.$forceUpdate();
                
            },
            handleTabsEdit2(targetName, action){
                    this.wsplist.push({StartDate:'',EndDate:'', value:[],childrenlist:[{StartHours:'',EndHours:'',Id:0,Content:'',MediumIds:'',MediumModel:[]}] })
                    this.$forceUpdate();
                
            },
            handleTabsclose(index,type=1){
                let that  = this;
                if(type==1){//周期
                    that.wwlist.splice(index,1)
                    that.editableTabsValue='0'

                }else{//特殊
                    that.wsplist.splice(index,1)
                    that.editableTabsValue2='0'


                }
                
                that.$forceUpdate();

                
            },
            addwelcomesjd(index,type=1){//周期添加欢迎语时间段
                let obj={StartHours:'',EndHours:'',Id:0,Content:'',MediumIds:'',MediumModel:[]}
                if(type==1){//周期
                    this.wwlist[index].childrenlist.push(obj)
                    this.wwlist.sort()
                }else{//特殊
                    this.wsplist[index].childrenlist.push(obj)
                    this.wsplist.sort()
                }
                

            },
            abv(index,y,x,type=1){
                
                if(type==1){//周期
                    this.wwlist[index].childrenlist.splice(y,1)
                    this.wwlist.sort()
                    this.$forceUpdate();
                }else{//特殊
                    this.wsplist[index].childrenlist.splice(y,1)
                    this.wsplist.sort()
                    this.$forceUpdate();

                }



                
            },
            changeTime(dindex,index,type){//时间段的冲突判断
                let list 
                if(type==1){
                    list = this.spweeklist[dindex].list
                }else if(type==2){
                    list = this.wwlist[dindex].childrenlist
                }else if(type==3){
                    list = this.wsplist[dindex].childrenlist

                }
                if(list[index].StartHours=='' || list[index].StartHours==null || list[index].EndHours=='' || list[index].EndHours==null){
                    return
                }
                if(list[index].StartHours>list[index].EndHours){//开始时间大于结束时间
                    list[index].EndHours  = ''
                }
                let a = list[index].StartHours
                let b = list[index].EndHours
                let continuemap = true
                list.map((x,y)=>{
                    if(continuemap == false){
                        return
                    }
                    if(x.IsDefault !=1 && y!=index){//默认和选择自己的时候不做判断
                        let bx = x.StartHours;
                        let by = x.EndHours;
                        continuemap =  this.checkTime(a,b,bx,by)
                        if(continuemap == false){
                            this.$q.notify({
                                type: 'negative',
                                message: `时间段存在冲突`,
                                position: 'top'
                            })
                            list[index].StartHours='';
                            list[index].EndHours='';
                        }
                    }
                })
            },
            checkTime(a,b,x,y){
                var times1=[],times2 = [];
                times1.push([a,b]);
                times2.push([x,y]);
                var flag = false;
                //循环比较时间段是否冲突
                for (var i = 0; i < times1.length; i++) {
                    if (flag) break;
                    for (var j = 0; j < times2.length; j++) {
                        if(this.check(times1[i][0],times1[i][1],times2[j][0],times2[j][1])){
                            flag = true;
                            break;
                        }
                    }
                }
                if (flag) {
                    return false;
                }else{
                    return true;
                }

            },
            check (a,b,x,y) {
                if(a>=y || x>=b){  
                    return false;
                }else{
                    return true;
                }
            },
            empAddLimittext(row){
                let index = row.$index;
                let obj = row.row
                this.$set(this.addMsg.LimitList,index,obj)
            }
         
        }
    }

</script>