<template>
  <a-spin :spinning="confirmLoading">
    <div v-show="!isEditMedia"
      style="padding-top: 14px;padding-left: 14px;padding-right: 14px; background-color: #fff;border-radius: 10px;box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);margin-bottom: 14px;">
      <JFormContainer :disabled="disabled">
        <template #detail>
          <a-form ref="formRef" :labelCol="labelCol" :wrapperCol="wrapperCol" :colon="false"
            name="ConfigService2DirectiveForm" style="padding: 0px 0px;">
            <a-row v-show="!isEditMedia">
              <a-col :span="8">
                <a-form-item label="分类标签" v-bind="validateInfos.instructionTagId"
                  id="ConfigServiceDirectiveForm-instructionTagId" name="instructionTagId">
                  <j-dict-select-tag v-model:value="formData.instructionTagId" :orgCode="mainOrgCode"
                    :dictCode="`nu_config_service_instruction_tag,instruction_name,id,del_flag = 0 and iz_enabled = 'Y' order by sort asc`"
                    placeholder="请选择分类标签" allowClear @upDictCode="upInstructionDictCode" disabled />
                </a-form-item>
              </a-col>
              <a-col :span="8">
                <a-form-item label="服务类别" v-bind="validateInfos.categoryId" id="ConfigServiceDirectiveForm-categoryId"
                  name="categoryId">
                  <j-dict-select-tag type="list" v-model:value="formData.categoryId" disabled :orgCode="mainOrgCode"
                    :dictCode="categoryDictCode" placeholder="请选择服务类别" allow-clear @upDictCode="upCategoryDictCode" />
                </a-form-item>
              </a-col>
              <a-col :span="8">
                <a-form-item label="服务类型" v-bind="validateInfos.typeId" id="ConfigServiceDirectiveForm-typeId"
                  name="typeId">
                  <j-dict-select-tag type="list" v-model:value="formData.typeId" :dictCode="typeDictCode"
                    :orgCode="mainOrgCode" disabled placeholder="请选择服务类型" allowClear @upDictCode="upTypeDictCode" />
                </a-form-item>
              </a-col>
            </a-row>
          </a-form>
        </template>
      </JFormContainer>
    </div>
    <div style="padding: 14px;background-color: #fff;border-radius: 10px;box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);">
      <JFormContainer>
        <template #detail>
          <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol" :colon="false"
            name="ConfigService2DirectiveForm" style="padding: 20px 0px;">
            <a-row v-show="!isEditMedia">
              <!-- <a-col :span="12" v-show="false">
                <a-form-item label="分类标签" v-bind="validateInfos.instructionTagId"
                  id="ConfigServiceDirectiveForm-instructionTagId" name="instructionTagId">
                  <j-dict-select-tag v-model:value="formData.instructionTagId" :orgCode="mainOrgCode"
                    :dictCode="`nu_config_service_instruction_tag,instruction_name,id,del_flag = 0 and iz_enabled = 'Y' order by sort asc`"
                    placeholder="请选择分类标签" allowClear @upDictCode="upInstructionDictCode" :disabled="!!formData.id" />
                </a-form-item>
              </a-col>
              <a-col :span="12" v-show="false">
                <a-form-item label="服务类别" v-bind="validateInfos.categoryId" id="ConfigServiceDirectiveForm-categoryId"
                  name="categoryId">
                  <j-dict-select-tag type="list" v-model:value="formData.categoryId" :disabled="!!formData.id"
                    :orgCode="mainOrgCode" :dictCode="categoryDictCode" placeholder="请选择服务类别" allow-clear
                    @upDictCode="upCategoryDictCode" />
                </a-form-item>
              </a-col>
              <a-col :span="12" v-show="false">
                <a-form-item label="服务类型" v-bind="validateInfos.typeId" id="ConfigServiceDirectiveForm-typeId"
                  name="typeId">
                  <j-dict-select-tag type="list" v-model:value="formData.typeId" :dictCode="typeDictCode"
                    :orgCode="mainOrgCode" :disabled="!!formData.id" placeholder="请选择服务类型" allowClear
                    @upDictCode="upTypeDictCode" />
                </a-form-item>
              </a-col> -->
              <a-col :span="12">
                <a-form-item label="服务指令名称" v-bind="validateInfos.directiveName"
                  id="ConfigServiceDirectiveForm-directiveName" name="directiveName">
                  <a-input v-model:value="formData.directiveName" placeholder="请输入服务指令名称" allow-clear :maxlength="20"
                    :showCount="true" :disabled="!!formData.id"></a-input>
                </a-form-item>
              </a-col>
              <a-col :span="12">
                <a-form-item label="服务属性" v-bind="validateInfos.serviceAttribute"
                  id="ConfigServiceDirectiveForm-serviceAttribute" name="serviceAttribute">
                  <j-dict-select-tag v-model:value="formData.serviceAttribute" dictCode="service_attribute"
                    placeholder="请选择服务属性" allow-clear :disabled="disabled" />
                </a-form-item>
              </a-col>
              <a-col :span="12">
                <a-form-item label="收费价格(元)" v-bind="validateInfos.tollPrice" id="ConfigServiceDirectiveForm-tollPrice"
                  name="tollPrice">
                  <a-input-number v-model:value="formData.tollPrice" placeholder="请输入收费价格" style="width: 100%" :min="0"
                    :max="99999.99" :precision="2" @keydown="onPriceKeydown" :disabled="disabled" />
                </a-form-item>
              </a-col>
              <a-col :span="12">
                <a-form-item label="服务时长" v-bind="validateInfos.serviceDuration"
                  id="ConfigServiceDirectiveForm-serviceDuration" name="serviceDuration">
                  <a-input-number v-model:value="formData.serviceDuration" :min="0" :max="55" :step="5" addon-after="分钟"
                    placeholder="请输入服务时长（分钟）" allow-clear @keydown="onDurationKeydown" :disabled="disabled" />
                </a-form-item>
              </a-col>
              <!-- <a-col :span="12">
                <a-form-item label="指令类型" v-bind="validateInfos.cycleType" id="ConfigServiceDirectiveForm-cycleType"
                  name="cycleType">
                  <j-dict-select-tag type="list" v-model:value="formData.cycleType" dictCode="period_type"
                    placeholder="请选择指令类型" allowClear @upDictCode="upCycleTypeDictCode" :disabled="!!formData.id" />
                </a-form-item>
              </a-col> -->
              <!-- <a-col :span="12">
                <a-form-item label="超时时长" v-bind="validateInfos.timeoutDuration"
                  id="ConfigServiceDirectiveForm-timeoutDuration" name="timeoutDuration">
                  <a-input-number v-model:value="formData.timeoutDuration" :min="0" :max="55" :step="5" addon-after="分钟"
                    placeholder="请输入超时时长（分钟）" allow-clear @keydown="onDurationKeydown" :disabled="disabled" />
                </a-form-item>
              </a-col> -->
              <a-col :span="12">
                <a-form-item label="提成价格(元)" v-bind="validateInfos.comPrice" id="ConfigServiceDirectiveForm-comPrice"
                  name="comPrice">
                  <a-input-number v-model:value="formData.comPrice" placeholder="请输入提成价格" style="width: 100%" :min="0"
                    :max="99999.99" :precision="2" @keydown="onPriceKeydown" :disabled="disabled" />
                </a-form-item>
              </a-col>
              <a-col :span="12">
                <a-form-item label="是否启用" v-bind="validateInfos.izEnabled" id="ConfigServiceDirectiveForm-izEnabled"
                  name="izEnabled">
                  <j-dict-select-tag type='radio' v-model:value="formData.izEnabled" dictCode="iz_enabled"
                    placeholder="请选择是否启用"  allowClear :disabled="disabled" />
                </a-form-item>
              </a-col>
              <a-col :span="12">
                <a-form-item label="默认指令图标" v-bind="validateInfos.immediateFile" id="ElderTagForm-immediateFile"
                  name="immediateFile">
                  <JImageUploadToOpe v-if="opeType == 'dmlook'" :fileMax="1"
                    :value="opeMediaAddress + formData.immediateFile">
                  </JImageUploadToOpe>
                  <JImageUploadToOpe v-else :toOpe="true" :bizPath="formComputedData.mediaFileSavePath" :fileMax="1"
                    v-model:value="formData.immediateFile" :disabled="disabled">
                  </JImageUploadToOpe>
                </a-form-item>
              </a-col>
              <a-col :span="12">
                <a-form-item label="焦点指令图标" v-bind="validateInfos.immediateFileFocus"
                  id="ElderTagForm-immediateFileFocus" name="immediateFileFocus">
                  <JImageUploadToOpe v-if="opeType == 'dmlook'" :fileMax="1"
                    :value="opeMediaAddress + formData.immediateFileFocus">
                  </JImageUploadToOpe>
                  <JImageUploadToOpe v-else :toOpe="true" :bizPath="formComputedData.mediaFileSavePath" :fileMax="1"
                    v-model:value="formData.immediateFileFocus" :disabled="disabled">
                  </JImageUploadToOpe>
                </a-form-item>
              </a-col>
              <a-col :span="24">
                <a-form-item label="服务指令描述" v-bind="validateInfos.serviceContent"
                  id="ConfigServiceDirectiveForm-serviceContent" :labelCol="labelCol2" :wrapperCol="wrapperCol2"
                  name="serviceContent">
                  <a-textarea v-model:value="formData.serviceContent" placeholder="请输入服务指令描述" :maxlength="200" :rows="3"
                    :autosize="{ minRows: 3 }" :showCount="true" :disabled="disabled || (!isMain && !!formData.id)" />
                </a-form-item>
              </a-col>
            </a-row>
            <a-row style="padding: 20px;">
              <a-col :span="6">
                <DirectiveRadioCom :directiveMediaBtnValue="directiveMediaBtnValue" @change="mediaBtnChanged">
                </DirectiveRadioCom>
              </a-col>
            </a-row>
            <a-row style="padding: 0px 20px;">
              <a-col :span="24" v-show="directiveMediaBtnValue == 0">
                <JImageUploadtz v-if="opeType == 'dmlook'"
                  :value="!!formData.previewFile ? formData.previewFile : defaultPrePic" maxCount="1"
                  :disabled="disabled" />
                <JImageUploadtz v-else v-model:value="formData.previewFile" maxCount="1" :toOpe="true"
                  :bizPath="formComputedData.mediaFileSavePath" />
              </a-col>
              <!-- <a-col :span="24" v-show="directiveMediaBtnValue == 1">
                <JImageUploadtz v-if="opeType == 'dmlook'"
                  :value="!!formData.previewFileSmall ? formData.previewFileSmall : defaultPreSmallPic" maxCount="1" />
                <JImageUploadtz v-else v-model:value="formData.previewFileSmall" maxCount="1" :toOpe="true"
                  :bizPath="formComputedData.mediaFileSavePath" />
              </a-col> -->
              <a-col :span="24" v-show="directiveMediaBtnValue == 1">
                <JUploadMP3 v-if="opeType == 'dmlook'" :value="formData.mp3File" maxCount="1" />
                <JUploadMP3 v-else v-model:value="formData.mp3File" :toOpe="true"
                  :bizPath="formComputedData.mediaFileSavePath" maxCount="1" />
              </a-col>
              <a-col :span="24" v-show="directiveMediaBtnValue == 2">
                <JUploadMP4 v-if="opeType == 'dmlook'" :value="formData.mp4File" maxCount="1" fileType="mp4" />
                <JUploadMP4 v-else v-model:value="formData.mp4File" :toOpe="true"
                  :bizPath="formComputedData.mediaFileSavePath" maxCount="1" fileType="mp4" />
              </a-col>
              <!-- <a-col :span="24" v-show="directiveMediaBtnValue == 3">
                <JImageUploadtz v-if="opeType == 'dmlook'"
                  :value="!!formData.immediateFile ? formData.immediateFile : defaultImmediatePic" maxCount="1" />
                <JImageUploadtz v-else v-model:value="formData.immediateFile" :toOpe="true"
                  :bizPath="formComputedData.mediaFileSavePath" maxCount="1" />
              </a-col>
              <a-col :span="24" v-show="directiveMediaBtnValue == 4">
                <JImageUploadtz v-if="opeType == 'dmlook'"
                  :value="!!formData.immediateFileFocus ? formData.immediateFileFocus : defaultImmediatePic"
                  maxCount="1" />
                <JImageUploadtz v-else v-model:value="formData.immediateFileFocus" :toOpe="true"
                  :bizPath="formComputedData.mediaFileSavePath" maxCount="1" />
              </a-col> -->
            </a-row>
            <!-- <a-row style="padding: 20px;">
              <a-col :span="2">
                <div class="description-container">
                  <a-button @click="toggleDescription">服务指令描述</a-button>
                  <div v-show="showDescription" class="description-box">
                    <div class="box-header">
                      <span class="title">服务指令描述</span>
                      <span class="collapse-icon" @click="toggleDescription">
                        <DownOutlined style="margin-right: 3px;" />收起
                      </span>
                    </div>
                    <div>
                      <
                    </div>
                  </div>
                </div>
              </a-col>
            </a-row> -->
          </a-form>
        </template>
      </JFormContainer>
    </div>
  </a-spin>
