<template>
  <sd-data-table-ex
    ref="commonwordsTable"
    :actions="actions"
    form-id="oaPersonalOpinion"
    page-id="personal/opinion/oaPersonalOpinion"
    data-url="api/framework/v1/opinion-manager/opinion/list"
    show-selection
    :columns="columns"
    :row-key="'id'"
    :search-fields="['opinionContent']"
    :filter-expressions="filterExpressions"
    @formBtnClick="formBtnClick"
  >
    <template v-slot:form>
      <sd-form-item name="opinionContent"></sd-form-item>
      <sd-form-item name="sortNum"></sd-form-item>
      <sd-form-item
        name="opinionType"
        :hidden="true"
        :input-props="{ defaultValue: 0 }"
      ></sd-form-item>
    </template>
  </sd-data-table-ex>
</template>

<script>
import { Modal } from 'ant-design-vue'
import TableColumnTypes from '../common/services/table-column-types'
import TableActionTypes from '../common/services/table-action-types'
import components from './_import-components/sd-commonwords-list-import'
import frequentlyService from './frequently-service'
export default {
  name: 'SdCommonwordsList',
  components,
  data() {
    return {
      filterExpressions: [
        {
          dataType: 'int',
          name: 'opinionType',
          op: 'eq',
          intValue: 0,
        },
      ],
      columns: [
        {
          title: '意见',
          dataIndex: 'opinionContent',
          sdClickable: true, // 列内容是否可点击
        },
        {
          title: '排序号',
          dataIndex: 'sortNum',
          width: '150px',
          defaultSortOrder: 'asc', // 默认的排序列
        },
        {
          title: '最后一次修改时间',
          dataIndex: 'lastModifiedTime',
          defaultSortOrder: 'descend', // 默认排序列，排序号相同时，按照最后一次修改时间降序排列
          sdRender: TableColumnTypes.dateTime,
        },
      ],
      actions: [
        {
          label: '新建',
          id: 'new',
          type: TableActionTypes.ex.create,
        },
        {
          label: '删除',
          id: 'delete',
          type: TableActionTypes.ex.delete,
        },
      ],
      opinionId: null,
    }
  },
  methods: {
    formBtnClick(evt, btn, { form, recordId }) {
      if (btn.buttonId === 'save') {
        const params = {
          id: recordId,
          opinionContent: form.getFieldValue('opinionContent'),
          opinionType: form.getFieldValue('opinionType'),
        }
        evt.waitUntil(
          new Promise((resolve, reject) => {
            frequentlyService.checkPersonalOpinion(params).then((res) => {
              if (!res.status) {
                Modal.confirm({
                  title: '意见已存在，是否重复增加',
                  onOk: () => {
                    // 等待结束，会继续走原有的按钮逻辑
                    resolve()
                  },
                  onCancel: () => {
                    // 等待结束，并禁止了原有的按钮逻辑
                    evt.preventDefault()
                    resolve()
                  },
                })
              } else {
                resolve()
              }
            })
          })
        )
      }
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
</style>
