<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="组所在地址" width="300px" align="center">
          <template slot-scope="{ row }">
            <span>{{ row.address }}</span>
          </template>
        </el-table-column>

        <el-table-column label="组名" width="300px" align="center">
          <template slot-scope="{ row }">
            <span>{{ row.name }}</span>
          </template>
        </el-table-column>

        <el-table-column label="男/女宿舍" align="center">
          <template slot-scope="{ row }">
            <span>{{ row.gender }}</span>
          </template>
        </el-table-column>

        <el-table-column label="组类型" align="center">
          <template slot-scope="{ row }">
            <span>{{ typeNames[row.type] }}</span>
          </template>
        </el-table-column>

        <el-table-column label="备注" align="center">
          <template slot-scope="{ row }">
            <span>{{ row.comment }}</span>
          </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="address"
        >
          <el-col :span="12">
            <el-input
              v-model="temp.address"
              autocomplete="off"
              placeholder="请输入组所在地址"
            ></el-input>
          </el-col>
        </el-form-item>

        <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="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.id"
                :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="gender"
        >
          <el-col :span="12">
            <el-select
              v-model="temp.gender"
              size="mini"
              style="width: 100%"
              placeholder="男/女"
            >
              <el-option
                v-for="item in sexs"
                :key="item.value"
                :label="item.label"
                :value="item.label"
              >
              </el-option>
            </el-select>
          </el-col>
        </el-form-item>

        <el-form-item
          style="white-space: pre"
          label="备  注  信  息："
          class="switch-button-item"
          prop="comment"
        >
          <el-col :span="12">
            <el-input
              v-model="temp.comment"
              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,
  createGroup,
  updateGroup,
  deletGroup,
} from "@/api/groupmanagement";
export default {
  components: {
    Pagination,
  },
  data() {
    return {
      typeNames: ["禁用", "学生宿舍", "教师宿舍", "教学楼", "大门", "行政楼"],
      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: "",
        address: "",
        name: "",
        comment: "",
        gender: "",
        type: "",
        status: "1",
      },
      rules: {
        address: [
          {
            required: true,
            message: "请输入组所在地址",
            trigger: "change",
          },
        ],
        name: [
          {
            required: true,
            message: "请输入组名",
            trigger: "change",
          },
        ],
        type: [
          {
            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("是否删除？")) {
        deletGroup({ id: row.id }).then(() => {
          this.$notify({
            title: "删除成功",
            type: "success",
            duration: 1500,
          });
          this.list.splice(index, 1);
        });
      }
      this.getList();
    },
    resetTemp() {
      this.temp = {
        id: undefined,
        serialNumber: "",
        name: "",
        manufacturer: "",
        model: "",
        ipAddress: "",
        status: "1",
      };
    },
    handleCreate() {
      this.resetTemp();
      this.dialogStatus = "create";
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].clearValidate();
      });
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row); // copy obj
      this.dialogStatus = "update";
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].clearValidate();
      });
    },
    createData() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          createGroup(this.temp).then(() => {
            this.list.push(this.temp);
            this.dialogVisible = false;
            this.$notify({
              title: "添加成功",
              type: "success",
              duration: 1500,
            });
          });
        }
      });
    },
    updateData() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.temp);
          updateGroup(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>
