<template>
  <w-dialog ref="dialogRef" :title="state.dialogTitle" width="80%" height="80%" :buttons="dialogButtons">
    <div class="p-1.5">
      <w-grid
        ref="customerGridRef"
        title="客户列表"
        :height="430"
        :fetch-data-url="Environment.apiContextPath('api/irbs/companyCustomer/query')"
        :sort-no="true"
        :checkbox-selection="false"
        :config-button="false"
        :query-form-cols-num="2"
        :query-form-fields="customerGrid.queryFormFields"
        :columns="customerGrid.columns"
        :toolbar-actions="customerGrid.buttons"
      ></w-grid>
    </div>
  </w-dialog>
</template>

<script setup lang="ts">
import { ref, toRaw, reactive, nextTick } from 'vue';
import { axios, Environment, NotifyManager, Tools, Formater } from 'platform-core';

const dialogRef = ref();
const customerGridRef = ref();
const props = defineProps({
  dictionary: {
    type: Object,
    default: () => {
      return {};
    },
  },
});
const emit = defineEmits(['refresh']);

const state = reactive({
  dialogTitle: '选择评级客户',
});

const dialogButtons = [
  {
    label: '确定',
    icon: 'beenhere',
    click: () => {
      const rows = customerGridRef.value.getSelectedRows();
      if (rows.length === 0) {
        NotifyManager.warn('请选择要发起评级的客户');
        return;
      }
      axios
        .post(Environment.apiContextPath('api/irbs/companyRating/generateRating/' + rows[0]['custNo']))
        .then((resp) => {
          if (resp && resp['code'] === 200 && resp['data']) {
            emit('refresh');
            dialogRef.value.hide();
          }
        })
        .catch((error) => {
          console.info('error-------------', error);
        });
    },
  },
];

const customerGrid = {
  queryFormFields: [
    { label: '客户号', name: 'custNo', type: 'text', defaultValue: '2100601306' },
    { label: '客户名称', name: 'custName', type: 'text' },
  ],
  buttons: ['query', 'reset'],
  columns: [
    { name: 'custNo', label: '客户号' },
    { name: 'custName', label: '客户名称' },
    { name: 'induSortName', label: '行业类型' },
    { name: 'custTypeCd', label: '客户类型', format: Formater.dictionary(props.dictionary.dictCustomerTypeCd) },
    { name: 'buildDt', label: '成立时间' },
    { name: 'corpSizeCd', label: '企业规模', format: Formater.dictionary(props.dictionary.dictCustomerSizeCd) },
  ],
};

const show = (data: any) => {
  dialogRef.value.show();
};
const hide = () => {
  dialogRef.value.hide();
};

defineExpose({
  show,
  hide,
});
</script>
