<template>
  <div id="index">
    <el-col class="main animate__animated animate__backInRight">
      <el-col :span="24" class="one">
        <component :is="partsSearch" :is_search="true" :fields="fields" @search="partSearch">
          <template #studio_id>
            <el-option v-for="i in studioList" :key="i._id" :label="i.name" :value="i._id"></el-option>
          </template>
        </component>
      </el-col>
      <el-col :span="24" class="two">
        <component
          :is="CTable"
          :fields="fields"
          :opera="opera"
          :select="false"
          :selected="selected"
          @handleSelect="handleSelect"
          @query="search"
          :data="tableData"
          :total="total"
          @edit="toEdit"
        >
        </component>
      </el-col>
    </el-col>
  </div>
</template>
<script setup lang="ts">
import store from '@/stores/counter';
import { ElMessage } from 'element-plus';
// #region 组件
import partsSearch from '@/components/c-search.vue';
import CTable from '@/components/c-table.vue';
// #endregion
import type { Ref } from 'vue';
import { ref, onMounted, getCurrentInstance } from 'vue';
import { useRouter } from 'vue-router';
// #region 接口
import { ScientistsettleStore } from '@common/src/stores/studio/studios/scientistsettle'; // 入驻科学家工作室
import { StudioStore } from '@common/src/stores/studio/studios/studio'; // 工作室
import { UnitStudioApplyStore } from '@common/src/stores/studio/role/unitStudioApply'; // 依托单位申请科学家工作室权限表
import type { IQueryResult } from '@/util/types.util';
const studio = StudioStore();
const unitStudioApply = UnitStudioApplyStore();
const scientistsettle = ScientistsettleStore();
const { proxy } = getCurrentInstance() as any;
const router = useRouter();
// #endregion
// 列表数据
let tableData: Ref<any[]> = ref([]);
// 总数
let total: Ref<number> = ref(0);
let skip = 0;
let limit: number = proxy.$limit;
// 列表
let fields: Ref<any[]> = ref([
  { label: '序号', options: { type: 'index' } },
  {
    label: '科学家工作室',
    model: 'studio_id',
    type: 'select',
    format: (i) => {
      let data = studioList.value.find((r) => r._id == i);
      if (data) return data.name;
    },
    isSearch: true,
  },
  { label: '科学家名称', model: 'scientist_name', isSearch: true },
  { label: '工作单位', model: 'company', isSearch: true },
  { label: '职称', model: 'zc', isSearch: true },
]);
// 操作
let opera: Ref<any[]> = ref([{ label: '修改', method: 'edit' }]);
// 多选
let selected: Ref<any[]> = ref([]);
// 用户信息
let user: Ref<{ _id: string; name: string; unit_name: string; nick_name: string }> = ref({ _id: '', name: '', unit_name: '', nick_name: '' });
// 查询数据
let searchForm: Ref<{}> = ref({});
// 依托单位信息
let unitInfo: Ref<{ _id: String; status: String }> = ref({ _id: '', status: '' });
// 工作室
let studioList: Ref<any[]> = ref([]);
onMounted(async () => {
  user.value = store.state.user as { _id: string; name: string; unit_name: string; nick_name: string };
  await searchUnit();
  await searchOther();
  await search({ skip, limit });
});
// 依托单位信息
const searchUnit = async () => {
  const res: IQueryResult = await unitStudioApply.query({ unit_id: user.value._id });
  let list = res.data as any[];
  if (res.total > 0) unitInfo.value = list[0];
};
// 查询
const search = async (e: { skip: number; limit: number }) => {
  if (unitInfo && unitInfo.value._id && unitInfo.value.status == '1') {
    const { skip, limit } = e;
    let info = { limit: limit, skip: skip, ...searchForm.value, company_id: unitInfo.value._id };
    const res: IQueryResult = await scientistsettle.query(info);
    if (res.errcode == 0) {
      tableData.value = res.data as any[];
      total.value = res.total;
    } else ElMessage({ type: 'warning', message: `${res.errmsg}` });
  } else {
    ElMessage({ type: 'warning', message: `用户未完成信息填报/信息填报未完成审核，无法查询相关信息` });
  }
};
// 查询
const partSearch = (form: { [x: string]: any }) => {
  searchForm.value = form;
  search({ skip, limit });
};
// 修改
const toEdit = (data: { _id: string }) => {
  router.push({ path: '/unit/scientist/add', query: { id: data._id } });
};
// 选择
const handleSelect = () => {};
// 查询其他信息
const searchOther = async () => {
  // 工作室
  const p1: IQueryResult = await studio.query({ user_id: user.value._id });
  studioList.value = p1.data as [];
};
</script>
<style lang="scss" scoped>
.main {
  .thr {
    margin: 1vw 0 0 0;
  }
}
</style>
