<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="小区" prop="communityId" v-if="username === 'admin'">
        <el-select v-model="queryParams.communityId" placeholder="请选择小区" filterable clearable>
          <el-option v-for="item in communityList" :key="item.communityId" :value="item.communityId" :label="item.communityName" />
        </el-select>
      </el-form-item>
      <el-form-item label="访客姓名" prop="guestName">
        <el-input
          v-model="queryParams.guestName"
          placeholder="请输入访客姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="到访时间" prop="guestTime">
        <el-date-picker
          v-model="guestTime"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd HH:mm:ss"
          :default-time="['00:00:00', '23:59:59']"
        />
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.guestTime"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="请选择到访时间">-->
<!--        </el-date-picker>-->
      </el-form-item>
      <el-form-item label="访客电话" prop="guestPhone">
        <el-input
          v-model="queryParams.guestPhone"
          placeholder="请输入访客电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="访客户号" prop="guestRoom">
        <el-input
          v-model="queryParams.guestRoom"
          placeholder="请输入访客户号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车牌号" prop="plateNumber">
        <el-input
          v-model="queryParams.plateNumber"
          placeholder="请输入车牌号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </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-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="['business:guest: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="['business:guest: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="['business:guest:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:guest:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="guestList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="主键" align="center" prop="guestId" v-if="false"/>-->
      <el-table-column label="访问小区" align="center" prop="communityName" />
      <el-table-column label="访问户号" align="center" prop="guestRoom" />
      <el-table-column label="访客姓名" align="center" prop="guestName" />
      <el-table-column label="到访时间" align="center" prop="guestTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.guestTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="访客手机" align="center" prop="guestPhone" />
      <el-table-column label="车牌号" align="center" prop="plateNumber" />
      <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="['business:guest:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:guest:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @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="访问小区" prop="communityId">
          <el-select v-model="form.communityId" filterable style="width: 100%" placeholder="请选择访问小区">
            <el-option v-for="item in communityList" :key="item.communityId" :value="item.communityId" :label="item.communityName"/>
          </el-select>
        </el-form-item>
        <el-form-item label="访问户号" prop="guestRoom">
          <el-input v-model="form.guestRoom" placeholder="请输入访问户号" />
        </el-form-item>
        <el-form-item label="访客姓名" prop="guestName">
          <el-input v-model="form.guestName" placeholder="请输入访客姓名" />
        </el-form-item>
        <el-form-item label="到访时间" prop="guestTime">
          <el-date-picker clearable
            v-model="form.guestTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            style="width: 100%"
            placeholder="请选择到访时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="访客手机" prop="guestPhone">
          <el-input v-model="form.guestPhone" placeholder="请输入访客手机" />
        </el-form-item>
        <el-form-item label="车牌号" prop="plateNumber">
          <el-input v-model="form.plateNumber" placeholder="请输入车牌号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGuest, getGuest, delGuest, addGuest, updateGuest } from "@/api/business/guest";
import {listCommunityAll} from "@/api/business/community";

export default {
  name: "Guest",
  data() {
    return {
      username: undefined,
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 访客表格数据
      guestList: [],
      communityList: [],
      guestTime: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: undefined,
        guestName: undefined,
        guestTime: undefined,
        guestPhone: undefined,
        guestRoom: undefined,
        plateNumber: undefined,
        communityId: undefined,
        communityName: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        guestName: [
          { required: true, message: "访客姓名不能为空", trigger: "blur" }
        ],
        guestTime: [
          { required: true, message: "到访时间不能为空", trigger: "blur" }
        ],
        guestPhone: [
          { required: true, message: "访客手机不能为空", trigger: "blur" }
        ],
        guestRoom: [
          { required: true, message: "访客房间号不能为空", trigger: "blur" }
        ],
        plateNumber: [
          { required: true, message: "车牌号不能为空", trigger: "blur" }
        ],
        communityId: [
          { required: true, message: "小区不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    let sessionObj = JSON.parse(sessionStorage.getItem('sessionObj'));
    if (sessionObj) {
      let loginUser = JSON.parse(sessionObj.data);
      if (loginUser) {
        this.username = loginUser.username
      }
    }
    this.getList();
    this.getCommunityListAll();
  },
  methods: {
    /** 查询访客列表 */
    getList() {
      this.loading = true;
      if (null != this.guestTime && this.guestTime.length > 0) {
        this.queryParams.guestStartTime = this.guestTime[0];
        this.queryParams.guestEndTime = this.guestTime[1];
      }else {
        this.queryParams.guestStartTime = undefined;
        this.queryParams.guestEndTime = undefined;
      }
      listGuest(this.queryParams).then(response => {
        this.guestList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询全部小区列表 */
    getCommunityListAll() {
      listCommunityAll().then(response => {
        this.communityList = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        guestId: undefined,
        userId: undefined,
        guestName: undefined,
        guestTime: undefined,
        guestPhone: undefined,
        guestRoom: undefined,
        plateNumber: undefined,
        communityId: undefined,
        communityName: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.guestTime = []
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.guestId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加访客";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const guestId = row.guestId || this.ids
      getGuest(guestId).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改访客";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.guestId != null) {
            updateGuest(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addGuest(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const guestIds = row.guestId || this.ids;
      this.$modal.confirm('是否确认删除访客编号为"' + guestIds + '"的数据项？').then(() => {
        this.loading = true;
        return delGuest(guestIds);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/guest/export', {
        ...this.queryParams
      }, `guest_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
