<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 #status>
            <el-option v-for="i in statusList" :key="i.dict_value" :label="i.dict_label" :value="i.dict_value"></el-option>
          </template>
        </component>
      </el-col>
      <el-col :span="24" class="two">
        <component :is="Btn1" @toAdd="toAdd"></component>
      </el-col>
      <el-col :span="24" class="thr">
        <component
          :is="CTable"
          :fields="fields"
          :opera="opera"
          :select="false"
          :selected="selected"
          @handleSelect="handleSelect"
          @query="search"
          :data="tableData"
          :total="total"
          @view="toView"
        >
        </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 '@common/src/components/frame/c-search.vue';
import CTable from '@common/src/components/frame/c-table.vue';
import Btn1 from '@common/src/components/frame/btn-1.vue';
// #endregion
import type { Ref } from 'vue';
import { ref, onMounted, getCurrentInstance } from 'vue';
import { useRouter } from 'vue-router';
// #region 接口
import { StudioStore } from '@common/src/stores/studio/studios/studio'; // 工作室
import { UnitStudioApplyStore } from '@common/src/stores/studio/role/unitStudioApply'; // 依托单位申请科学家工作室权限表
import { DictDataStore } from '@common/src/stores/users/sysdictdata'; // 字典表
import type { IQueryResult } from '@/util/types.util';
const studio = StudioStore();
const unitStudioApply = UnitStudioApplyStore();
const sysdictdata = DictDataStore();
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: 'name', isSearch: true },
  { label: '依托单位', model: 'company_name', isSearch: true },
  { label: '入驻科学家', model: 'scientist_name', isSearch: true },
  { label: '申报日期', model: 'apply_time' },
  {
    label: '申报状态',
    model: 'status',
    type: 'select',
    format: (i) => {
      let data = statusList.value.find((r) => r.dict_value == i);
      if (data) return data.dict_label;
    },
    isSearch: true,
  },
]);
// 操作
let opera: Ref<any[]> = ref([{ label: '申报进度', method: 'view' }]);
// 多选
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 statusList: 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 studio.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 toAdd = () => {
  if (unitInfo && unitInfo.value._id && unitInfo.value.status == '1') {
    router.push({ path: '/unit/studio/add' });
  } else {
    ElMessage({ message: '用户未完成信息填报/信息填报未完成审核，无法查询相关信息', type: 'warning' });
  }
};
// 申报进度
const toView = (data: { _id: string; status: string }) => {
  router.push({ path: '/unit/studio/add', query: { id: data._id, status: data.status } });
};
// 选择
const handleSelect = () => {};
// 查询其他信息
const searchOther = async () => {
  // 审核状态
  const p1: IQueryResult = await sysdictdata.query({ dict_type: 'studio_studio_status', is_use: '0' });
  statusList.value = p1.data as [];
};
</script>
<style lang="scss" scoped>
.main {
  .thr {
    margin: 1vw 0 0 0;
  }
}
</style>