</template>

<script lang="ts" setup>
import { ref, reactive, defineExpose, nextTick, defineProps, computed, onMounted, watch } from 'vue';
import { defHttp } from '/@/utils/http/axios';
import { useMessage } from '/@/hooks/web/useMessage';
import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
import { JCheckbox } from '/@/components/Form';
import JUploadMP3 from '../../../../components/Form/src/jeecg/components/JUpload/JUploadMP3.vue';
import JUploadMP4 from '../../../../components/Form/src/jeecg/components/JUpload/JUploadMP4.vue';
import JImageUploadtz from '/@/components/Form/src/jeecg/components/JImageUploadtz.vue';
import JImageUpload from '/@/components/Form/src/jeecg/components/JImageUpload.vue';
import { getValueType } from '/@/utils';
import { saveOrUpdate, syncMediaForBiz, syncMediaForAllBiz } from '../ConfigServiceDirective.api';
import { Form } from 'ant-design-vue';
import JFormContainer from '/@/components/Form/src/container/JFormContainer.vue';
import { getFileAccessHttpUrl } from '/@/utils/common/compUtils';
import { env } from 'process';
import DirectiveRadioCom from './DirectiveRadioCom.vue'
import { QuestionCircleOutlined } from '@ant-design/icons-vue';
import { DownOutlined } from '@ant-design/icons-vue';
import { uploadUrl } from '/@/api/common/api';
import { queryUpBizPrefix } from '/@/api/common/api'
import JImageUploadToOpe from '/@/components/Form/src/jeecg/components/JImageUploadToOpe.vue';

