<template>
  <div class="body">
    <!-- 头部区域 -->
    <div class="head">
      <div class="head-img">
        <img src="../../../public/images/img-1.png" />
      </div>
      <div class="head-info">用户管理</div>
    </div>

    <!-- 添加按钮 -->
    <div class="switch">
      所有用户
      <div class="switch-button">
        <el-row>
          <el-button type="primary" size="small" @click="handleCreate"
            ><img src="../../../public/images/img-3.png" />添加用户
          </el-button>
        </el-row>
      </div>
    </div>

    <!-- 表格区域 -->
    <div class="form">
      <el-table :data="list">
        <el-table-column label="姓名" align="center">
          <template slot-scope="{ row }">
            <span>{{ row.name }}</span>
          </template>
        </el-table-column>

        <el-table-column label="卡号" width="300px" align="center">
          <template slot-scope="{ row }">
            <span>{{ row.cardNumber }}</span>
          </template>
        </el-table-column>

        <el-table-column label="学校" align="center">
          <template slot-scope="{ row }">
            <span>{{ row.college }}</span>
          </template>
        </el-table-column>

        <el-table-column label="专业" align="center">
          <template slot-scope="{ row }">
            <span>{{ row.professor }}</span>
          </template>
        </el-table-column>

        <!-- <el-table-column label="开关" align="center">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              active-color="#3BAAFD"
              inactive-color="#CCCCCC"
              :active-value="1"
              :inactive-value="0"
              @change="changeSwitch(scope.row.status)"
            >
            </el-switch>
          </template>
        </el-table-column> -->

        <el-table-column label="操作" width="180px" align="center">
          <template slot-scope="{ row, $index }">
            <el-button type="text" size="small" @click="handleUpdate(row)"
              >编辑</el-button
            >

            <el-button
              @click="handleDelete(row, $index)"
              type="text"
              size="small"
              class="button"
              ><img src="../../../public/images/img-2.png" />删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 底部分页区 -->
      <div class="block">
        <template>
          <pagination
            :total="total"
            :page.sync="listQuery.page"
            :limit.sync="listQuery.limit"
            @pagination="getList"
          />
        </template>
      </div>
    </div>

    <!-- 添加编辑按钮弹窗区域 -->
    <el-dialog
      :title="textMap[dialogStatus]"
      :visible.sync="dialogVisible"
      width="30%"
      center
    >
      <el-form ref="dataForm" :model="temp" :rules="rules">
        <el-form-item
          style="white-space: pre"
          label="姓名："
          class="switch-button-item"
          prop="name"
        >
          <el-col :span="12">
            <el-input
              v-model="temp.name"
              autocomplete="off"
              placeholder="请输入姓名"
            ></el-input>
          </el-col>
        </el-form-item>

        <el-form-item
          style="white-space: pre"
          label="卡号："
          class="switch-button-item"
          prop="cardNumber"
        >
          <el-col :span="12">
            <el-input
              v-model="temp.cardNumber"
              autocomplete="off"
              placeholder="请输入卡号"
            ></el-input>
          </el-col>
        </el-form-item>

        <!-- <el-form-item
          style="white-space: pre"
          label="组   类    型："
          class="switch-button-item"
          prop="type"
        >
          <el-col :span="12">
            <el-select
              v-model="temp.type"
              size="mini"
              style="width: 100%"
              placeholder="分组类型"
            >
              <el-option
                v-for="item in types"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-col>
        </el-form-item> -->

        <el-form-item
          style="white-space: pre"
          label="学校："
          class="switch-button-item"
          prop="college"
        >
          <el-col :span="12">
            <el-input
              v-model="temp.college"
              autocomplete="off"
              placeholder="请输入学校"
            ></el-input>
          </el-col>
        </el-form-item>

        <el-form-item
          style="white-space: pre"
          label="专业："
          class="switch-button-item"
          prop="professor"
        >
          <el-col :span="12">
            <el-input
              v-model="temp.professor"
              autocomplete="off"
              placeholder="请输入专业"
            ></el-input>
          </el-col>
        </el-form-item>
      </el-form>

      <span slot="footer">
        <el-button
          type="primary"
          @click="dialogStatus === 'create' ? createData() : updateData()"
          >确 定</el-button
        >
        <el-button @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from "@/components/Pagination";
