<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="预约人" >
        <el-input
          v-model="queryParams.name"
          placeholder="请输入预约人"
          clearable
          size="small"
          style="width: 140px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品类型" >
          <el-select v-model="queryParams.type"  placeholder="请选择商品类型" size="small"  style="width: 140px">
            <el-option :label="index.label"  :value="index.value" v-for = "(index, item) in typeList" :key="item" />
          </el-select>
      </el-form-item>
      <el-form-item label="预约日期" >
         <el-date-picker v-model="queryParams.orderTime" type="date" placeholder="选择预约日期" value-format="yyyy-MM-dd" style="width: 140px"/>
      </el-form-item>

      <el-form-item label="预约时段" >
          <el-select v-model="queryParams.startTime"  placeholder="请选择开始时间" size="small"  style="width: 140px">
            <el-option :label="index.value"  :value="index.value" v-for = "(index, item) in implementTimeStart" :key="item" />
          </el-select>
          --
          <el-select v-model="queryParams.endTime"  placeholder="请选择截止时间" size="small"  style="width: 140px">
            <el-option :label="index.value"  :value="index.value" v-for = "(index, item) in implementTimeEnd" :key="item" />
          </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary"  size="mini" @click="handleQuery">搜索</el-button>
        <el-button plain size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
      <br>
      <el-form-item>
        <el-button
          plain
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:admin:export']"
        >导出</el-button>
        <el-button
          type="primary"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:admin:add']"
        >新增</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="adminList" >
      <el-table-column label="序号" align="left" prop="" show-overflow-tooltip >
        <template slot-scope="scope">
         {{scope.$index + 1}}
        </template>
      </el-table-column>
      <el-table-column label="预约人" align="left" prop="a" show-overflow-tooltip/>
      <el-table-column label="商品类型" align="left" prop="a" show-overflow-tooltip/>
      <el-table-column label="商品名称" align="left" prop="b" show-overflow-tooltip />
      <el-table-column label="预约日期" align="left" prop="b" show-overflow-tooltip />
      <el-table-column label="预约时段" align="left" prop="b" show-overflow-tooltip />
      <el-table-column label="状态" align="left" prop="b" show-overflow-tooltip />
      <el-table-column label="单价（元、每次）" align="left" prop="b" show-overflow-tooltip />
      <el-table-column label="创建时间" align="left" prop="d"  show-overflow-tooltip />
      <el-table-column label="操作" align="left" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:admin:remove']"
          >退订</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog   :title="title" :visible.sync="open" width="600px" append-to-body :before-close="cancel" :close-on-click-modal="false"  >
      <el-form ref="form" :model="form" :rules="rules" label-width="120px" >
        <el-row :gutter="0">
          <el-col :span="16">
            <el-form-item label="商品名称" prop="name" >
              <el-input v-model="form.name" placeholder="请输入商品名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="预约商品" prop="status" >
              <el-select v-model="form.status"  placeholder="请选择预约商品" size="small"  style="width: 160px">
                <el-option :label="index.value"  :value="index.value" v-for = "(index, item) in statusList" :key="item" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="商品类型" prop="type" >
              <el-select v-model="form.type"  placeholder="请选择商品类型" size="small"  style="width: 160px">
                <el-option :label="index.label"  :value="index.value" v-for = "(index, item) in typeList" :key="item" />
              </el-select>
            </el-form-item>
          </el-col>
           <el-col :span="12">
            <el-form-item label="预约日期" prop="type" >
              <el-date-picker v-model="queryParams.orderTime" type="date" placeholder="选择预约日期" value-format="yyyy-MM-dd" style="width: 160px"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="商品单价" prop="price" >
              <el-input v-model="form.name" placeholder="商品单价" style="display:inline-block;width:100px;"/> <span style="display:inline-block"> 元/每次</span>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="执行时段" >
                <el-select v-model="form.startTime"  placeholder="请选择开始时间" size="small"  style="width: 140px;display:inline-block">
                  <el-option :label="index.value"  :value="index.value" v-for = "(index, item) in implementTimeStart" :key="item" />
                </el-select>
                --
                <el-select v-model="form.endTime"  placeholder="请选择截止时间" size="small"  style="width: 140px;display:inline-block">
                  <el-option :label="index.value"  :value="index.value" v-for = "(index, item) in implementTimeEnd" :key="item" />
                </el-select>
            </el-form-item>
          </el-col>
           <el-col :span="16">
            <el-form-item label="场地锁定" prop="name" >
              <el-checkbox-group v-model="checkList" @change="handleCheckedDatesChange">
                <el-checkbox v-for="date in lock" :label="date.value" :key="date.value">{{date.label}}</el-checkbox>
              </el-checkbox-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm" v-if="!loading">确 定</el-button>
          <el-button :loading="loading" type="primary" @click="submitForm" v-else>提交中...</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
    </el-dialog>
  </div>