setInterval(() => {
  console.log("🌊 ~ formData:", formData)
}, 2000)
const opeMediaAddress = import.meta.env.VITE_OPE_MEDIA_ADDRESS
const showDescription = ref(false);

// 切换悬浮容器的显示状态
const toggleDescription = () => {
  showDescription.value = !showDescription.value;
};
const defaultPrePic = import.meta.env.VITE_DEFAULT_DIRECTIVE_PRE_PIC
const defaultPreSmallPic = import.meta.env.VITE_DEFAULT_DIRECTIVE_PRESMALL_PIC
const defaultImmediatePic = import.meta.env.VITE_DEFAULT_DIRECTIVE_IM_PIC
const fileList = ref([])
const upBizPrefix = ref('')
const onPriceKeydown = (e: KeyboardEvent) => {
  const key = e.key;
  // 放行控制键
  if (['Backspace', 'Delete', 'ArrowLeft', 'ArrowRight', 'Tab'].includes(key)) return;
  // 只能输数字和点
  if (!/[\d.]/.test(key)) {
    e.preventDefault();
    return;
  }
  const input = e.target as HTMLInputElement;
  const { value, selectionStart: s, selectionEnd: t } = input;
  const next = value.slice(0, s!) + key + value.slice(t!);
  // 整数最多5位，小数最多2位
  if (!/^\d{0,5}(?:\.\d{0,2})?$/.test(next)) {
    e.preventDefault();
  }
};

