<template>
  <div class="guns-layout">
    <div class="guns-layout-content">
      <div class="guns-layout">
        <div class="guns-layout-content-header">生成记录</div>
        <div class="guns-layout-content-application">
          <div class="content-mian">
            <div class="content-mian-body">
              <div class="table-content">
                <common-table
                  :columns="columns"
                  :where="where"
                  showTableTool
                  :showToolTotal="false"
                  rowId="id"
                  ref="tableRef"
                  :rowSelection="false"
                  fieldBusinessCode="EXCEL_INIT_LOG"
                  url="/businessAiParseLog/page"
                  :scroll="{ y: '100%' }"
                >
                  <template #toolLeft>
                    <a-input
                      v-model:value="where.searchText"
                      placeholder="请输入关键词（回车搜索）"
                      @pressEnter="reload"
                      :bordered="false"
                      class="search-input"
                    >
                      <template #prefix>
                        <icon-font iconClass="icon-opt-search"></icon-font>
                      </template>
                    </a-input>
                  </template>
                  <template #bodyCell="{ column, record }">
                    <template v-if="column?.dataIndex == 'parseStatus'">
                      <a-tag v-if="record.parseStatus == 10" color="green">进行中</a-tag>
                      <a-tag v-if="record.parseStatus == 20" color="blue">解析成功</a-tag>
                      <a-tag v-if="record.parseStatus == 30" color="red">解析失败</a-tag>
                      <a-tag v-if="record.parseStatus == 40" color="orange">停止解析</a-tag>
                    </template>
                    <template v-if="column?.dataIndex == 'originExcelFileId'">
                      <a @click="downloadFile(record)">{{ record?.originExcelFileIdWrapper?.name }}</a>
                    </template>
                    <template v-if="column?.dataIndex == 'originPicFileId'">
                      <a-image :width="200" :src="record?.originPicFileIdWrapper?.thumbUrl" />
                    </template>
                    <template v-if="['aiResponseString', 'errorMessage', 'parseBusinessResult'].includes(column?.dataIndex)">
                      <a @click="lookDetail(record, column?.dataIndex)">{{ record[column?.dataIndex] }}</a>
                    </template>
                    <!-- 操作 -->
                    <template v-if="column?.key == 'action'">
                      <a-space :size="8">
                        <icon-font
                          iconClass="icon-opt-shuaxin"
                          font-size="24px"
                          title="刷新状态"
                          color="#60666b"
                          @click="refresh(record)"
                          v-if="record.parseStatus == 10"
                        ></icon-font>
                        <icon-font
                          iconClass="icon-opt-shanchu"
                          font-size="24px"
                          title="删除"
                          color="#60666b"
                          @click="remove(record)"
                        ></icon-font>
                      </a-space>
                    </template>
                  </template>
                </common-table>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <LogDetail v-model:visible="showLog" v-if="showLog" :data="current" :currentType="currentType" :title="title" />
  </div>
</template>

<script setup name="ExcelInitLog">
import { ExcelInitLogApi } from '../../api/ExcelInitLogApi';
import { ref, createVNode, onMounted, defineAsyncComponent } from 'vue';
import { message, Modal } from 'ant-design-vue/es';
import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
import { getToken } from '@/utils/token-util';
import { FileApi } from '@/views/system/backend/file/api/FileApi';

const LogDetail = defineAsyncComponent(() => import('./log-detail.vue'));

const props = defineProps({
  businessId: String
});

// 表格配置
const columns = ref([
  {
    key: 'index',
    title: '序号',
    width: 48,
    align: 'center',
    isShow: true,
    hideInSetting: true
  },
  {
    dataIndex: 'beginTime',
    title: '开始解析时间',
    ellipsis: true,
    width: 150,
    isShow: true
  },
  {
    dataIndex: 'endTime',
    title: '结束解析时间',
    ellipsis: true,
    width: 150,
    isShow: true
  },
  {
    dataIndex: 'parseStatus',
    title: '解析状态',
    ellipsis: true,
    width: 100,
    isShow: true
  },
  {
    dataIndex: 'originExcelFileId',
    title: '原始excel文件id',
    ellipsis: true,
    width: 150,
    isShow: true
  },
  {
    dataIndex: 'originPicFileId',
    title: 'excel转化为png的文件id',
    ellipsis: true,
    width: 180,
    isShow: true
  },
  {
    dataIndex: 'aiResponseString',
    title: 'AI解析的结果',
    ellipsis: true,
    width: 150,
    isShow: true
  },
  {
    dataIndex: 'errorMessage',
    title: '解析过程中的报错信息',
    ellipsis: true,
    width: 150,
    isShow: true
  },
  {
    dataIndex: 'parseBusinessResult',
    title: 'AI解析为业务数据的结果',
    ellipsis: true,
    width: 160,
    isShow: true
  },
  {
    key: 'action',
    title: '操作',
    width: 100,
    fixed: 'right',
    align: 'center',
    isShow: true
  }
]);
// ref
const tableRef = ref(null);

// 搜索条件
const where = ref({
  searchText: '',
  businessId: props.businessId
});

const showLog = ref(false);

const current = ref('');

const title = ref('');

const currentType = ref('');

onMounted(() => {});

// 点击搜索
const reload = () => {
  if (tableRef.value) tableRef.value.reload();
};

// 删除单个
const remove = record => {
  Modal.confirm({
    title: '提示',
    content: '确定要删除选中的数据吗?',
    icon: createVNode(ExclamationCircleOutlined),
    maskClosable: true,
    onOk: async () => {
      const res = await ExcelInitLogApi.delete({ id: record.id });
      message.success(res.message);
      reload();
    }
  });
};

const refresh = record => {
  ExcelInitLogApi.detail({ id: record.id }).then(res => {
    message.success('操作成功');
    record = { ...res };
  });
};

const downloadFile = record => {
  FileApi.download({ token: getToken(), fileId: record.originExcelFileId, secretFlag:'Y' });
};

const lookDetail = (record, type) => {
  if (type == 'aiResponseString') {
    title.value = 'AI解析的结果';
  } else if (type == 'errorMessage') {
    title.value = '解析过程中的报错信息';
  } else {
    title.value = 'AI解析为业务数据的结果';
  }
  current.value = record[type];
  currentType.value = type;
  showLog.value = true;
};
</script>

<style scoped lang="less"></style>
