<template>
  <div class="app-container">
    <!--操作区-->
    <el-button
      type="primary"
      icon="el-icon-plus"
      @click="create('add')"
      class="margin-bottom"
      >添加账号</el-button
    >
    <!--搜索区-->
    <div class="float">
      <!-- <el-select
        filterable
        clearable
        v-model="page.data.adminId"
        placeholder="请选择账号"
        class="margin-left input"
      >
        <el-option
          v-for="item in account_list"
          :key="item.adminId"
          :label="item.username"
          :value="item.adminId"
        >
        </el-option>
      </el-select> -->
      <el-input
        v-model="page.data.manageName"
        placeholder="请输入姓名"
        clearable
        class="margin-left input"
      ></el-input>
      <el-input
        v-model="page.data.managePhone"
        placeholder="请输入手机号"
        clearable
        class="margin-left input"
      ></el-input>
      <el-button
        type="primary"
        icon="el-icon-search"
        @click="fetchData"
        class="margin-left"
        >搜索</el-button
      >
    </div>
    <!--主表格-->
    <el-table
      class="table"
      ref="table_form"
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column label="序号" align="center" width="80px">
        <template slot-scope="scope">
          {{ indexMethod(scope.$index) }}
        </template>
      </el-table-column>
      <el-table-column label="账号名称" align="center">
        <template slot-scope="scope">
          {{ scope.row.username }}
        </template>
      </el-table-column>
      <el-table-column label="管理员姓名" align="center">
        <template slot-scope="scope">
          {{ scope.row.manageName }}
        </template>
      </el-table-column>
      <el-table-column label="管理员手机号 " align="center">
        <template slot-scope="scope">
          {{ scope.row.managePhone }}
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center">
        <template slot-scope="scope">
          {{ scope.row.createTime }}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="" label="操作区" width="200">
        <template slot-scope="scope">
          <el-button
            type="danger"
            size="small"
            @click="deleteFun(scope.row.adminId)"
            >删除</el-button
          >
          <el-button
            type="primary"
            size="small"
            @click="create('update', scope.row)"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!--分页栏-->
    <pagination
      :total="total"
      :page.sync="page.pageNum"
      :limit.sync="page.pageSize"
      @pagination="fetchData"
    />
    <!--对话框-->
    <el-dialog
      :title="dialogTitle[dialogType]"
      :visible.sync="dialogVisible"
      width="900px"
      :close-on-click-modal="false"
    >
      <el-form
        :model="form"
        :rules="rules"
        ref="form"
        label-width="120px"
        label-position="left"
      >
        <div class="flex">
          <el-form-item
            label="账号名称"
            class="form-item-style"
            prop="username"
          >
            <el-input
              :disabled="dialogType === 'update'"
              placeholder="请输入账号名称"
              v-model.trim="form.username"
            />
          </el-form-item>
          <el-form-item
            label="账号密码"
            class="form-item-style"
            prop="password"
          >
            <el-input
              placeholder="请输入账号密码"
              v-model.trim="form.password"
              show-password
            />
          </el-form-item>
        </div>
        <div class="flex">
          <el-form-item
            label="管理员姓名"
            class="form-item-style"
            prop="manageName"
          >
            <el-input
              maxlength="20"
              show-word-limit
              placeholder="请输入管理员姓名"
              v-model.trim="form.manageName"
            />
          </el-form-item>
          <el-form-item
            label="管理员手机号"
            class="form-item-style"
            prop="managePhone"
          >
            <el-input
              maxlength="11"
              show-word-limit
              placeholder="请输入管理员手机号"
              v-model.trim="form.managePhone"
            />
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false"> 取消 </el-button>
        <el-button type="primary" @click="addOrUpdate"> 确定 </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from "@/components/Pagination";
import { adminDelete, adminEdit, getPageList } from "@/api/user_manage";
export default {
  components: { Pagination },
  filters: {},
  computed: {},
  data() {
    return {
      // 主表格&主表格加载状态
      list: [],
      listLoading: true,
      // 分页参数
      page: {
        data: {
          manageName: null,
          managePhone: null,
        },
        pageNum: 1,
        pageSize: 10,
      },
      total: 0,
      // 是否显示对话框
      dialogVisible: false,
      // 对话框标题
      dialogTitle: {
        add: "添加账号",
        update: "编辑账号",
      },
      dialogType: "add",
      form_copy: null,
      // 添加角色参数
      form: {
        username: null,
        password: null,
        manageName: null,
        managePhone: null,
      },
      // 验证规则
      rules: {
        username: [{ required: true, message: "请输入", trigger: "change" }],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            pattern:
              /^(((?=.*\d)(?=.*[a-z])(?=.*[A-Z]))|((?=.*\d)(?=.*[a-z])(?=.*[~!@#$%^&*]))|((?=.*\d)(?=.*[A-Z])(?=.*[~!@#$%^&*]))|((?=.*[a-z])(?=.*[A-Z])(?=.*[~!@#$%^&*]))|((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[~!@#$%^&*]))).{10,32}$/,
            message:
              "必须大小写字母、数字、特殊字符,4种组合中至少满足3种，且密码长度在10-32位之间",
          },
        ],
        manageName: [{ required: true, message: "请输入", trigger: "change" }],
        managePhone: [{ required: true, message: "请输入", trigger: "change" }],
      },
      account_list: [],
    };
  },
  created() {
    this.fetchData();
    this.form_copy = JSON.parse(JSON.stringify(this.form));
  },
  watch: {},
  methods: {
    // 获取列表数据
    fetchData() {
      this.listLoading = true;
      getPageList(this.page).then((res) => {
        this.list = res.data.records;
        this.total = res.data.total;
        this.listLoading = false;
      });
    },
    // 打开添加角色对话框
    create(type, row) {
      if (type === "add") {
        this.form = JSON.parse(JSON.stringify(this.form_copy));
      } else {
        this.form = Object.assign(this.form, row);
        this.form.password = "";
      }
      this.dialogType = type;
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs["form"].clearValidate();
      });
    },
    // 确定添加/编辑
    addOrUpdate() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          adminEdit(this.form).then((res) => {
            this.dialogVisible = false;
            this.fetchData();
            this.$message({
              type: "success",
              message: "操作成功!",
            });
          });
        }
      });
    },
    // 删除角色
    deleteFun(id) {
      this.$confirm("确定要删除吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true,
      }).then(() => {
        adminDelete(id).then((res) => {
          this.fetchData();
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        });
      });
    },
    // 返回列表序号
    indexMethod(index) {
      return (this.page.pageNum - 1) * this.page.pageSize + index + 1;
    },
  },
};
</script>
<style scoped lang="scss">
</style>
