<template>
  <div class="card-container">
    <el-card class="card-heard">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
        <el-form-item label="停车场名称" prop="parkingId">
          <el-select
            v-model="queryParams.param.parkingId"
            placeholder="请输入停车场名称"
            clearable
          >
            <el-option
              v-for="item in parkNameList"
              :key="item.id"
              :label="item.parkingName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="车位编号" prop="spaceNo">
          <el-input
            v-model="queryParams.param.spaceNo"
            placeholder="请输入车位编号"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="业主手机号" prop="userPhone">
          <el-input
            v-model="queryParams.param.userPhone"
            placeholder="请输入业主手机号"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-select
            v-model="queryParams.param.state"
            placeholder="请选择"
            clearable
            size="small"
          >
            <el-option
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="发布日期">
          <el-date-picker
            v-model="dateRange"
            size="small"
            style="width: 280px"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="card-body">
      <!--
              <el-row :gutter="10" class="mb8">
                  <el-col :span="1.5">
                      <el-button
                              type="primary"
                              plain
                              icon="el-icon-plus"
                              size="mini"
                              @click="handleAdd"
                              v-hasPermi="['shared:add']"
                      >新增
                      </el-button>
                  </el-col>
                  <el-col :span="1.5">
                      <el-button
                              type="success"
                              plain
                              icon="el-icon-edit"
                              size="mini"
                              :disabled="single"
                              @click="handleUpdate"
                              v-hasPermi="['shared:edit']"
                      >修改
                      </el-button>
                  </el-col>
                  <el-col :span="1.5">
                      <el-button
                              type="danger"
                              plain
                              icon="el-icon-delete"
                              size="mini"
                              :disabled="multiple"
                              @click="handleDelete"
                              v-hasPermi="['shared:remove']"
                      >删除
                      </el-button>
                  </el-col>
              </el-row>
      -->

      <el-table v-loading="loading" :data="sharedList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column label="停车场名称" align="center" prop="parkingName" :show-overflow-tooltip="true"/>
        <el-table-column label="车位编号" align="center" prop="spaceNo" :show-overflow-tooltip="true"/>
        <el-table-column label="位置描述" align="center" prop="address" :show-overflow-tooltip="true"/>
        <el-table-column label="车位坐标" align="center" prop="longitude" :show-overflow-tooltip="true">
          <template slot-scope="scope" v-if="scope.row.longitude!=null && scope.row.latitude!=null">
            {{ scope.row.longitude + ',' + scope.row.latitude }}
          </template>
        </el-table-column>
        <el-table-column label="业主手机号" align="center" prop="userPhone" :show-overflow-tooltip="true"/>
        <el-table-column label="状态" align="center" prop="state" :show-overflow-tooltip="true"/>
        <el-table-column label="替换车牌号" align="center" prop="carNumber" :show-overflow-tooltip="true"/>
        <el-table-column label="起止时间" align="center" prop="leaseStart" width="280" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.leaseStart, '{y}-{m}-{d} {h}:{i}') }}</span> 至
            <span>{{ parseTime(scope.row.leaseEnd, '{y}-{m}-{d} {h}:{i}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="出租价格" align="center" prop="rentalPrice" :show-overflow-tooltip="true"/>
        <el-table-column label="发布时间" align="center" prop="createTime" :show-overflow-tooltip="true"/>
        <!--            <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="['shared:edit']"
                            >修改
                            </el-button>
                            <el-button
                                    size="mini"
                                    type="text"
                                    icon="el-icon-delete"
                                    @click="handleDelete(scope.row)"
                                    v-hasPermi="['shared:remove']"
                            >删除
                            </el-button>
                        </template>
                    </el-table-column>-->
      </el-table>
    </el-card>
    <pagination
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />

    <!-- 添加或修改共享车位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="停车场ID" prop="parkingId">
          <el-input v-model="form.parkingId" placeholder="请输入停车场ID"/>
        </el-form-item>
        <el-form-item label="车辆ID" prop="carId">
          <el-input v-model="form.carId" placeholder="请输入车辆ID"/>
        </el-form-item>
        <el-form-item label="车位ID" prop="spaceId">
          <el-input v-model="form.spaceId" placeholder="请输入车位ID"/>
        </el-form-item>
        <el-form-item label="租赁开始时间" prop="leaseStart">
          <el-date-picker clearable size="small"
                          v-model="form.leaseStart"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择租赁开始时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="租赁结束时间" prop="leaseEnd">
          <el-date-picker clearable size="small"
                          v-model="form.leaseEnd"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择租赁结束时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="出租价格" prop="rentalPrice">
          <el-input v-model="form.rentalPrice" placeholder="请输入出租价格"/>
        </el-form-item>
        <el-form-item label="发起人ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入发起人ID"/>
        </el-form-item>
        <el-form-item label="001 未接单 002 已接单 003 已完成 004已取消" prop="state">
          <el-input v-model="form.state" placeholder="请输入001 未接单 002 已接单 003 已完成 004已取消"/>
        </el-form-item>
        <el-form-item label="手机号" prop="userPhone">
          <el-input v-model="form.userPhone" placeholder="请输入手机号"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="删除标志" prop="isDel">
          <el-input v-model="form.isDel" placeholder="请输入删除标志"/>
        </el-form-item>
        <el-form-item label="版本号" prop="version">
          <el-input v-model="form.version" placeholder="请输入版本号"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listParkName
} from '@/api/carManage/parking'
import {
  listShared,
  getShared,
  delShared,
  addShared,
  updateShared,
  exportShared
} from '@/api/carManage/shared'

export default {
  name: 'Shared',
  components: {},
  data() {
    return {
      // 状态数据字典
      statusOptions: [],
      // 选择停车场名称列表
      parkNameList: [],
      // 日期范围
      dateRange: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 共享车位表格数据
      sharedList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        param: {
          parkingId: null,
          carId: null,
          spaceId: null,
          leaseStart: null,
          leaseEnd: null,
          rentalPrice: null,
          userId: null,
          state: null,
          userPhone: null,
          isDel: null,
          version: null
        }
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        parkingId: [
          {
            required: true,
            trigger: 'blur'
          }
        ],
        carId: [
          {
            required: true,
            trigger: 'blur'
          }
        ],
        spaceId: [
          {
            required: true,
            trigger: 'blur'
          }
        ],
        leaseStart: [
          {
            required: true,
            trigger: 'blur'
          }
        ],
        leaseEnd: [
          {
            required: true,
            trigger: 'blur'
          }
        ],
        rentalPrice: [
          {
            required: true,
            trigger: 'blur'
          }
        ],
        userId: [
          {
            required: true,
            trigger: 'blur'
          }
        ],
        userPhone: [
          {
            required: true,
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    this.initTime()
    this.getList()
    this.getDicts("shared_status").then(response => {
      this.statusOptions = response.data;
    });
    listParkName({}).then(response => {
      this.parkNameList = response.result
    })
  },
  methods: {
    initTime() {
      // 初始化查询，默认为前30天到今天
      let date = new Date();
      let startDate = new Date(date.getTime() - 24 * 60 * 60 * 1000 * 30);
      let endDate = date;
      this.dateRange = [
        this.getDateDay(startDate),
        this.getDateDay(endDate),
      ];
    },
    getDateDay(date) {
      //获取当前时间的年份转为字符串
      let year = date.getFullYear().toString(); //'2019'
      //获取月份，由于月份从0开始，此处要加1，判断是否小于10，如果是在字符串前面拼接'0'
      let month =
        date.getMonth() + 1 < 10
          ? "0" + (date.getMonth() + 1).toString()
          : (date.getMonth() + 1).toString(); //'04'
      //获取天，判断是否小于10，如果是在字符串前面拼接'0'
      let da =
        date.getDate() < 10
          ? "0" + date.getDate().toString()
          : date.getDate().toString(); //'12'
      //字符串拼接，开始时间，结束时间
      return year + "-" + month + "-" + da; //当天'2019-04-12'
    },
    /** 查询共享车位列表 */
    getList() {
      this.loading = true
      if (null != this.dateRange && '' != this.dateRange) {
        this.queryParams.param["beginTime"] = this.dateRange[0];
        this.queryParams.param["endTime"] = this.dateRange[1];
      }
      listShared(this.queryParams).then(response => {
        this.sharedList = response.records
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        parkingId: null,
        carId: null,
        spaceId: null,
        leaseStart: null,
        leaseEnd: null,
        rentalPrice: null,
        userId: null,
        state: null,
        userPhone: null,
        createTime: null,
        updateTime: null,
        remark: null,
        isDel: null,
        version: null
      }

      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.queryParams = {
        current: 1,
        size: 10,
        param: {},
      };
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加共享车位'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id =
        row.id || this.ids
      getShared(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改共享车位'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateShared(this.form).then(response => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addShared(this.form).then(response => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除共享车位编号为"' + ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delShared(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      })
    }
  }
}
</script>
