<template>
  <!-- 角色权限设置 -->
  <div class="role-management-page">
    <s-header :name="$t('role.rolePermissionSettings')" :noback="false"></s-header>

    <div class="management-container">
      <!-- 头部操作栏 -->
      <div class="action-header">
        <div class="total-count">
          <span class="count-text">{{ $t('role.total') }}</span>
          <van-tag type="primary" class="count-badge">{{ accountPerList.length }}</van-tag>
          <span class="count-text">{{ $t('role.inTotal') }}</span>
        </div>
        <van-button type="primary" icon="plus" class="add-btn" @click="pushAdd">
          {{ $t('role.addTo') }}
        </van-button>
      </div>

      <!-- 角色列表 -->
      <div class="role-list">
        <div v-for="(item, index) in accountPerList" :key="index" class="role-card">
          <div class="card-content">
            <!-- 角色信息 -->
            <div class="role-info">
              <h3 class="role-name">{{ item.roleName }}</h3>

              <!-- 权限分类展示 -->
              <div class="permission-section">
                <div class="permission-group">
                  <div class="group-header">
                    <van-icon name="todo-list" class="group-icon" />
                    <span>{{ $t('role.commonPermission') }}</span>
                  </div>
                  <div class="permission-tags">
                    <van-tag v-for="(role, rindex) in item.menuCodesJson" :key="'common-' + rindex" type="primary"
                      class="permission-tag">
                      {{ $t("permission." + role) }}
                    </van-tag>
                  </div>
                </div>

                <div class="permission-group" v-if="item.menuCodesJson.includes('M1')">
                  <div class="group-header">
                    <van-icon name="setting" class="group-icon warn-icon" />
                    <span>{{ $t('role.remotePermission') }}</span>
                  </div>
                  <div class="permission-tags">
                    <van-tag v-for="(role, rindex) in item.controlCodesJson" :key="'remote-' + rindex" type="warning"
                      class="permission-tag">
                      {{ $t("remote." + role) }}
                    </van-tag>
                  </div>
                </div>
              </div>
            </div>

            <!-- 操作按钮 -->
            <div class="action-buttons">
              <van-button icon="edit" size="small" class="edit-btn vertical-btn" @click="roleEdit(item)">
                {{ $t('role.edit') }}
              </van-button>
              <van-button icon="delete" size="small" type="danger" class="delete-btn vertical-btn"
                @click="confirmDelete(item)">
                {{ $t('role.delete') }}
              </van-button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { onMounted, reactive, toRefs, ref } from 'vue';
import sHeader from "../../components/SimpleHeader";
import { getSysRoleAdminList, deleteSysRole } from '../../service/role/index';
import { showConfirmDialog, showFailToast, showSuccessToast } from "vant";
import { getLoginUser } from "../../common/js/utils";
import { useRouter } from "vue-router";
import { useI18n } from "vue-i18n";

