<template>
  <div class="app-container">
          <el-button type="primary" @click="handleAddUser" icon="el-icon-plus">新增</el-button>
        <el-table
          :data="userList"
          style="width: 100%;margin-top:10px;"
          border
          fit
          v-loading="listLoading"
          highlight-current-row
          max-height="600"
        >
          <el-table-column type="index" width="50"></el-table-column>
          <el-table-column align="header-center" label="账户">
            <template slot-scope="scope">{{ scope.row.username }}</template>
          </el-table-column>
          <el-table-column label="创建日期">
            <template slot-scope="scope">
              <span>{{ scope.row.date_joined }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作">
            <template slot-scope="scope">
              <el-button
                type="danger"
                size="small"
                @click="handleDelete(scope)"
                icon="el-icon-delete"
                :disabled="!checkPermission(['user_delete'])"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="listQuery.page"
          :limit.sync="listQuery.limit"
          @pagination="getList"
        />

    <el-dialog :visible.sync="dialogVisible" title="新增管理员">
      <el-form
        :model="user"
        label-width="80px"
        label-position="right"
        :rules="rule1"
        ref="userForm"
      >
        <el-form-item label="账户" prop="username">
          <el-input v-model="user.username" placeholder="账户" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="user.password" placeholder="密码" />
        </el-form-item>
      </el-form>
      <div style="text-align:right;">
        <el-button type="danger" @click="dialogVisible=false">取消</el-button>
        <el-button type="primary" @click="confirmUser('userForm')">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getUserList, createUser, deleteUser} from "@/api/user";
import checkPermission from "@/utils/permission";
import Pagination from "@/components/Pagination"; // secondary package based on el-pagination

const defaultUser = {
  id: "",
  username: "",
  password: "",
  is_superuser:true
};
export default {
  components: { Pagination },
  data() {
    return {
      user: defaultUser,
      userList: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20
      },
      dialogVisible: false,
      rule1: {
        username: [{ required: true, message: "请输入账号", trigger: "change" }],
        password: [
          { required: true, message: "请输入密码", trigger: "change" }
        ],
      },
      filterOrgText: "",
      treeLoding: false,
      orgData: []
    };
  },
  computed: {},
  created() {
    this.getList();
  },
  methods: {
    checkPermission,
    getList() {
      this.listLoading = true;
      getUserList(this.listQuery).then(response => {
        this.userList = response.data.results;
        this.total = response.data.count;
        this.listLoading = false;
      });
    },
    handleAddUser() {
      this.user = Object.assign({}, defaultUser);
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs["userForm"].clearValidate();
      });
    },
    handleDelete(scope) {
      this.$confirm("确认删除?", "警告", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "error"
      })
        .then(async () => {
          await deleteUser(scope.row.id);
          this.userList.splice(scope.row.index, 1);
          this.$message({
            type: "success",
            message: "成功删除!"
          });
        })
        .catch(err => {
          console.error(err);
        });
    },
    async confirmUser(form) {
      this.$refs[form].validate(valid => {
        if (valid) {
            createUser(this.user).then(res => {
              // this.user = res.data
              // this.userList.unshift(this.user)
              this.getList();
              this.dialogVisible = false;
              this.$notify({
                title: "成功",
                message: "新增成功",
                type: "success",
                duration: 2000
              });
            });
        } else {
          return false;
        }
      });
    }
  }
};
</script>
