<template>
  <div>
    <el-row type="flex" justify="center" class="search-box">
      <el-col :span="18">
        <el-input
          placeholder="输入姓名或身份证号进行搜索"
          clearable
          v-model="searchText"
          @change="searchPriority"
        ></el-input
      ></el-col>
      <el-col :span="2" style="text-align:center;">
        <el-button
          type="primary"
          icon="el-icon-search"
          @click="searchPriority"
        >搜索</el-button
      ></el-col>
    </el-row>

    <el-table :data="searchData" border style="width: 100%">
      <el-table-column fixed="left" prop="username" label="姓名">
      </el-table-column>
      <el-table-column prop="id_card" label="身份证号" min-width="180px"></el-table-column>
      <el-table-column prop="job_read" label="岗位读权限" min-width="100px"></el-table-column>
      <el-table-column prop="job_write" label="岗位写权限" min-width="100px"></el-table-column>
      <el-table-column prop="person_read" label="人员读权限" min-width="100px"></el-table-column>
      <el-table-column prop="person_write" label="人员写权限" min-width="100px"></el-table-column>
      <el-table-column prop="event_read" label="事件读权限" min-width="100px"></el-table-column>
      <el-table-column prop="event_write" label="事件写权限" min-width="100px"></el-table-column>
      <el-table-column fixed="right" label="操作" min-width="100px">
        <template slot-scope="scope">
          <el-button @click="clickModify(scope.row)" type="text" size="small"
            >修改</el-button
          >
          <el-button @click="clickDelete(scope.row)" type="text" size="small"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="[20, 50, 70, 100]"
      :page-size="pageSize"
      :hide-on-single-page="true"
      layout="total, sizes, prev, pager, next, jumper"
      :total="totalNum"
      class="pagination"
    >
    </el-pagination>
  </div>
</template>

<script>
import * as priorityApi from "@/api/priotity";
import { MessageBox, Message } from "element-ui";

export default {
  data() {
    return {
      searchText: "",
      pageSize: 20,
      pageNum: 1,
      totalNum: null,
      searchData: null,
    };
  },
  components: {},
  mounted() {
    if (!this.searchData) {
      this.searchPriority();
    }
  },
  methods: {
    searchPriority() {
      // 发请求
      priorityApi
        .getPriority({
          condition: this.searchText,
          page_num: this.pageNum,
          page_size: this.pageSize,
        })
        .then(
          (res) => {
            // 获取数据
            this.totalNum = res.data.total;
            this.searchData = res.data.data;
          },
          (err) => {
            console.log("err:", err);
          }
        );
    },
    clickModify(row) {
      // 单击修改
      this.$emit("changeCom", "PriorityModify", row);
    },
    clickDelete(row) {
      MessageBox.confirm(
        "永久删除用户 " + row.username + " " + row.id_card + "?",
        "注意",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          // 确定，发送删除请求
          priorityApi
            .deleteUser({
              data: { id_card: row.id_card },
            })
            .then(
              // todo 判断返回码
              (res) => {
                Message({
                  type: "success",
                  message: "删除成功！",
                });
                // 再次请求，刷新数据
                this.searchPriority();
              },
              (err) => {
                console.log("err:", err);
              }
            );
        })
        .catch(() => {});
    },
    handleSizeChange(curPageSize) {
      this.pageSize = curPageSize;
      this.searchPriority();
    },
    handleCurrentChange(curPageNum) {
      // 重发请求
      this.pageNum = curPageNum;
      this.searchPriority();
    },
  }
};
</script>

<style scoped>
.search-box {
  margin-bottom: 20px;
}

.pagination {
  margin-top: 10px;
}
</style>