const onDurationKeydown = (e: KeyboardEvent) => {
  const key = e.key;
  // 放行控制键
  if (['Backspace', 'Delete', 'ArrowLeft', 'ArrowRight', 'Tab'].includes(key)) return;
  // 只能输数字和点
  if (!/[\d.]/.test(key)) {
    e.preventDefault();
    return;
  }
  const input = e.target as HTMLInputElement;
  const { value, selectionStart: s, selectionEnd: t } = input;
  const next = value.slice(0, s!) + key + value.slice(t!);
  // 整数最多5位，小数最多2位
  if (!/^\d{0,2}$/.test(next)) {
    e.preventDefault();
  }
};

const props = defineProps({
  formDisabled: { type: Boolean, default: false },
  formData: { type: Object, default: () => ({}) },
  formBpm: { type: Boolean, default: true },
  mainOrgCode: '',
  mediaApiAddress: '',//指令资源请求地址
  opeType: 'look',
  isMain: false,//是否主指令库
});
const checkMp3 = (file) => {
  const isPDF = file.type === 'application/mp3' || file.name.endsWith('.mp3');
  if (!isPDF) {
    createMessage.error('只能上传 mp3 文件！');
    return false; // 阻止上传
  }
  return true;
};
const checkMp4 = (file) => {
  const isPDF = file.type === 'application/mp4' || file.name.endsWith('.mp4');
  if (!isPDF) {
    createMessage.error('只能上传 mp4 文件！');
    return false; // 阻止上传
  }
  return true;
};
const bodyTagDictCode = ref(`nu_config_body_tag,tag_name,id,del_flag = 0 and iz_enabled = 0 order by sort asc`)
const emotionTagDictCode = ref(`nu_config_emotion_tag,tag_name,id,del_flag = 0 and iz_enabled = 0 order by sort asc`)
const formRef = ref();
const useForm = Form.useForm;
const emit = defineEmits(['register', 'ok']);
const formData = reactive<Record<string, any>>({
  id: '',
  categoryId: '',
  typeId: '',
  instructionTagId: '',
  directiveName: '',
  serviceAttribute: '',
  tollPrice: 0,
  comPrice: 0,
  izReimbursement: 'N',
  izPreferential: 'N',
  chargingFrequency: '',
  cycleType: '',
  sort: 99,
  serviceContent: '',
  serviceDuration: '5',
  timeoutDuration: '0',
  status: '',
  izEnabled: 'Y',
  createBy: '',
  createTime: '',
  updateBy: '',
  updateTime: '',
  mp3File: '',
  mp4File: '',
  previewFile: defaultPrePic,
  previewFileSmall: defaultPreSmallPic,
  immediateFile: defaultImmediatePic,
  immediateFileFocus: defaultImmediatePic,
});
const { createMessage } = useMessage();
const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 6 } });
const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
const labelCol2 = ref<any>({ xs: { span: 24 }, sm: { span: 3 } });
const wrapperCol2 = ref<any>({ xs: { span: 24 }, sm: { span: 17 } });
const confirmLoading = ref<boolean>(false);
const isEditMedia = ref(false)
const instructionTagName = ref('')
const categoryName = ref('')
const typeName = ref('')
const cycleTypeName = ref('')
const orgMediaPathAddress = ref('')//对应机构媒体资源静态访问路径
//表单验证
const validatorRules = reactive({
  categoryId: [{ required: true, message: '请选择服务类别!' },],
  typeId: [{ required: true, message: '请选择服务类型!' },],
  instructionTagId: [{ required: true, message: '请选择分类标签!' },],
  directiveName: [{ required: true, message: '请输入服务指令名称!' },],
  serviceAttribute: [{ required: true, message: '请选择服务属性!' },],
  tollPrice: [{ required: true, message: '请输入收费价格!' }, { pattern: /^(([0-9]*)|([0]\.\d{0,4}|[1-9][0-9]*\.\d{0,4}))$/, message: '请输入正确的金额!' },],
  comPrice: [{ required: false }, { pattern: /^(([0-9]*)|([0]\.\d{0,4}|[1-9][0-9]*\.\d{0,4}))$/, message: '请输入正确的金额!' },],
  // izReimbursement: [{ required: true, message: '请选择是否参与医保报销!' },],
  // izPreferential: [{ required: true, message: '请选择是否参与机构优惠!' },],
  // chargingFrequency: [{ required: true, message: '请选择收费频次!' },],
  // cycleType: [{ required: true, message: '请选择指令类型!' },],
  // sort: [{ required: true, message: '请输入排序!' }, { pattern: /^\d+$/, message: '请输入正整数!' },],
  serviceDuration: [
    { required: true, message: '请输入服务时长（分钟）!' },
    { pattern: /^\d+$/, message: '请输入正整数!' },
    {
      validator: (_, value) => {
        if (value % 5 !== 0) {
          return Promise.reject('请输入5的倍数!');
        }
        return Promise.resolve();
      },
    },
    {
      validator: (_, value) => {
        if (value < 0 || value > 55) {
          return Promise.reject('请输入0到55之间的值!');
        }
        return Promise.resolve();
      },
    },
  ],
  izEnabled: [{ required: true, message: '请选择是否启用!' },],
});