</template>
<script>
import { addBooking, updataBooking, delBooking, getBooking, exportBooking } from "@/api/recharge/consumePlaces";
export default {
  name: "admin",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      dialogFormVisible:false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 参数表格数据
      adminList: [
        {a:1,b:2,c:3,d:4,f:5}
      ],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 类型数据字典
      typeOptions: [],
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        name: undefined,
        aliasName: undefined,
      },


      typeList:[
        {value:"",lable:"全部"},
        {value:"1",lable:"商品1"},
        {value:"2",lable:"商品2"},
      ],
      lock:[
        {value:1,label:"锁定"},
      ],
      checkList:[],
      implementTimeEnd:[],
      implementTimeEnd:[],
      // 表单参数
      form: {

      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: "商品名称不能为空", trigger: "blur" }
        ],
        logo: [
          { required: true, message: "商品类型不能为空", trigger: "change" }
        ],
        logo: [
          { required: true, message: "重复周期不能为空", trigger: "change" }
        ],
        logo: [
          { required: true, message: "执行时段不能为空", trigger: "change" }
        ],
        logo: [
          { required: true, message: "单价不能为空", trigger: "change" }
        ],

      },


    };
  },
  created() {
    this.getList()
    this.zxtimeStart()
    this.zxtimeEnd()
  },
  methods: {
    handleCheckedDatesChange(){

    },
    /** 执行开始时间 */
    zxtimeStart(){
      let arr = []
      for(let i = 0; i<48;i++){
        arr[i] = {}
        if(i%2 == "NaN"){
          arr[i].value="00:00:00"
        }
        if(i%2 == "0"){
          if(i/2>=10){
            arr[i].value= i/2 + ":00:00"
          }else{
            arr[i].value="0" + i/2 + ":00:00"
          }
        }
        if(i%2 == "1"){
          if(i/2>=10){
            arr[i].value=(i-1)/2 + ":30:00"
          }else{
            arr[i].value="0" + (i-1)/2 + ":30:00"
          }
        }
      }
      this.implementTimeStart = arr
    },
    /** 执行开结束时间 */
    zxtimeEnd(){
      let arr = []
      for(let i = 0; i<49;i++){
        arr[i] = {}
        if(i%2 == "NaN"){
          arr[i].value="00:00:00"
        }
        if(i%2 == "0"){
          if(i/2>=10){
            arr[i].value= i/2 + ":00:00"
          }else{
            arr[i].value="0" + i/2 + ":00:00"
          }
        }
        if(i%2 == "1"){
          if(i/2>=10){
            arr[i].value=(i-1)/2 + ":30:00"
          }else{
            arr[i].value="0" + (i-1)/2 + ":30:00"
          }
        }
      }
      this.implementTimeEnd = arr.slice(1,arr.length)
    },
    /** 查询参数列表 */
    getList() {
      this.loading = true;
      getBooking(this.queryParams).then(response => {
        this.adminList = response.data.records
        this.loading = false;
      })
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        name: undefined,
        status: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.form.id =undefined
      this.form.name =undefined
      this.form.introduce =undefined
      this.reset();
      this.open = true;
      this.title = "添加参数";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form =JSON.stringify(row)
      this.open = true;
      this.title = "修改参数";
    },

    /** 提交按钮 */
    submitForm(row) {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.loading = true
          if (this.form.id != undefined) {
            updataBooking(this.form).then(response => {
              this.loading = false
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).catch(()=>{
              this.loading = false,
              this.open = false
            })
          } else {
            addBooking(this.form).then(response => {
              this.loading = false
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).catch(()=>{
              this.loading = false,
              this.open = false
            })
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认退订', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delBooking(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 刷新缓存按钮操作 */
    handleRefreshCache() {
      refreshCache().then(() => {
        this.msgSuccess("刷新成功");
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      let queryParams = {...this.queryParams};
      delete queryParams.current
      delete queryParams.size
      this.exportLoading = true;
      exportBooking(queryParams).then(response =>{
        this.exportLoading = false;
        localStorage.setItem('fileExportId', response.data)
        this.$router.push({path:'/recharge/fileExport'})
      })
    },
  }
};
</script>
<style lang="scss" scoped>
</style>

