<template>
  <div class="informCreate">
    <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm">
      <div class="flex">
        <el-form-item label="标题" prop="ssn_title">
          <el-input v-model="ruleForm.ssn_title" />
        </el-form-item>
        <el-form-item label="类型" prop="ssn_lx">
          <el-select v-model="ruleForm.ssn_lx" placeholder=" ">
            <el-option v-for="item in typeData" :key="item.v" :label="item.n" :value="item.v" />
          </el-select>
        </el-form-item>
      </div>
      <div class="flex">
        <el-form-item label="创建人" prop="ssn_fbr">
          <el-input readonly v-model="ruleForm.ssn_fbr" />
        </el-form-item>
        <el-form-item label="关联" prop="ssn_glr">
          <remote-load-multi-select url="/user/main/index" multiple :fields="['user_id','user_realname']" :d="{user_role_id:'75'}" v-model="ruleForm.ssn_glr" v-model:model-name="ruleForm.ssn_glr_json"></remote-load-multi-select>
        </el-form-item>
      </div>
      <el-form-item label="上传附件" prop="ssn_file">
        <file-upload-progress v-model:part="ruleForm.ssn_file" :limit="1">
          <template #default>
            <div class="flex_center w-65px h-65px border border-dashed border-hex-E4E6E8 bg-hex-F9F9F9 text-hex-9D9AB9">
              <i-akar-icons:plus class="text-xl" />
            </div>
          </template>
        </file-upload-progress>
      </el-form-item>
      <div class="btnDiv" style="text-align: center;">
        <el-button @click="emit('update', 2)">取消</el-button>
        <el-button type="primary" @click="submitForm(ruleFormRef)">确定</el-button>
      </div>
    </el-form>
  </div>
</template>

<script setup>
import { reactive, ref, watch } from "vue";
import { user } from '~/store/user';
import { Picture } from "@element-plus/icons-vue";
import { getFullUrl } from "~/utils/helper";
import { schoolNoticeAdd, schoolNoticeEdit, schoolNoticeDetail } from "../api";

const props = defineProps({
  current: {
    type: String,
    default: "",
  },
  parentId: {
    type: String,
    default: "",
  },
  typeData: {
    type: Object,
    default: []
  }
});

const ruleFormRef = ref();
const ruleForm = reactive({
  ss_id: props.parentId,
  ssn_title: "",
  ssn_lx: "",
  ssn_fbr: user.user_realname,
  ssn_glr: "",
  ssn_file: ""
})

const rules = reactive({
  ssn_title: [{ required: true, message: "请输入标题", trigger: "blur" }],
  ssn_lx: [{ required: true, message: "请选择类型", trigger: "change" }],
  ssn_fbr: [{ required: true, message: "请输入创建人", trigger: "blur" }],
  ssn_glr: [{ required: true, message: "请选择关联人", trigger: "change" }],
  ssn_file: [{ required: true, message: "请上传附件", trigger: "submit" }]
});

const emit = defineEmits(["update", "load"]);
const submitForm = async (formEl) => {
  if (!formEl) return;
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      let data = {
        szmsg_school_notice: ruleForm,
      };
      emit("load", true);
      if (props.current == "") {
        await schoolNoticeAdd(data);
      } else {
        data = Object.assign({ ssn_id: props.current }, data);
        await schoolNoticeEdit(data);
      }
      emit("load", false);
      ElMessage.success("保存成功！");
      emit("update", 1);
    } else {
      console.log("error submit!", fields);
    }
  });
};

const resetForm = (formEl) => {
  if (!formEl) return;
  formEl.resetFields();
};

watch(
  () => props.current,
  (newVal, oldVal) => {
    if (newVal != "") {
      initDetailInfo(newVal);
    } else {
      resetForm(ruleFormRef.value);
    }
  },
  { immediate: true }
);

const handleLoad = (val) => {
  loading.value = val;
};

async function initDetailInfo(ssn_id) {
  emit("load", true);
  let data = await schoolNoticeDetail({ ssn_id });
  for (let key in ruleForm) {
    ruleForm[key] = data.one_info[key];
  }
  emit("load", false);
}
</script>

<style lang="scss" scoped>
.informCreate {
  ::v-deep .uploadDiv {
    flex: 1;
    .el-upload--picture-card {
      width: 180px;
      height: 96px;
    }
    .mainUpload {
      .el-upload--picture-card {
        width: 180px;
        height: 96px;
      }
      .el-upload-list--picture-card .el-upload-list__item {
        display: none;
      }
    }
    .attachUpload {
      .el-upload-dragger {
        width: 65px;
        height: 65px;
        padding: 0;
        line-height: 65px;
        background: #f9f9f9;
      }
    }
    .tips {
      img {
        width: 90px;
        height: 48px;
        border-radius: 4px;
        margin-right: 10px;
      }
    }
  }
}
</style>