const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: false });
// 表单禁用
const disabled = computed(() => {
  if (props.formBpm === true) {
    if (props.formData.disabled === false) {
      return false;
    } else {
      return true;
    }
  }
  return props.formDisabled;
});
const categoryDictCode = ref('')
const typeDictCode = ref('')
// 计算属性：是否可以上传预览图片
const canUploadPreviewImage = computed(() => {
  return formData.instructionTagId &&
    formData.categoryId &&
    formData.typeId
  // &&
  // formData.directiveName &&
  // formData.cycleType;
});

//分类标签名称
const instructionComDictCode = ref([])
function upInstructionDictCode(v_) {
  instructionComDictCode.value = v_
}
//服务类别名称
const categoryComDictCode = ref([])
function upCategoryDictCode(v_) {
  categoryComDictCode.value = v_
}
//服务类型名称
const typeComDictCode = ref([])
function upTypeDictCode(v_) {
  typeComDictCode.value = v_
}
//指令类型名称
const cycleTypeComDictCode = ref([])
function upCycleTypeDictCode(v_) {
  cycleTypeComDictCode.value = v_
}
//体型标签名称
const upBodyTagsComDictCode = ref([])
function upBodyTagsDictCode(v_) {
  upBodyTagsComDictCode.value = v_
}
//情绪标签名称
const upEmotionTagsComDictCode = ref([])
function upEmotionTagsDictCode(v_) {
  upEmotionTagsComDictCode.value = v_
}


