<!--
 * @Descripttion: 当前组件信息
 * @version: 1.0.0
 * @Author: 无尘
 * @Date: 2019-04-12 17:56:45
 * @LastEditors  : 无尘
 * @LastEditTime : 2019-12-26 16:07:33
 -->
<template>
  <div class="notice-list-wrap">
    <div class="notice-list">
      <common-detail-top :topMenuData="topMenuData" :activeId="activeId"></common-detail-top>
      <el-form :model="ruleFormData" :rules="rules" ref="noteForm" label-width="120px" class="demo-ruleForm">
        <el-form-item label="考勤组名称：" prop="attenceGroupName" class="input-item">
          <limitInput :inputWidth="286" :inputValue.sync="ruleFormData.attenceGroupName" :holder="'请输入考勤组名称'" :getByType="'word'" :maxLength="15"> </limitInput>
        </el-form-item>
        <el-form-item label="参与考勤人员：" class="input-item" prop="">
          <div class="flex">
            <el-checkbox v-model="ruleFormData.attenceRangeType">行政架构</el-checkbox>
            <div v-if="ruleFormData.attenceRangeType" class="flex-column item-cell-select inline-block m-l-10">
              <div class="depart-item-wrap">
                <div style="width:470px" class="el-select el-select--large depart-item-content" @click="showDialogMust('depart', ruleFormData.adminList)">
                  <span class="font-14 color-c0c4cc p-l-10" v-if="!ruleFormData.adminList.length">请选择行政架构</span>
                  <div class="el-select__tags">
                    <span>
                      <template v-for="(item, index) in ruleFormData.adminList">
                        <span class="el-tag el-tag--info el-tag--small" :key="index">
                          <span class="el-select__tags-text">{{ item.name || item.storeName }}</span
                          ><i class="el-tag__close el-icon-close" @click.stop="delDepart(index, ruleFormData.adminList, 'depart')"></i>
                        </span>
                      </template>
                    </span>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <div class="flex m-t-20">
            <el-checkbox v-model="ruleFormData.attenceRangeStoreType">门店架构</el-checkbox>
            <div v-if="ruleFormData.attenceRangeStoreType" class="flex-column item-cell-select inline-block m-l-10">
              <div class="depart-item-wrap">
                <div style="width:470px" class="el-select el-select--large depart-item-content" @click="showDialogMust('store', ruleFormData.storeList)">
                  <span class="font-14 color-c0c4cc p-l-10" v-if="!ruleFormData.storeList.length">请选择门店架构</span>
                  <div class="el-select__tags">
                    <span>
                      <template v-for="(item, index) in ruleFormData.storeList">
                        <span class="el-tag el-tag--info el-tag--small" :key="index">
                          <span class="el-select__tags-text">{{ item.name || item.storeName }}</span
                          ><i class="el-tag__close el-icon-close" @click.stop="delDepart(index, ruleFormData.storeList, 'store')"></i>
                        </span>
                      </template>
                    </span>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </el-form-item>
        <el-form-item label="考勤白名单：" class="input-item" prop="">
          <div class="flex">
            <el-checkbox v-model="ruleFormData.noAttenceRangeType">行政架构</el-checkbox>
            <div v-if="ruleFormData.noAttenceRangeType" class="flex-column item-cell-select inline-block m-l-10">
              <div class="depart-item-wrap">
                <div style="width:470px" class="el-select el-select--large depart-item-content" @click="showDialogNo('depart', ruleFormData.noAdminList)">
                  <span class="font-14 color-c0c4cc p-l-10" v-if="!ruleFormData.noAdminList.length">请选择行政架构</span>
                  <div class="el-select__tags">
                    <span>
                      <template v-for="(item, index) in ruleFormData.noAdminList">
                        <span class="el-tag el-tag--info el-tag--small" :key="index">
                          <span class="el-select__tags-text">{{ item.name || item.storeName }}</span
                          ><i class="el-tag__close el-icon-close" @click.stop="delDepart(index, ruleFormData.noAdminList, 'depart')"></i>
                        </span>
                      </template>
                    </span>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <div class="flex m-t-20">
            <el-checkbox v-model="ruleFormData.noAttenceRangeStoreType">门店架构</el-checkbox>
            <div v-if="ruleFormData.noAttenceRangeStoreType" class="flex-column item-cell-select inline-block m-l-10">
              <div class="depart-item-wrap">
                <div style="width:470px" class="el-select el-select--large depart-item-content" @click="showDialogNo('store', ruleFormData.noStoreList)">
                  <span class="font-14 color-c0c4cc p-l-10" v-if="!ruleFormData.noStoreList.length">请选择门店架构</span>
                  <div class="el-select__tags">
                    <span>
                      <template v-for="(item, index) in ruleFormData.noStoreList">
                        <span class="el-tag el-tag--info el-tag--small" :key="index">
                          <span class="el-select__tags-text">{{ item.name || item.storeName }}</span
                          ><i class="el-tag__close el-icon-close" @click.stop="delDepart(index, ruleFormData.noStoreList, 'store')"></i>
                        </span>
                      </template>
                    </span>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </el-form-item>
        <el-form-item label="考勤类型：" prop="">
          <div class="work-type m-b-8">
            <el-radio :disabled="$route.query.attenceGroupId ? true : false" v-model="ruleFormData.attenceType" :label="1">固定时间上下班【所有人按照相同时间打卡】</el-radio>
            <div class="line-h-15 font-14 color-909399">适用于：办公室等</div>
          </div>
          <div class="work-type m-b-8">
            <el-radio :disabled="$route.query.attenceGroupId ? true : false" v-model="ruleFormData.attenceType" :label="2">排班时间上下班【不同人员根据各自排班打卡】</el-radio>
            <div class="line-h-15 font-14 color-909399">适用于：工厂、门店等</div>
          </div>
          <!--  <div class="work-type">
            <el-radio v-model="ruleFormData.attenceType" :label="3">自由时间上下班【上下班时间不固定，可随时打卡】</el-radio>
            <div class="line-h-15 font-14 color-909399">适用于：销售、计时工等</div>
          </div> -->
        </el-form-item>
        <el-form-item v-if="ruleFormData.attenceType == 1" label="工作日设置：" prop="">
          <!-- <div class="quick-set-class"><span>快捷设置班次：</span><el-button type="text" @click="showClasses()" plain>更改班次</el-button></div> -->
          <div class="quick-set-table">
            <el-table ref="multipleTable" :data="ruleFormData.attenceWorkJson" tooltip-effect="dark" style="width: 567px">
              <el-table-column width="55">
                <template slot-scope="scope">
                  <!-- <el-checkbox v-model="scope.row.workType"></el-checkbox> -->
                </template>
              </el-table-column>
              <el-table-column label="工作日" width="120">
                <template slot-scope="scope">{{ weekDays[scope.row.weekDay] }}</template>
              </el-table-column>
              <el-table-column prop="name" label="排班时间段" width="120">
                <template slot-scope="scope">
                  <div v-if="scope.row.workType">
                    <div v-for="(item, index) in scope.row.classessTimesJson" :key="index + 'time'">{{ item.startTime }}-{{ item.endTime }}</div>
                  </div>
                  <div v-else>休息</div>
                </template>
              </el-table-column>
              <el-table-column prop="address" label="操作" show-overflow-tooltip>
                <template slot-scope="scope">
                  <el-button type="text" @click.stop="modClasses(scope.$index, scope.row)">更改班次</el-button>
                </template>
              </el-table-column>
            </el-table>
          </div>
          <div><el-checkbox v-model="ruleFormData.lawFreeFlag">法定节假日自动排休</el-checkbox></div>
        </el-form-item>
        <!--  <el-form-item label="特殊日期：" prop="">
          <div class="must-clock">
            <span class="font-14 color-606266">必须打卡的日期</span><el-button @click="cancelSend" plain>添加</el-button>
            <div class="must-clock-table">
              <el-table :data="ruleFormData.mustJson" tooltip-effect="dark" style="width: 567px">
                <el-table-column label="日期" width="120">
                  <template slot-scope="scope">{{ scope.row.weekDay }}</template>
                </el-table-column>
                <el-table-column prop="name" label="考勤时间" width="120">
                  <template slot-scope="scope">{{ scope.row.weekDay }}</template>
                </el-table-column>
                <el-table-column prop="address" label="操作" show-overflow-tooltip>
                  <el-button type="text" @click="modMust(scope.row)" plain>编辑</el-button>
                  <el-button type="text" @click="delMust(scope.row)" plain>删除</el-button>
                </el-table-column>
              </el-table>
            </div>
          </div>
          <div class="must-clock">
            <span class="font-14 color-606266">不用打卡日期</span><el-button @click="cancelSend" plain>添加</el-button>
            <div class="must-clock-table">
              <el-table :data="ruleFormData.noMustJson" tooltip-effect="dark" style="width: 567px">
                <el-table-column type="selection" width="55"> </el-table-column>
                <el-table-column label="日期" width="120">
                  <template slot-scope="scope">{{ scope.row.weekDay }}</template>
                </el-table-column>
                <el-table-column prop="name" label="考勤时间" width="120">
                  <template slot-scope="scope">{{ scope.row.weekDay }}</template>
                </el-table-column>
                <el-table-column prop="address" label="操作" show-overflow-tooltip>
                  <el-button type="text" @click="modNoMust(scope.row)" plain>编辑</el-button>
                  <el-button type="text" @click="delNoMust(scope.row)" plain>删除</el-button>
                </el-table-column>
              </el-table>
            </div>
          </div>
        </el-form-item> -->
        <el-form-item v-show="ruleFormData.attenceType == 2" label="考勤班次：" prop="classesJson">
          <div v-if="!ruleFormData.classesJson.length" class="classes-btn">
            <el-button type="primary" plain @click="toShowClassMult">选择班次</el-button>
          </div>
          <div v-else class="tags-classes">
            <div class="attend-type-classes" @click="toShowClassMult">
              <div class="attend-type-classes-display">
                <div :class="['class-tag', pIndex % 7 == 0 ? 'day-tag-blue' : '', pIndex % 7 == 1 ? 'day-tag-red' : '', pIndex % 7 == 2 ? 'day-tag-purple' : '', pIndex % 7 == 3 ? 'day-tag-orange' : '', pIndex % 7 == 4 ? 'day-tag-green' : '', pIndex % 7 == 5 ? 'day-tag-pink' : '', pIndex % 7 == 6 ? 'day-tag-cyan' : '']" v-for="(pItem, pIndex) in ruleFormData.classesJson" :key="pIndex">
                  <span class="ant-tag-text">班次{{ pItem.classesName }}：</span>
                  <span v-for="(item, index) in pItem.classessTimesJson" :key="index + 'time'">{{ item.startTime }}-{{ item.endTime }}</span>
                </div>
              </div>
            </div>
          </div>
        </el-form-item>
        <el-form-item v-if="ruleFormData.attenceType == 2 && ruleFormData.classesJson.length" label="排班周期：" prop="">
          <div v-if="!ruleFormData.attenceClassesRangeJSON.length" class="classes-btn">
            <el-button type="primary" plain @click="toShowRangeMult">请设置排班周期</el-button>
          </div>
          <el-table v-else :data="ruleFormData.attenceClassesRangeJSON" tooltip-effect="dark" style="width: 567px">
            <el-table-column prop="ranageName" label="周期名称" show-overflow-tooltip>
              <template slot-scope="scope">{{ scope.row.rangeName }}</template>
            </el-table-column>
            <el-table-column prop="name" label="周期班次" show-overflow-tooltip>
              <template slot-scope="scope">
                <span v-for="(item, index) in scope.row.classes" :key="index + 'time'">{{ index > 1 ? '-' : '' }}{{ item.classesName }}</span>
              </template>
            </el-table-column>
            <el-table-column prop="ranageName" label="周期天数">
              <template slot-scope="scope">{{ scope.row.days }}</template>
            </el-table-column>
            <el-table-column prop="address" label="操作" show-overflow-tooltip>
              <template slot-scope="scope">
                <el-button type="text" @click="editRange(scope.$index, scope.row)">编辑</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
        <el-form-item label="考勤方式：" prop="">
          <div><el-checkbox v-model="ruleFormData.gpsFlag">GPS打卡</el-checkbox><span v-if="!ruleFormData.gpsFlag" class="p-l-10 font-12 color-909399">你尚未设置任何考勤方式，保存设置后成员可在任何地点打卡</span></div>
          <div v-if="ruleFormData.gpsFlag" class="gps-body p-20 bg-f7f8fa" style="width: 567px">
            <div>根据工作地点考勤（可添加多个考勤地点）</div>
            <div>
              <el-button type="primary" plain="" @click="addMap">添加考勤地点</el-button> <span class="p-l-20 p-r-6 font-14 color-606266">有效范围</span>
              <el-select v-model="ruleFormData.clockDistance" placeholder="请选择">
                <el-option v-for="item in distance" :key="item.value" :label="item.label" :value="item.value"> </el-option>
              </el-select>
            </div>
            <div v-if="ruleFormData.distanceAddress.length" class="m-t-20">
              <el-table :data="ruleFormData.distanceAddress" tooltip-effect="dark" style="width: 567px">
                <el-table-column prop="name" label="考勤地址">
                  <template slot-scope="scope">{{ scope.row.address }}</template>
                </el-table-column>
                <el-table-column prop="address" label="操作" show-overflow-tooltip>
                  <template slot-scope="scope">
                    <el-button type="text" @click="delGps(scope.$index, scope.row)">删除</el-button>
                  </template>
                </el-table-column>
              </el-table>
            </div>
            <div>
              <el-checkbox v-model="ruleFormData.storeClockFlag">根据门店地址经纬度自动配置</el-checkbox>
              <p class="font-14 color-909399">如门店经纬度有误，请在通讯录修改门店相关信息，未设置门店打卡无须选择此项</p>
            </div>
          </div>
        </el-form-item>
        <el-form-item>
          <div class="btn-wrap">
            <!-- <el-button @click="cancelSend" plain>取消</el-button> -->
            <el-button v-if="ruleFormData.attenceType == 1" @click="toSubmit('noteForm', '1')" type="primary">保存</el-button>
            <el-button v-if="ruleFormData.attenceType == 2" @click="toSubmit('noteForm', '2')" type="primary">保存设置，开始排班 </el-button>
          </div>
        </el-form-item>
      </el-form>
    </div>
    <!-- 考勤行政架构 -->
    <vue-select-employee :appScene="appScene" :defaultSelection="mustSelection" :onlyPerson="onlyPerson" :treeSet="treeSet" :changed="changed" @handleSelectedList="handleMustList"> </vue-select-employee>
    <!-- 考勤门店选择 :currentBrand="activeGroup"-->
    <vue-select-store ref="storeSelector" :appScene="appScene" :treeSet="storeTreeSet" :selectType="'all'" :defaultList="mustStoreList" @handleSelectedList="handleMustList"> </vue-select-store>

    <!-- 白名单行政架构 -->
    <vue-select-employee :appScene="appScene" :defaultSelection="noSelection" :onlyPerson="true" :treeSet="noTreeSet" :changed="changedNo" @handleSelectedList="handleNoList"> </vue-select-employee>
    <!-- 白名单门店选择 :currentBrand="activeGroup"-->
    <vue-select-store ref="storeSelector" :appScene="appScene" :treeSet="noStoreTreeSet" :selectType="'person'" :defaultList="noStoreList" @handleSelectedList="handleNoList"> </vue-select-store>
    <el-dialog title="选择规则生效时间" :visible.sync="showEffect" width="420px" :before-close="handleClose">
      <span>立即生效：今日考勤结果将按新规则重算</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="sendData('noteForm', '0')">明天生效</el-button>
        <!-- <el-button type="primary" @click="sendData('noteForm', '1')">立即生效</el-button> -->
      </span>
    </el-dialog>
    <class-select :workClassShow="workClassShow" :classesId="classesId" @hideClassDialog="hideClassDialog"> </class-select>
    <map-select :mapShow="mapShow" @hideMapDialog="hideMapDialog"> </map-select>
    <class-select-mult :workClassMultShow="workClassMultShow" :classesData="ruleFormData.classesJson" @hideMultClassDialog="hideMultClassDialog"> </class-select-mult>
    <cycle-set :rangeShow="rangeShow" :classesJson="classesJson" :attenceGroupData="attenceGroupData" :attenceGroupId="$route.query.attenceGroupId" @hideCycleDialog="hideCycleDialog"> </cycle-set>
  </div>
