<template>
    <d2-container>
        <div class="d2-table-view-full" style="overflow: hidden">
            <div class="d2-table-view-header">
                <el-form label-width="100px" labelPosition="right" size="small" :inline="true" :model="filter" @keyup.enter.native="handleList" class="demo-form-inline">
                    <el-form-item label="节假日名称">
                        <el-input style="width: 192px" v-model.trim="filter.holidayName" placeholder="节假日名称" clearable/>
                    </el-form-item>
                    <el-form-item>
                        <el-button   type="primary" @click="handleList('1')">查询</el-button>
                    </el-form-item>
                </el-form>
            </div>
            <div class="d2-table-view-content">
                <div class="d2-table-view-handle">
                    <el-button @click="onCreateClick" size="small" icon="el-icon-plus">新增</el-button>
                    <el-button size="small" @click="onMutilDelClick">批量删除</el-button>
                    <!--<el-button>下发设备</el-button>-->
                </div>
                <d2-crud
                        title="节假日登记"
                        ref="d2Crud"
                        :options="tableOptions"
                        :loading="loading"
                        :columns="columns"
                        :data="data"
                        :rowHandle="rowHandle"
                        @edit="handleRowEdit"
                        @delete="handleRowRemove"
                        selection-row
                        @selection-change="handleSelectionChange"
                >
                </d2-crud>
            </div>
            <div class="d2-table-view-footer">
                <el-pagination
                        @current-change="handleCurrentChange"
                        @size-change="handleSizeChange"
                        :page-size="tableOptions.pageList[0]"
                        :page-sizes="tableOptions.pageList"
                        :current-page.sync="currentPage"
                        layout="total,sizes, prev, pager, next,jumper"
                        :total="pagination.total"
                ></el-pagination>
            </div>
        </div>
        <el-dialog
                :title="diaLogName"
                :visible.sync="operVisible"
                @close="clearForm"
                width="60%"
                :close-on-click-modal="false"
        >
            <el-form ref="form" :model="form" label-width="120px" labelPosition="right" :rules="rules">
                <el-row :gutter="15">
                    <el-col :span="12">
                        <el-form-item label="节假日名称" prop="holidayName">
                            <el-input maxlength="25" v-model.trim="form.holidayName"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="9">
                        <el-form-item label="备注" prop="remark" label-width="50px">
                            <el-input maxlength="120" v-model.trim="form.remark"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="15">
                        <el-form-item label="假日日期" prop="effecTime">
                            <el-date-picker
                                    v-model="form.effecTime"
                                    type="datetimerange"
                                    value-format="yyyy-MM-dd HH:mm:ss"
                                    :default-time="['00:00:00', '23:59:59']"
                                    @change="dateChange"
                                    range-separator="至"
                                    start-placeholder="开始时间"
                                    end-placeholder="结束时间">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="有效星期" prop="effectiveDay" style="width: 400px">
                            <!--<el-checkbox-group >-->
                            <el-col :span="6">
                                <el-checkbox :checked="isChecked" v-model="isChecked" @change="allClick">全选</el-checkbox>
                            </el-col>
                            <el-col :span="6" v-for="(item,index) in checkList" :key="index">
                                <el-checkbox :checked="item.isChecked" v-model="item.isChecked" @change="checkClick">{{item.label}}</el-checkbox>
                            </el-col>
                            <!--</el-checkbox-group>-->
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-card class="box-card">
                    <el-row :gutter="15">
                    <el-col :span="24">
                        有效时段 <el-form-item prop="value1">
                        <div class="timeBox">
                            <!--通行时间段一：<el-input v-model.trim="form.allowIn1" style="width: 100px" prop="allowIn1"></el-input>至<el-input v-model.trim="form.allowOut1" style="width: 100px" class="timePay" prop="allowOut1"></el-input>-->
                            时间段一：<el-time-picker
                                is-range
                                v-model="form.value1"
                                value-format="HH:mm"
                                format="HH:mm"
                                range-separator="至"
                                start-placeholder="开始时间"
                                end-placeholder="结束时间"
                                placeholder="选择时间范围"
                                @change="timeChange1"
                        >
                        </el-time-picker>
                        </div>
                    </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item prop="value2">
                            <div class="timeBox">
                                <!--通行时间段一：<el-input v-model.trim="form.allowIn1" style="width: 100px" prop="allowIn1"></el-input>至<el-input v-model.trim="form.allowOut1" style="width: 100px" class="timePay" prop="allowOut1"></el-input>-->
                                时间段二：<el-time-picker
                                    is-range
                                    v-model="form.value2"
                                    value-format="HH:mm"
                                    format="HH:mm"
                                    range-separator="至"
                                    start-placeholder="开始时间"
                                    end-placeholder="结束时间"
                                    placeholder="选择时间范围"
                                    @change="timeChange2"
                            >
                            </el-time-picker>
                            </div>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item prop="value3">
                            <div class="timeBox">
                                <!--通行时间段一：<el-input v-model.trim="form.allowIn1" style="width: 100px" prop="allowIn1"></el-input>至<el-input v-model.trim="form.allowOut1" style="width: 100px" class="timePay" prop="allowOut1"></el-input>-->
                                时间段三：<el-time-picker
                                    is-range
                                    v-model="form.value3"
                                    value-format="HH:mm"
                                    format="HH:mm"
                                    range-separator="至"
                                    start-placeholder="开始时间"
                                    end-placeholder="结束时间"
                                    placeholder="选择时间范围"
                                    @change="timeChange3"
                            >
                            </el-time-picker>
                            </div>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item prop="value4">
                            <div class="timeBox">
                                <!--通行时间段一：<el-input v-model.trim="form.allowIn1" style="width: 100px" prop="allowIn1"></el-input>至<el-input v-model.trim="form.allowOut1" style="width: 100px" class="timePay" prop="allowOut1"></el-input>-->
                                时间段四：<el-time-picker
                                    is-range
                                    v-model="form.value4"
                                    value-format="HH:mm"
                                    format="HH:mm"
                                    range-separator="至"
                                    start-placeholder="开始时间"
                                    end-placeholder="结束时间"
                                    placeholder="选择时间范围"
                                    @change="timeChange4"
                            >
                            </el-time-picker>
                            </div>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item prop="value5">
                            <div class="timeBox">
                                <!--通行时间段一：<el-input v-model.trim="form.allowIn1" style="width: 100px" prop="allowIn1"></el-input>至<el-input v-model.trim="form.allowOut1" style="width: 100px" class="timePay" prop="allowOut1"></el-input>-->
                                时间段五：<el-time-picker
                                    is-range
                                    v-model="form.value5"
                                    value-format="HH:mm"
                                    format="HH:mm"
                                    range-separator="至"
                                    start-placeholder="开始时间"
                                    end-placeholder="结束时间"
                                    placeholder="选择时间范围"
                                    @change="timeChange5"
                            >
                            </el-time-picker>
                            </div>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item prop="value6">
                            <div class="timeBox">
                                <!--通行时间段一：<el-input v-model.trim="form.allowIn1" style="width: 100px" prop="allowIn1"></el-input>至<el-input v-model.trim="form.allowOut1" style="width: 100px" class="timePay" prop="allowOut1"></el-input>-->
                                时间段六：<el-time-picker
                                    is-range
                                    v-model="form.value6"
                                    value-format="HH:mm"
                                    format="HH:mm"
                                    range-separator="至"
                                    start-placeholder="开始时间"
                                    end-placeholder="结束时间"
                                    placeholder="选择时间范围"
                                    @change="timeChange6"
                            >
                            </el-time-picker>
                            </div>
                        </el-form-item>
                    </el-col>
                </el-row>
                </el-card>
            </el-form>
            <span slot="footer" class="dialog-footer">
        <zl-button :loading="staLoading" @click="operInstance">保存</zl-button>
      </span>
        </el-dialog>
    </d2-container>
