<template>
  <a-spin :spinning="confirmLoading">
    <JFormContainer :disabled="disabled">
      <template #detail>
        <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol" name="DirectiveAppraisalForm">
          <a-row>
						<a-col :span="24">
							<a-form-item label="护理单元名称" v-bind="validateInfos.nuName" id="DirectiveAppraisalForm-nuName" name="nuName">
								<a-input v-model:value="formData.nuName" placeholder="请输入护理单元名称"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="员工姓名" v-bind="validateInfos.employeeName" id="DirectiveAppraisalForm-employeeName" name="employeeName">
								<a-input v-model:value="formData.employeeName" placeholder="请输入员工姓名"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="分类标签名称" v-bind="validateInfos.instructionTagName" id="DirectiveAppraisalForm-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="DirectiveAppraisalForm-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="服务属性  ds定时 js计时" v-bind="validateInfos.serviceAttribute" id="DirectiveAppraisalForm-serviceAttribute" name="serviceAttribute">
								<a-input v-model:value="formData.serviceAttribute" placeholder="请输入服务属性  ds定时 js计时"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="服务类型名称" v-bind="validateInfos.typeName" id="DirectiveAppraisalForm-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="DirectiveAppraisalForm-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="周期类型ID" v-bind="validateInfos.cycleTypeId" id="DirectiveAppraisalForm-cycleTypeId" name="cycleTypeId">
								<a-input v-model:value="formData.cycleTypeId" placeholder="请输入周期类型ID"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="周期类型" v-bind="validateInfos.cycleType" id="DirectiveAppraisalForm-cycleType" name="cycleType">
								<a-input v-model:value="formData.cycleType" placeholder="请输入周期类型"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="周期值" v-bind="validateInfos.cycleValue" id="DirectiveAppraisalForm-cycleValue" name="cycleValue">
								<a-input v-model:value="formData.cycleValue" placeholder="请输入周期值"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="服务时长（分钟）" v-bind="validateInfos.serviceDuration" id="DirectiveAppraisalForm-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.serviceContent" id="DirectiveAppraisalForm-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.comPrice" id="DirectiveAppraisalForm-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="应收提成价格" v-bind="validateInfos.ysComPrice" id="DirectiveAppraisalForm-ysComPrice" name="ysComPrice">
								<a-input-number v-model:value="formData.ysComPrice" placeholder="请输入应收提成价格" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="服务指令包名称" v-bind="validateInfos.packageName" id="DirectiveAppraisalForm-packageName" name="packageName">
								<a-input v-model:value="formData.packageName" placeholder="请输入服务指令包名称"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="是否是服务指令包 Y是 N否" v-bind="validateInfos.izPackage" id="DirectiveAppraisalForm-izPackage" name="izPackage">
								<a-input v-model:value="formData.izPackage" placeholder="请输入是否是服务指令包 Y是 N否"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="开始时间" v-bind="validateInfos.startTime" id="DirectiveAppraisalForm-startTime" name="startTime">
								<a-date-picker placeholder="请选择开始时间"  v-model:value="formData.startTime" value-format="YYYY-MM-DD"  style="width: 100%"  allow-clear />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="结束时间" v-bind="validateInfos.endTime" id="DirectiveAppraisalForm-endTime" name="endTime">
								<a-date-picker placeholder="请选择结束时间"  v-model:value="formData.endTime" showTime value-format="YYYY-MM-DD HH:mm:ss" style="width: 100%"  allow-clear />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="实际开始时间" v-bind="validateInfos.beginTime" id="DirectiveAppraisalForm-beginTime" name="beginTime">
								<a-date-picker placeholder="请选择实际开始时间"  v-model:value="formData.beginTime" showTime value-format="YYYY-MM-DD HH:mm:ss" style="width: 100%"  allow-clear />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="实际结束时间" v-bind="validateInfos.finishTime" id="DirectiveAppraisalForm-finishTime" name="finishTime">
								<a-date-picker placeholder="请选择实际结束时间"  v-model:value="formData.finishTime" showTime value-format="YYYY-MM-DD HH:mm:ss" style="width: 100%"  allow-clear />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="是否开始 Y是 N否" v-bind="validateInfos.izStart" id="DirectiveAppraisalForm-izStart" name="izStart">
								<a-input v-model:value="formData.izStart" placeholder="请输入是否开始 Y是 N否"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="是否完成 Y是 N否" v-bind="validateInfos.izFinish" id="DirectiveAppraisalForm-izFinish" name="izFinish">
								<a-input v-model:value="formData.izFinish" placeholder="请输入是否完成 Y是 N否"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="tplink下载地址" v-bind="validateInfos.tplinkPath" id="DirectiveAppraisalForm-tplinkPath" name="tplinkPath">
								<a-input v-model:value="formData.tplinkPath" placeholder="请输入tplink下载地址"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="手动拍照" v-bind="validateInfos.manuallyPicPath" id="DirectiveAppraisalForm-manuallyPicPath" name="manuallyPicPath">
								<a-textarea v-model:value="formData.manuallyPicPath" :rows="4" placeholder="请输入手动拍照" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="手动录制" v-bind="validateInfos.manuallyMp4Path" id="DirectiveAppraisalForm-manuallyMp4Path" name="manuallyMp4Path">
								<a-textarea v-model:value="formData.manuallyMp4Path" :rows="4" placeholder="请输入手动录制" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="执行类型 1单人 2协助 3转单" v-bind="validateInfos.optType" id="DirectiveAppraisalForm-optType" name="optType">
								<j-dict-select-tag v-model:value="formData.optType" dictCode="directive_order_opt_type" placeholder="请选择执行类型 1单人 2协助 3转单"  allow-clear />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="执行状态 1正常 2未执行 3超时" v-bind="validateInfos.optStatus" id="DirectiveAppraisalForm-optStatus" name="optStatus">
								<j-dict-select-tag v-model:value="formData.optStatus" dictCode="directive_order_opt_status" placeholder="请选择执行状态 1正常 2未执行 3超时"  allow-clear />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="实际执行人名称（多个）； 主要执行人+协助人" v-bind="validateInfos.optNames" id="DirectiveAppraisalForm-optNames" name="optNames">
								<a-input v-model:value="formData.optNames" placeholder="请输入实际执行人名称（多个）； 主要执行人+协助人"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="审核状态 0待审核 1通过 2未通过" v-bind="validateInfos.status" id="DirectiveAppraisalForm-status" name="status">
								<j-dict-select-tag v-model:value="formData.status" dictCode="appraisal_status" placeholder="请选择审核状态 0待审核 1通过 2未通过"  allow-clear />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="驳回原因" v-bind="validateInfos.content" id="DirectiveAppraisalForm-content" name="content">
								<a-textarea v-model:value="formData.content" :rows="4" placeholder="请输入驳回原因" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="撤回人（汉字）" v-bind="validateInfos.revocation" id="DirectiveAppraisalForm-revocation" name="revocation">
								<a-input v-model:value="formData.revocation" placeholder="请输入撤回人（汉字）"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="撤回时间" v-bind="validateInfos.revocationTime" id="DirectiveAppraisalForm-revocationTime" name="revocationTime">
								<a-date-picker placeholder="请选择撤回时间"  v-model:value="formData.revocationTime" showTime value-format="YYYY-MM-DD HH:mm:ss" style="width: 100%"  allow-clear />
							</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 JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
  import { getValueType } from '/@/utils';
  import { saveOrUpdate } from '../DirectiveAppraisal.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: '',
    nuName: '',   
    employeeName: '',   
    instructionTagName: '',   
    categoryName: '',   
    serviceAttribute: '',   
    typeName: '',   
    directiveName: '',   
    cycleTypeId: '',   
    cycleType: '',   
    cycleValue: '',   
    serviceDuration: '',   
    serviceContent: '',   
    comPrice: undefined,
    ysComPrice: undefined,
    packageName: '',   
    izPackage: '',   
    startTime: '',   
    endTime: '',   
    beginTime: '',   
    finishTime: '',   
    izStart: '',   
    izFinish: '',   
    tplinkPath: '',   
    manuallyPicPath: '',   
    manuallyMp4Path: '',   
    optType: '',   
    optStatus: '',   
    optNames: '',   
    status: '',   
    content: '',   
    revocation: '',   
    revocationTime: '',   
  });
  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>
