<template>
  <div style="height: 100%">
    <w-grid
      ref="companyRatingGridRef"
      title="客户评级列表"
      :data-url="Environment.apiContextPath('api/irbs/companyRating')"
      :fetch-data-url="Environment.apiContextPath('api/irbs/companyRating/query')"
      :sort-no="true"
      group-mode="alone"
      :checkbox-selection="false"
      :query-form-cols-num="4"
      :query-form-fields="companyRatingGrid.queryFormFields"
      :columns="companyRatingGrid.columns"
      :toolbar-actions="companyRatingGrid.buttons"
      :query-criteria="{
        operator: 'and',
        criteria: [
          {
            fieldName: 'triggerType',
            operator: 'equals',
            value: 'INDEPENDENT',
          },
          {
            fieldName: 'launchUser',
            operator: 'equals',
            value: SessionManager.getUser().loginName,
          },
        ],
      }"
      :sort-by="['-createDate']"
    ></w-grid>
    <LaunchRatingDialog ref="launchRatingdialogRef" :dictionary="dictionary" @refresh="refreshTable"></LaunchRatingDialog>
    <RatingDialog ref="ratingdialogRef" @refresh="refreshTable"></RatingDialog>
    <GrayModelDialog ref="grayDialogRef" @refresh="refreshTable"></GrayModelDialog>
  </div>
</template>

<script setup lang="ts">
import { ref, shallowRef } from 'vue';
import { useQuasar } from 'quasar';
import { Environment, EnumTools, DictionaryTools, Options, Formater, axios, NotifyManager, Tools, SessionManager } from 'platform-core';
import LaunchRatingDialog from './LaunchRatingDialog.vue';
import RatingDialog from './RatingDialog.vue';
import GrayModelDialog from './GrayModelDialog.vue';
import { RatingLevelOptions, RatingProcessStatus, Round } from './CustRating.ts';
import RatingLevel from './RatingLevel.vue';

const $q = useQuasar();
const companyRatingGridRef = ref();
const launchRatingdialogRef = ref();
const ratingdialogRef = ref();
const grayDialogRef = ref();

const RatingStatusEnum = await EnumTools.fetch('irbs.cust.rating.enums.RatingStatus');
const RatingProcessStatusEnum = await EnumTools.fetch('irbs.cust.rating.enums.RatingProcessStatus');
const dictCustomerTypeCd = await DictionaryTools.fetch('CustomerTypeCd');
const optionsCustomerTypeCd = Options.dictionary(dictCustomerTypeCd);
const dictCustomerSizeCd = await DictionaryTools.fetch('CustomerSizeCd');
const optionsCustomerSizeCd = Options.dictionary(dictCustomerSizeCd);

const dictionary = {
  dictCustomerTypeCd: dictCustomerTypeCd,
  optionsCustomerTypeCd: optionsCustomerTypeCd,
  dictCustomerSizeCd: dictCustomerSizeCd,
  optionsCustomerSizeCd: optionsCustomerSizeCd,
};

const companyRatingGrid = {
  buttons: [
    'query',
    'reset',
    'separator',
    {
      extend: 'add',
      label: '发起评级',
      click: () => {
        launchRatingdialogRef.value.show();
      },
    },
    {
      extend: 'edit',
      label: '评级',
      enableIf: (args) => {
        if (
          args.selected &&
          (args.selected['processStatus'] === RatingProcessStatus.AWAIT_RATING ||
            args.selected['processStatus'] === RatingProcessStatus.AWAIT_SUBMIT ||
            args.selected['processStatus'] === RatingProcessStatus.BACK)
        ) {
          return true;
        }
        return false;
      },
      click: (args) => {
        ratingdialogRef.value.show(args.selected);
      },
    },
    // {
    //   extend: 'remove',
    //   label: '撤销',
    //   click: () => {},
    // },
    'separator',
    {
      extend: 'view',
      click: (args) => {
        ratingdialogRef.value.show(args.selected, true);
      },
    },
    'export',
    'separator',
    [
      {
        name: 'otherOp',
        label: '其他操作',
        icon: 'device_hub',
      },
      {
        name: 'viewGray',
        extend: 'view',
        label: '查看灰度模型结果',
        click: (args) => {
          grayDialogRef.value.show(args.selected);
        },
      },
      'separator',
      {
        extend: 'remove',
        name: 'delete',
        label: '强制删除',
        click: (args) => {
          // args._click();
          $q.dialog({
            title: '询问',
            message: '是否确认删除已生成评级信息及流程信息？',
            cancel: { noCaps: true },
            ok: { noCaps: true },
            persistent: true,
          }).onOk(() => {
            axios
              .post(Environment.apiContextPath('api/irbs/companyRating/removeRatingAndProcess/' + args.selected['id']))
              .then((resp) => {
                if (resp && resp.data) {
                  NotifyManager.info('删除成功');
                  companyRatingGridRef.value.refresh();
                }
              })
              .catch((error) => {
                console.info('error====', error);
                NotifyManager.warn('删除失败');
              });
          });
        },
      },
    ],
    'separator',
  ],
  queryFormFields: [
    { label: '申请编号', name: 'id', type: 'w-text' },
    { label: '客户号', name: 'custNo', type: 'w-text' },
    { label: '客户名称', name: 'custName', type: 'w-text' },
    {
      label: '认定等级',
      name: 'finalLevel',
      type: 'w-select',
      options: RatingLevelOptions,
    },
    { label: '流程状态', name: 'processStatus', type: 'w-select', options: Options.enum(RatingProcessStatusEnum) },
    { label: '评级状态', name: 'ratingStatus', type: 'w-select', options: Options.enum(RatingStatusEnum) },
  ],
  columns: [
    { name: 'id', label: '申请编号', align: 'center' },
    { name: 'custNo', label: '客户号', align: 'center' },
    { name: 'custName', label: '客户名称' },
    { name: 'industryTypeName', label: '行业类型' },
    { name: 'modelScore', label: '模型得分' },
    { name: 'modelLevel', label: '模型等级' },
    { name: 'adjLevel', label: '调整后等级' },
    { name: 'initLevel', label: '初评等级' },
    {
      name: 'finalLevel',
      label: '认定等级',
      format: (val) => {
        if (!Tools.isEmpty(val)) {
          return {
            componentType: RatingLevel,
            attrs: {
              level: val,
              dense: true,
            },
          };
        }
        return val;
      },
    },
    {
      name: 'pd',
      label: '违约概率',
      format: (val) => {
        if (val && typeof val === 'number') {
          return Round(val * 100, 2) + '%';
        }
        return val;
      },
    },
    { name: 'effectiveTime', label: '评级生效日', format: Formater.dateOnly() },
    { name: 'matureTime', label: '评级失效日', format: Formater.dateOnly() },
    { name: 'ratingStatus', label: '评级状态', format: Formater.enum(RatingStatusEnum) },
    { name: 'launchUser', label: '发起人' },
    { name: 'processStatus', label: '流程状态', format: Formater.enum(RatingProcessStatusEnum) },
    { name: 'currentAssignee', label: '当前处理人' },
  ],
};

const refreshTable = () => {
  companyRatingGridRef.value.refresh();
};
</script>
