<template>
  <!-- 平台用户管理 -->
  <div class="user-wrapper height100">
    <div class="search-container">
      <el-form :inline="true" :model="form">
        <el-form-item>
          <el-input
            clearable
            v-model="form.userName"
            placeholder="请输入平台用户名"
            suffix-icon="el-icon-search"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <div class="btn-group">
            <el-button type="primary" @click="handleSubmit">查询</el-button>
            <el-button @click="handleReset">重置</el-button>
          </div>
        </el-form-item>
      </el-form>
      <div class="page-tip">
        <span class="page-tip-title">页面说明:</span>
        <span class="page-tips"
          >可根据用户名称搜索对用户信息进行筛选。可以新增账号，“*”为必填项。可以对账号信息进行修改及重置密码</span
        >
      </div>
    </div>
    <div class="table-content">
      <div class="btn-group">
        <el-button type="primary" @click="handleAdd()">新建账户</el-button>
      </div>
      <account-table
        :feildList="feildList"
        :list="list"
        :rolesList="rolesList"
        @action="handleAction"
        :currentPage="page._index"
      />
      <party-pagination :page="page" @changePage="handleChangeCurrent" />
    </div>
    <add-dialog
      ref="addDialog"
      :organList="organList"
      :rolesList="rolesList"
      @success="getFirstPageList()"
    />
    <edit-dialog
      ref="editDialog"
      :organList="organList"
      :rolesList="rolesList"
      @success="getFirstPageList()"
    />
    <msg-dialog ref="msgDilaog" :msgInfo="msgInfo" />
  </div>
</template>
<script>
import { partyPagination } from "@/components/index";
import accountTable from "./components/accountTable";
import { addDialog, editDialog } from "./userDialog/index";
import msgDialog from "./msgDialog.vue";
import { getOrgListWithOutPage } from "@/config/organ";
import { getRoles } from "@/config/roles";
export default {
  data() {
    return {
      form: {
        userName: "",
        type: 1, //1.用户账号 2.平台单位单位管理员账号 3.机顶盒账号 4.运维账号 5. 统计账号
      },
      feildList: [
        { prop: "userName", label: "平台用户名" },
        { prop: "orgName", label: "所在机构" },
        { prop: "exiredDate", label: "到期时间" },
        { prop: "roleList", label: "账号类型" },
        { prop: "statusName", label: "账号状态" },
        { prop: "", label: "操作", isEdit: true, width: 280 },
      ],
      list: [],
      page: {
        _index: 1,
        _size: 10,
        total: 0,
      },
      activeRow: {},
      msgInfo: {},
      organList: [],
      rolesList: [],
    };
  },
  components: {
    partyPagination,
    accountTable,
    addDialog,
    editDialog,
    msgDialog,
  },
  mounted() {
    this.getOrgList();
    this.getRolesList();
    this.getFirstPageList();
  },
  methods: {
    // 获取机构列表
    getOrgList() {
      getOrgListWithOutPage().then((res) => {
        this.organList = res;
      });
    },
    // 获取角色列表
    getRolesList() {
      getRoles().then((res) => {
        this.rolesList = res;
      });
    },
    getFirstPageList() {
      this.page._index = 1;
      this.getPageList();
    },
    handleSubmit() {
       this.getFirstPageList();
    },
    handleReset() {
      this.form.userName = "";
      this.getFirstPageList();
    },
    getPageList() {
      let requestParams = {};
      requestParams._index = this.page._index;
      requestParams._size = this.page._size;
      requestParams.type = this.form.type;
      if (this.form.userName) {
        requestParams.userName = this.form.userName;
      }
      this.$https(
        {
          method: "get",
          url: "tUser/getPageList",
          authType: this.backToken,
        },
        requestParams
      )
        .then((res) => {
          if (res.status != 200) {
            this.getResWithOutData();
          } else {
            if (res.data.resultCode == 200) {
              this.list = res.data.data.records;
              this.page._size = res.data.data.size;
              this.page.total = res.data.data.total;
            } else {
              this.getResWithOutData();
            }
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    // 页面返回值为空
    getResWithOutData() {
      this.list = [];
      this.page = {
        _index: 1,
        _size: 10,
        total: 0,
      };
    },
    // 新增账号
    handleAdd() {
      this.$refs.addDialog.dialogVisible = true;
    },
    // 编辑
    handleEdit() {
      this.$refs.editDialog.id = this.activeRow.id;
      this.$refs.editDialog.getDetailById();
      this.$refs.editDialog.dialogVisible = true;
    },
    // 重置密码
    handleResetPwd() {
      let _this = this;
      this.$https({
        method: "put",
        url: `tUser/resetPassword?userId=${this.activeRow.id}`,
        authType: this.backToken,
      })
        .then((res) => {
          if (res.status != 200) {
            _this.$message.error(res.data.message);
          } else {
            if (res.data.resultCode == 200) {
              _this.msgInfo = {
                type: "success",
                des: `用户${this.activeRow.userName}密码已重置！`,
              };
              _this.$refs.msgDilaog.dialogVisible = true;
            } else {
              _this.$message.error(res.data.message);
            }
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    // 禁用
    handleDisable() {
      let _this = this;
      this.$https({
        method: "put",
        url: `tUser/disable?userId=${this.activeRow.id}`,
        authType: this.backToken,
      })
        .then((res) => {
          if (res.status != 200) {
            _this.$message.error(res.data.message);
          } else {
            if (res.data.resultCode == 200) {
              _this.msgInfo = {
                type: "wait",
                des: `${this.activeRow.userName}账号禁用申请已提交，待审核…`,
              };
              _this.$refs.msgDilaog.dialogVisible = true;
              _this.getPageList();
            } else {
              _this.$message.error(res.data.message);
            }
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    // 激活
    handleActive() {
      let _this = this;
      this.$https({
        method: "put",
        url: `tUser/enable?userId=${this.activeRow.id}`,
        authType: this.backToken,
      })
        .then((res) => {
          if (res.status != 200) {
            _this.$message.error(res.data.message);
          } else {
            if (res.data.resultCode == 200) {
              _this.msgInfo = {
                type: "success",
                des: `用户${this.activeRow.userName}账号激活成功！`,
              };
              _this.$refs.msgDilaog.dialogVisible = true;
              _this.getPageList();
            } else {
              this.$message.error(res.data.message);
            }
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    handleAction(params) {
      this.activeRow = params.row;
      switch (params.type) {
        case "enable":
          this.handleActive();
          break;
        case "disable":
          this.handleDisable();
          break;
        case "reset":
          this.handleResetPwd();
          break;
        case "edit":
          this.handleEdit();
          break;
        default:
          break;
      }
    },
    // 翻页
    handleChangeCurrent(val) {
      this.page._index = val;
      this.getPageList();
    },
  },
};
</script>
<style lang="less" scoped>
</style>