<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 #type>
              <el-option v-for="(i, index) in typeList" :key="index" :label="i.label" :value="i.value"></el-option>
            </template>
            <template #status>
              <el-option v-for="(i, index) in statusList" :key="index" :label="i.label" :value="i.value"></el-option>
            </template>
          </cSearch>
        </el-col>
        <el-col :span="24" class="thr">
          <cTable :fields="fields" :opera="opera" :list="list" @query="search" :total="total" @view="toView" @file="toFile"> </cTable>
        </el-col>
      </el-col>
    </el-row>
  </div>
</template>
<script lang="ts" setup>
import _ from 'lodash';
import type { Ref } from 'vue';
import { ref, onMounted, getCurrentInstance } from 'vue';
import { ElMessage } from 'element-plus';
import { useRouter } from 'vue-router';

// 接口
import { TranstionStore } from '@common/src/stores/patent/transtion';
import { DictDataStore } from '@common/src/stores/system/dictData'; // 字典表
import type { IQueryResult } from '@/util/types.util';
import store from '@/stores/counter';
const transAxios = TranstionStore();
const dictAxios = DictDataStore();

// 路由
const router = useRouter();

const { proxy } = getCurrentInstance() as any;

// 加载中
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: 'create_number' },
  { label: '专利名称', model: 'patent_name', isSearch: true },
  { label: '交易类型', model: 'type', format: (i: any) => getDict(i, 'type'), isSearch: true, type: 'select' },
  { label: '联系人', model: 'contact' },
  { label: '手机号', model: 'phone' },
  { label: '状态', model: 'status', format: (i: any) => getDict(i, 'status'), isSearch: true, type: 'select' }
]);
// 操作
let opera: Ref<any[]> = ref([
  { label: '查看', method: 'view' },
  { label: '交易归档', method: 'file', confirm: true, type: 'warning', display: (i: any) => i.status == '6' }
]);

// 查询数据
let searchForm: Ref<any> = ref({});

// 字典表
const typeList: Ref<any> = ref([]);
const statusList: Ref<any> = ref([]);

onMounted(async () => {
  loading.value = false;
  await searchOther();
  await search({ skip, limit });
  loading.value = false;
});
// 查询
const search = async (e: { skip: number; limit: number }) => {
  let user = store.state.user;
  const { skip, limit } = e;
  const condition = _.cloneDeep(searchForm.value);
  let info = { limit: limit, skip: skip, ...condition, mech_id: user._id, status: '6' };
  let res: IQueryResult = await transAxios.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 = (value, model) => {
  if (model == 'type') {
    let data = typeList.value.find((i) => i.value == value);
    if (data) return data.label;
    else return '暂无';
  } else if (model == 'status') {
    let data = statusList.value.find((i) => i.value == value);
    if (data) return data.label;
    else return '暂无';
  }
};
// 查看
const toView = (data) => {
  router.push({ path: '/common/trans/info', query: { id: data._id } });
};
// 交易归档
const toFile = async (data) => {
  data.status = '7';
  let res: IQueryResult = await transAxios.update(data);
  if (res.errcode == '0') {
    ElMessage({ message: '交易归档成功，交易完成', type: 'success' });
    toClose();
  } else {
    ElMessage({ message: `${res.errmsg}`, type: 'error' });
  }
};

// 查询其他信息
const searchOther = async () => {
  let res: IQueryResult;
  // 类型
  res = await dictAxios.query({ type: 'patent_trans_type' });
  if (res.errcode == '0') typeList.value = res.data;
  // 状态
  res = await dictAxios.query({ type: 'patent_trans_status' });
  if (res.errcode == '0') {
    let list: any = res.data as [];
    statusList.value = list;
  }
};
</script>
<style lang="scss" scoped>
.main {
  .one {
    margin: 0 0 10px 0;
  }
  .two {
    margin: 0 0 10px 0;
  }
}
</style>