export default {
  name: "role",
  components: { sHeader },
  setup() {
    // 引入语言
    const { t } = useI18n();
    const user = getLoginUser();
    const router = useRouter();
    const searchRef = ref(null);
    const accountPerList = ref([]); // 列表集合
    let searchParams = reactive({ adminId: user.id });
    // 初始化页面获取列表
    onMounted(async () => {
      // 加载样式
      searchGetList();
    });
    // 搜索点击
    const searchClick = () => { searchRef.value.showSearch(); };
    // 跳转添加账号
    const pushAdd = () => { localStorage.removeItem('roleItem'); router.push('/roleSet'); }
    // 查询列表
    const searchGetList = () => { accountPerList.value = []; getList(); }
    // 搜索弹窗触发搜索
    const search = (data) => {
      searchParams = Object.assign(searchParams, data);
      searchGetList();
    };
    // 获取设备列表数据
    const getList = async () => {
      const { data } = await getSysRoleAdminList(searchParams);
      if (data.code === "00000") {
        accountPerList.value = accountPerList.value.concat(data.data.map(item => {
          return {
            ...item,
            menuCodesJson: JSON.parse(item.menuCodesJson),
            controlCodesJson: JSON.parse(item.controlCodesJson)
          }
        }));
        console.log(accountPerList.value);
      } else { showFailToast(data.message); }
    }
    const roleEdit = async (item) => { localStorage.setItem('roleItem', JSON.stringify(item)); router.push('/roleSet'); };

    const confirmDelete = (item) => {
      showConfirmDialog({
        title: t('user.tips'),
        message: t('role.deleteTip'),
      }).then(async () => {
        const { data } = await deleteSysRole({ adminId: user.id, roleId: item.roleId });
        if (data.code === '00000') {
          showSuccessToast(t('device.deletionSucceeded'));
          setTimeout(() => {
            searchGetList();
          }, 1000);
        } else { showFailToast(data.message); }
      }).catch((error) => {
        console.error(error);
      })
    }

    return {
      searchRef,
      searchClick,
      pushAdd,
      search,
      accountPerList,
      roleEdit,
      ...toRefs(searchParams),
      confirmDelete,
    };
  },
};
</script>

<style lang="less" scoped>
@import "../../common/style/common.less";

.role-management-page {
  background: #f8f9ff;
  min-height: 100vh;

  .management-container {
    padding: 16px;

    .action-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 20px;
      padding: 12px;
      background: #fff;
      border-radius: 8px;
      box-shadow: 0 2px 8px rgba(78, 107, 221, 0.08);

      .total-count {
        display: flex;
        align-items: center;

        .count-text {
          font-size: 14px;
          color: #666;
          margin-right: 8px;
        }

        .count-badge {
          margin-right: 4px;
        }
      }

      .add-btn {
        height: 36px;
        padding: 0 20px;
        border-radius: 18px;
      }
    }

    .role-list {
      .role-card {
        margin-bottom: 12px;
        border-radius: 8px;
        overflow: hidden;
        box-shadow: 0 2px 8px rgba(78, 107, 221, 0.08);

        .card-content {
          display: flex;
          justify-content: space-between;
          align-items: center;
          padding: 16px;
          background: #fff;
        }

        .role-info {
          flex: 1;

          .role-name {
            margin: 0 0 12px 0;
            font-size: 16px;
            color: #2c3e50;
          }

          .permission-section {
            .permission-group {
              margin-bottom: 12px;

              &:last-child {
                margin-bottom: 0;
              }

              .group-header {
                display: flex;
                align-items: center;
                margin-bottom: 8px;

                .group-icon {
                  font-size: 16px;
                  margin-right: 6px;
                  color: #4e6bdd;

                  &.warn-icon {
                    color: #ff5252;
                  }
                }

                span {
                  font-size: 13px;
                  color: #666;
                }
              }

              .permission-tags {
                display: flex;
                flex-wrap: wrap;
                gap: 6px;

                .permission-tag {
                  font-size: 12px;
                  padding: 4px 8px;
                  border-radius: 4px;
                }
              }
            }
          }
        }

        .action-buttons {
          display: flex;
          flex-direction: column;
          justify-content: center; // 垂直居中
          align-items: center; // 水平居中
          min-width: 100px;
          height: 100%; // 继承父容器高度
          margin-left: 16px;

          .vertical-btn {
            width: 80px; // 固定按钮宽度
            margin: 4px 0; // 垂直间距
            display: flex;
            align-items: center;
            justify-content: center;

            // 图标文字间距
            .van-button__icon+span {
              margin-left: 4px;
            }
          }

          .edit-btn {
            background: #f0f3ff;
            color: #4e6bdd;
            border: 1px solid #d0d5ff;
          }

          .delete-btn {
            background: #fff5f5;
            color: #ff5252;
            border: 1px solid #ffd0d0;
          }
        }
      }
    }
  }
}
</style>
