<template>
  <div class="container">
    <TableSearch :query="query" :options="searchOpt" :search="handleSearch" />
    <div class="table-container">
      <TableCustom :columns="columns" :tableData="tableData" :total="paging.total" :refresh="getData" :currentPage="paging.page" :changePage="changePage">
        <template #toolbarBtn>
          <el-button type="primary" @click="typeVisible = true">类型编辑</el-button>
          <el-button type="primary" @click="handelRow('add')">新增用户</el-button>
          <el-button @click="handleImp">批量创建用户</el-button>
          <!-- <el-button>导出</el-button> -->
          <!-- <el-button type="danger">删除</el-button> -->
        </template>

        <template #state="{ rows }">
          <el-switch v-model="rows.state" :active-value="1" :inactive-value="0" />
        </template>

        <template #roleId="{ rows }">
          {{ roleEnum[rows.roleId] }}
        </template>

        <template #password="{ rows }">
          <div v-if="rows.flag" @click="rows.flag = false">
            {{ rows.password }}
            <el-icon style="cursor: pointer"><View /></el-icon>
          </div>
          <div v-else @click="rows.flag = true">
            ******
            <el-icon style="cursor: pointer"><Hide /></el-icon>
          </div>
        </template>

        <template #operator="{ rows }">
          <el-button link type="primary" size="small" @click="handelRow('pwd', rows)"> 重置密码 </el-button>
          <el-button link type="primary" size="small" @click="handelRow('edit', rows)"> 修改 </el-button>
          <el-button link type="danger" size="small" @click="handelDel(rows)"> 删除 </el-button>
        </template>
      </TableCustom>
    </div>
    <el-dialog :title="dialogTitle" v-model="visible" width="700px" destroy-on-close :close-on-click-modal="false" @close="closeDialog">
      <TableEdit :form-data="rowData" :options="options" :edit="isEdit" :update="updateData" @close="closeDialog" />
    </el-dialog>
    <el-dialog title="编辑类型" v-model="typeVisible" width="800px" destroy-on-close :close-on-click-modal="false" @close="closeDialog">
      <UserType />
    </el-dialog>
    <BatchImp ref="batchImpRef" />
  </div>
</template>

<script setup lang="ts" name="basetable">
import { ref, reactive } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { accountAdd, accountModify, accountList, accountDeletet, passwordReset } from "@/api/index";
import TableCustom from "@/components/table-custom.vue";
import TableSearch from "@/components/table-search.vue";
import TableEdit from "@/components/table-edit.vue";
import BatchImp from "@/components/batch-imp.vue";
import { useCommonStore } from "@/store/common";
import UserType from "./userType.vue";
import { TableItem } from "@/types/table";
import { FormOption, FormOptionList } from "@/types/form-option";
import { TAccount } from "@/api/index.d";
// import { Hide, View } from "@element-plus/icons-vue";

enum roleEnum {
  管理员 = -1,
  警察 = 1,
  辅警 = 2,
  协警 = 3,
}

const comm = useCommonStore();
const visible = ref(false);
const typeVisible = ref(false);
const isEdit = ref(false);
const dialogTitle = ref("");
const rowData = ref<TAccount.IListRes | undefined>();
// 查询相关
const query = reactive({
  name: "",
});
const batchImpRef = ref();
const paging = reactive({
  page: 1,
  size: 10,
  total: 0,
});
const tableData = ref<TAccount.IListRes[]>([]);

const searchOpt = ref<FormOptionList[]>([
  { type: "input", label: "用户名：", prop: "name" },
  {
    type: "select",
    label: "用户类型：",
    prop: "name1",
    opts: [
      { label: "管理员", value: "1" },
      { label: "警察", value: "2" },
      { label: "辅警", value: "3" },
      { label: "协警", value: "4" },
    ],
  },
  { type: "daterange", label: "创建时间：", prop: "name2" },
  {
    type: "select",
    label: "状态：",
    prop: "name3",
    opts: [
      { label: "启用", value: "1" },
      { label: "禁用", value: "0" },
    ],
  },
]);

