<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" class="v-table" :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="toactive(scope.row)" type="success"
                  style="margin-left: 10px;">订单</Button>
                <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="info" @click="changeStatus(scope.row)"
                  style="margin-left: 10px;">下架</Button>
                <Button v-if="scope.row['status']==0" ghost size="small" type="warning" @click="changeStatus(scope.row)"
                  style="margin-left: 10px;">上架</Button>
              </div>
              <div v-else-if="column.type=='dateRange'">
                {{scope.row["start_time"]+"至"+scope.row["end_time"]}}
              </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" width="60%">
      <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="address">
          <el-input v-model="form.address" placeholder="请填写停车场地址" autocomplete="off"></el-input>
        </el-form-item>
        <el-row>
          <el-col :span="10">
            <el-form-item label="地址经度" prop="longitude">
              <el-input v-model="form.longitude" placeholder="请填写地址经度" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="10" :offset="4">
            <el-form-item label="地址纬度" prop="latitude">
              <el-input v-model="form.latitude" placeholder="请填写地址纬度" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="10">
            <el-form-item label="开始时间" prop="start_time">
              <el-time-select style="width:100%" placeholder="开始时间" v-model="form.start_time" :picker-options="{
                      start: '00:00',
                      step: '00:15',
                      end: '24:00'
                    }">
              </el-time-select>
            </el-form-item>
          </el-col>
          <el-col :span="10" :offset="4">
            <el-form-item label="结束时间" prop="end_time">
              <el-time-select style="width:100%" placeholder="结束时间" v-model="form.end_time" :picker-options="{
                    start: '00:00',
                    step: '00:15',
                    end: '24:00',
                    minTime: form.start_time
                  }">
              </el-time-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="小车位数" prop="small_park_total">
              <el-input v-model="form.small_park_total" placeholder="请填写小车位数" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="大车位数(09:00至12:00)" prop="big_park_total">
              <el-input v-model="form.big_park_total" placeholder="请填写大车位数(09:00至12:00)" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="大车位数(13:00至16:00)" prop="big_park_total">
              <el-input v-model="form.big_park_total2" placeholder="请填写大车位数(13:00至16:00)" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="残疾人车位数" prop="special_park_total">
              <el-input v-model="form.special_park_total" placeholder="请填写残疾人车位数" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="充电桩总数" prop="charge_total">
              <el-input v-model="form.charge_total" placeholder="请填写充电桩总数" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

      </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 {
    listpark,
    store,
    save,
    del,
    get,
    chanStatus
  } from "../../api/resource/parking.js";
  export default {
    components: {
      LxHeader
    },
    created() {
      this.initLoad();
      this.load();
    },
    mounted() {},
    data() {
      return {
        dialogFormVisible: false,
        formLabelWidth: "120px",
        tableHeight: 0,
        //查询条件字段
        searchFields: {
          name: ""
        },
        paginations: {
          page: 1,
          page_size: 15,
          total: 0
        },
        tableData: [],
        form: {
          name: "",
          address: "",
          start_time: "",
          end_time: "",
          charge_total: "",
          small_park_total: "",
          big_park_total: "",
          big_park_total2: "",
          special_park_total:"",
          longitude: "",
          latitude: "",
        },
        columns: [{
            field: "name",
            title: "停车场名称",
            type: "string",
          },
          {
            field: "address",
            title: "地址",
            type: "string",
          },
          {
            field: "dateRange",
            title: "可入场时段",
            type: "dateRange",
            align: "center",
            width: 120
          },
          {
            field: "charge_total",
            title: "充电桩总数",
            type: "string",
            align: "center",
            width: 120
          },
          {
            field: "small_park_total",
            title: "小车位数",
            type: "string",
            align: "center",
            width: 120
          },
          {
            field: "big_park_total",
            title: "大车位数上午",
            type: "string",
            align: "center",
            width: 120
          },
          {
            field: "big_park_total2",
            title: "大车位数下午",
            type: "string",
            align: "center",
            width: 120
          },
          {
            field: "special_park_total",
            title: "残疾人车位数",
            type: "string",
            align: "center",
            width: 120
          },
          {
            field: "status",
            title: "状态",
            type: "status",
            align: "center",
            width: 120
          },
          {
            field: "操作",
            title: "操作",
            width: 240,
            type: "opt",
          }
        ],
        rules: {
          name: [{
            required: true,
            message: '请输入停车场名称',
            trigger: 'blur'
          }],
          address: [{
            required: true,
            message: '请输入停车场地址',
            trigger: 'blur'
          }],
          longitude: [{
            required: true,
            message: '请输入经度',
            trigger: 'blur'
          }],
          latitude: [{
            required: true,
            message: '请输入纬度',
            trigger: 'blur'
          }]

        },

      }
    },
    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;
        listpark({
          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 => {

        })
      },
      show(obj) {
        this.clientHeight = document.documentElement.clientHeight - 84 - 110;
        this.dialogViewVisible = true;
        this.info(obj);
      },
      info(obj) {
        var that = this;
        get(obj.id).then(res => {
          let result = Object.assign(that.form, res);
          that.form = result;

        }).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;
      },
      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');
            }
          });
        }
      },
      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');
            }
          });
        }
      },
      handleCurrentChange(page) {
        this.paginations.page = page;
        this.load();
      },
      toactive(obj) {
        if (obj.id) {
          this.$router.push({
            path: '/order/parkorder',
            query: {
              car_park_id: obj.id
            }
          })

        }
      }
    }
  };
</script>
