<template>
  <div :class="$style.wrap">
    <div :class="$style.rightcontent">
      <a-card :class="$style['search-result']">
        <sd-data-table
          ref="dataTable"
          :data-url="listDataUrl"
          :actions="actions"
          :columns="columns"
          show-selection
          :process-req="processReq"
          @rowClick="rowClick"
        >
          <template slot="randerFirstLoginChangePwd" slot-scope="text, record">
            {{ record.firstLoginChangePwd === true ? '是' : '否' }}
          </template>
        </sd-data-table>
      </a-card>
    </div>

    <sd-workday
      ref="detailForm"
      :unid="clickedRow"
      @feedback="
        (res) => {
          if (res) {
            refresh()
          }
        }
      "
    ></sd-workday>
  </div>
</template>

<script>
import { Modal } from '@/common/one-ui'
import { message } from 'ant-design-vue'
import TableActionTypes from '@/common/services/table-action-types'

import WorkdayService from './workday-service'
import components from './_import-components/sd-workday-list-import'

export default {
  name: 'SdWorkdayList',
  metaInfo: {
    title: '工作日历',
  },
  components,
  data() {
    return {
      columns: [
        {
          title: '工作日历编码',
          dataIndex: 'calendarCode',
          sdClickable: true,
        },
        {
          title: '工作日历名称',
          dataIndex: 'calendarName',
          sdClickable: true,
        },
        {
          title: '上下班时间',
          dataIndex: 'workTimeScope',
          sdClickable: true,
        },
        {
          title: '所属机构编码',
          dataIndex: 'orgCode',
          sdClickable: true,
        },

        {
          title: '所属机构名称',
          dataIndex: 'orgName',
          sdClickable: true,
        },
        {
          title: '包含年度',
          dataIndex: 'includeYears',
          sdClickable: true,
        },
        {
          title: '创建时间',
          dataIndex: 'creationTime',
          sdHidden: true,
          defaultSortOrder: 'desc',
        },
      ],

      actions: [
        {
          label: '新建',
          id: 'new',
          permission: null,
          type: TableActionTypes.primary,
          callback: () => {
            this.$refs.detailForm.show('新建日历', null)
          },
        },
        {
          label: '删除',
          id: 'delete',
          permission: null,
          type: TableActionTypes.batch,
          callback: () => {
            this.delete(this.$refs.dataTable.getSelectedRowKeys().join(','))
          },
        },
      ],
      listDataUrl: `api/framework/v1/workcalendar/list`,
      clickedRow: null,
      visible: false,
      loadingMsg: null,
    }
  },

  methods: {
    processReq(req) {
      req.params = {
        startPosition: req.data.startPosition,
        maxResults: req.data.maxResults,
        orderBy: req.data.orderBy,
      }

      return req
    },
    rowClick(record) {
      this.$refs.detailForm.show(
        '编辑日历',
        record.id.toString(),
        record.includeYears.split(',').pop()
      )
    },
    delete(keys) {
      return new Promise((resolve) => {
        Modal.confirm({
          title: '您确定删除这项内容吗？',
          content: '删除这条数据后，就无法恢复初始的状态。',
          okText: '删除',
          okType: 'danger',
          onOk: () => {
            this.loadingMsg = message.loading({
              key: this.msgKey,
              content: '处理中...',
              duration: 0,
            })
            WorkdayService.deleteDocs(keys)
              .then((res) => {
                if (res.status === 200) {
                  this.$refs.dataTable.clearSelection(true)
                  this.$refs.dataTable.refresh()

                  message.success('处理成功')
                } else {
                  message.error(res.data?.message || '处理失败')
                }
              })
              .catch(() => message.error('处理失败'))
              .finally(() => {
                this.loadingMsg?.()
                resolve()
              })
          },
          onCancel: () => {
            resolve()
          },
        })
      })
    },

    refresh() {
      this.$refs.dataTable.clearSelection(true)
      this.$refs.dataTable.refresh()
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
.wrap {
  display: flex;
  height: 100%;

  .rightcontent {
    width: 100%;
    height: 100%;
    overflow-y: auto;
    // :global(.ant-card) {
    //   min-height: 100%;
    // }
  }
}
.no-action {
  :global(.ant-modal-footer) {
    display: none;
  }
}
.modal {
  :global(.ant-modal) {
    // 覆盖style标签里面的样式
    /* stylelint-disable-next-line  */
    top: 0 !important;
    /* stylelint-disable-next-line  */
    width: 100% !important;
    /* stylelint-disable-next-line  */
    height: 100% !important;
  }
  :global(.ant-modal-body) {
    height: 100vh;
    max-height: 100vh;
    padding: 0;
  }
}
</style>
