<template>
  <div class="page_container">
    <bg-breadcrumb></bg-breadcrumb>
    <div class="main_container">
      <bg-filter-group
        @search="changeSearch"
        v-model="filter.search"
        placeholder="请输入账号、手机号和所属机构">
        <template v-slot:left_action>
          <div class="apaas_button">
            <el-button type="primary" @click="exportFile">
              <bg-icon
                style="font-size: 12px; color: #fff; margin-right: 8px"
                icon="#bg-ic-file-send"></bg-icon>
              导出
            </el-button>
            <el-popconfirm
              class="force-modify-btn"
              width="220"
              icon-color="#ff9900"
              title="确认修改密码吗？"
              @confirm="forceModifyPwd">
              <template #reference>
                <el-button slot="reference" type="primary">
                  <span>强制修改密码</span>
                </el-button>
              </template>
            </el-popconfirm>
            <span class="sleceted_tip"
              >已选<span class="num">{{ selectedNum }}</span
              >项</span
            >
            <span class="clean" @click="cleanSelected">清空</span>
          </div>
        </template>

        <template v-slot:filter_group>
          <div class="left-filter filter_list">
            <div class="filter_item">
              <span class="filter_title">用户类型</span>
              <el-select v-model="filter.is_admin" placeholder="请选择" style="width: 300px">
                <el-option
                  v-for="(item, index) in userTypeList"
                  :key="index"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </div>
            <div class="filter_item">
              <span class="filter_title">上次访问时间</span>
              <el-date-picker
                v-model="filter.time"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="YYYY-MM-DD"
                style="width: 300px" />
            </div>
            <div class="filter_item">
              <span class="filter_title">活跃度</span>
              <el-select v-model="filter.active" placeholder="请选择" style="width: 300px">
                <el-option
                  v-for="(item, index) in activeOptions"
                  :key="index"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </div>
            <div class="filter_item">
              <span class="filter_title">密码强度</span>
              <el-select v-model="filter.pwd_level" placeholder="请选择" style="width: 300px">
                <el-option
                  v-for="(item, index) in pwdLevelList"
                  :key="index"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </div>
          </div>
          <div class="right-action apaas_button">
            <el-button type="primary" @click="filterAction"> 查询 </el-button>
            <el-button type="default" @click="filterClear"> 重置 </el-button>
          </div>
        </template>
      </bg-filter-group>
      <div class="table_container">
        <bg-table
          ref="accountTable"
          :headers="headers"
          :rows="tableRows"
          :stripe="true"
          select
          @selectAc="selectChange">
        </bg-table>
        <bg-pagination
          :page="filter.page"
          :size="filter.limit"
          :total="filter.total"
          @change-page="changePage"
          @change-size="changeSize">
        </bg-pagination>
      </div>
    </div>
  </div>
</template>

<script setup>
import { reactive, toRefs, ref, computed, onBeforeMount } from "vue";
import { useStore } from "vuex";
import bgBreadcrumb from "@/components/bg-breadcrumb.vue";
import axios from "@/request/http.js";
import { ElMessage } from "element-plus";
import { downloadBlob } from "@/services/helper.js";

const { getters } = useStore();

const userTypeList = computed(() => {
  const temp = getters.getUserTypeConfig || {};
  return Object.keys(temp).map((key) => ({
    label: temp[key],
    value: +key,
  }));
});
const accountTable = ref(null);
const state = reactive({
  filter: {
    search: "",
    is_admin: "",
    time: [],
    active: 0,
    pwd_level: "",
    page: 1,
    limit: 10,
    total: 0,
  },
  activeOptions: [
    { label: "全部", value: "" },
    { label: "低", value: 1 },
    { label: "中", value: 2 },
    { label: "高", value: 3 },
  ],
  pwdLevelList: [
    { label: "全部", value: "" },
    { label: "弱", value: 1 },
    { label: "中", value: 2 },
    { label: "强", value: 3 },
  ],

  headers: [
    {
      label: "账号",
      prop: "system_account",
      // minWidth: 280,
    },
    {
      label: "手机号",
      prop: "contact_phone",
      // minWidth: 360,
    },
    {
      label: "用户类型",
      prop: "is_admin",
      // width: 200,
    },
    {
      label: "所属机构",
      prop: "name",
      // width: 120,
    },
    {
      label: "活跃度",
      prop: "active",
      // width: 120,
    },
    {
      label: "密码强度",
      prop: "pwd_level",
      width: 240,
    },
    {
      label: "密码使用时长（天）",
      prop: "pwd_is_used",
      width: 240,
    },
    {
      label: "创建时间",
      prop: "created_time",
      width: 240,
    },
  ],

  tableRows: [],
  //选中数量
  selectedNum: 0,
  actionRow: {},
  dialogDetail: false,
  cacheDialog: false,
});