import {
  fetchList,
  createUser,
  updateUser,
  deletUser,
} from "@/api/usermanagement";
export default {
  components: {
    Pagination,
  },
  data() {
    return {
      list: [],
      total: 0, //数据的总数//
      listQuery: {
        page: 1, //当前在第几页//
        limit: 8, //一页几条//
      },
      sexs: [
        {
          value: "1",
          label: "男",
        },
        {
          value: "2",
          label: "女",
        },
      ],
      types: [
        { id: "1", value: "0-禁用", label: "0-禁用" },
        { id: "2", value: "1-学生宿舍", label: "1-学生宿舍" },
        { id: "3", value: "2-教师宿舍", label: "2-教师宿舍" },
        { id: "4", value: "3-教学楼", label: "3-教学楼" },
        { id: "5", value: "4-大门", label: "4-大门" },
        { id: "6", value: "5-行政楼", label: "5-行政楼" },
      ],
      value: "1",
      dialogVisible: false,
      dialogStatus: "",
      textMap: {
        update: "编辑用户",
        create: "添加用户",
      },
      temp: {
        id: "",
        name: "",
        cardNumber: "",
        classname: "",
        college: "",
        type: "",
        professor: "",
      },
      rules: {
        cardNumber: [
          {
            required: true,
            message: "请输入卡号",
            trigger: "change",
          },
        ],
        name: [
          {
            required: true,
            message: "请输入姓名",
            trigger: "change",
          },
        ],
        college: [
          {
            required: true,
            message: "请输入学校",
            trigger: "change",
          },
        ],
        professor: [
          {
            required: true,
            message: "请输入专业",
            trigger: "change",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      fetchList(this.listQuery).then((response) => {
        this.list = response.data;
        this.total = response.total;
      });
    },
    handleDelete(row, index) {
      if (confirm("是否删除？")) {
        deletUser({ id: row.id }).then(() => {
          this.$notify({
            title: "删除成功",
            type: "success",
            duration: 1500,
          });
          this.list.splice(index, 1);
        });
      }
    },
    resetTemp() {
      this.temp = {
        id: "",
        name: "",
        cardNumber: "",
        classname: "",
        college: "",
        type: "",
        professor: "",
      };
    },
    handleCreate() {
      this.resetTemp();
      this.dialogStatus = "create";
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].clearValidate();
      });
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row);
      this.dialogStatus = "update";
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].clearValidate();
      });
    },
    createData() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          createUser(this.temp).then(() => {
            this.list.push(this.temp);
            this.dialogVisible = false;
            this.$notify({
              title: "添加成功",
              type: "success",
              duration: 1500,
            });
          });
        }
      });
      this.getList();
    },
    updateData() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.temp);
          updateUser(tempData).then(() => {
            const index = this.list.findIndex((v) => v.id === this.temp.id);
            this.list.splice(index, 1, this.temp);
            this.dialogVisible = false;
            this.$notify({
              title: "编辑成功",
              type: "success",
              duration: 1500,
            });
          });
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.body {
  width: 1551px;
  margin: 29px 29px 0 31px;
  .head {
    display: flex;
    width: 1380px;
    height: 55px;
    margin-left: 40px;
    border-bottom: 1px solid #cccccc;

    .head-img {
      width: 30px;
      height: 30px;

      img {
        width: 30px;
      }
    }

    .head-info {
      width: 95px;
      height: 22px;
      margin-top: 5px;
      margin-left: 16px;
      font-weight: bold;
    }
  }

  .switch {
    display: flex;
    width: 1380px;
    line-height: 58px;
    margin-left: 40px;
    font-size: 9px;

    .switch-sw {
      width: 45px;
      margin-left: 19px;
    }

    .switch-button {
      width: 50px;
      margin-left: 103px;

      img {
        width: 10.5px;
        margin-right: 8px;
      }
    }
  }

  .form {
    margin-left: 40px;
    width: 1380px;

    .button-img {
      img {
        width: 53.5px;
      }
    }

    .button-info {
      margin-top: 39px;
    }

    .button {
      color: red;
    }

    img {
      width: 8.5px;
      margin-right: 8px;
    }

    .block {
      float: right;
    }

    .foot-button {
      margin-top: 53px;
    }
  }
  .switch-button-item {
    width: 400px;
    margin-left: 120px;
    margin-top: 30px;
  }
}
</style>
