<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    :title="title"
    @visible-change="handleVisibleChange"
    @cancel="resetFields"
    @ok="handleSubmit"
  >
    <div class="pt-2px pr-3px">
      <BasicForm @register="registerForm" :model="model" >
        <template #text="{ model, field }">
          {{ model[field]  }}
        </template>
      </BasicForm>
    </div>
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref, nextTick, onMounted, reactive, h } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form/index';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { uploadApi } from '/@/api/product/index';
  import { newAddNews, addOrUpdate } from '/@/api/operate'
  import { useI18n } from '/@/hooks/web/useI18n';
  import { Tinymce } from '/@/components/Tinymce/index';

  const { t } = useI18n();
  export default defineComponent({
    components: { BasicModal, BasicForm },
    props: {
      userData: { type: Object },
    },
    emits: ['update', 'register'],
    setup(props, { emit }) {
      const modelRef = ref({});
      const title = ref('新增职位');
      const fileFlow = reactive({
        coverImageUrl:''
      })
      const { createMessage } = useMessage();
      const schemas: FormSchema[] = [
        {
          field: 'id',
          component: 'Input',
          show:false,
          label: 'id',
          required: false,
        },{
            field: 'workName',
            component: 'Input',
            required: true,
            label: '职位名称',
            componentProps: {
              maxLength: 15,
            },
            colProps: {
              span: 22,
            },
        },{
            field: 'workAddress',
            component: 'Input',
            required: true,
            label: '工作地点',
            componentProps: {
              maxLength: 15,
            },
            colProps: {
              span: 22,
            },
        },{
            field: 'salary',
            component: 'Input',
            label: '薪资待遇',
            required: true,
            componentProps: {
              maxLength: 15,
              placeholder: '请输入',
              max: 999,
              min: 0,
            },
            colProps: {
              span: 22,
            },
        },{
            field: 'employNum',
            component: 'InputNumber',
            required: true,
            label: '招聘人数',
            defaultValue:1,
            componentProps: {
              maxLength: 5,
              placeholder: '请输入招聘人数',
              max: 999,
              min: 1,
            },
            colProps: {
              span: 22,
            },
        },{
            field: 'workContent',
            component: 'InputTextArea',
            required: true,
            label: '工作职责',
            componentProps: {
              maxLength: 500,
              placeholder: '请输入职位要求',
              rows:4,
            },
            colProps: {
              span: 22,
            },
        },{
            field: 'qualification',
            component: 'InputTextArea',
            required: true,
            label: '任职资格',
            componentProps: {
              maxLength: 500,
              placeholder: '请输入任职资格',
              rows:4,
            },
            colProps: {
              span: 22,
            },
        },
      ];
      const [registerForm, { validate, resetFields, setFieldsValue, updateSchema }] = useForm({
        labelWidth: 120,
        schemas,
        showActionButtonGroup: false,
        actionColOptions: {
          span: 24,
        },
      });
      onMounted(() => {});
      let addListFunc = () => {};
      const [register, { closeModal }] = useModalInner((data) => {
        data && onDataReceive(data);
      });
      async function onDataReceive(data) {
        modelRef.value = data
        title.value = data?'编辑职位':'新增职位'
        resetFields();
        setFieldsValue(data);
      }
      function NewTypeChange(val){
        console.log('NewTypeChange',val)
        updateSchema([
          { field: 'content', ifShow:val == 2,},
          { field: 'newsUrl', ifShow:val != 2,},
        ])
      } 
      const handleSubmit = async () => {
        const params = await validate();
        const apiData = {
          ...params as any,
          coverImageUrl:params.coverImageUrl && params.coverImageUrl[0],
          content:params.newType == 1?params.newsUrl:params.content
        }
        try {
          await addOrUpdate(apiData);
          closeModal();
          resetFields();
          createMessage.success(t('common.optSuccess'));
          emit('update');
        } catch (error) {
          console.log('not passing', error);
        }
      };
      function handleVisibleChange(v) {
        v && props.userData && nextTick(() => onDataReceive(props.userData));
      }
      return {
        register,
        schemas,
        title,
        registerForm,
        model: modelRef,
        fileFlow,
        NewTypeChange,
        handleVisibleChange,
        handleSubmit,
        addListFunc,
        resetFields,
        t,
      };
    },
  });
</script>
