<template>
  <j-modal :title="title" width="70vw" :visible="visible" @ok="handleOk"
    :okButtonProps="{ class: { 'jee-hidden': disableSubmit } }" @cancel="handleCancel" cancelText="关闭"
    :maskClosable="false">
    <template #footer>
      <a-button @click="handleCancel">关闭</a-button>
      <a-button @click="handleOk" v-show="opeType == 'add' || opeType == 'edit' || opeType == 'editMedia'"
        :loading="loading">确定</a-button>
      <!-- <a-button @click="handleSave" v-show="opeType == 'editMedia' || opeType == 'auditMedia'">暂存</a-button>
      <a-button type="primary" @click="handleMediaAsync" v-show="opeType == 'auditMedia'">同步</a-button>
      <a-button type="primary" @click="handleMediaSyncAllPlat" v-show="opeType == 'editMedia'">同步</a-button> -->
    </template>
    <a-spin :spinning="loading">
      <ConfigServiceDirectiveForm ref="registerForm" v-if="visible" @ok="submitCallback" :formDisabled="disableSubmit"
        :formBpm="false"></ConfigServiceDirectiveForm>
    </a-spin>
  </j-modal>
</template>

<script lang="ts" setup>
import { ref, nextTick, defineExpose, defineProps } from 'vue';
import ConfigServiceDirectiveForm from './ConfigServiceDirectiveForm.vue'
import JModal from '/@/components/Modal/src/JModal/JModal.vue';
import { useMessage } from '/@/hooks/web/useMessage';
import { saveOrUpdate, queryById } from '../ConfigServiceDirective.api';

const loading = ref(false)
const { createMessage } = useMessage();
const props = defineProps({
});
const title = ref<string>('');
const visible = ref<boolean>(false);
const disableSubmit = ref<boolean>(false);
const registerForm = ref();
const emit = defineEmits(['register', 'success']);
const opeType = ref('')//操作类型 add新增 edit编辑 editMedia主列表-指令资源 look详情 audit审核 auditMedia审核列表-指令资源

/**
 * 新增
 */
function add() {
  title.value = '新增';
  visible.value = true;
  nextTick(() => {
    registerForm.value.add();
  });
}

/**
 * 编辑
 * @param record
 */
function edit(record) {
  title.value = disableSubmit.value ? '详情' : '编辑';
  visible.value = true;
  nextTick(() => {
    registerForm.value.edit(record);
  });
}

/**
 * 编辑指令资源
 * @param record
 */
function queryAndEditMedia(id) {
  title.value = '编辑指令资源';
  visible.value = true;
  nextTick(() => {
    loading.value = true
    queryById({ id }).then(res => {
      registerForm.value.edit(res, true);
    }).finally(() => {
      loading.value = false
    })
  });
}

/**
 * 编辑指令资源
 * @param record
 */
function editMedia(record) {
  title.value = '编辑指令资源';
  visible.value = true;
  nextTick(() => {
    registerForm.value.edit(record, true);
  });
}

/**
 * 确定按钮点击事件
 */
function handleOk() {
  registerForm.value.submitForm();
}

/**
 * 暂存
 */
function handleSave() {
  registerForm.value.submitForm();
}

/**
 * 给审核对应的业务平台同步资源
 */
function handleMediaAsync() {
  registerForm.value.syncMediaForBizFunc();
}

/**
 * 给所有业务平台同步资源
 */
function handleMediaSyncAllPlat() {
  registerForm.value.syncMediaForAllBizFunc();
}

/**
 * form保存回调事件
 */
function submitCallback() {
  handleCancel();
  emit('success');
}

/**
 * 取消按钮回调事件
 */
function handleCancel() {
  visible.value = false;
}

function usingOrStop(id, izEnabled) {
  let params = {
    id,
    izEnabled
  }
  saveOrUpdate(params, true).then((res) => {
    if (res.success) {
      createMessage.success(res.message);
    } else {
      createMessage.warning(res.message);
    }
    emit('success')
  })
}

function queryByIdFunc(id) {
  loading.value = true
  queryById({ id }).then(res => {
    edit(res)
  }).finally(() => {
    loading.value = false
  })
}

defineExpose({
  add,
  edit,
  editMedia,
  disableSubmit,
  usingOrStop,
  queryByIdFunc,
  opeType,
  queryAndEditMedia,
});
</script>

<style lang="less">
/**隐藏样式-modal确定按钮 */
.jee-hidden {
  display: none !important;
}
</style>
<style lang="less" scoped></style>
