<template>
  <CommonTable ref="typeTable" :have-page="false" :columns="assetColumns" :data-source="assetDatas">
    <template #title>资产详情</template>
    <template #bodyCell="{ column }">
      <template v-if="column.dataIndex === 'operation'">
        <a-button type="link" @click="viewDetail">资产明细</a-button>
      </template>
    </template>
  </CommonTable>
  <a-modal v-model:open="open" width="75%" :footer="null" title="查看明细" @cancel="open = false">
    <component
      :is="dynamicComponent" v-if="open" :info-window-no="infoWindowNo"
      :where-clause-source="whereClauseSource" :is-search-widget="true"
    />
  </a-modal>
</template>

<script setup>
import { queryByIdApi } from './api.js';
import { message } from 'ant-design-vue';
import Common from '../../common/Common.js';
import { InfoWindow } from 'pc-component-v3';
import { assetColumn } from './assetConfig.js';
import { ref, defineProps, defineEmits, watch } from 'vue';
import CommonTable from '../assetsDisposal/AssetCommonTable.vue';

const emits = defineEmits(['getEvaluateValue']);

const props = defineProps({
  disposalId: {
    type: String,
    default: null,
  },
  selectAssetIds: {
    type: Array,
    default: () => [],
  },
  isReadonly: {
    type: Boolean,
    default: false,
  },
  readDatas: {
    type: Object,
    default: null,
  },
});

const open = ref(false);
const dynamicComponent = ref(null);
const infoWindowNo = ref('20241103_160417');
const whereClauseSource = ref(null);

const assetDatas = ref([]);
const assetColumns = ref(assetColumn);

// 查看明细
const viewDetail = () => {
  whereClauseSource.value = {
    customWhere: 'adp.id IS NOT NULL AND adp.id = ' + props.disposalId,
  };
  dynamicComponent.value = InfoWindow;
  open.value = true;
};

// 查询资产详情
const queryAsset = () => {
  const params = {
    ids: props.selectAssetIds,
    recordId: props.disposalId,
  };
  queryByIdApi(params).then(
    success => {
      if (success.errorCode === 0) {
        const { approvalAmount, choiceDisposedAmount, evaluateValue } = success.data;
        if (approvalAmount !== null && approvalAmount !== undefined) success.data.approvalAmount = approvalAmount.toFixed(2);
        if (choiceDisposedAmount !== null && choiceDisposedAmount !== undefined) success.data.choiceDisposedAmount = choiceDisposedAmount.toFixed(2);
        if (evaluateValue !== null && evaluateValue !== undefined) success.data.evaluateValue = evaluateValue.toFixed(2);
        assetDatas.value = [success.data];
        emits('getEvaluateValue', success.data.evaluateValue);
      } else {
        message.warning(success.errorMessage);
      }
    },
    error => {
      Common.processException(error);
    },
  );
};

watch(() => props.disposalId, (newValue, oldValue) => {
  if (newValue !== oldValue) {
    if (!props.isReadonly) {
      queryAsset();
    } else {
      if (props.readDatas) {
        assetDatas.value = [props.readDatas.assetDetailsVo];
      }
    }
  }
}, { immediate: true });

</script>

<style scoped></style>