// 表格相关
let columns = ref([
  // { type: "selection" },
  { type: "index", label: "序号", width: 55, align: "center" },
  { prop: "name", label: "用户名称" },
  { prop: "username", label: "警员号" },
  { prop: "phone", label: "手机号" },
  { prop: "password", label: "密码", width: 120 },
  { prop: "roleId", label: "类型" },
  { prop: "createTime", label: "创建时间" },
  { prop: "state", label: "状态" },
  { prop: "operator", label: "操作", width: 250 },
]);

let addOpt = {
  labelWidth: "100px",
  span: 24,
  list: [
    { placeholder: "请输入用户名称", type: "input", label: "用户名称", prop: "name", required: true },
    { placeholder: "请输入电话号码", type: "input", label: "电话号码", prop: "phone", required: true },
    { placeholder: "请输入警员号", type: "input", label: "警员号", prop: "username", required: true },
    {
      placeholder: "请选择类型",
      type: "select",
      label: "类型",
      opts: [
        { label: "管理员", value: -1 },
        { label: "警察", value: 1 },
        { label: "辅警", value: 2 },
        { label: "协警", value: 3 },
      ],
      prop: "roleId",
      required: true,
    },
    { placeholder: "请输入密码", type: "password", label: "密码", prop: "password", required: true },
    { type: "switch", label: "状态", prop: "status", activeValue: 1, inactiveValue: 0 },
    // { placeholder: "请输入确认密码", type: "password", label: "确认密码", prop: "confirmpassword" },
    // { placeholder: "请输入备注", type: "textarea", label: "备注", prop: "mon8ey" },
  ],
};

// 新增/编辑弹窗相关
let options = ref<FormOption>();
// 重置密码
let pwdOpt = {
  labelWidth: "100px",
  span: 24,
  list: [
    // { type: "password", label: "原始密码", prop: "name1", required: true },
    { type: "password", label: "新密码", prop: "password", required: true },
    // { type: "password", label: "确认密码", prop: "money3", required: true },
  ],
};

const handleImp = () => {
  batchImpRef.value.dialogVisible = true;
};
const handleSearch = () => {
  paging.page = 1;
  getData();
};

const changePage = (val: number) => {
  paging.page = val;
  getData();
};

const getData = async () => {
  const res = await accountList(paging);
  tableData.value = res.records.map((item) => {
    item.flag = false;
    return item;
  });
  paging.total = res.total;
};
getData();

// 删除相关
const handelDel = (row: TableItem) => {
  ElMessageBox.confirm("确定要删除吗？", "提示", {
    type: "warning",
  }).then(async () => {
    accountDeletet({ id: row.id }).then((res) => {
      ElMessage.success("删除成功");
      getData();
    });
  });
};

const updateData = (res) => {
  let api, msg, p;
  if (dialogTitle.value == "重置密码") {
    api = passwordReset;
    p = { ...res, id: rowData.value.id };
    msg = "重置成功";
  } else {
    p = { ...res, orgId: comm.user.orgId };
    api = isEdit.value ? accountModify : accountAdd;
    msg = isEdit.value ? "修改成功" : "新增成功";
  }

  api(p).then((res) => {
    ElMessage.success(msg);
    closeDialog();
    getData();
  });
};

const closeDialog = () => {
  visible.value = false;
  isEdit.value = false;
};

const handelRow = (name: string, row?: TAccount.IListRes) => {
  if (name == "add") {
    dialogTitle.value = "新增用户";
    options.value = { ...addOpt };
    rowData.value = undefined;
    isEdit.value = false;
  } else if (name == "pwd") {
    dialogTitle.value = "重置密码";
    options.value = { ...pwdOpt };
    rowData.value = { ...row };
    isEdit.value = true;
  } else if (name == "edit") {
    dialogTitle.value = "编辑";
    isEdit.value = true;
    options.value = { ...addOpt };
    rowData.value = { ...row };
  }
  visible.value = true;
};
</script>

<style scoped>
.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}
</style>
