<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.status"  placeholder="请选择生效状态" size="small"  style="width: 160px">
            <el-option :label="index.label"  :value="index.value" v-for = "(index, item) in statusList" :key="item" />
          </el-select>
      </el-form-item>
      <el-form-item label="商品类型" >
          <el-select v-model="queryParams.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-form-item label="重复周期" >
          <el-select v-model="queryParams.cycle"  placeholder="请选择重复周期" size="small"  style="width: 160px">
            <el-option :label="index.label"  :value="index.value" v-for = "(index, item) in cycleList" :key="item" />
          </el-select>
      </el-form-item>
      <el-form-item label="执行时段" >
          <el-select v-model="queryParams.startTime"  placeholder="请选择开始时间" size="small"  style="width: 160px">
            <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: 160px">
            <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" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button type="primary" icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['system:admin:export']"
        >导出</el-button>
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          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="center" prop="" show-overflow-tooltip >
        <template slot-scope="scope">
         {{scope.$index + 1}}
        </template>
      </el-table-column>
      <el-table-column label="商品名称" align="center" prop="a" show-overflow-tooltip/>
      <el-table-column label="商品类型" align="center" prop="a" show-overflow-tooltip/>
      <el-table-column label="生效状态" align="center" prop="b" show-overflow-tooltip />
      <el-table-column label="重复周期" align="center" prop="b" show-overflow-tooltip />
      <el-table-column label="开始时间" align="center" prop="b" show-overflow-tooltip />
      <el-table-column label="截止时间" align="center" prop="b" show-overflow-tooltip />
      <el-table-column label="单价（元、每次）" align="center" prop="b" show-overflow-tooltip />
      <el-table-column label="创建时间" align="center" prop="d"  show-overflow-tooltip />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:admin:edit']"
          >修改</el-button>
          <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="请输入商品名称" maxlength="15" show-word-limit />
            </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: 140px">
                <el-option :label="index.label"  :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: 140px">
                <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="24">
            <el-form-item label="重复周期" prop="checkList" >
              <el-checkbox-group v-model="checkList" @change="handleCheckedDatesChange">
                <el-checkbox v-for="date in cycleList" :label="date.value" :key="date.value">{{date.label}}</el-checkbox>
              </el-checkbox-group>
            </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-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 { addManage, updataManage, delManage, getManage, exportManage } from "@/api/recharge/consumePlaces";
import { getTimeFormat, YearMonthDate } from "@/assets/js/dataFormate.js";
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,
      },
      statusList:[
        {value:"",label:"全部"},
        {value:"1",label:"有效"},
        {value:"2",label:"无效"},
      ],
      typeList:[
        {value:"",label:"全部"},
        {value:"1",label:"商品1"},
        {value:"2",label:"商品2"},
      ],
      // cycleList:[
      //   {value:"",label:"全部"},
      //   {value:"1",label:"每日"},
      //   {value:"2",label:"每工作日"},
      //   {value:"3",label:"周末"},
      // ],
      implementTimeStart:[],//执行开始时段
      implementTimeEnd:[],//执行结束时段
      cycleList:[
        {value:1,label:"周一"},
        {value:2,label:"周二"},
        {value:3,label:"周三"},
        {value:4,label:"周四"},
        {value:5,label:"周五"},
        {value:6,label:"周六"},
        {value:7,label:"周日"},
        {value:8,label:"每天"},
        {value:9,label:"每工作日"},
        {value:10,label:"周末"}
      ],
      // 表单参数
      form: {
      },
      checkList:[],
      // 表单校验
      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(value){
      console.log(value,this.checkList)

    },
    /** 执行开始时间 */
    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;
      getManage(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;
      if(this.queryParams.startTime && this.queryParams.endTime){
        let start = YearMonthDate().split(" ")[0] + " " + this.queryParams.startTime
        let end = YearMonthDate().split(" ")[0] + " " + this.queryParams.endTime
        if(getTimeFormat(end) <= getTimeFormat(start)){
          this.msgError("结束时间不能小于或等于开始时间");
          return
        }
      }
      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
          let start = YearMonthDate().split(" ")[0] + " " + this.form.startTime
          let end = YearMonthDate().split(" ")[0] + " " + this.form.endTime
          if(getTimeFormat(end) <= getTimeFormat(start)){
            this.msgError("结束时间不能小于或等于开始时间");
            return
          }
          if (this.form.id != undefined) {
            updataManage(this.form).then(response => {
              this.loading = false
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).catch(()=>{
              this.loading = false,
              this.open = false
            })
          } else {
            addManage(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 delManage(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;
      exportManage(queryParams).then(response =>{
        this.exportLoading = false;
        localStorage.setItem('fileExportId', response.data)
        this.$router.push({path:'/recharge/fileExport'})
      })
    },
  }
};
</script>
<style lang="scss" scoped>
</style>