</template>
<script>
  import { tableOptions } from '@/assets/options'
  import {getHolidayRegisterList,saveOrUpdateHolidayRegister,getHolidayRegisterById,onDeleteHolidayRegister} from '@/api/accessControl/holidayRegister'
  export default {
    name: 'system-staff',
    components: {

    },
    data () {
      return {
        effectiveDay:[],
        isSelected:[],
        isChecked:false,
        options:[],
        gtId:[],
        gtIds:"",
        isCreate:false,
        userId:"",
        checkList:[{label:"星期一",value:0,isChecked:false},{label:"星期二",value:0,isChecked:false},{label:"星期三",value:0,isChecked:false},{label:"星期四",value:0,isChecked:false},{label:"星期五",value:0,isChecked:false},{label:"星期六",value:0,isChecked:false},{label:"星期天",value:0,isChecked:false}],
        // classVisible:false,
        staLoading: false,
        multipleSelection:[],
        func: {
          add: false,
          delete: false,
          update: false,
          select: false
        },
        falg: true,
        isExpend: true,
        tableOptions,
        loading: false,
        operVisible: false,
        diaLogName: '新增',
        lanuchs: [],
        form: {
          holidayName:"",
          value1:'',
          value2:'',
          value3:'',
          value4:'',
          value5:'',
          value6:'',
          effecTime:"",
          startDate:"",
          endDate:"",
          remark:"",
          effectiveDay:[],
          allowIn1:"",
          allowOut1:"",
          allowIn2:"",
          allowOut2:"",
          allowIn3:"",
          allowOut3:"",
          allowIn4:"",
          allowOut4:"",
          allowIn5:"",
          allowOut5:"",
          allowIn6:"",
          allowOut6:"",
        },
        filter: {
          pageSize: 10,
          pageNum: 1,
          holidayName:""
        },
        pagination: {
          total: 0
        },
        currentPage: 1,
        treeData: [],
        defaultProps: {
          children: 'children',
          label: 'name'
        },
        columns: [
          {
            title: '节假日序号',
            key: 'index',
            minWidth: '50',
            formatter (row, column, cellValue, index) {
              return index+1
            }
          },
          {
            title: '节假日名称',
            key: 'holidayName',
            minWidth: '90',
            showOverflowTooltip: true
          },
          {
            title: '假日起始日期',
            key: 'startDate',
            minWidth: '120',
            showOverflowTooltip: true
          },
          {
            title: '假日终止日期',
            key: 'endDate',
            minWidth: '120',
            showOverflowTooltip: true
          },
          {
            title: '有效星期',
            key: 'effectiveDay',
            minWidth: '120',
            showOverflowTooltip: true
          }
        ],
        rowHandle: {
          columnHeader: '操作',
          minWidth: '180',
          align: 'center',
          custom: [
            {
              text: '编辑',
              type: 'primary',
              size: 'small',
              emit: 'edit'
            },
            {
              text: '删除',
              type: 'danger',
              size: 'small',
              emit: 'delete'
            }
          ]
        },
        data: [],
        rules: {
          holidayName:[{required: true, message: '节假日名称不能为空', trigger: 'blur'}],
          effecTime:[{required: true, message: '请选择假日日期', trigger: 'blur'}],
          value1:[{required: true, message: '请选择时间段一', trigger: 'blur'}],
          allowIn1:[{required: true, message: '请输入时间', trigger: 'blur'}],
          allowOut1:[{required: true, message: '请输入时间', trigger: 'blur'}],
        }
      }
    },

    methods: {
      checkClick(){
        if(this.isChecked){
          this.checkList.forEach((item,index)=>{
            item.isChecked=true;
            item.value=1
          })
        }else{
          this.isSelected=[]
          this.checkList.forEach((item,index)=>{
            if(item.isChecked){
              this.isSelected.push(item)
            }
          })
        }
      },
      allClick(){
        if(this.isChecked){
          this.checkList.forEach((item,index)=>{
            item.isChecked=true;
            item.value=1
          })
        }else{
          this.checkList.forEach((item,index)=>{
            item.isChecked=false;
            item.value=0
          })
        }
      },
      timeChange1(val){
        if(val == null){
          this.form.allowIn1 = '',
            this.form.allowOut1 = ''
        }else{
          this.form.allowIn1 = val[0],
            this.form.allowOut1 = val[1]
        }
      },
      timeChange2(val){
        if(val == null){
          this.form.allowIn2 = ''
          this.form.allowOut2 = ''
        }else{
          this.form.allowIn2 = val[0],
            this.form.allowOut2 = val[1]
        }
      },
      timeChange3(val){
        if(val == null){
          this.form.allowIn3 = ''
          this.form.allowOut3 = ''
        }else{
          this.form.allowIn3= val[0],
            this.form.allowOut3= val[1]
        }
      },
      timeChange4(val){
        if(val == null){
          this.form.allowIn4 = '',
            this.form.allowOut4 = ''
        }else{
          this.form.allowIn4 = val[0],
            this.form.allowOut4 = val[1]
        }
      },
      timeChange5(val){
        if(val == null){
          this.form.allowIn5 = ''
          this.form.allowOut5 = ''
        }else{
          this.form.allowIn5 = val[0],
            this.form.allowOut5 = val[1]
        }
      },
      timeChange6(val){
        if(val == null){
          this.form.allowIn6 = ''
          this.form.allowOut6 = ''
        }else{
          this.form.allowIn6= val[0],
            this.form.allowOut6= val[1]
        }
      },
      dateChange (val) {
        if(val == null){
          this.form.startDate = ''
          this.form.endDate = ''
        }else{
          this.$set(this.form,'startDate',val[0])
          this.$set(this.form,'endDate',val[1])
        }
      },
      onCreateClick(){
        this.operVisible=true
      },
      handleTreeNode () {
        this.handleList()
      },
      handleList (val) {
        if (val) {
          this.filter.pageNum = 1
          this.currentPage = 1
        }
        this.loading = true
        getHolidayRegisterList(this.filter)
          .then(res => {
            this.loading = false
            this.data = res.page.list
            this.pagination.total = res.page.total
          })
          .catch(err => {
            this.loading = false
          })
      },
      handleSelectionChange(val) {
        this.isCreate=false
        this.multipleSelection = val;
      },
      handleCurrentChange (val) {
        this.filter.pageNum = val
        this.handleList()
      },
      handleSizeChange (val) {
        this.filter.pageNum = 1
        this.filter.pageSize = val
        this.handleList()
      },
      operInstance (val) {
        this.$refs['form'].validate(valid => {
          if (valid) {
            if(!this.isChecked){
              if(this.isSelected.length<1){
                this.$message({
                  message: '请选择有效星期',
                  type: 'warning'
                })
                return
              }
            }
            this.effectiveDay=[]
            this.checkList.forEach((item, index) => {
              if (item.isChecked) {
                item.value = 1
              } else {
                item.value = 0
              }
              this.effectiveDay.push(item.value)
            })
            this.staLoading = true
            this.form.effectiveDay = this.effectiveDay.join()
            this.form.startDate=this.form.effecTime[0]
            this.form.endDate=this.form.effecTime[1]
            saveOrUpdateHolidayRegister(this.form).then(res => {
              this.staLoading = false
              this.operVisible = false
              this.$message({
                message: '操作成功',
                type: 'success'
              })
              this.clearForm()
              this.handleTreeNode()
            }).catch(err => {
              this.staLoading = false
            })
          }
        })
      },
      handleRowEdit ({index, row}) {
        this.isSelected=[]
        this.diaLogName = '修改'
        getHolidayRegisterById({id:row.id}).then(res => {
          this.form = res.doorSystemHoliday
          this.effectiveDay=res.doorSystemHoliday.effectiveDay.split(",")
          this.effectiveDay.forEach((item,index)=>{
            this.checkList[index].value=this.effectiveDay[index]
            if(this.checkList[index].value==1){
              this.checkList[index].isChecked=true
              this.isSelected.push(item)
            }
          })
          this.$set(this.form,"effecTime",res.value1)
          this.$set(this.form,"value1",res.value2)
          this.$set(this.form,"value2",res.value3)
          this.$set(this.form,"value3",res.value4)
          this.$set(this.form,"value4",res.value5)
          this.$set(this.form,"value5",res.value6)
          this.$set(this.form,"value6",res.value7)
        })
        this.operVisible = true
      },
      onMutilDelClick(){
        if(!this.multipleSelection.length){
          this.$message({
            message: '请选择记录',
            type: 'warning'
          })
        }else{
          this.$confirm(`确认删除当前节假日?`, '确认操作', {
            confirmButtonText: '确定',
            cancelButtonText: '放弃',
            type: 'warning'
          }).then(() => {
            this.gtId=[]
            this.multipleSelection.forEach((item,index)=>{
              this.gtId.push(item.id)
            })
            this.gtIds=this.gtId.join(",")
            onDeleteHolidayRegister({id:this.gtIds}).then(res=>{
              this.$message({
                message: '删除成功',
                type: 'success'
              })
              this.handleTreeNode()
            })
          })
        }
      },
      handleRowRemove ({index, row}) {
        // console.log(row)
        this.$confirm(`确认删除当前节假日?`, '确认操作', {
          confirmButtonText: '确定',
          cancelButtonText: '放弃',
          type: 'warning'
        }).then(() => {
          onDeleteHolidayRegister({id:row.id}).then(res=>{
            this.$message({
              message: '删除成功',
              type: 'success'
            })
            this.handleTreeNode()
          })
        })
      },
      clearForm () {
        if (this.$refs['form']!==undefined) {
          this.$refs['form'].resetFields()
        }
        this.form={
          holidayName:"",
          value1:'',
          value2:'',
          value3:'',
          value4:'',
          value5:'',
          value6:'',
          effecTime:"",
          startDate:"",
          remark:"",
          endDate:"",
          effectiveDay:[],
          allowIn1:"",
          allowOut1:"",
          allowIn2:"",
          allowOut2:"",
          allowIn3:"",
          allowOut3:"",
          allowIn4:"",
          allowOut4:"",
          allowIn5:"",
          allowOut5:"",
          allowIn6:"",
          allowOut6:"",
        },
          this.effectiveDay=[]
        this.isSelected=[]
        this.checkList.forEach((item,index)=> {
          item.isChecked=false;
          item.value=0
        })
        this.isChecked=false
        this.diaLogName = '新增'
      },
    },
    mounted () {
      this.handleTreeNode()
      this.filter.pageSize = this.tableOptions.pageList[0]
      this.tableOptions = this.$calcTableHeight(tableOptions,45)
    }
  }
