<template>
  <div class="application scroll-y">
    <div class="application-head flex">
      <div class="manageTitle">账号组管理</div>
      <div class="flex-wrap">
        <Search @clear="clear" @search="search" />
        <el-button size="default" @click="addApp" plain>新增</el-button>
      </div>
    </div>
    <div class="application-content">
      <Table :tableHeader="tableHeader" @btnClick="btnClick" :tableBtnGroup="tableBtnGroup" :tableData="tableData" />
    </div>
    <!--删除弹框-->
    <Dialog :flag="flag" msgTitle="删除账号组" type="del" :delName="title" @delRest="delRest" @delRemove="remove" />
    <!--新增/编辑-->
    <Dialog :flag="editDialogVisible" :msgTitle="editDialogTitle" @submitForm="submitForm(ruleFormRef)" @resetForm="resetForm(ruleFormRef)" :show-flag="true">
      <el-form ref="ruleFormRef" :model="ruleForm" label-width="110px" class="demo-ruleForm">
        <el-form-item label="账号组名称" prop="user_group_name">
          <el-input v-model="ruleForm.user_group_name" size="default" placeholder="请输入账号组名称" />
        </el-form-item>
        <el-form-item label="账号组描述" prop="user_group_comment">
          <el-input v-model="ruleForm.user_group_comment" size="default" type="textarea" :rows="3" placeholder="请输入账号组描述" />
        </el-form-item>
      </el-form>
    </Dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, onBeforeMount } from "vue";
import { ElMessage, FormInstance, FormRules } from "element-plus";
import Table from "@/components/tableTemp/index.vue";
import Search from "@/components/search/index.vue";
import * as _ from "lodash";
import table from "../hooks/useTable";
const ruleFormRef = ref<FormInstance>();
const tableHeader = ref<any>([]);
const tableData = ref<any>([]);
const tableBtnGroup = [
  {
    name: "编辑",
    className: "editBtn",
    param: "edit",
  },
  {
    name: "删除",
    className: "delBtn",
    param: "del",
  },
];
const flag = ref(false);
const title = ref("");
const searchInfo = ref("");
const dT = ref("add");
const editDialogVisible = ref(false);
const editDialogTitle = ref("新增账号组");
const ruleForm = ref<any>({
  user_group_name: "",
  user_group_comment: "",
});
const rowIndex = ref<any>({});

onBeforeMount(() => {
  accountList();
});

//获取账号组数据
const accountList = async () => {
  const res = await table.getTable(DATACONTENT_ID.accountGroupTableId, [
    searchInfo.value,
  ]);
  tableHeader.value = res.tableHeader.value;
  tableData.value = res.tableData.value;
};

const search = (val) => {
  searchInfo.value = val;
  accountList();
};
const clear = () => {
  searchInfo.value = "";
  accountList();
};
const btnClick = (index, row, param) => {
  rowIndex.value = _.cloneDeep(row);
  if (param == "del") {
    flag.value = true;
    title.value = row.user_group_name;
  } else {
    dT.value = "edit";
    editDialogVisible.value = true;
    editDialogTitle.value = "编辑账号组";
    ruleForm.value = _.cloneDeep(row);
  }
};
const delRest = () => {
  flag.value = false;
};
const remove = async () => {
  try {
    const { code, message } = await table.dataChange(
      SERVICE_ID.accountGroupScId,
      3,
      rowIndex.value
    );

    if (code == 0) {
      ElMessage.success(message);
      accountList();
    } else {
      ElMessage.error(message);
    }
  } catch (err: any) {}
  flag.value = false;
};
const addApp = () => {
  editDialogVisible.value = true;
  editDialogTitle.value = "新增账号组";
  dT.value = "add";
  ruleForm.value = {};
};
// 新增/编辑账号
const saveAddAccount = async (event) => {
  try {
    const { code, message } = await table.dataChange(
      SERVICE_ID.accountGroupScId,
      event,
      ruleForm.value
    );
    if (code == 0) {
      ElMessage.success(message);
      accountList();
    } else {
      ElMessage.error(message);
    }
  } catch (error) {}
};
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      if (dT.value == "add") {
        saveAddAccount(1);
      } else {
        // ruleForm.value = rowIndex.value;
        saveAddAccount(2);
      }
      editDialogVisible.value = false;
    } else {
      console.log("error submit!", fields);
    }
  });
};

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
  editDialogVisible.value = false;
};
</script>

<style lang="scss" scoped>
.application {
  &-content {
    margin-top: 24px;
    height: calc(100% - 56px);
  }
}
</style>