<template>
  <div>
    <a-card :class="$style.tabclass">
      <a-tabs type="line" class="ant-card sd-has-table">
        <a-tab-pane key="1" tab="用户信息">
          <template v-if="groupId">
            <sd-data-table
              ref="userTable"
              :data-url="`api/framework/v1/user-manager/${groupId}/members`"
              :filter-expressions="expressions"
              :columns="columns"
              :row-key="'id'"
              :search-fields="['addrUser']"
              :process-res="processRes"
              @rowClick="handleUserClick"
            />
          </template>
          <sd-user-info
            ref="signatureDetail"
            :record-id="signatureId"
            page-id="base/signature/oaUserSignature"
            :user="curUserObj"
            @saved="saved"
          />
        </a-tab-pane>
      </a-tabs>
    </a-card>
  </div>
</template>

<script>
import TableColumnTypes from '@/common/services/table-column-types'
import SignaturemanageService from './signaturemanage-service'
import components from './_import-components/sd-user-list-import'

export default {
  name: 'SdUserList',
  components,
  props: {
    groupId: {
      type: String,
      default: '',
    },
    isContainSubOrg: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      expressions: [
        {
          name: 'isContainsSub',
          stringValue: 'y',
        },
      ], // 默认搜索
      columns: [
        {
          title: '姓名',
          dataIndex: 'name',
          sdClickable: true,
        },
        {
          title: '更新时间',
          dataIndex: 'props.signDate',
          sdRender: TableColumnTypes.dateTime,
        },
        {
          title: '状态',
          dataIndex: 'status',
        },
      ],
      signatureId: null,
      curUserObj: {},
      attachmentNum: -1,
    }
  },
  computed: {},
  watch: {
    isContainSubOrg(val) {
      this.$set(
        this.expressions,
        0,
        Object.assign(
          {},
          {
            name: 'isContainsSub',
            stringValue: val ? 'y' : 'n',
          }
        )
      )
    },
  },
  methods: {
    handleUserClick(record, dataIndex, value) {
      if (record) {
        const params = {
          columns: '',
          maxResults: 1, // 用户签名列表仅有一条数据
          startPosition: 0,
          expressions: [
            {
              dataType: 'str',
              name: 'userId',
              op: 'eq',
              stringValue: record.id,
            },
          ],
          formId: 'oaUserSignature',
        }
        SignaturemanageService.getUserSignatureList(params).then((res) => {
          if (res.data.data.length > 0) {
            this.signatureId = res.data.data[0].id
          } else {
            // 该用户无签名
            this.curUserObj = { ...record }
            this.signatureId = null
          }
          this.$nextTick(() => {
            this.$refs.signatureDetail.show()
          })
        })
      }
    },
    processRes(data) {
      const dataBase = data.data
      dataBase.forEach((e) => {
        e.props.signDate = parseInt(e.props.signDate)
        if (e.status === '0') {
          e.status = '未激活'
        } else if (e.status === '1') {
          e.status = '正常'
        } else if (e.status === '2') {
          e.status = '已注销'
        } else if (e.status === '4') {
          e.status = '账号已过期'
        } else if (e.status === '8') {
          e.status = '密码已过期'
        } else if (e.status === '16') {
          e.status = '已被锁定'
        } else if (e.status === '32') {
          e.status = '用户停用'
        } else if (e.status === '64') {
          e.status = '用户禁止登录'
        } else {
          e.status = '未标识的编号'
        }
      })
      return {
        datas: dataBase,
        total: data.totalSize,
      }
    },
    saved() {
      this.$refs.userTable.refresh()
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
.use-sign {
  position: relative;
  :global(.ant-form-item-label),
  :global(.ant-input) {
    visibility: hidden;
  }
  :global(.has-error) {
    position: relative;
    top: -96px;
  }
}
.tabclass {
  :global(.sd-has-table.ant-tabs) {
    .wrapper {
      margin-top: -64px;
    }
  }
}
</style>
