<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="visitDate">
              <el-date-picker clearable
                v-model="queryParams.visitDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择访问日期">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="被访部门" prop="visitDept">
              <el-input
                v-model="queryParams.visitDept"
                placeholder="请输入被访部门"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="被访人" prop="visitObject">
              <el-input
                v-model="queryParams.visitObject"
                placeholder="请输入被访人"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="到访理由" prop="visitReason">
              <el-input
                v-model="queryParams.visitReason"
                placeholder="请输入到访理由"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="预计离开时间" prop="departureTime">
              <el-date-picker clearable
                v-model="queryParams.departureTime"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择预计离开时间">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="领导确认" prop="leaderConfirm">
              <el-input
                v-model="queryParams.leaderConfirm"
                placeholder="请输入领导确认"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="访问区域" prop="visitArea">
              <el-input
                v-model="queryParams.visitArea"
                placeholder="请输入访问区域"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="入口名称" prop="entryName">
              <el-input
                v-model="queryParams.entryName"
                placeholder="请输入入口名称"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="车辆数量" prop="carNumber">
              <el-input
                v-model="queryParams.carNumber"
                placeholder="请输入车辆数量"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="携带物品" prop="bringThings">
              <el-input
                v-model="queryParams.bringThings"
                placeholder="请输入携带物品"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="物品描述" prop="thingsDesc">
              <el-input
                v-model="queryParams.thingsDesc"
                placeholder="请输入物品描述"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="物品数量" prop="thingsNumber">
              <el-input
                v-model="queryParams.thingsNumber"
                placeholder="请输入物品数量"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="操作人员" prop="operator">
              <el-input
                v-model="queryParams.operator"
                placeholder="请输入操作人员"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="操作时间" prop="operateTime">
              <el-date-picker clearable
                v-model="queryParams.operateTime"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择操作时间">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="是否预约" prop="isBook">
              <el-input
                v-model="queryParams.isBook"
                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="['visit:info: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="['visit:info: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="['visit:info: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="['visit:info:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList"  @sort-change="handleSortChange"  @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
              <el-table-column label="Id" align="center" prop="id" sortable='custom' />
              <el-table-column label="访问日期" align="center" prop="visitDate" width="180" sortable='custom'>
                <template slot-scope="scope">
                  <span>{{ parseTime(scope.row.visitDate, '{y}-{m}-{d}') }}</span>
                </template>
              </el-table-column>
              <el-table-column label="被访部门" align="center" prop="visitDept" sortable='custom' />
              <el-table-column label="被访人" align="center" prop="visitObject" sortable='custom' />
              <el-table-column label="到访理由" align="center" prop="visitReason" sortable='custom' />
              <el-table-column label="预计离开时间" align="center" prop="departureTime" width="180" sortable='custom'>
                <template slot-scope="scope">
                  <span>{{ parseTime(scope.row.departureTime, '{y}-{m}-{d}') }}</span>
                </template>
              </el-table-column>
              <el-table-column label="领导确认" align="center" prop="leaderConfirm" sortable='custom' />
              <el-table-column label="访问区域" align="center" prop="visitArea" sortable='custom' />
              <el-table-column label="入口名称" align="center" prop="entryName" sortable='custom' />
              <el-table-column label="车辆类型" align="center" prop="carType" sortable='custom' />
              <el-table-column label="车辆数量" align="center" prop="carNumber" sortable='custom' />
              <el-table-column label="携带物品" align="center" prop="bringThings" sortable='custom' />
              <el-table-column label="物品描述" align="center" prop="thingsDesc" sortable='custom' />
              <el-table-column label="物品数量" align="center" prop="thingsNumber" sortable='custom' />
              <el-table-column label="操作人员" align="center" prop="operator" sortable='custom' />
              <el-table-column label="操作时间" align="center" prop="operateTime" width="180" sortable='custom'>
                <template slot-scope="scope">
                  <span>{{ parseTime(scope.row.operateTime, '{y}-{m}-{d}') }}</span>
                </template>
              </el-table-column>
              <el-table-column label="状态0录入1确认2驳回" align="center" prop="status" sortable='custom' />
              <el-table-column label="备注" align="center" prop="remark" sortable='custom' />
              <el-table-column label="是否预约" align="center" prop="isBook" sortable='custom' />
      <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="['visit:info:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['visit:info:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      hide-on-single-page
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改访客访问信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="70%" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
                <el-col :span="12">
                  <el-form-item label="访问日期" prop="visitDate">
                    <el-date-picker clearable
                      v-model="form.visitDate"
                      type="date"
                      value-format="yyyy-MM-dd"
                      placeholder="请选择访问日期">
                    </el-date-picker>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="被访部门" prop="visitDept">
                    <el-input v-model="form.visitDept" placeholder="请输入被访部门" />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="被访人" prop="visitObject">
                    <el-input v-model="form.visitObject" placeholder="请输入被访人" />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="到访理由" prop="visitReason">
                    <el-input v-model="form.visitReason" placeholder="请输入到访理由" />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="预计离开时间" prop="departureTime">
                    <el-date-picker clearable
                      v-model="form.departureTime"
                      type="date"
                      value-format="yyyy-MM-dd"
                      placeholder="请选择预计离开时间">
                    </el-date-picker>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="领导确认" prop="leaderConfirm">
                    <el-input v-model="form.leaderConfirm" placeholder="请输入领导确认" />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="访问区域" prop="visitArea">
                    <el-input v-model="form.visitArea" placeholder="请输入访问区域" />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="入口名称" prop="entryName">
                    <el-input v-model="form.entryName" placeholder="请输入入口名称" />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="车辆数量" prop="carNumber">
                    <el-input v-model="form.carNumber" placeholder="请输入车辆数量" />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="携带物品" prop="bringThings">
                    <el-input v-model="form.bringThings" placeholder="请输入携带物品" />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="物品描述" prop="thingsDesc">
                    <el-input v-model="form.thingsDesc" placeholder="请输入物品描述" />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="物品数量" prop="thingsNumber">
                    <el-input v-model="form.thingsNumber" placeholder="请输入物品数量" />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="操作人员" prop="operator">
                    <el-input v-model="form.operator" placeholder="请输入操作人员" />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="操作时间" prop="operateTime">
                    <el-date-picker clearable
                      v-model="form.operateTime"
                      type="date"
                      value-format="yyyy-MM-dd"
                      placeholder="请选择操作时间">
                    </el-date-picker>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="备注" prop="remark">
                    <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="是否预约" prop="isBook">
                    <el-input v-model="form.isBook" placeholder="请输入是否预约" />
                  </el-form-item>
                </el-col>
      </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 { pageListInfo, getInfo, delInfo, addInfo, updateInfo } from "@/api/visit/info";

  export default {
    name: "Info",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 选中名字
        names: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 访客访问信息表格数据
              infoList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
                    visitDate: null,
                    visitDept: null,
                    visitObject: null,
                    visitReason: null,
                    departureTime: null,
                    leaderConfirm: null,
                    visitArea: null,
                    entryName: null,
                    carType: null,
                    carNumber: null,
                    bringThings: null,
                    thingsDesc: null,
                    thingsNumber: null,
                    operator: null,
                    operateTime: null,
                    status: null,
                    isBook: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询访客访问信息列表 */
      getList() {
        this.loading = true;
        pageListInfo(this.queryParams).then(response => {
          this.infoList = response.rows;
          this.total = response.total;
          this.loading = false;
        }).catch(e => {
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
                    id: null,
                    visitDate: null,
                    visitDept: null,
                    visitObject: null,
                    visitReason: null,
                    departureTime: null,
                    leaderConfirm: null,
                    visitArea: null,
                    entryName: null,
                    carType: null,
                    carNumber: null,
                    bringThings: null,
                    thingsDesc: null,
                    thingsNumber: null,
                    operator: null,
                    operateTime: null,
                    status: null,
                    remark: null,
                    isBook: null
        };
        this.resetForm("form");
      },
      //排序
      handleSortChange(col) {
        this.$sortBy(col, this.queryParams);
        this.getList();
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id);
        this.names = 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;
        getInfo(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) {
              updateInfo(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addInfo(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        const names = row.id || this.names;
        this.$modal.confirm('是否确认删除访客访问信息为"' + names + '"的数据项？').then(function() {
          return delInfo(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
  /** 导出按钮操作 */
  handleExport() {
    this.download('visit/info/export', {
      ...this.queryParams
    }, `info_${new Date().getTime()}.xlsx`)
  }
  }
  };
</script>