const { filter, activeOptions, pwdLevelList, headers, tableRows, selectedNum } = toRefs(state);
//选择项发生变化
const selectChange = (params) => {
  state.selectedNum = params.allLength;
};
//导出
const exportFile = () => {
  let params = { ...state.filter };
  params.start_at = params.time ? params.time[0] || "" : "";
  params.end_at = params.time ? params.time[1] || "" : "";
  delete params.time;
  axios
    .get(`/apaas/system/v5/log/userAccountAudit/list/export`, {
      params,
    })
    .then((res) => {
      if (res.status === 200) {
        downloadBlob(res.data);
      } else {
        ElMessage.error(res.statusText);
      }
    });
};

//强制修改密码
const forceModifyPwd = () => {
  const selectedObj = accountTable.value.dealSelectData();
  const selectedRows = Object.values(selectedObj);
  if (!selectedRows.length) return ElMessage.warning("请先选择修改项");
  const ids = selectedRows.map((item) => item.id);
  axios.post("/apaas/system/v5/user/resetpwd", { ids }).then((res) => {
    if (res.data.code === 200) {
      ElMessage.success(res.data.msg);
      cleanSelected();
      getTableRows();
    } else {
      ElMessage.error(res.data.data);
    }
  });
};
//清空
const cleanSelected = () => {
  accountTable.value.clearTable();
};
const changeSearch = (val) => {
  state.filter.search = val;
  changePage(1);
};
const changePage = (page) => {
  state.filter.page = page;
  getTableRows();
};
const getTableRows = () => {
  let params = { ...state.filter };
  params.start_at = params.time ? params.time[0] || "" : "";
  params.end_at = params.time ? params.time[1] || "" : "";
  delete params.time;
  axios
    .get(`/apaas/system/v5/log/userAccountAudit/list`, {
      params,
    })
    .then((res) => {
      if (res.data.code == 200) {
        state.tableRows = res.data.data || [];
        state.filter.total = res.data.total;
      } else {
        ElMessage.error(res.data.data);
      }
    });
};
const filterAction = () => {
  changePage(1);
};
const changeSize = (size) => {
  state.filter.limit = size;
  changePage(1);
};
const filterClear = () => {
  state.filter = {
    search: "",
    is_admin: "",
    time: [],
    active: 0,
    pwd_level: "",
    page: 1,
    limit: 10,
    total: 0,
  };
  changePage(1);
};

onBeforeMount(() => {
  getTableRows();
});
</script>

<style lang="scss" scoped>
.page_container {
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  align-items: stretch;
  width: 100%;
  padding: 0 24px;
  min-height: 100%;
  .main_container {
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    align-items: stretch;
    flex-grow: 1;
    width: 100%;
    background-color: #fff;
    box-shadow: 0px 1px 4px 0px rgba(0, 7, 101, 0.15);
    border-radius: 6px;
    padding: 0 0 16px 0;
    margin-bottom: 16px;
    position: relative;
    .sleceted_tip {
      margin: 0 24px 0 40px;
      vertical-align: middle;
      font-size: 14px;
      color: #404a62;
      .num {
        margin: 0 3px 0 3px;
        font-weight: 600;
        color: #202531;
      }
    }
    .clean {
      vertical-align: middle;
      font-size: 14px;
      color: #3759be;
      cursor: pointer;
    }
    .filter-group {
      .left-filter {
        flex: 1;
        display: flex;
        justify-content: start;
        flex-wrap: wrap;
        .filter_item {
          display: flex;
          align-items: center;
          .filter_title {
            width: 84px;
            white-space: pre-wrap;
            font-weight: normal;
          }
        }
      }
      .right-action {
        width: 144px;
        padding-bottom: 16px;
        .el-button {
          width: 64px;
        }
      }
    }
    .table_container {
      padding: 0 16px;
      .el-table {
        flex: 1;
        .circle {
          display: inline-block;
          width: 6px;
          height: 6px;
          border-radius: 3px;
          margin-right: 8px;
          transform: translateY(-2px);
        }
        .bgc_1 {
          background-color: #48ad97;
        }
        .bgc_2 {
          background-color: #d75138;
        }
        .bgc_3 {
          background-color: #3759be;
        }
        .bgc_4 {
          background-color: #ea7d19;
        }
        .bgc_0 {
          background-color: #9e9e9e;
        }
        .mock_tip {
          display: inline-block;
          margin-left: 4px;
          width: 43px;
          height: 20px;
          font-size: 12px;
          color: #2b4695;
          text-align: center;
          background-color: #eff2fa;
          border-radius: 3px;
          border: solid 1px #b0bee8;
        }
        :deep() td {
          padding: 9px 0 !important;
        }
        :deep() .el-switch {
          height: 20px;
          width: 44px;
          .el-switch__core {
            height: 20px;
            min-width: 44px;
          }
        }
      }
    }
  }

  :deep() .dialog_box_detail {
    .el-dialog__body {
      padding: 0 0 18px 0;
      height: 580px;
    }

    .form_filter {
      display: flex;
      justify-content: space-between;
      align-items: center;
      height: 69px;
      border-bottom: 1px solid #e6e9ef;
      .right {
        margin-right: 16px;
        .el-button {
          width: 64px;
          &:last-child {
            margin-left: 16px;
          }
        }
      }
    }
    .table_content {
      padding: 0 16px;
    }
  }
}
</style>