watch([instructionComDictCode, categoryComDictCode, typeComDictCode, cycleTypeComDictCode, upBodyTagsComDictCode, upEmotionTagsComDictCode, upBizPrefix], () => {
  // 当这些字典数据变化时，强制重新计算 formComputedData
}, { deep: true });

const bodyTagsObj = ref([])
const emotionTagsObj = ref([])
const formComputedData = computed(() => {
  if (!canUploadPreviewImage.value) return {};

  // 获取各个字段的字典值
  const instructionName = instructionComDictCode.value.find(d => d.value == formData.instructionTagId)?.label || '';
  console.log("🌊 ~ instructionName:", instructionName)
  const categoryName = categoryComDictCode.value.find(d => d.value == formData.categoryId)?.label || '';
  console.log("🌊 ~ categoryName:", categoryName)
  const typeName = typeComDictCode.value.find(d => d.value == formData.typeId)?.label || '';
  console.log("🌊 ~ typeName:", typeName)
  const cycleTypeName = cycleTypeComDictCode.value.find(d => d.value == formData.cycleType)?.label || '';

  // 处理标签
  const processTags = (tags, dict) => {
    if (!tags) return [];
    return tags.split(',')
      .map(id => dict.find(d => d.value === id))
      .filter(Boolean)
      .map(item => ({ id: item.value, label: item.label }));
  };

  return {
    // mediaFileSavePath: `directive/${instructionName}/${categoryName}/${typeName}/${cycleTypeName}/${formData.directiveName}`,
    mediaFileSavePath: upBizPrefix.value + `/directive/${instructionName}/${categoryName}/${typeName}`,
    instructionName,
    categoryName,
    typeName,
    cycleTypeName,
    bodyTagsObj: JSON.stringify(processTags(formData.bodyTags, upBodyTagsComDictCode.value)),
    emotionTagsObj: JSON.stringify(processTags(formData.emotionTags, upEmotionTagsComDictCode.value))
  };
});

