<template>
  <div>
    <back-box>
      <template slot="title">
        <span>账号管理</span>
      </template>
    </back-box>
    <div class="answer-header">
      <div class="sel-box">
        <el-select
          class="sel"
          v-model="query.id"
          placeholder="选择账号角色"
          @change="(page = 1),_QueryData(1)"
        >
          <el-option
            v-for="item in roleList"
            :key="item.id"
            :label="item.roleName"
            :value="item.id"
          >
          </el-option>
        </el-select>
        <el-select
          class="sel"
          v-model="query.available"
          placeholder="选择账号状态"
          @change="(page = 1),_QueryData(1)"
        >
          <el-option label="可用" :value="0"></el-option>
          <el-option label="不可用" :value="1"></el-option>
        </el-select>
        <el-input
          type="number"
          placeholder="请输入老师账号"
          v-model="query.loginName"
          class="input-with-select"
          @keyup.enter.native="(page = 1),_QueryData(2)"
        >
          <el-button
            slot="append"
            icon="el-icon-search"
            @click="(page = 1),_QueryData(2)"
          ></el-button>
        </el-input>
        <el-input
          placeholder="请输入老师姓名"
          v-model="query.realName"
          class="input-with-select"
          @keyup.enter.native="(page = 1),_QueryData(3)"
        >
          <el-button
            slot="append"
            icon="el-icon-search"
            @click="(page = 1),_QueryData(3)"
          ></el-button>
        </el-input>
      </div>
    </div>
    <div class="table-box">
      <el-table
        :data="tableData"
        border
        style="width: 100%"
        v-loading="loading"
      >
        <el-table-column
          prop="loginName"
          label="账号"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="realName"
          label="姓名"
          align="center"
        ></el-table-column>
        <el-table-column prop="roleList" label="角色" align="center">
          <template slot-scope="scoped">
            <span v-for="(item, index) in scoped.row.roleList" :key="item.id">{{
              `${item.roleName}${
                index != scoped.row.roleList.length - 1 ? "、" : ""
              }`
            }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="available" label="状态" align="center"
          ><template slot-scope="scoped">{{
            `${scoped.row.available == 0 ? "可用" : "不可用"}`
          }}</template></el-table-column
        >
        <el-table-column
          prop="createdTime"
          label="创建时间"
          align="center"
        ></el-table-column>
        <el-table-column label="操作" align="center" width="160">
          <template slot-scope="scoped">
            <el-tooltip content="修改账号" placement="left">
              <el-button
                class="set-count"
                type="primary"
                circle
                icon="el-icon-edit-outline"
                size="mini"
                @click="setCount(scoped.row)"
              ></el-button>
            </el-tooltip>
            <el-dropdown
              trigger="hover"
              @command="handleDropdownClick($event, scoped.row)"
            >
              <el-button
                type="info"
                size="mini"
                circle
                icon="el-icon-more"
              ></el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item :command="0">重置密码</el-dropdown-item>
                <el-dropdown-item :command="1">禁用</el-dropdown-item>
                <el-dropdown-item :command="2">删除</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-box">
        <el-pagination
          small=""
          layout="total,prev, pager, next"
          :hide-on-single-page="true"
          :total="total"
          @current-change="changePage"
          :current-page="page"
          :page-size="size"
        >
        </el-pagination>
      </div>
      <el-empty
        v-if="!loading && tableData.length == 0"
        content="没有更多数据"
        :image-size="100"
      ></el-empty>
    </div>
    <el-dialog title="修改年级信息" :visible.sync="diaCount" width="400">
      <el-form
        class="form-box"
        :model="formCount"
        :rules="ruleCount"
        ref="formCount"
        label-width="160px"
      >
        <el-form-item label="手机号码：" prop="loginName">
          <el-col :span="12">
            <el-input
              placeholder="请输入联系电话"
              v-model.trim="formCount.loginName"
              maxlength="11"
            >
            </el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="姓名：" prop="realName">
          <el-col :span="12">
            <el-input
              placeholder="请输入教师姓名"
              v-model.trim="formCount.realName"
              maxlength="11"
            >
            </el-input>
          </el-col>
        </el-form-item>
      </el-form>
      <div class="dialog-footer" slot="footer">
        <el-button @click="saveCount">确 定</el-button>
        <el-button @click="diaCount = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loading: false,
      diaCount: false,
      formCount: {
        userId: "",
        loginName: "",
        realName: "",
      },
      ruleCount: {
        loginName: [
          { required: true, message: "请输入联系电话", trigger: "blur" },
        ],
        realName: [
          { required: true, message: "请输入教师姓名", trigger: "blur" },
        ],
      },
      roleList: [],
      query: {
        roleId: "",
        available: "",
        loginName: "",
        realName: "",
      },
      tableData: [],
      total: 0,
      page: 1,
      size: 20,
    };
  },
  created() {
    this._QueryData(4);
    this._RoleList();
  },
  methods: {
    setCount(obj) {
      this.formCount.userId = obj.userId;
      this.formCount.loginName = obj.loginName;
      this.formCount.realName = obj.realName;
      this.diaCount = true;
    },
    handleDropdownClick(value, item) {
      //更多
      this.updateUser(item, value);
    },
    changePage(page) {
      this.page = page;
      this._QueryData(4);
    },

    saveCount() {
      this.$refs.formCount.validate(async (valid) => {
        if (valid) {
          const { data, status, info } = await this.$request.updateUser({
            type: 3,
            ...this.formCount,
          });
          if (status === 0) {
            this.$message.success(info);
            this._QueryData(4);
          } else {
            this.$message.error(info);
          }
        } else {
          console.log("输入有误请检查！");
          return false;
        }
      });
    },
    async updateUser(obj, type) {
      let query = {
        userId: obj.userId,
        type: type,
      };
      if (type == 1) {
        query.available = obj.available == 0 ? 1 : 0;
      }
      const { data, status, info } = await this.$request.updateUser({
        ...query,
      });
      if (status === 0) {
        this.$message.success(info);
        this._QueryData(4);
      } else {
        this.$message.error(info);
      }
    },
    async _RoleList() {
      const { data, status, info } = await this.$request.roleList();
      if (status === 0) {
        this.roleList = data.list || [];
      } else {
        this.$message.error(info);
      }
    },
    async _QueryData(type) {
      let query = {};
      if (type == 1) {
        query.roleId = this.query.roleId;
        query.available = this.query.available;
        this.query.loginName = "";
        this.query.realName = "";
      } else if (type == 2) {
        this.query.roleId = "";
        this.query.available = "";
        query.loginName = this.query.loginName;
        this.query.realName = "";
      } else if (type == 3) {
        this.query.roleId = "";
        this.query.available = "";
        this.query.loginName = "";
        query.realName = this.query.realName;
      } else {
        query = { ...this.query };
      }
      this.loading = true;
      this.tableData = [];
      const { data, status, info } = await this.$request.userPage({
        ...query,
        page: this.page,
        size: this.size,
      });
      this.loading = false;
      if (status === 0) {
        this.tableData = data.list || [];
        this.total = data.count;
      } else {
        this.$message.error(info);
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.set-count {
  margin-right: 12px;
}
.table-box {
  padding: 0 20px;
}
</style>