<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">资产明细</a-button>
      </template>
    </template>
  </CommonTable>
</template>

<script setup>
import { queryByIdApi } from './api.js';
import Common from '../common/Common.js';
import { message } from 'ant-design-vue';
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 assetDatas = ref([]);
const assetColumns = ref(assetColumn);


// 查询资产详情
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>