<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="filter.hgNm" 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>
                </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="8">
                        <el-form-item label="节假日编号" prop="ruleId">
                            <el-input maxlength="20" disabled v-model.trim="form.hgId" placeholder="系统自动生成"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="节假日名称" prop="hgNm">
                            <el-input maxlength="25" v-model.trim="form.hgNm"/>
                        </el-form-item>
                    </el-col>

                    <el-col :span="24">
                        <el-form-item label="假日日期">
                            <el-date-picker
                                    v-model="time"
                                    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="假日时段">
                            <div class="timeBox">
                               <!--<el-input v-model.trim="form.allowIn" style="width: 100px"></el-input>至<el-input v-model.trim="form.allowOut" style="width: 100px" class="timePay"></el-input>-->
                                <el-time-picker
                                        is-range
                                        v-model="form.allow"
                                        value-format="HH:mm"
                                        format="HH:mm"
                                        range-separator="至"
                                        start-placeholder="开始时间"
                                        end-placeholder="结束时间"
                                        placeholder="选择时间范围"
                                        @change="timeChange"
                                >
                                </el-time-picker>
                            </div>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="备注" prop="hgDesc">
                            <el-input maxlength="25" v-model.trim="form.hgDesc"/>
                        </el-form-item>
                    </el-col>
                </el-row>
            </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 {getHolidaysList,saveOrUpdateHolidays,getHolidaysById,onDeleteHolidays} from '@/api/parking/holidays'
  export default {
    name: 'system-staff',
    components: {

    },
    data () {
      return {
        allowInOut:"",
        time:"",
        options:[],
        hgIds:[],
        hgIdss:"",
        isCreate:false,
        userId:"",
        checkList:[],
        // 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: {
          allow:"",
          hgId:"",
          hgNm:"",
          hgDesc:"",
          bgTm:"",
          endTm:"",
          allowIn:"",
          allowOut:""
        },
        filter: {
          pageSize: 10,
          pageNum: 1,
          hgNm:""
        },
        pagination: {
          total: 0
        },
        currentPage: 1,
        treeData: [],
        defaultProps: {
          children: 'children',
          label: 'name'
        },
        columns: [
          {
            title: '节假日编号',
            key: 'hgId',
            minWidth: '120',
            showOverflowTooltip: true
          },
          {
            title: '节假日名称',
            key: 'hgNm',
            minWidth: '90',
            showOverflowTooltip: true
          },
          {
            title: '假日起始日期',
            key: 'bgTm',
            minWidth: '120',
            showOverflowTooltip: true
          },
          {
            title: '假日终止日期',
            key: 'endTm',
            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: {
          hgNm:[{required: true, message: '节假日名称不能为空', trigger: 'blur'}],
        }
      }
    },

    methods: {
      timeChange(val){
        if(val == null){
          this.form.allowIn= '',
            this.form.allowOut = ''
        }else{
          this.form.allowIn= val[0],
            this.form.allowOut= val[1]
        }
      },
      dateChange (val) {
        if(val == null){
          this.filter.bgTm = ''
          this.filter.endTm = ''
        }else{
          this.filter.bgTm = val[0]
          this.filter.endTm = val[1]
          this.form.bgTm = val[0]
          this.form.endTm = val[1]
        }
      },
      onCreateClick(){
        this.operVisible=true
        this.time=""
      },
      handleTreeNode () {
        this.handleList()
      },
      handleList (val) {
        if (val) {
          this.filter.pageNum = 1
          this.currentPage = 1
        }
        this.loading = true
        getHolidaysList(this.filter)
          .then(res => {
            // console.log(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) {
        if(!this.time.length){
          this.$message({
            message: '请选择假日日期',
            type: 'warning'
          })
          return
        }
        if(!this.form.allowIn){
          this.$message({
            message: '请选择假日时段',
            type: 'success'
          })
          return
        }
        if(!this.form.allowOut){
          this.$message({
            message: '请选择假日时段',
            type: 'success'
          })
          return
        }
        this.$refs['form'].validate(valid => {
          if (valid) {
            this.staLoading = true
            saveOrUpdateHolidays(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.diaLogName = '修改'
        // this.form.stationId=row.stationId
        getHolidaysById({hgId:row.hgId}).then(res => {
          this.form = res.parkGarageHoliday
          this.time=[]
          this.time[0]=res.parkGarageHoliday.bgTm
          this.time[1]=res.parkGarageHoliday.endTm
          this.$set(this.form,"allow",res.allow)
        })
        this.operVisible = true
      },
      onMutilDelClick(){
        if(!this.multipleSelection.length){
          this.$message({
            message: '请选择记录',
            type: 'warning'
          })
        }else{
          this.$confirm(`确定要删除当前节假日吗?`, '确认操作', {
            confirmButtonText: '确定',
            cancelButtonText: '放弃',
            type: 'warning'
          }).then(() => {
            this.hgIds=[]
            this.multipleSelection.forEach((item,index)=>{
              this.hgIds.push(item.hgId)
            })
            this.hgIdss=this.hgIds.join(",")
            onDeleteHolidays({hgIds:this.hgIdss}).then(res=>{
              this.$message({
                message: '删除成功',
                type: 'success'
              })
              this.handleTreeNode()
            })
          })
        }
      },
      handleRowRemove ({index, row}) {
        // console.log(row)
        this.$confirm(`确定要删除当前节假日吗?`, '确认操作', {
          confirmButtonText: '确定',
          cancelButtonText: '放弃',
          type: 'warning'
        }).then(() => {
          onDeleteHolidays({hgIds:row.hgId}).then(res=>{
            this.$message({
              message: '删除成功',
              type: 'success'
            })
            this.handleTreeNode()
          })
        })
      },
      clearForm () {
        if (this.$refs['form']!==undefined) {
          this.$refs['form'].resetFields()
        }
        this.form = {}
        this.diaLogName = '新增'

      },
    },
    mounted () {
      this.filter.pageSize = this.tableOptions.pageList[0]
      this.tableOptions = this.$calcTableHeight(tableOptions, 51)
      this.handleTreeNode()
    }
  }
</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>