watch(
  () => formData.instructionTagId,
  (newInstructionTagId) => {
    if (needWatch.value) {
      formData.categoryId = ''
      formData.typeId = ''
      // formData.cycleType = ''
    }
    if (!newInstructionTagId) {
      categoryDictCode.value = 'nu_config_service_category,category_name,id,1=2';
    } else {
      categoryDictCode.value = `nu_config_service_category,category_name,id,del_flag = 0 and iz_enabled = 'Y' and instruction_id = '${newInstructionTagId}' order by sort asc`;
    }
  }
);
watch(
  () => formData.categoryId,
  (newCategoryId) => {
    if (needWatch.value) {
      formData.typeId = ''
      // formData.cycleType = ''
    }
    if (!newCategoryId) {
      typeDictCode.value = 'nu_config_service_type,type_name,id,1=2';
    } else {
      typeDictCode.value = `nu_config_service_type,type_name,id,del_flag = 0 and iz_enabled = 'Y' and category_id = '${newCategoryId}' order by sort asc`;
    }
  }
);
watch(
  () => formData.typeId,
  (newTypeId) => {
    if (needWatch.value) {
      // formData.cycleType = ''
    }
  }
);
/**
 * 新增
 */
function add() {
  edit({});
}
const needWatch = ref(false)
const showMedia = ref(true)
/**
 * 编辑
 * isEditMedia_是否为编辑指令资源 （隐藏业务字段）
 */
function edit(record, isEditMedia_ = false, showMedia_ = true, showExistTags = true) {
  if (!!record.bodyTags && showExistTags) {
    // 将逗号分隔的字符串转换为 "id = 'id1' or id = 'id2'" 格式
    const bodyTagConditions = record.bodyTags.split(',')
      .map(id => `id = '${id}'`)
      .join(' or ');
    bodyTagDictCode.value = `nu_config_body_tag,tag_name,id,del_flag = 0 and iz_enabled = 0 ` +
      (bodyTagConditions ? ` and (${bodyTagConditions})` : '') +
      ` order by sort asc`;
  }
  if (!!record.emotionTags && showExistTags) {
    // 将逗号分隔的字符串转换为 "id = 'id1' or id = 'id2'" 格式
    const emotionTagConditions = record.emotionTags.split(',')
      .map(id => `id = '${id}'`)
      .join(' or ');
    emotionTagDictCode.value = `nu_config_emotion_tag,tag_name,id,del_flag = 0 and iz_enabled = 0 ` +
      (emotionTagConditions ? ` and (${emotionTagConditions})` : '') +
      ` order by sort asc`;
  }
  needWatch.value = false
  showMedia.value = showMedia_
  setTimeout(() => {
    needWatch.value = true
  }, 1000)
  isEditMedia.value = isEditMedia_
  formData.bodyTags = ''
  formData.emotionTags = ''
  nextTick(() => {
    resetFields();
    const tmpData = {};
    Object.keys(formData).forEach((key) => {
      if (record.hasOwnProperty(key)) {
        tmpData[key] = record[key]
      }
    })
    //赋值
    Object.assign(formData, tmpData);
    if (props.opeType != 'dmlook') {
      if (!formData.previewFile) {
        formData.previewFile = defaultPrePic
      }
      if (!formData.previewFileSmall) {
        formData.previewFileSmall = defaultPreSmallPic
      }
      if (!formData.immediateFile) {
        formData.immediateFile = defaultImmediatePic
      }
      if (!formData.immediateFileFocus) {
        formData.immediateFileFocus = defaultImmediatePic
      }
    }
  });
}

/**
 * 提交数据
 */