</script>
<style>
    /*.d2-table-view-full{*/
        /*height: 100%;*/
    /*}*/
    /*.timeBox{*/
        /*margin-top: 10px;*/
    /*}*/
    /*.timePay{*/
        /*margin-left: 8px;*/
    /*}*/
    /*.avatar-uploader .el-upload {*/
        /*border: 1px dashed #d9d9d9;*/
        /*border-radius: 6px;*/
        /*cursor: pointer;*/
        /*position: relative;*/
        /*overflow: hidden;*/
    /*}*/

    /*.avatar-uploader .el-upload:hover {*/
        /*border-color: #409eff;*/
    /*}*/

    /*.avatar-uploader-icon {*/
        /*font-size: 28px;*/
        /*color: #8c939d;*/
        /*width: 178px;*/
        /*height: 178px;*/
        /*line-height: 178px;*/
        /*text-align: center;*/
    /*}*/

    /*.avatar {*/
        /*width: 178px;*/
        /*height: 178px;*/
        /*display: block;*/
    /*}*/
    /*.timeLabel label{*/
        /*width: 110px !important;*/
    /*}*/
    /*.bankLabel label{*/
        /*font-size: 13px;*/
    /*}*/
    /*.el-form-item__label {*/
        /*text-align: left !important;*/
    /*}*/
    /*.d2-table-view{*/
        /*overflow: hidden;*/
    /*}*/
    /*.d2-table-view-content{*/
        /*height: 100%;*/
    /*}*/
    /*.el-table{*/
        /*height: 450px !important;*/
    /*}*/
</style>