<template>
  <div id="index">
    <el-row>
      <el-col :span="24" class="main animate__animated animate__backInRight" v-loading="loading">
        <el-col :span="24" class="one">
          <cSearch :is_title="false" :is_search="true" :fields="fields" @search="toSearch">
            <template #area>
              <el-option v-for="i in areaList" :key="i.value" :label="i.label" :value="i.value"></el-option>
            </template>
            <template #status>
              <el-option v-for="i in statusList" :key="i.value" :label="i.label" :value="i.value"></el-option>
            </template>
          </cSearch>
        </el-col>
        <el-col :span="24" class="two">
          <cButton @toAdd="toAdd()"> </cButton>
        </el-col>
        <el-col :span="24" class="thr">
          <cTable :fields="fields" :opera="opera" :list="list" @query="search" :total="total" @view="toView" @edit="toEdit" @exam="toExam" @del="toDel">
          </cTable>
        </el-col>
      </el-col>
    </el-row>
    <cDialog :dialog="dialog" @handleClose="toClose">
      <template v-slot:info>
        <cForm v-if="dialog.type == '1'" :span="24" :fields="formFields" :form="form" :rules="{}" @save="onSubmit">
          <template #status>
            <el-option v-for="(i, index) in statusList" :key="index" :label="i.label" :value="i.value"></el-option>
          </template>
          <template #role>
            <el-select v-model="form.role" multiple placeholder="请选择角色" style="width: 100%">
              <el-option v-for="i in roleList" :key="i._id" :label="`${i.name}(${i.code})`" :value="i._id"> </el-option>
            </el-select>
          </template>
        </cForm>
      </template>
    </cDialog>
  </div>
</template>
<script lang="ts" setup>
import _ from 'lodash';
import type { Ref } from 'vue';
import { ref, onMounted, getCurrentInstance } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';

// 接口
import { ExpertStore } from '@common/src/stores/admins/expert';
import { DictDataStore } from '@common/src/stores/system/dictData'; // 字典表
import { RoleStore } from '@common/src/stores/system/role'; // 角色
import type { IQueryResult } from '@/util/types.util';
const expert = ExpertStore();
const dictData = DictDataStore();
const roleAxios = RoleStore();

const { proxy } = getCurrentInstance() as any;

// 路由
const router = useRouter();

// 加载中
const loading = ref(false);
// 列表数据
let list: Ref<any> = ref([]);
// 总数
let total: Ref<number> = ref(0);
let skip = 0;
let limit: number = proxy.$limit;
// 列表
let fields: Ref<any[]> = ref([
  { label: '邀请码', model: 'code', isSearch: true },
  { label: '账号', model: 'account', isSearch: true },
  { label: '用户名', model: 'name', isSearch: true },
  { label: '手机号', model: 'phone', isSearch: true },
  { label: '所属辖区', model: 'area', format: (i) => getDict(i, 'area'), isSearch: true, type: 'select' },
  { label: '审核状态', model: 'status', format: (i) => getDict(i, 'status'), isSearch: true, type: 'select' }
]);
// 操作
let opera: Ref<any[]> = ref([
  { label: '查看', method: 'view', type: 'success' },
  { label: '审核/角色', method: 'exam' },
  { label: '修改', method: 'edit' },
  { label: '删除', method: 'del', confirm: true, type: 'danger' }
]);
const dialog: Ref<{ type: string; show: boolean; title: string }> = ref({ type: '1', show: false, title: '信息管理' });
let form: Ref<any> = ref({});
// 表单
let formFields: Ref<any[]> = ref([
  { label: '状态', model: 'status', type: 'select' },
  { label: '角色', model: 'role', custom: true }
]);
// 查询数据
let searchForm: Ref<any> = ref({});
// 字典表
let statusList: Ref<any> = ref([]);
let areaList: Ref<any> = ref([]);
const roleList: Ref<any> = ref([]);
onMounted(async () => {
  loading.value = true;
  await searchOther();
  await search({ skip, limit });
  loading.value = false;
});
// 查询
const search = async (e: { skip: number; limit: number }) => {
  const { skip, limit } = e;
  const condition = _.cloneDeep(searchForm.value);
  let info = { limit: limit, skip: skip, ...condition, type: '6' };
  let res: IQueryResult = await expert.query(info);
  if (res.errcode == 0) {
    list.value = res.data;
    total.value = res.total;
  }
};
const toSearch = (query) => {
  searchForm.value = query;
  search({ skip, limit });
};
const getDict = (e, model) => {
  if (model == 'status') {
    let data: any = statusList.value.find((i: any) => i.value == e);
    if (data) return data.label;
    else return '暂无';
  } else if (model == 'area') {
    let data: any = areaList.value.find((i: any) => i.value == e);
    if (data) return data.label;
    else return '暂无';
  }
};
// 新增
const toAdd = () => {
  router.push({ path: '/user/expert/detail' });
};
const toView = (data) => {
  router.push({ path: '/user/expert/detail', query: { id: data._id, isdisabled: `${true}` } });
};
// 修改
const toEdit = async (data) => {
  router.push({ path: '/user/expert/detail', query: { id: data._id } });
};
const toExam = async (data) => {
  let res: IQueryResult = await expert.fetch(data._id);
  if (res.errcode == 0) {
    form.value = res.data;
    dialog.value = { title: '信息管理', show: true, type: '1' };
  }
};
// 提交
const onSubmit = async (data) => {
  let res: IQueryResult;
  if (data._id) res = await expert.update(data);
  else res = await expert.create(data);
  if (res.errcode == 0) {
    ElMessage({ type: `success`, message: `审核成功` });
    toClose();
  }
};
// 弹框关闭
const toClose = () => {
  form.value = {};
  searchForm.value = {};
  dialog.value = { title: '信息管理', show: false, type: '1' };
  search({ skip, limit });
};
// 删除
const toDel = async (data) => {
  let res: IQueryResult = await expert.del(data._id);
  if (res.errcode == 0) {
    ElMessage({ type: `success`, message: `刪除信息成功` });
    search({ skip, limit });
  }
};
const searchOther = async () => {
  let res: IQueryResult;
  // 状态
  res = await dictData.query({ type: 'common_status' });
  if (res.errcode == 0) statusList.value = res.data;
  // 状态
  res = await dictData.query({ type: 'jl_area' });
  if (res.errcode == 0) areaList.value = res.data;
  // 角色
  res = await roleAxios.query({ account_type: '6', is_use: '0' });
  if (res.errcode == 0) roleList.value = res.data;
};
</script>
<style lang="scss" scoped>
.main {
  .one {
    margin: 0 0 10px 0;
  }
  .two {
    margin: 0 0 10px 0;
  }
}
</style>
