<template>
  <div>
    <sd-data-table-ex
      ref="dataTable1"
      :form-id="formId"
      :page-id="pageId"
      :columns="columns"
      show-selection
      :actions="actions"
      :filter-expressions="expressions"
      :search-fields="['addrGroupName']"
    >
      <template v-slot:form>
        <sd-form-item name="addrGroupName"> </sd-form-item>
        <sd-form-item
          :hidden="true"
          name="addrGroupType"
          :input-props="{ defaultValue: addrGroupType }"
        >
        </sd-form-item>
      </template>
    </sd-data-table-ex>
  </div>
</template>

<script>
import { Message, Modal } from 'ant-design-vue'
import PageService from '@/common/services/page-service'
import storeMixin from '@/common/store-mixin'
import TableActionTypes from '@/common/services/table-action-types'
import AddressbookService from '../addressbook-service'
import components from './_import-components/sd-address-group-setting-import'

export default {
  name: 'SdAddressGroupSetting',
  components,
  mixins: [storeMixin],
  props: {
    addrGroupType: {
      type: [Number, String],
      default: null,
    },
  },
  data() {
    return {
      columns: [
        {
          title: '组名称',
          dataIndex: 'addrGroupName',
          sdClickable: true, // 列内容是否可点击
        },
      ],
      expressions: [
        {
          dataType: 'int',
          name: 'addrGroupType',
          op: 'eq',
          intValue: this.addrGroupType, // 个人通讯录
        },
      ],
      actions: [
        {
          label: '新建',
          id: 'new',
          type: TableActionTypes.ex.create,
        },
        {
          label: '删除',
          id: 'delete',
          type: TableActionTypes.batch,
          permission: 'delete',
          callback: (keys) => {
            return new Promise((resolve) => {
              const expressionsValue = []

              keys.forEach((item) => {
                expressionsValue.push({
                  dataType: 'str',
                  name: 'parentId',
                  op: 'eq',
                  stringValue: item,
                })
              })

              const params = {
                ids: keys.join(),
              }

              AddressbookService.groupIsCanDelete(params).then((res) => {
                if (!res.data) {
                  resolve()
                  Message.warn('要删除的组内有成员，不能删除!')
                  this.$refs.dataTable1.clearSelection(true)
                } else {
                  Modal.confirm({
                    title: '你确定删除这项内容吗？',
                    content: '删除这条数据后，就无法恢复初始的状态。',
                    okText: '删除',
                    okType: 'danger',
                    onOk: () => {
                      PageService.delete(params, 'oaBaseAddrGroup').then((res) => {
                        this.$refs.dataTable1.clearSelection(true)
                        this.$refs.dataTable1.refresh()
                        resolve()
                      })
                    },
                    onCancel: () => {
                      resolve()
                    },
                  })
                }
              })
            })
          },
        },
      ],
      formId: 'oaBaseAddrGroup',
      pageId: 'base/address/oaBaseAddrGroup',
    }
  },
  created() {
    // 添加个人通讯录设置个人组过滤
    if (this.addrGroupType === '0') {
      this.expressions.push({
        dataType: 'long',
        name: 'creatorId',
        op: 'eq',
        longValue: this.userInfo.id,
      })
    }
  },
  methods: {},
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
.editable-add-btn {
  width: 100%;
  margin: 15px auto 0;
  text-align: center;
}
</style>
