<template>
  <div style="height: 100%">
    <w-grid
      ref="mainScaleVersionGridRef"
      title="客户评级主标尺版本列表"
      :data-url="Environment.apiContextPath('/api/irbs/mainScaleVersion')"
      :fetch-data-url="Environment.apiContextPath('/api/irbs/mainScaleVersion/query')"
      :sort-no="true"
      :dense="true"
      :checkbox-selection="false"
      :pageable="false"
      :query-form-cols-num="4"
      :query-form-fields="mainScaleVersionGrid.queryFormFields"
      :refresh-data="true"
      db-click-operation="edit"
      :columns="mainScaleVersionGrid.columns"
      :editor="{
        dialog: {
          width: '60%',
        },
        form: {
          colsNum: 2,
          fields: [
            { name: 'code', label: '版本代码', type: 'w-text' },
            { name: 'name', label: '版本名称', type: 'w-text' },
            { name: 'version', label: '版本号', type: 'w-text' },
          ],
        },
      }"
      :toolbar-actions="mainScaleVersionGrid.buttons"
      :sort-by="['-version']"
    ></w-grid>
    <MainScale ref="mainScaleRef" @refresh="refreshTable"></MainScale>
    <MainScaleVersionFlowDialog ref="mainScaleVersionFlowDialogRef" @refresh="refreshTable"></MainScaleVersionFlowDialog>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { useQuasar } from 'quasar';
import { Environment, EnumTools, DictionaryTools, Options, Formater, axios, NotifyManager, SessionManager } from 'platform-core';
import MainScale from './MainScale.vue';
import MainScaleVersionFlowDialog from './MainScaleVersionFlowDialog.vue';

const $q = useQuasar();
const mainScaleVersionGridRef = ref();
const mainScaleRef = ref();
const mainScaleVersionFlowDialogRef = ref();

const fromStatusFormat = (val, row) => {
  if (val === 'RELEASE') {
    return '发布';
  } else if (val === 'DRAFT') {
    return '草稿';
  } else if (val === 'HISTORY') {
    return '历史';
  }
  return '';
};
const fromApprvStatusFormat = (val, row) => {
  if (val === 'AWAIT_SUBMIT') {
    return '待发布';
  } else if (val === 'APPROVALING') {
    return '审批中';
  } else if (val === 'PASS') {
    return '通过';
  } else if (val === 'NEGATIVED') {
    return '否决';
  }
  return '';
};
const statusOptions = [
  { label: '发布', value: 'RELEASE' },
  { label: '草稿', value: 'DRAFT' },
  { label: '历史', value: 'HISTORY' },
];
const apprvStatusOptions = [
  { label: '待发布', value: 'AWAIT_SUBMIT' },
  { label: '审批中', value: 'APPROVALING' },
  { label: '通过', value: 'PASS' },
  { label: '否决', value: 'NEGATIVED' },
];

const mainScaleVersionGrid = {
  buttons: [
    'query',
    'reset',
    'separator',
    'add',
    {
      extend: 'edit',
      enableIf: (args) => {
        if (args.selected && args.selected['status'] === 'DRAFT') {
          return true;
        }
        return false;
      },
    },
    {
      extend: 'view',
      label: '详情',
      click: (args) => {
        mainScaleRef.value.show(args.selected);
      },
    },
    {
      extend: 'clone',
      label: '复制',
      enableIf: (args) => {
        if (args.selected && (args.selected['status'] === 'RELEASE' || args.selected['status'] === 'HISTORY')) {
          return true;
        }
        return false;
      },
      click: (args) => {
        axios
          .post(Environment.apiContextPath('api/irbs/mainScaleVersion/clone/' + args.selected['id']))
          .then((resp) => {
            if (resp && resp.data) {
              mainScaleVersionGridRef.value.refresh();
            }
          })
          .catch((error) => {
            console.info('error====', error);
            NotifyManager.warn('版本复制失败');
          });
      },
    },
    {
      extend: 'remove',
      enableIf: (args) => {
        if (args.selected && args.selected['apprvStatus'] === 'AWAIT_SUBMIT') {
          return true;
        }
        return false;
      },
    },
    'export',
    'separator',
    {
      name: 'release',
      label: '发布',
      enableIf: (args) => {
        if (args.selected && args.selected['apprvStatus'] === 'AWAIT_SUBMIT') {
          return true;
        }
        return false;
      },
      click: (args) => {
        $q.dialog({
          title: '询问',
          message: '是否确认发布？',
          cancel: { noCaps: true },
          ok: { noCaps: true },
          persistent: true,
        }).onOk(() => {
          axios
            .post(Environment.apiContextPath('api/irbs/mainScaleVersion/releaseMainScaleVersion/' + args.selected['id']))
            .then((resp) => {
              if (resp && resp.data) {
                NotifyManager.info('发布流程提交成功');
                mainScaleVersionGridRef.value.refresh();
              }
            })
            .catch((error) => {
              console.info('error====', error);
              NotifyManager.warn('发布流程提交失败');
            });
        });
      },
    },
    {
      name: 'approve',
      label: '审批',
      enableIf: (args) => {
        if (args.selected && args.selected['apprvStatus'] === 'APPROVALING' && args.selected['currentAssignee'] === SessionManager.getUser().loginName) {
          return true;
        }
        return false;
      },
      click: (args) => {
        mainScaleVersionFlowDialogRef.value.show(args.selected);
      },
    },
    'separator',
    {
      name: 'forseStop',
      label: '强制停止',
      click: (args) => {
        axios.post(Environment.apiContextPath('api/irbs/corpfeatureret/forseStop/'));
      },
    },
  ],
  queryFormFields: [
    { label: '版本代码', name: 'code', type: 'w-text' },
    { label: '版本名称', name: 'name', type: 'w-text' },
    { label: '版本状态', name: 'status', type: 'w-select', options: statusOptions },
    { label: '版本号', name: 'version', type: 'w-text' },
    { label: '审核状态', name: 'apprvStatus', type: 'w-select', options: apprvStatusOptions },
  ],
  columns: [
    { name: 'code', label: '版本代码' },
    { name: 'name', label: '版本名称' },
    { name: 'status', label: '版本状态', format: fromStatusFormat },
    { name: 'version', label: '版本号' },
    { name: 'apprvStatus', label: '审核状态', format: fromApprvStatusFormat },
    { name: 'currentAssignee', label: '当前处理人' },
  ],
};
const refreshTable = () => {
  mainScaleVersionGridRef.value.refresh();
};
</script>