</template>
<script>
import classSelect from '@/components/app/dailyAttendance/class-select.vue';
import classSelectMult from '@/components/app/dailyAttendance/class-select-mult.vue';
import mapSelect from '@/components/app/dailyAttendance/map-select.vue';
import commonDetailTop from '@/components/app/common-detail-top.vue';
import cycleSet from '@/components/app/dailyAttendance/cycle-set.vue';
import limitInput from '@/components/limit-input.vue';
import vueSelectStore from 'components/common/vueSelectStore';
import vueSelectEmployee from '@/components/common/vueSelectEmployee.vue';
import limitTextarea from '@/components/limit-textarea.vue';
import { getRequest, postRequest } from '@/api/api';
// import showMsg from '@/common/js/showmsg';
import { _debounce } from '@/common/js/public';
import errMsg from '@/common/js/error';
export default {
  name: 'workGroupSet',
  props: {
    brandId: {
      type: String,
      default() {
        return '';
      }
    },
    activeGroupId: {
      type: String,
      default() {
        return '';
      }
    }
  },
  data() {
    const checkClass = (rule, value, callback) => {
      if (!value.length && this.ruleFormData.attenceType == 2) {
        callback(new Error('请选择班次'));
      } else {
        callback();
      }
    };
    return {
      projectName: 'haoban-manage-web',
      topMenuData: [
        {
          id: '1',
          name: '考勤组管理',
          path: `/workSet?appIcon=${this.$route.query.appIcon}`
        },
        {
          id: '2',
          name: this.$route.query.attenceGroupId ? '编辑考勤组' : '新建考勤组',
          path: ''
        }
      ],
      weekDays: {
        '0': '周日',
        '1': '周一',
        '2': '周二',
        '3': '周三',
        '4': '周四',
        '5': '周五',
        '6': '周六'
      },
      classifyOptions: [], // 分类列表
      activeId: '2', // 详情头部选中 id
      activeBrand: this.brandId, // 商户(品牌) id
      activeGroup: this.activeGroupId, // 商户(品牌) groupId
      enterpriseId: JSON.parse(localStorage.getItem('userInfo')).enterpriseId,
      userId: JSON.parse(localStorage.getItem('userInfo')).userId,
      ruleFormData: {
        attenceGroupId: '',
        attenceGroupName: '',
        attenceType: 1, // 考勤组类型1固定排班2排班制3自由排班
        attenceRangeType: false, // 是否设置行政架构考勤名单 1是0否
        attenceRangeStoreType: false, // 是否选择考勤范围门店结构1是0否
        adminList: [],
        storeList: [],
        noAttenceRangeType: false, // 是否设置白名单行政架构考勤名单 1是0否
        noAttenceRangeStoreType: false, // 是否选择考勤范围门店结构1是0否
        noAdminList: [],
        noStoreList: [],
        lawFreeFlag: false, // 是否法定节假日自动排休
        clockDistance: 300, // 考勤距离
        storeClockFlag: false, // 是否开启门店打卡
        gpsFlag: true, // 是否gps打卡1是0否
        distanceAddress: [], // 考勤地址范围集合{ address: '', longitude: '', latitude: '' }
        attenceRangeJson: [],
        // 需要打卡的范围1需要打卡的2无须打卡的 storeFlag 1门店架构0行政架构{ rangeType: '', userId: '', storeId: '', groupId: '', storeFlag: '' }
        sperialRangeJson: [],
        mustJson: [],
        noMustJson: [],
        //特殊考勤日期sperialType 1必须打卡0无须打卡
        attenceWorkJson: [
          { weekDay: '1', workType: false, classesName: '', classesId: '-1', classessTimesJson: [] },
          { weekDay: '2', workType: false, classesName: '', classesId: '-1', classessTimesJson: [] },
          { weekDay: '3', workType: false, classesName: '', classesId: '-1', classessTimesJson: [] },
          { weekDay: '4', workType: false, classesName: '', classesId: '-1', classessTimesJson: [] },
          { weekDay: '5', workType: false, classesName: '', classesId: '-1', classessTimesJson: [] },
          { weekDay: '6', workType: false, classesName: '', classesId: '-1', classessTimesJson: [] },
          { weekDay: '0', workType: false, classesName: '', classesId: '-1', classessTimesJson: [] }
        ],
        // 类型上班还是休息1上班0休息
        attenceClassesRangeJSON: [],
        classesJson: []
      },
      rules: {
        // receiveType: [{ required: true, message: '请选择发送对象', trigger: 'change' }],
        // img: [{ required: true, message: '请上传封面图片', trigger: 'blur' }],
        attenceGroupName: [{ required: true, message: '请输入考勤组名称', trigger: 'blur' }],
        classesJson: [{ type: 'array', required: true, validator: checkClass, trigger: 'change' }]
      },
      wxFlag: 1,
      actionUrl: '/haoban-manage-web/upload-img',
      datePickerBool: false,
      dateValue: '',

      // 考勤行政
      mustType: '', // 考勤 行政、门店区分
      selectNoType: '', // 白名单 行政、门店区分
      treeSet: {
        isSelectPerson: false, // 控制只能选部门(false)
        dialogVisible: false, // 控制显示/隐藏
        isSingle: false, // 是否单选
        openNextBool: true // 展开下级不调用接口
      },
      mustSelection: [], // 默认行政架构
      changed: '',
      onlyPerson: false, // 控制只能选人(true)
      // 考勤门店选择
      appScene: 1,
      mustStoreList: [], //  默认的门店数据
      storeTreeSet: {
        isSelectPerson: true, // 控制只能选部门(false)
        dialogVisible: false, // 控制显示/隐藏
        isSingle: false // 是否单选
      },
      // 白名单
      changedNo: '',
      noTreeSet: {
        isSelectPerson: false, // 控制只能选部门(false)
        dialogVisible: false, // 控制显示/隐藏
        isSingle: false, // 是否单选
        openNextBool: true // 展开下级不调用接口
      },
      noSelection: [], // 默认行政架构
      noStoreList: [], //  默认的门店数据
      noStoreTreeSet: {
        isSelectPerson: true, // 控制只能选部门(false)
        dialogVisible: false, // 控制显示/隐藏
        isSingle: false // 是否单选
      },

      defaultStoreBool: false,
      // 是否保存的标识
      saveDataBool: true,
      showEffect: false,
      // 工作日设置
      workClassShow: false,
      modIndex: '',
      modRow: {},
      showClassesFlag: false,
      multipleSelection: [],
      distance: [
        {
          value: '50',
          label: '50米'
        }
      ],
      mapShow: false,
      // 周期
      rangeShow: false,
      classesJson: [],
      attenceGroupData: {
        classesRangeId: '',
        days: 3,
        rangeName: '',
        classes: [
          {
            classesId: '',
            classesName: ''
          },
          {
            classesId: '',
            classesName: ''
          },
          {
            classesId: '',
            classesName: ''
          }
        ]
      },
      workClassMultShow: false
    };
  },

  methods: {
    /**
     * 请设置排班周期
     */
    toShowRangeMult() {
      const that = this;
      that.classesJson = JSON.parse(JSON.stringify(that.ruleFormData.classesJson));
      that.rangeShow = true;
    },
    hideCycleDialog(val) {
      const that = this;
      that.rangeShow = false;
      if (!val) {
        return false;
      }
      that.ruleFormData.attenceClassesRangeJSON = JSON.parse(JSON.stringify([val]));
      that.$forceUpdate();
    },
    /**
     * 排班选择班次
     */
    toShowClassMult() {
      const that = this;
      that.workClassMultShow = true;
      that.classesData = JSON.parse(JSON.stringify(that.ruleFormData.classesJson));
    },
    hideMultClassDialog(val) {
      const that = this;
      that.workClassMultShow = false;
      that.classesData = [];
      if (!val) {
        return false;
      }
      that.ruleFormData.classesJson = JSON.parse(JSON.stringify(val));
      that.classesJson = JSON.parse(JSON.stringify(val));
    },
    /**
     * 周期
     */
    editRange(index, row) {
      const that = this;
      that.classesJson = JSON.parse(JSON.stringify(that.ruleFormData.classesJson));
      that.attenceGroupData = JSON.parse(JSON.stringify(row));
      that.rangeShow = true;
    },
    /**
     * 地图
     */
    addMap() {
      const that = this;
      that.mapShow = true;
    },
    hideMapDialog(obj) {
      const that = this;
      that.mapShow = false;
      if (!obj) {
        return false;
      }
      that.ruleFormData.distanceAddress.push({
        address: obj.address,
        longitude: obj.longitude,
        latitude: obj.latitude
      });
    },
    delGps(index, row) {
      const that = this;
      that.ruleFormData.distanceAddress.splice(index, 1);
    },
    /**
     * 工作日设置
     *
     */
    showClasses() {
      const that = this;
      that.workClassShow = true;
    },
    modClasses(index, row) {
      const that = this;
      that.modIndex = index;
      that.modRow = row;
      that.classesId = row.classesId == -1 ? String(row.classesId) : row.classesId || '';
      that.workClassShow = true;
    },
    hideClassDialog(val) {
      const that = this;
      that.workClassShow = false;
      that.classesId = '';
      if (!val) {
        return false;
      }
      that.modRow.workType = val.classesId == -1 ? false : true;
      const obj = JSON.parse(JSON.stringify(val));
      that.modRow.classesName = obj.classesName;
      that.modRow.classesId = obj.classesId;
      that.modRow.classessTimesJson = obj.classessTimesJson;
    },
    handleSelectionChange(val) {
      const that = this;
      that.multipleSelection = val;
      // 设置班次
    },

    // 返回 的 brandId
    selectBrandId(id, groupId) {
      this.brandId = id;
    },

    // 取消
    cancelSend() {
      let that = this;
      if (that.$route.query.attenceGroupId) {
        that.$router.go(-1);
        return false;
      }
      that
        .$confirm('是否确认返回，返回后当前页面内容将丢失', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        .then(() => {
          that.$router.go(-1);
        })
        .catch(() => {});
    },
    // 考勤门店弹窗显示
    showDialogMust(type, selData) {
      const that = this;

      that.mustType = type;
      that.changed = type;
      if (type === 'store') {
        that.nustStoreList = selData;
        that.storeTreeSet = {
          dialogVisible: true,
          isSingle: false,
          isSelectPerson: true,
          openNextBool: true // 展开下级不调用接口
        };
        return;
      }
      that.treeSet = {
        dialogVisible: true,
        isSingle: false,
        isSelectPerson: true,
        openNextBool: true // 展开下级不调用接口
      };
      that.nustSelection = selData;
    },
    // 白名单门店弹窗显示
    showDialogNo(type, selData) {
      const that = this;

      that.selectNoType = type;
      that.changedNo = type;
      if (type === 'store') {
        that.noStoreList = selData;
        that.noStoreTreeSet = {
          dialogVisible: true,
          isSingle: false,
          isSelectPerson: true,
          openNextBool: true // 展开下级不调用接口
        };
        return;
      }
      that.noTreeSet = {
        dialogVisible: true,
        isSingle: false,
        isSelectPerson: true,
        openNextBool: true // 展开下级不调用接口
      };
      that.noSelection = selData;
    },
    /**
     * 处理已选部门
     */
    handleMustList(group) {
      // type:	1:员工,2:部门,3:门店;
      const that = this;
      if (that.mustType === 'store') {
        that.ruleFormData.storeList = group;
        that.mustStoreList = group;
      } else {
        that.ruleFormData.adminList = group;
        that.mustSelection = group;
      }
    },
    /**
     * 处理已选部门
     */
    handleNoList(group) {
      // type:	1:员工,2:部门,3:门店;
      const that = this;
      /* eslint-disable */
      console.log(group);
      if (that.selectNoType === 'store') {
        that.ruleFormData.noStoreList = group;
        that.noStoreList = group;
      } else {
        that.ruleFormData.noAdminList = group;
        that.noSelection = group;
      }
    },
    /**
     * 删除选的部门
     */
    delDepart(index, obj, type) {
      const that = this;
      obj.splice(index, 1);
    },
    // 选择定时发送的日期
    changeMyDate() {
      if (!this.dateValue) {
        this.datePickerBool = true;
      } else {
        this.saveDataBool = true;
      }
    },
    // 改变发送方式
    changeSendType(val) {
      if (val == '0') {
        this.saveDataBool = true;
      }
    },
    /**
     * 保存
     */
    toSubmit(formName,type) {
      const that = this;
      let adminStructor = [];
      if (that.ruleFormData.attenceRangeType && that.ruleFormData.adminList.length) {
        adminStructor = that.ruleFormData.adminList.map(item => {
          let obj = {
            rangeType: 1,
            storeFlag: 0,
            id: item.id,
            name: item.name || item.label,
            type: item.type
          }
          if (item.type == 1) {
            obj.userId = item.userId; //item.id
            obj.clerkId = item.id
          }
          if (item.type == 2) {
            obj.groupId = item.id
          }
          if (item.type == 3) {
            obj.storeId = item.id
          }
          return obj;
        });
      }
      let storeStructor = [];
      if (that.ruleFormData.attenceRangeStoreType && that.ruleFormData.storeList.length) {
        storeStructor = that.ruleFormData.storeList.map(item => {
          let obj = {
            rangeType: 1,
            storeFlag: 1,
            id: item.id,
            name: item.name || item.label,
            type: item.type
          }
          if (item.type == 1) {
            obj.userId = item.userId; //item.id
            obj.clerkId = item.id
          }
          if (item.type == 2) {
            obj.groupId = item.id
          }
          if (item.type == 3) {
            obj.storeId = item.id
          }
          return obj;
        });
      }
      let noAdminStructor = [];
      if (that.ruleFormData.noAttenceRangeType && that.ruleFormData.noAdminList.length) {
        noAdminStructor = that.ruleFormData.noAdminList.map(item => {
          return {
            rangeType: 2,
            storeFlag: 0,
            id: item.id,
            userId:  item.userId,
            clerkId: item.id,
            name: item.name || item.label,
            type: item.type
          };
        });
      }
      let noStoreStructor = [];
      if (that.ruleFormData.noAttenceRangeStoreType && that.ruleFormData.noStoreList.length) {
        noStoreStructor = that.ruleFormData.noStoreList.map(item => {
          return {
            rangeType: 2,
            storeFlag: 1,
            id: item.id,
            userId:  item.userId,
            clerkId: item.id,
            name: item.name || item.label,
            type: item.type
          };
        });
      }
      that.ruleFormData.attenceRangeJson = [...adminStructor, ...storeStructor, ...noAdminStructor, ...noStoreStructor];
      /* eslint-disabled */
      /* if (that.ruleFormData.gpsFlag && !that.ruleFormData.distanceAddress.length) {
        showMsg.showmsg('请设置打卡地点', 'warning');
        return false;
      } */
      console.log(that.ruleFormData);
      that.$refs[formName].validate(valid => {
        if (valid) {
          if (!that.$route.query.attenceGroupId) {
            that.sendData(formName, 1)
          }else{
            that.showEffect = true;
          }
        }
      })
    },

    // 发布
    sendData: _debounce(function(formName, effectFlag) {
      const that = this;
      let para = {
        enterpriseId: that.enterpriseId,
        effectFlag: effectFlag,
        attenceGroupId: that.ruleFormData.attenceGroupId,
        attenceGroupName: that.ruleFormData.attenceGroupName,
        attenceType: that.ruleFormData.attenceType, // 考勤组类型1固定排班2排班制3自由排班
        attenceRangeType: that.ruleFormData.attenceRangeType ? 1 : 0, // 是否设置行政架构考勤名单 1是0否
        attenceRangeStoreType: that.ruleFormData.attenceRangeStoreType ? 1 : 0, // 是否选择考勤范围门店结构1是0否
        noAttenceRangeType: that.ruleFormData.noAttenceRangeType ? 1 : 0, // 是否设置白名单行政架构考勤名单 1是0否
        noAttenceRangeStoreType: that.ruleFormData.noAttenceRangeStoreType ? 1 : 0, // 是否选择考勤范围门店结构1是0否
        lawFreeFlag: that.ruleFormData.lawFreeFlag ? 1 : 0, // 是否法定节假日自动排休
        clockDistance: that.ruleFormData.clockDistance, // 考勤距离
        storeClockFlag: that.ruleFormData.storeClockFlag ? 1 : 0, // 是否开启门店打卡
        gpsFlag: that.ruleFormData.gpsFlag ? 1 : 0, // 是否gps打卡1是0否
        distanceAddress: JSON.stringify(that.ruleFormData.distanceAddress), // 考勤地址范围集合{ address: '', longitude: '', latitude: '' }
        attenceRangeJson: JSON.stringify(that.ruleFormData.attenceRangeJson),
        // 需要打卡的范围1需要打卡的2无须打卡的 storeFlag 1门店架构0行政架构{ rangeType: '', userId: '', storeId: '', groupId: '', storeFlag: '' }
        sperialRangeJson: JSON.stringify(that.ruleFormData.sperialRangeJson),
        //特殊考勤日期sperialType 1必须打卡0无须打卡
        attenceWorkJson: JSON.stringify(that.ruleFormData.attenceWorkJson),
        // 类型上班还是休息1上班0休息
        attenceClassesRangeJSON: JSON.stringify(that.ruleFormData.attenceClassesRangeJSON),
        classesJson: JSON.stringify(that.ruleFormData.classesJson)
      };
      if (that.saveDataBool) {
        postRequest(that.$route.query.attenceGroupId?'/haoban-app-attence-web/attence-edit':'/haoban-app-attence-web/attence-add', para).then(res => {
          let data = res.data;
          if (data.errorCode == 1) {
            if (that.ruleFormData.attenceType == 1) {
              that.$message.success('保存成功');
              that.$router.go(-1);
            }else {
              that.$nextTick(()=>{
                that.$router.push(`/workTimeManage?appIcon=${that.$route.query.appIcon}&attenceGroupId=${that.$route.query.attenceGroupId || data.result.attenceGroupId}`);
              })
            }
          } else {
            that.$message.error(data.message);
          }
        });
      }

    }, 500),
    /**
     * 获取班次
     */
    getClassifyData() {
      const that = this;
      let para = {
        enterpriseId: that.enterpriseId
      };
      getRequest('/haoban-app-attence-web/attence-classes-list', para)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            if (resData.result.list && resData.result.list.length) {
              resData.result.list.forEach(ele => {
                if (ele.classessTimesJson) {
                  ele.classessTimesJson = JSON.parse(ele.classessTimesJson);
                }
              });
            }
            that.classifyList = resData.result.list || [];
            return false;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    },
    /**
     * 获取考勤组详情
     */
    getGroupData() {
      const that = this;
      let para = {
        attenceGroupId: that.$route.query.attenceGroupId
      };
      getRequest('/haoban-app-attence-web/attence-detail', para)
        .then(res => {
          let data = res.data;
          if (data.errorCode == 1) {
            data.result.attenceRangeType = data.result.attenceRangeType ==1 ? true : false;
            data.result.attenceRangeStoreType = data.result.attenceRangeStoreType ==1 ? true : false;
            data.result.noAttenceRangeType = data.result.noAttenceRangeType ==1 ? true : false;
            data.result.noAttenceRangeStoreType = data.result.noAttenceRangeStoreType ==1 ? true : false;
            data.result.attenceRangeJson = data.result.attenceRangeJson ? JSON.parse(data.result.attenceRangeJson) : [];
            data.result.lawFreeFlag = data.result.lawFreeFlag==1 ? true : false;
            data.result.storeClockFlag = data.result.storeClockFlag == 1 ? true : false;
            data.result.gpsFlag = data.result.gpsFlag==1 ? true : false;
            data.result.clockDistance = parseInt(data.result.clockDistance), // 考勤距离
            data.result.distanceAddress = data.result.distanceAddress ? JSON.parse(data.result.distanceAddress) : [];
            data.result.sperialRangeJson = data.result.sperialRangeJson ? JSON.parse(data.result.sperialRangeJson) : [];
            data.result.attenceWorkJson = data.result.attenceWorkJson ? JSON.parse(data.result.attenceWorkJson) : [];
            data.result.attenceClassesRangeJSON = data.result.attenceClassesRangeJSON ? JSON.parse(data.result.attenceClassesRangeJSON) : [];
            data.result.classesJson = data.result.classesJson ? JSON.parse(data.result.classesJson) : [];
            data.result.adminList = [];
            data.result.storeList = [];
            data.result.noAdminList = [];
            data.result.noStoreList = [];
            if (data.result.attenceRangeJson) {
              data.result.attenceRangeJson.forEach(ele => {
                ele.label = ele.name;
                ele.type = !!ele.userId? '1' :!!ele.groupId? '2' : '3';
                ele.id = !!ele.clerkId ? ele.clerkId : ele.groupId ? ele.groupId : ele.storeId;
                if (ele.rangeType == 1&& ele.storeFlag == 0) {
                  data.result.adminList.push(ele);
                }
                if (ele.rangeType == 2&& ele.storeFlag == 0) {
                  data.result.noAdminList.push(ele);
                }
                if (ele.rangeType == 1&& ele.storeFlag == 1) {
                  data.result.storeList.push(ele);
                }
                if (ele.rangeType == 2&& ele.storeFlag == 1) {
                  data.result.noStoreList.push(ele);
                }
              })
            }
            if (data.result.attenceWorkJson) {
              data.result.attenceWorkJson.forEach(ele => {
                ele.workType = ele.workType==1? true : false;
              })
            }
            if (!!data.result.attenceWorkJson && data.result.attenceWorkJson.length) {
              data.result.attenceWorkJson.forEach(child => {
                child.classessTimesJson =  child.classessTimesJson? JSON.parse(child.classessTimesJson): []
              });
            }
            if (!!data.result.classesJson && data.result.classesJson.length) {
              data.result.classesJson.forEach(child => {
                child.classessTimesJson =  child.classessTimesJson? JSON.parse(child.classessTimesJson): []
              });
            }
            that.mustSelection = data.result.adminList;
            that.mustStoreList = data.result.storeList;
            that.noSelection = data.result.noAdminList;
            that.noStoreList = data.result.noStoreList;
            that.ruleFormData = JSON.parse(JSON.stringify(data.result));
            return false;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    },
    setDistance() {
      const that = this;
      for (let index = 1; index < 11; index++) {
        that.distance.push({
          value: index * 100,
          label: index * 100 + '米'
        });
      }
      that.distance.push({
        value: 2000,
        label: '2000米'
      });
    }
  },
  watch: {
    brandId: function(newData, oldData) {
      const that = this;
      if (!!newData) {
        that.activeBrand = newData;
        that.getClassifyData();
      }
    },
    activeGroupId: function(newData, oldData) {
      const that = this;
      if (!!newData) {
        that.activeGroup = newData;
      }
    }
  },
  components: {
    commonDetailTop,
    limitInput,
    limitTextarea,
    vueSelectStore,
    vueSelectEmployee,
    classSelect,
    mapSelect,
    classSelectMult,
    cycleSet
  },
  mounted() {
    const that = this;
    that.$emit('showTab', '2');
    that.setDistance();
    if (that.$route.query.attenceGroupId) {
      that.$nextTick(() => {
        that.getGroupData();
      });
    }
  }
};
</script>
<style type="text/less" lang="less" scoped>
.line-h-15 {
  line-height: 15px;
}
.m-b-8 {
  margin-bottom: 8px;
}
.icon-xinxixianshi {
  .font-12 {
    font-size: 12px;
  }
}
.bg-f7f8fa {
  background-color: #f7f8fa;
}

.tags-classes {
  width: 607px;
  line-height: 31.9999px;
    position: relative;
    zoom: 1;
    &::before {
    content: " ";
    display: table;
}
  .attend-type-classes {
    cursor: pointer;

    .attend-type-classes-display{
      border: 1px solid #e4e4e4;
      border-radius: 4px;
      padding: 2px 8px 5px 8px;
      height: auto;
      overflow: auto;
      max-height: 100px;
    }
  }

}

.notice-list-wrap {
  height: 100%;

  .notice-list {
    background: #fff;
    margin-bottom: 34px;
    min-height: 100%;
    // min-height: calc(100% + 24px);
  }
}
.demo-ruleForm {
  padding-top: 30px;
}

.input-item {
  width: 680px;
  position: relative;
  &.w-1012 {
    width: 1012px;
  }
}

.input-item-textarea /deep/ textarea {
  height: 94px;
  font-size: 14px;
  color: #606266;
  font-family: 'Helvetica Neue';
}

.el-upload__tip {
  line-height: 20px;
}

.active-info-num {
  position: absolute;
  right: 0;
  bottom: -31px;
  color: #909399;
  font-size: 12px;
}
.my-date-picker {
  margin-left: 15px;
}
.demo-ruleForm /deep/ .el-form-item__label {
  color: #606266;
}
.btn-wrap {
  font-size: 0;
}
.el-button {
  &.p-l-20 {
    padding-left: 20px;
    -webkit-transform: translateY(2px);
    -moz-transform: translateY(2px);
    -ms-transform: translateY(2px);
    transform: translateY(2px);
  }
}
// 发送范围
.depart-item-content {
  width: 100%;
  height: 32px;
  overflow: hidden;
  white-space: nowrap;
  border-radius: 4px;
  border: 1px solid #dcdfe6;
  cursor: pointer;
  box-sizing: border-box;
}

.item-cell-select {
  /deep/ .el-select__tags {
    white-space: nowrap;
    overflow: hidden;
  }
}

.avatar-wrap {
  width: 100px;
  img {
    width: 100%;
  }
}
.date-picker-error {
  padding-left: 193px;
}
.el-form-item {
  /deep/ .file-list {
    .notice-upload-file .el-icon-circle-check:before {
      content: '\E60F';
    }
  }
}
</style>
