<template>
  <layout-card>
    <el-form
      ref="formRef"
      :model="data.form"
      :rules="rules"
      label-width="175px"
    >
      <el-divider content-position="left">添加</el-divider>
      <el-row>
        <el-col :span="8">
          <el-form-item label="会议标题" prop="MEETING_TITLE">
            <el-input
              v-model="data.form.MEETING_TITLE"
              placeholder="请输入会议标题"
            />
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="会议类型" prop="MEETING_TYPE">
            <el-select
              v-model="data.form.MEETING_TYPE"
              placeholder="请选择会议类型"
              @change="handleMeetingTypeChange"
            >
              <el-option
                v-for="item in MEETINGTYPEMENU"
                :key="item.id"
                :label="item.id"
                :value="item.name"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="会议地点" prop="MEETING_ADDRESS">
            <el-input
              v-model="data.form.MEETING_ADDRESS"
              placeholder="请输入会议地点"
            />
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item prop="MEETING_DATE" label="会议时间">
            <el-date-picker
              v-model="data.form.MEETING_DATE"
              type="daterange"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="主持人" prop="HOST_PERSON">
            <el-input
              v-model="data.form.HOST_PERSON"
              placeholder="请输入主持人"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="记录人" prop="RECORDER">
            <el-input v-model="data.form.RECORDER" placeholder="请输入记录人" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="人员选择" prop="PERSON">
            <div class="flexBox">
              <el-input
                v-model="data.form.PERSON"
                type="textarea"
                placeholder="请选择人员"
                disabled
              ></el-input>
              <el-button
                class="addBtn"
                type="primary"
                @click="data.SelectPersonDialogVisible = true"
              >
                选择
              </el-button>
            </div>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="会议文本附件" prop="fileList">
            <layout-upload
              v-model:file-list="data.form.fileList"
              accept=".pdf"
              delete-to-server
              :limit="9"
              :before-upload="fnUpload"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="会议视频附件" prop="videoList">
            <layout-upload
              v-model:file-list="data.form.videoList"
              accept=".mp4"
              delete-to-server
              :limit="9"
              :before-upload="fnUpload"
            />
          </el-form-item>
        </el-col>
        <el-col :span="50">
          <el-form-item label="会议记要" prop="MEETING_CONTENT">
            <layout-editor
              v-model="data.form.MEETING_CONTENT"
              placeholder="请输入内容"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="tc mt-10">
      <el-button type="primary" @click="fnSubmit"> 确定 </el-button>
    </div>
    <select-person
      v-model:visible="data.SelectPersonDialogVisible"
      :list-data="data.listAll"
      @submit="fnSelectPersonSubmit"
      @submitall="fnSelectAllRiskSubmit"
    />
  </layout-card>
</template>

<script setup>
import { getUserListAll } from "@/request/safety_production_related.js";
import { addSafetyMeetingView } from "@/request/traffic_safety_meeting.js";
import { reactive, ref } from "vue";
import { MEETINGTYPEMENU } from "@/assets/js/constant.js";
import LayoutUpload from "@/components/upload/index.vue";
import LayoutEditor from "@/components/editor/index.vue";
import useFormValidate from "@/assets/js/useFormValidate.js";
import { ElMessage } from "element-plus";
import SelectPerson from "@/views/safety_production_related/security_notice/components/select_person.vue";
import { useRouter } from "vue-router";
const formRef = ref(null);
const rules = {
  MEETING_TITLE: [
    { required: true, message: "请输入会议标题", trigger: "blur" },
  ],
  MEETING_TYPE: [
    { required: true, message: "请选择会议类型", trigger: "change" },
  ],
  MEETING_ADDRESS: [
    { required: true, message: "请输入会议地点", trigger: "blur" },
  ],
  MEETING_DATE: [
    { required: true, message: "请选择会议时间", trigger: "change" },
  ],
  HOST_PERSON: [{ required: true, message: "请输入主持人", trigger: "blur" }],
  RECORDER: [{ required: true, message: "请输入记录人", trigger: "blur" }],
  MEETING_CONTENT: [
    { required: true, message: "请输入会议记要", trigger: "blur" },
  ],
};

const router = useRouter();
const fnGetUnitsList = async () => {
  const resData = await getUserListAll({});
  data.unitsList = resData.varList;
};
fnGetUnitsList();

const data = reactive({
  form: {
    MEETING_TITLE: "",
    MEETING_TYPE: "",
    MEETING_ADDRESS: "",
    MEETING_DATE: "",
    PERSON: "",
    HOST_PERSON: "",
    RECORDER: "",
    fileList: [],
    videoList: [],
    PRACTITIONER_TYPE: [],
    NOTIFICATIONCONTENT: "",
  },
});
const handleMeetingTypeChange = (value) => {
  data.form.MEETING_TYPE = value;
};
const fnSubmit = async () => {
  await useFormValidate(formRef);
  const formData = new FormData();

  Object.keys(data.form).forEach((key) => {
    if (key === "MEETING_DATE") {
      const dateRangeString = data.form.MEETING_DATE.join(" , ");
      formData.append(key, dateRangeString);
    } else if (!Array.isArray(data.form[key])) {
      formData.append(key, data.form[key]);
    }
  });

  data.form.fileList.forEach((file) => {
    if (file.raw) formData.append("FFILE", file.raw);
  });

  data.form.videoList.forEach((video) => {
    if (video.raw) formData.append("VVIDEO", video.raw); // 注意: 用不同的字段名区分文件和视频
  });

  await addSafetyMeetingView(formData);
  ElMessage.success("添加成功");
  router.push("/safety_production_related/safety_meeting");
};

const fnUpload = (file) => {
  const isLt100M = file.size / 1024 / 1024 < 100;
  if (!isLt100M) {
    ElMessage.error("文件大小不能超过100M");
    return false;
  }
  return true;
};

const fnSelectPersonSubmit = (selectionData) => {
  const userIds = selectionData.map((item) => item.USER_ID).join(", ");
  const names = selectionData.map((item) => item.NAME).join(", ");
  data.form.PERSON_ID = userIds;
  data.form.PERSON = names;
};
const fnSelectAllRiskSubmit = (allData) => {
  const dataList = allData.value;

  const userIds = dataList.map((item) => item.USER_ID).join(", ");
  const names = dataList.map((item) => item.NAME).join(", ");

  data.form.PERSON_ID = userIds;
  data.form.PERSON = names;

  data.form.allStatus = 1;
  data.form.PERSON = "已全部选择";
};
</script>

<style scoped lang="scss">
.flexBox {
  display: flex;
  align-items: flex-end;

  .addBtn {
    margin-left: 12px;
  }
}
</style>
