<template>
  <a-spin :spinning="confirmLoading">
    <JFormContainer :disabled="disabled">
      <template #detail>
        <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol" name="DirectiveSyncLogInfoForm">
          <a-row>
						<a-col :span="24">
							<a-form-item label="分类标签名称" v-bind="validateInfos.instructionTagName" id="DirectiveSyncLogInfoForm-instructionTagName" name="instructionTagName">
								<a-input v-model:value="formData.instructionTagName" placeholder="请输入分类标签名称"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="服务类别名称" v-bind="validateInfos.categoryName" id="DirectiveSyncLogInfoForm-categoryName" name="categoryName">
								<a-input v-model:value="formData.categoryName" placeholder="请输入服务类别名称"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="服务类型名称" v-bind="validateInfos.typeName" id="DirectiveSyncLogInfoForm-typeName" name="typeName">
								<a-input v-model:value="formData.typeName" placeholder="请输入服务类型名称"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="服务指令名称" v-bind="validateInfos.directiveName" id="DirectiveSyncLogInfoForm-directiveName" name="directiveName">
								<a-input v-model:value="formData.directiveName" placeholder="请输入服务指令名称"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="收费价格" v-bind="validateInfos.tollPrice" id="DirectiveSyncLogInfoForm-tollPrice" name="tollPrice">
								<a-input-number v-model:value="formData.tollPrice" placeholder="请输入收费价格" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="提成价格" v-bind="validateInfos.comPrice" id="DirectiveSyncLogInfoForm-comPrice" name="comPrice">
								<a-input-number v-model:value="formData.comPrice" placeholder="请输入提成价格" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="周期类型  1日常护理 2周期护理 3即时护理" v-bind="validateInfos.cycleType" id="DirectiveSyncLogInfoForm-cycleType" name="cycleType">
								<a-input v-model:value="formData.cycleType" placeholder="请输入周期类型  1日常护理 2周期护理 3即时护理"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="服务说明" v-bind="validateInfos.serviceContent" id="DirectiveSyncLogInfoForm-serviceContent" name="serviceContent">
								<a-input v-model:value="formData.serviceContent" placeholder="请输入服务说明"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="服务时长（分钟）" v-bind="validateInfos.serviceDuration" id="DirectiveSyncLogInfoForm-serviceDuration" name="serviceDuration">
								<a-input v-model:value="formData.serviceDuration" placeholder="请输入服务时长（分钟）"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="语音文件" v-bind="validateInfos.mp3File" id="DirectiveSyncLogInfoForm-mp3File" name="mp3File">
								<a-input v-model:value="formData.mp3File" placeholder="请输入语音文件"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="视频文件" v-bind="validateInfos.mp4File" id="DirectiveSyncLogInfoForm-mp4File" name="mp4File">
								<a-input v-model:value="formData.mp4File" placeholder="请输入视频文件"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="服务指令图片大图" v-bind="validateInfos.previewFile" id="DirectiveSyncLogInfoForm-previewFile" name="previewFile">
								<a-input v-model:value="formData.previewFile" placeholder="请输入服务指令图片大图"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="服务指令图片小图" v-bind="validateInfos.previewFileSmall" id="DirectiveSyncLogInfoForm-previewFileSmall" name="previewFileSmall">
								<a-input v-model:value="formData.previewFileSmall" placeholder="请输入服务指令图片小图"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="即时指令图片" v-bind="validateInfos.immediateFile" id="DirectiveSyncLogInfoForm-immediateFile" name="immediateFile">
								<a-input v-model:value="formData.immediateFile" placeholder="请输入即时指令图片"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="即时指令焦点图片" v-bind="validateInfos.immediateFileFocus" id="DirectiveSyncLogInfoForm-immediateFileFocus" name="immediateFileFocus">
								<a-input v-model:value="formData.immediateFileFocus" placeholder="请输入即时指令焦点图片"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
          </a-row>
        </a-form>
      </template>
    </JFormContainer>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick, defineProps, computed, onMounted } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { getValueType } from '/@/utils';
  import { saveOrUpdate } from '../DirectiveSyncLogInfo.api';
  import { Form } from 'ant-design-vue';
  import JFormContainer from '/@/components/Form/src/container/JFormContainer.vue';
  const props = defineProps({
    formDisabled: { type: Boolean, default: false },
    formData: { type: Object, default: () => ({})},
    formBpm: { type: Boolean, default: true }
  });
  const formRef = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
    instructionTagName: '',   
    categoryName: '',   
    typeName: '',   
    directiveName: '',   
    tollPrice: undefined,
    comPrice: undefined,
    cycleType: '',   
    serviceContent: '',   
    serviceDuration: '',   
    mp3File: '',   
    mp4File: '',   
    previewFile: '',   
    previewFileSmall: '',   
    immediateFile: '',   
    immediateFileFocus: '',   
  });
  const { createMessage } = useMessage();
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
  const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const confirmLoading = ref<boolean>(false);
  //表单验证
  const validatorRules = reactive({
  });
  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;
  });

  
  /**
   * 新增
   */
  function add() {
    edit({});
  }

  /**
   * 编辑
   */
  function edit(record) {
    nextTick(() => {
      resetFields();
      const tmpData = {};
      Object.keys(formData).forEach((key) => {
        if(record.hasOwnProperty(key)){
          tmpData[key] = record[key]
        }
      })
      //赋值
      Object.assign(formData, tmpData);
    });
  }

  /**
   * 提交数据
   */
  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);
    //时间格式化
    let model = formData;
    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(',');
        }
      }
    }
    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;
      });
  }


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

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