<script setup>
import { h, onMounted, ref, resolveDirective, withDirectives } from 'vue'
import {
  NButton,
  NForm,
  NFormItem,
  NInput,
  NInputNumber,
  NSpace,
  NTag,
  NPopconfirm,
} from 'naive-ui'

import CommonPage from '@/components/page/CommonPage.vue'
import QueryBarItem from '@/components/query-bar/QueryBarItem.vue'
import CrudModal from '@/components/table/CrudModal.vue'
import CrudTable from '@/components/table/CrudTable.vue'

import { formatDate, renderIcon } from '@/utils'
import { useCRUD } from '@/composables'
import api from '@/api'
import TheIcon from '@/components/icon/TheIcon.vue'

defineOptions({ name: '行业修正' })

const $table = ref(null)
const queryItems = ref({})
const vPermission = resolveDirective('permission')

const {
  modalVisible,
  modalTitle,
  modalAction,
  modalLoading,
  handleSave,
  modalForm,
  modalFormRef,
  handleEdit,
  handleDelete,
  handleAdd,
} = useCRUD({
  name: '行业',
  initForm: { code: '', name: '', roe: 0, fix_num: 0, remark: '' },
  doCreate: api.createIndustry,
  doUpdate: api.updateIndustry,
  doDelete: api.deleteIndustry,
  refresh: () => $table.value?.handleSearch(),
})

onMounted(() => {
  $table.value?.handleSearch()
})

const columns = [
  {
    title: '行业代码',
    key: 'code',
    width: 120,
    align: 'center',
    ellipsis: { tooltip: true },
  },
  {
    title: '行业名称',
    key: 'name',
    width: 200,
    align: 'center',
    ellipsis: { tooltip: true },
  },
  {
    title: 'ROE',
    key: 'roe',
    width: 100,
    align: 'center',
    render(row) {
      return h(
        NTag,
        { type: 'info' },
        { default: () => `${row.roe}%` }
      )
    },
  },
  {
    title: '修正数值',
    key: 'fix_num',
    width: 120,
    align: 'center',
    render(row) {
      return h(
        NTag,
        { type: row.fix_num > 0 ? 'success' : row.fix_num < 0 ? 'error' : 'default' },
        { default: () => row.fix_num }
      )
    },
  },
  {
    title: '备注',
    key: 'remark',
    width: 200,
    align: 'center',
    ellipsis: { tooltip: true },
  },
  {
    title: '创建时间',
    key: 'created_at',
    align: 'center',
    width: 180,
    render(row) {
      return h(
        NButton,
        { size: 'small', type: 'text', ghost: true },
        {
          default: () => formatDate(row.created_at),
          icon: renderIcon('mdi:update', { size: 16 }),
        }
      )
    },
  },
  {
    title: '操作',
    key: 'actions',
    width: 240,
    align: 'center',
    fixed: 'right',
    hideInExcel: true,
    render(row) {
      return [
        withDirectives(
          h(
            NButton,
            {
              size: 'small',
              type: 'primary',
              secondary: true,
              onClick: () => handleEdit(row),
            },
            { default: () => '编辑', icon: renderIcon('material-symbols:edit-outline', { size: 16 }) }
          ),
          [[vPermission, 'post/api/v1/industry/update']]
        ),

        withDirectives(
          h(
            NPopconfirm,
            {
              onPositiveClick: () => handleDelete([row.id], false),
            },
            {
              default: () => '确认删除',
              trigger: () =>
                h(
                  NButton,
                  { size: 'small', type: 'error', style: 'margin-left: 12px;' },
                  { default: () => '删除', icon: renderIcon('material-symbols:delete-outline', { size: 16 }) }
                ),
            }
          ),
          [[vPermission, 'delete/api/v1/industry/delete']]
        ),
      ]
    },
  },
]

const modalRules = {
  code: [
    {
      required: true,
      message: '请输入行业代码',
      trigger: ['input', 'blur'],
    },
  ],
  name: [
    {
      required: true,
      message: '请输入行业名称',
      trigger: ['input', 'blur'],
    },
  ],
  roe: [
    {
      required: true,
      type: 'number',
      message: '请输入ROE值',
      trigger: ['input', 'blur'],
    },
  ],
  fix_num: [
    {
      required: true,
      type: 'number',
      message: '请输入修正数值',
      trigger: ['input', 'blur'],
    },
  ],
}
</script>

<template>
  <CommonPage show-footer title="行业修正">
    <template #action>
      <div>
        <NButton
          v-permission="'post/api/v1/industry/create'"
          class="float-right mb-8"
          type="primary"
          @click="handleAdd"
        >
          <TheIcon icon="material-symbols:add" :size="18" class="mr-5" />新建行业
        </NButton>
      </div>
    </template>

    <CrudTable
      ref="$table"
      v-model:query-items="queryItems"
      :extra-params="{ ordering: 'id' }"
      :scroll-x="1200"
      :columns="columns"
      :get-data="api.getIndustryList"
    >
      <template #queryBar>
        <QueryBarItem label="行业代码" :label-width="80">
          <NInput
            v-model:value="queryItems.code"
            type="text"
            placeholder="请输入行业代码"
            @keydown.enter="$table?.handleSearch"
          />
        </QueryBarItem>
        <QueryBarItem label="行业名称" :label-width="80">
          <NInput
            v-model:value="queryItems.name"
            type="text"
            placeholder="请输入行业名称"
            @keydown.enter="$table?.handleSearch"
          />
        </QueryBarItem>
      </template>
    </CrudTable>

    <!-- 新增/编辑弹窗 -->
    <CrudModal
      v-model:visible="modalVisible"
      :title="modalTitle"
      :loading="modalLoading"
      :show-footer="true"
      @save="handleSave"
    >
      <NForm
        ref="modalFormRef"
        label-placement="left"
        label-align="left"
        :label-width="80"
        :model="modalForm"
        :rules="modalRules"
      >
        <NFormItem label="行业代码" path="code">
          <NInput v-model:value="modalForm.code" placeholder="请输入行业代码" />
        </NFormItem>
        <NFormItem label="行业名称" path="name">
          <NInput v-model:value="modalForm.name" placeholder="请输入行业名称" />
        </NFormItem>
        <NFormItem label="ROE" path="roe">
          <NInputNumber v-model:value="modalForm.roe" placeholder="请输入ROE值" :precision="2" :step="0.01" />
        </NFormItem>
        <NFormItem label="修正数值" path="fix_num">
          <NInputNumber v-model:value="modalForm.fix_num" placeholder="请输入修正数值" :precision="2" :step="0.01" />
        </NFormItem>
        <NFormItem label="备注" path="remark">
          <NInput v-model:value="modalForm.remark" type="textarea" placeholder="请输入备注" />
        </NFormItem>
      </NForm>
    </CrudModal>
  </CommonPage>
</template>