<template>
  <h-row>
    <h-col>
      <div class="title">{{ t('tenant.tenantManagement.deleteTenant.delteTitle') }}</div>
    </h-col>
  </h-row>
  <h-row class="delete-tip">
    <h-col>
      <div class="tip">{{ t('tenant.tenantManagement.deleteTenant.deleteTip') }}</div>
    </h-col>
  </h-row>
  <h-row>
    <h-col :span="24" style="text-align: right">
      <h-button style="margin: 0 8px" @click="cancelClick">
        {{ t('tenant.common.cancel') }}
      </h-button>
      <h-button type="primary" :loading="btnLoading" @click.prevent="okClick">
        {{ t('tenant.common.ok') }}
      </h-button>
    </h-col>
  </h-row>
</template>

<script lang="ts">
import { defineComponent, reactive, ref } from 'vue';
import { useHttp } from '@crami/http';
import { message } from 'ant-design-vue';
import { HModal } from '@crami/ui';
import { useI18n } from '@crami/locale';

/**控件对外数据*/
const formProps = {
  componentProps: {
    type: Object,
  },
};
export default defineComponent({
  name: 'DeleteRenant',
  props: formProps,
  setup: (props, { emit }) => {
    const { t } = useI18n();
    const initEids = props.componentProps.params.getDataParams.ObjectID || [];
    const btnLoading = ref(false);
    const tipData = reactive({
      text: t('tenant.tenantManagement.deleteTenant.deleteTip'),
      title: 'title',
    });

    const dataDefault = reactive({
      items: [
        {
          name: 'cancel',
          text: t('tenant.common.cancel'),
        },
        {
          name: 'submit',
          text: t('tenant.common.ok'),
          options: {
            style: { border: 0, 'background-color': '#4A556B', color: '#fff' },
          },
        },
      ],
    });

    const submitEvent = async (params: any) => {
      btnLoading.value = true;
      const data = await useHttp({
        url: '/api/system/tenant/delete',
        method: 'post',
        params: params,
      });
      btnLoading.value = false;
      emit('end', { code: 200, success: true, msg: data?.msg });
    };
    function getResultInArr(arr, status) {
      let flag = true;
      for (let i = 0; i < arr.length; i++) {
        if (status != arr[i].AUDITSTATE) {
          return (flag = false);
        }
      }
      return flag;
    }
    const cancelClick = () => {
      // emit('end', { success: false, msg: '取消' });
      HModal.destroyAll();
    };
    const okClick = () => {
      //循环校验数据
      let result = getResultInArr(initEids, '停用');
      if (!result) {
        message.warning({
          content: t('tenant.tenantManagement.deleteTenant.deleteWarn'),
        });
        return;
      }
      let delArr = [];
      initEids.forEach(element => {
        delArr.push(element.EID);
      });
      submitEvent({ eidstr: delArr });
    };

    return {
      t,
      tipData,
      dataDefault,
      btnLoading,

      cancelClick,
      okClick,
    };
  },
});
</script>

<style lang="less" scoped>
.title {
  color: #333333;
  font-weight: bold;
  font-size: 16px;
}
.delete-tip {
  margin-top: 10px;
  .tip {
    color: #333333;
    font-size: 14px;
  }
}
.button-box {
  position: relative;
  height: 50px;
}
.button-submit {
  margin-top: 25px;
  width: 140px;
  position: absolute;
  right: 0;
  bottom: 0;
}
</style>
