<template>
  <div>
    <list-layout>
      <!-- 搜索区域 -->
      <template #search>
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="微信ID" prop="wxid">
            <el-input v-model="queryParams.wxid" placeholder="请输入微信ID" clearable style="width: 240px" @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="姓名" prop="name">
            <el-input v-model="queryParams.name" placeholder="请输入姓名" clearable style="width: 240px" @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="联盟ID" prop="unionId">
            <el-input v-model="queryParams.unionId" placeholder="请输入联盟ID" clearable style="width: 240px" @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="激活状态" prop="isActive">
            <el-select v-model="queryParams.isActive" placeholder="激活状态" clearable style="width: 240px">
              <el-option label="已激活" value="1" />
              <el-option label="未激活" value="0" />
            </el-select>
          </el-form-item>
          <el-form-item label="参与统计" prop="isCount">
            <el-select v-model="queryParams.isCount" placeholder="参与统计" clearable style="width: 240px">
              <el-option label="参与统计" value="1" />
              <el-option label="不参与统计" value="0" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['system:superadmin:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['system:superadmin:edit']">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['system:superadmin:remove']">删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['system:superadmin:export']">导出</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <!-- 表格区域 -->
      <template #table>
        <el-table v-loading="loading" :data="superadminList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="微信ID" align="center" prop="wxid" min-width="120" />
          <el-table-column label="姓名" align="center" prop="name" min-width="100" />
          <el-table-column label="联盟ID" align="center" prop="unionId" min-width="120" />
          <el-table-column label="应用密钥" align="center" prop="appKey" min-width="200" :show-overflow-tooltip="true" />
          <el-table-column label="秘密密钥" align="center" prop="secretKey" min-width="200" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <span>{{ scope.row.secretKey ? '***' + scope.row.secretKey.substring(scope.row.secretKey.length - 4) : '-' }}</span>
            </template>
          </el-table-column>
          <el-table-column label="激活状态" align="center" prop="isActive" min-width="100">
            <template slot-scope="scope">
              <el-tag :type="scope.row.isActive === 1 ? 'success' : 'info'">
                {{ scope.row.isActive === 1 ? '已激活' : '未激活' }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column label="参与统计" align="center" prop="isCount" min-width="100">
            <template slot-scope="scope">
              <el-tag :type="scope.row.isCount === 1 ? 'success' : 'info'">
                {{ scope.row.isCount === 1 ? '参与统计' : '不参与统计' }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" align="center" prop="createdAt" min-width="160">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createdAt) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="更新时间" align="center" prop="updatedAt" min-width="160">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.updatedAt) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" min-width="180" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)">查看</el-button>
              <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:superadmin:edit']">修改</el-button>
              <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['system:superadmin:remove']">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>

      <!-- 分页区域 -->
      <template #pagination>
        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
      </template>
    </list-layout>

    <!-- 添加或修改超级管理员对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="微信ID" prop="wxid">
          <el-input v-model="form.wxid" placeholder="请输入微信ID" />
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="联盟ID" prop="unionId">
          <el-input v-model="form.unionId" placeholder="请输入联盟ID" />
        </el-form-item>
        <el-form-item label="应用密钥" prop="appKey">
          <el-input v-model="form.appKey" placeholder="请输入应用密钥" />
        </el-form-item>
        <el-form-item label="秘密密钥" prop="secretKey">
          <el-input v-model="form.secretKey" placeholder="请输入秘密密钥" show-password />
        </el-form-item>
        <el-form-item label="激活状态" prop="isActive">
          <el-radio-group v-model="form.isActive">
            <el-radio :label="1">激活</el-radio>
            <el-radio :label="0">未激活</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="参与统计" prop="isCount">
          <el-radio-group v-model="form.isCount">
            <el-radio :label="1">参与统计</el-radio>
            <el-radio :label="0">不参与统计</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看超级管理员详情对话框 -->
    <el-dialog :title="'超级管理员详情 - ' + currentAdmin.name" :visible.sync="viewDialogVisible" width="600px" append-to-body>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="ID">{{ currentAdmin.id }}</el-descriptions-item>
        <el-descriptions-item label="微信ID">{{ currentAdmin.wxid }}</el-descriptions-item>
        <el-descriptions-item label="姓名">{{ currentAdmin.name }}</el-descriptions-item>
        <el-descriptions-item label="联盟ID">{{ currentAdmin.unionId }}</el-descriptions-item>
        <el-descriptions-item label="应用密钥" :span="2">{{ currentAdmin.appKey }}</el-descriptions-item>
        <el-descriptions-item label="秘密密钥" :span="2">{{ currentAdmin.secretKey ? '***' + currentAdmin.secretKey.substring(currentAdmin.secretKey.length - 4) : '-' }}</el-descriptions-item>
        <el-descriptions-item label="激活状态">
          <el-tag :type="currentAdmin.isActive === 1 ? 'success' : 'info'">
            {{ currentAdmin.isActive === 1 ? '已激活' : '未激活' }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="参与统计">
          <el-tag :type="currentAdmin.isCount === 1 ? 'success' : 'info'">
            {{ currentAdmin.isCount === 1 ? '参与统计' : '不参与统计' }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ parseTime(currentAdmin.createdAt) }}</el-descriptions-item>
        <el-descriptions-item label="更新时间">{{ parseTime(currentAdmin.updatedAt) }}</el-descriptions-item>
      </el-descriptions>
      <div slot="footer" class="dialog-footer">
        <el-button @click="viewDialogVisible = false">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSuperadmin, getSuperadmin, delSuperadmin, addSuperadmin, updateSuperadmin } from "@/api/system/superadmin";
import ListLayout from "@/components/ListLayout";

export default {
  name: "Superadmin",
  components: {
    ListLayout
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 超级管理员表格数据
      superadminList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        wxid: null,
        name: null,
        unionId: null,
        isActive: null,
        isCount: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        wxid: [
          { required: true, message: "微信ID不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
      },
      // 查看详情对话框
      viewDialogVisible: false,
      // 当前查看的管理员
      currentAdmin: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询超级管理员列表 */
    getList() {
      this.loading = true;
      listSuperadmin(this.queryParams).then(response => {
        this.superadminList = response.rows;
        this.total = response.total;
        this.loading = false;
      }).catch(error => {
        console.error('获取超级管理员列表失败:', error);
        this.loading = false;
        this.$message.error('获取超级管理员列表失败');
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        wxid: null,
        name: null,
        unionId: null,
        appKey: null,
        secretKey: null,
        isActive: 1,
        isCount: 1
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 查看超级管理员详情 */
    handleView(row) {
      this.currentAdmin = row;
      this.viewDialogVisible = true;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加超级管理员";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSuperadmin(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改超级管理员";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSuperadmin(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSuperadmin(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除超级管理员编号为"' + ids + '"的数据项？').then(function() {
        return delSuperadmin(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('jarvis/superadmin/export', {
        ...this.queryParams
      }, `superadmin_${new Date().getTime()}.xlsx`)
    }
  }
};
</script> 