<template>
  <div id="index">
    <el-row>
      <el-col :span="24" 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"
            @edit="toEdit"
            @del="toDel"
          >
          </component>
        </el-col>
      </el-col>
    </el-row>
  </div>
</template>
<script setup lang="ts">
import store from '@/stores/counter';
// #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 { ElMessage } from 'element-plus';
import { useRouter } from 'vue-router';
// #region 接口
import { YearreportStore } from '@common/src/stores/studio/studios/yearreport'; //年度报告
import { DictDataStore } from '@common/src/stores/users/sysdictdata'; // 字典表
import { UnitStudioApplyStore } from '@common/src/stores/studio/role/unitStudioApply'; // 依托单位申请科学家工作室权限表
import type { IQueryResult } from '@/util/types.util';
const yearreport = YearreportStore();
const sysdictdata = DictDataStore();
const unitStudioApply = UnitStudioApplyStore();
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_name', isSearch: true },
  { label: '依托单位名称', model: 'company_name' },
  { 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: 'edit', type: 'warning', display: (i) => i.status == '0' },
  { label: '删除', method: 'del', type: 'danger', confirm: true },
]);
// 多选
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 unitInfo: Ref<{ _id: String; status: String }> = ref({ _id: '', status: '' });
// 查询数据
let searchForm: Ref<{}> = ref({});
// 状态
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 yearreport.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/yearreport/add' });
  } else {
    ElMessage({ message: '用户未完成信息填报/信息填报未完成审核，无法查询相关信息', type: 'warning' });
  }
};
// 修改
const toEdit = (data: { _id: string }) => {
  router.push({ path: '/unit/yearreport/add', query: { id: data._id } });
};
// 删除
const toDel = async (data: { _id: string }) => {
  const res: IQueryResult = await yearreport.del(data._id);
  if (res.errcode == 0) {
    ElMessage({ message: '删除信息成功', type: 'success' });
    search({ skip, limit });
  }
};
// 选择
const handleSelect = () => {};
// 查询其他信息
const searchOther = async () => {
  // 字典表---审核状态
  const p1: IQueryResult = await sysdictdata.query({ dict_type: 'studio_status', is_use: '0' });
  statusList.value = p1.data as [];
};
</script>
<style lang="scss" scoped>
.main {
  .thr {
    margin: 1vw 0 0 0;
  }
}
</style>