async function submitForm() {
  try {
    // 触发表单验证
    await validate();
  } catch ({ errorFields }) {
    if (errorFields) {
      const firstField = errorFields[0];
      if (firstField) {
        formRef.value.scrollToField(firstField.name, { behavior: 'smooth', block: 'center' });
      }
    }
    return Promise.reject(errorFields);
  }
  confirmLoading.value = true;
  const isUpdate = ref<boolean>(false);
  //时间格式化
  const computedData = formComputedData.value;
  const model = {
    ...formData,
    ...computedData
  };
  if (model.id) {
    isUpdate.value = true;
  }
  //循环数据
  for (let data in model) {
    //如果该数据是数组并且是字符串类型
    if (model[data] instanceof Array) {
      let valueType = getValueType(formRef.value.getProps, data);
      //如果是字符串类型的需要变成以逗号分割的字符串
      if (valueType === 'string') {
        model[data] = model[data].join(',');
      }
    }
  }

  //提成价格不能高于收费价格
  if (model.comPrice != 0 && model.tollPrice < model.comPrice) {
    createMessage.warning('提成价格不能高于收费价格！');
    confirmLoading.value = false;
    return
  }

  model.mediaFileSavePath = formComputedData.value.mediaFileSavePath

  if (model.previewFile == defaultPrePic) {
    model.previewFile = null
  }
  if (model.previewFileSmall == defaultPreSmallPic) {
    model.previewFileSmall = null
  }
  if (model.immediateFile == defaultImmediatePic) {
    model.immediateFile = null
  }
  if (model.immediateFileFocus == defaultImmediatePic) {
    model.immediateFileFocus = null
  }

  await saveOrUpdate(model, isUpdate.value)
    .then((res) => {
      if (res.success) {
        createMessage.success(res.message);
        emit('ok');
      } else {
        createMessage.warning(res.message);
      }
    })
    .finally(() => {
      confirmLoading.value = false;
    });
}

/**
 * 同步媒体资源给指令所有者对应业务平台
 *
 */
function syncMediaForBizFunc() {
  formData.mediaFileSavePath = formComputedData.value.mediaFileSavePath
  syncMediaForBiz(formData)
    .then((res) => {
      createMessage.success('操作成功！');
      emit('ok');
    })
    .finally(() => {
      confirmLoading.value = false;
    });
}

/**
 * 同步媒体资源给所有业务平台
 *
 */
function syncMediaForAllBizFunc() {
  formData.mediaFileSavePath = formComputedData.value.mediaFileSavePath
  syncMediaForAllBiz(formData)
    .then((res) => {
      createMessage.success('操作成功！');
      emit('ok');
    })
    .finally(() => {
      confirmLoading.value = false;
    });
}

const directiveMediaBtnValue = ref(0)
function mediaBtnChanged(v_) {
  directiveMediaBtnValue.value = v_

}

onMounted(() => {
  queryUpBizPrefix().then(res => {
    upBizPrefix.value = res.result
  })
})

defineExpose({
  add,
  edit,
  submitForm,
  syncMediaForBizFunc,
  syncMediaForAllBizFunc
});
</script>

<style lang="less" scoped>
.antd-modal-form {
  padding: 14px;
}

:deep .ant-checkbox-wrapper {
  margin-top: 5px;
  margin-bottom: 10px;
  width: 30%;
}

.upload-area {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.upload-icon {
  margin-bottom: 16px;
}

.upload-text {
  font-size: 16px;
  color: rgba(0, 0, 0, 0.85);
  margin-bottom: 8px;
}

.upload-hint {
  font-size: 12px;
  color: rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: center;
  gap: 8px;
}

.divider {
  color: rgba(0, 0, 0, 0.2);
}

.description-container {
  position: relative;
  z-index: 1000;
}

.description-box {
  position: absolute;
  bottom: 100%;
  left: 0;
  background: #f6faff;
  border-radius: 4px;
  padding: 12px;
  width: 43vw;
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.1);
  margin-bottom: 8px;
}

.box-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-bottom: 8px;
  margin-bottom: 12px;
}

.title {
  font-weight: bold;
  font-size: 16px;
}

.collapse-icon {
  color: #1890ff;
  cursor: pointer;
}

.content {
  background: #fff;
  border-radius: 4px;
  padding: 12px;
  position: relative;
}

.instruction {
  color: #8c8c8c;
  margin-bottom: 30px;
}

.confirm-btn {
  position: absolute;
  right: 12px;
  bottom: 12px;
  background: #1890ff;
  border-radius: 20px;
}

/* 渐隐渐现动画 */
.slide-fade-enter-active {
  transition: opacity 0.3s ease-out;
}

.slide-fade-leave-active {
  transition: opacity 0.3s cubic-bezier(0.5, 0, 0.8, 1);
}

/* 进入时从透明开始 */
.slide-fade-enter-from {
  opacity: 0;
}

/* 离开时渐变到透明 */
.slide-fade-leave-to {
  opacity: 0;
}

/* 确保容器初始状态无变形 */
.slide-fade-enter-to,
.slide-fade-leave-from {
  opacity: 1;
}
</style>
