<template>
  <div class="container">
    <!-- 查询配置 -->
    <div style="padding: 0px 20px">
      <div ref="lxHeader">
        <LxHeader icon="md-apps" text="时段设置" style="margin-bottom: 10px; border: 0px; margin-top: 15px">
          <div slot="content"></div>
          <slot>
            <div>
              <Input style="width: 200px; margin-right: 10px" v-model="searchFields.name" placeholder="关键字搜索" />
              <Button type="primary" @click="load" style="margin-left: 10px">查询</Button>
              <Button type="primary" @click="edit()" style="margin-left: 10px">新增</Button>
            </div>
          </slot>
        </LxHeader>
      </div>
      <div class="table-tree">
        <el-table :data="tableData" :height="tableHeight" style="width: 100%">
          <el-table-column type="index" width="50" align="center" label="序号"> </el-table-column>
          <el-table-column :prop="column.field" :align="column.align" v-for="(column,index) in columns"
            :label="column.title" :width="column.width">
            <template slot-scope="scope">
              <div v-if="column.type=='opt'">
                <Button ghost size="small" @click="edit(scope.row)" type="primary"
                  style="margin-left: 10px;">编辑</Button>
                <Button ghost size="small" type="error" @click="del(scope.row)" style="margin-left: 10px;">删除</Button>
              <Button v-if="scope.row['status']==1" ghost size="small" type="success" @click="changeStatus(scope.row)"
                style="margin-left: 10px;">下架</Button>
                <Button v-if="scope.row['status']==0" ghost size="small" type="success" @click="changeStatus(scope.row)"
                  style="margin-left: 10px;">上架</Button>
              </div>
              <div v-else-if="column.type=='close_week'" v-for="item in parameters.weeks">
                <div v-if="item.id==scope.row[column.field]">
                  {{item.value}}
                </div>
              </div>
              <div v-else-if="column.type=='status'">
                <el-tag v-if="scope.row['status']==1" type="" effect="dark" >
                    上架中
                </el-tag>
                <el-tag v-if="scope.row['status']==0" type="info" effect="dark" >
                    下架
                </el-tag>
              </div>
              <div v-else>{{scope.row[column.field]}}</div>
            </template>
          </el-table-column>
        </el-table>
        <div class="pagination">
          <el-pagination @current-change="handleCurrentChange" :current-page="paginations.page"
            :page-size="paginations.page_size" background layout="prev, pager, next" :total="paginations.total">
          </el-pagination>
        </div>
      </div>
    </div>

    <el-dialog title="详情编辑" :visible.sync="dialogFormVisible" fullscreen width="90%">
      <el-form :model="form" :rules="rules" ref="form" label-position="right" :label-width="formLabelWidth">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请填写名称" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="预约须知" prop="notice">
          <el-input type="textarea" cols="3" v-model="form.notice" placeholder="请填写预约须知" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="每周几闭馆" prop="close_week">
          <el-select v-model="form.close_week" placeholder="请选择闭馆日期" style="width:100%">
            <el-option v-for="item in parameters.weeks" :key="item.value" :label="item.value" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-row>
          <el-col :span="7" :offset="1">
            <el-form-item class="longtext" label="可预约最大天数" prop="visit_max_day">
              <el-input v-model="form.visit_max_day" placeholder="请填写可预约最大天数" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="7" :offset="1">
            <el-form-item class="longtext" label="预约修改提前天数" prop="visit_update_day">
              <el-input v-model="form.visit_update_day" placeholder="请填写预约修改提前天数" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="7" :offset="1">
            <el-form-item class="longtext" label="三个月内爽约多少次数计入黑名单" prop="month_blacklist">
              <el-input v-model="form.month_blacklist" placeholder="请填写三个月内爽约多少次数计入黑名单" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="7" :offset="1">
            <el-form-item class="longtext" label="个人最大预定数量" prop="person_max_count">
              <el-input v-model="form.person_max_count" placeholder="请填写个人最大预定数量" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="7" :offset="1">
            <el-form-item class="longtext" label="团队最大预定数" prop="team_max_count">
              <el-input v-model="form.team_max_count" placeholder="请填写团队最大预定数" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="7" :offset="1">
            <el-form-item class="longtext" label="团队最小预定数" prop="team_min_count">
              <el-input v-model="form.team_min_count" placeholder="请填写团队最小预定数" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <div style="padding: 20px 0px;" class="form-sub-title">
          <span>预约时间
          </span>
        </div>
        <div class="table-tree tableswidth">
          <div style="display: flex;justify-content: flex-end;margin-right: 20px;margin-bottom: 10px;">
            <Button type="primary" @click="addRow()" size="small" style="margin-left: 10px;" ghost>新增</Button>
          </div>
          <el-table :data="form.rules_list" height="400" class="v-table" style="width: 100%;margin-bottom: 20px;">
            <el-table-column type="index" align="center">
            </el-table-column>
            <el-table-column prop="start_time" label="开始时间">
              <template slot-scope="scope">
                <el-time-select
                  style="width:100%"
                  v-model="scope.row.start_time"
                  :picker-options="{
                    start: '00:00',
                    step: '00:15',
                    end: '24:00'
                  }"
                  placeholder="选择开始时间">
                </el-time-select>
              </template>
            </el-table-column>
            <el-table-column prop="end_time" label="结束时间">
              <template slot-scope="scope">
                <el-time-select
                  style="width:100%"
                  v-model="scope.row.end_time"
                  :picker-options="{
                    start: '00:00',
                    step: '00:15',
                    end: '24:00',
                    minTime: scope.row.start_time
                  }"
                  placeholder="选择结束时间">
                </el-time-select>
              </template>
            </el-table-column>
            <el-table-column prop="total" label="可预约人数">
              <template slot-scope="scope">
                <el-input v-model="scope.row.total" placeholder="请填写可预约人数" autocomplete="off"></el-input>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="120px" align="center">
              <template slot-scope="scope">
                <Button type="error" @click="delRow(scope.$index)" size="small" style="margin-left: 10px;"
                  ghost>删除</Button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('form')">取 消</el-button>
        <el-button type="primary" @click="submitForm('form')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import LxHeader from "@/components/LxHeader/index.vue";
  import {
    listvisit,
    store,
    save,
    del,
    get,
    chanStatus
  } from "../../api/resource/visitresource.js";
  export default {
    components: {
      LxHeader
    },
    data() {
      return {
        dialogFormVisible: false,
        formLabelWidth: "120px",
        tableHeight: 0,
        //查询条件字段
        searchFields: {
          name: ""
        },
        tableData: [],
        paginations: {
          page: 1,
          page_size: 15,
          total: 0
        },
        parameters: {
          weeks: [{
            id:0,
            value:"不闭馆"
          },{
            id:1,
            value:"周一"
          },{
            id:2,
            value:"周二"
          },{
            id:3,
            value:"周三"
          },{
            id:4,
            value:"周四"
          },{
            id:5,
            value:"周五"
          },{
            id:6,
            value:"周六"
          },{
            id:7,
            value:"周日"
          }]
        },

        form: {
          name: "",
          notice: "",
          visit_max_day: "",
          visit_update_day: "",
          month_blacklist: "",
          close_week: 0,
          person_max_count: "",
          team_max_count: "",
          team_min_count: "",
          rules_list:[{
            start_time:"",
            end_time:"",
            total:""
          }]
        },

        columns: [{
            field: "name",
            title: "名称",
            type: "string",
          },
          {
            field: "status",
            title: "状态",
            type: "status",
            width:300,
            align:"center"
          },
          {
            field: "操作",
            title: "操作",
            width: 220,
            type: "opt",
          }
        ],
        rules: {
          name: [{
            required: true,
            message: '请输入名称',
            trigger: 'blur'
          }]

        },

      }
    },
    created() {
      this.initLoad();
      this.load();
    },
    mounted() {},
    methods: {
      initLoad() {
        var that = this;
        var clientHeight = document.documentElement.clientHeight
        var lxHeader_height = 96.5; //查询 头部
        var paginationHeight = 37; //分页的高度
        var topHeight = 50; //页面 头部
        let tableHeight = clientHeight - lxHeader_height - topHeight - paginationHeight - 20;
        that.tableHeight = tableHeight;
      },
      load() {
        var that = this;
        listvisit({
          page: this.paginations.page,
          page_size: this.paginations.page_size,
          name:this.searchFields.name
        }).then(res => {
          this.tableData = res.data;
          this.paginations.total = res.total
        }).catch(error => {

        })
      },
      info(obj) {
        var that = this;
        get(obj.id).then(res => {
          let result = Object.assign(that.form, res);
          that.form = result;
          that.form.rules_list = result.rules;
        }).catch(error => {
          //reject(error)
        })

      },
      edit(obj) {
        this.form = this.$options.data().form;
        this.clientHeight = document.documentElement.clientHeight - 84 - 110;
        if (obj) {
          var that = this;
          that.info(obj);
        } else {}
        this.dialogFormVisible = true;
      },
      submitForm(formName) {
        var that = this;
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (that.form.id) {
              save(that.form).then(response => {
                console.log(response)
                this.$Message.success('操作成功');
                that.dialogFormVisible = false;
                that.load();
              }).catch(error => {})
            } else {
              store(that.form).then(response => {
                console.log(response)
                this.$Message.success('操作成功');
                that.dialogFormVisible = false;
                that.load();
              }).catch(error => {})
            }

          } else {
            this.$Message.error('数据校验失败');
            return false;
          }
        });
      },
      resetForm(formName) {
        var that = this;
        this.$refs[formName].resetFields();
        that.dialogFormVisible = false;
      },
      changeStatus(obj){
        var that = this;
        if (obj) {
          let status = obj.status==0?1:0;
          let para={
            id:obj.id,
            status:status
          }
          this.$Modal.confirm({
            title: '确认要变更状态？',
            onOk: () => {
              chanStatus({
                id:para.id,
                status:para.status
              }).then(response => {
                this.$Message.success('操作成功');
                that.load();
              }).catch(error => {
                console.log(error)
                reject(error)
              })
            },
            onCancel: () => {
              //this.$Message.info('Clicked cancel');
            }
          });
        }
      },
      del(obj) {
        var that = this;
        if (obj) {
          this.$Modal.confirm({
            title: '确认要删除数据？',
            onOk: () => {
              del(obj.id).then(response => {
                this.$Message.success('操作成功');
                that.load();
              }).catch(error => {
                console.log(error)
                reject(error)
              })
            },
            onCancel: () => {
              //this.$Message.info('Clicked cancel');
            }
          });
        }
      },
      addRow() {
        var len = this.form.rules_list.length;
        this.form.rules_list.push({
          start_time: "",
          end_time:"",
          total:""
        });
      },
      delRow(index) {
        this.form.rules_list.splice(index, 1);
      },
      handleCurrentChange(page) {
        this.paginations.page = page;
        this.load();
      }
    }
  };
</script>
<style>
.longtext label{
  float:none;
  padding-right:0;
}
.longtext .el-form-item__content{
  margin-left:0!important
}
</style>
