<template>
  <div class="cmain">
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="标题" prop="conferenceTitle">
        <el-input
          :disabled="param.edit"
          v-model="form.conferenceTitle"
          placeholder="请输入会议标题"
        ></el-input>
      </el-form-item>

      <el-form-item label="内容" prop="conferenceContent">
        <el-input
          :disabled="param.edit"
          type="textarea"
          rows="4"
          placeholder="请输入会议内容"
          v-model="form.conferenceContent"
        ></el-input>
      </el-form-item>
      <p style="display: none" v-html="form.conferenceContent" ref="test"></p>
      <el-form-item label="协作单位" prop="cooperativeUnitId">
        <el-select
          v-model="seleValue"
          placeholder="请选择协作单位"
          @change="handleChange"
          >
          <!-- style="width: 100%" -->
          <el-option
            v-for="item in deptList"
            :key="item.deptId"
            :label="item.deptName"
            :value="item.deptId+''"
          >
          </el-option>
        </el-select>
        <!-- <el-cascader
          clearable
          v-model="seleValue"
          :options="deptList"
          :props="propOpt"
          @change="handleChange"
        >
        </el-cascader> -->
      </el-form-item>
      <el-form-item label="协作要求" prop="collaborativeContent">
        <el-input
          :disabled="param.edit"
          type="textarea"
          rows="4"
          placeholder="请输入协作要求"
          v-model="form.collaborativeContent"
        ></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="type">
        <template>
          <el-radio :disabled="param.edit" v-model="form.type" label="1"
            >完结</el-radio
          >
          <el-radio :disabled="param.edit" v-model="form.type" label="2"
            >长期</el-radio
          >
          <el-radio :disabled="param.edit" v-model="form.type" label="3"
            >限期</el-radio
          >
        </template>
      </el-form-item>
      <el-form-item style="padding-bottom: 20px" label="限期日期" prop="date">
        <el-date-picker
          :disabled="param.edit"
          v-model="form.date"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择日期"
        >
        </el-date-picker>
      </el-form-item>
    </el-form>
    <div class="mfooter">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="$layer.close(layerid)">取 消</el-button>
    </div>
  </div>
</template>

<script>
import { getDept } from "@/api/portal/CONFERENCEREPLY/CONFERENCEREPLY.js";
import { listDept } from "@/api/system/dept";
import { getUserInfo } from "@/api/portal/ARTICLE/ARTICLE";

import {
  addJOINTCONFERENCE,
  updateJOINTCONFERENCE,
  getJOINTCONFERENCE,
} from "@/api/portal/JOINTCONFERENCE/JOINTCONFERENCE";
export default {
  props: {
    param: {
      type: Object,
      default: () => {
        return {};
      },
    },
    layerid: {
      type: String,
    },
  },
  data() {
    var checkDeptId = (rule, value, callback) => {
      if (this.seleValue.length == 0) {
        return callback(new Error("协作单位不能为空!!!"));
      } else {
        callback();
      }
    };
    return {
      // 状态选择
      status: null,
      // 限期日期
      date: null,

      // propOpt: {
      //   value: "value",
      //   label: "label",
      //   children: "children",
      //   multiple: false,
      //   checkStrictly: true,
      //   emitPath: false,
      // },

      deptId: [],
      deptList: [],
      form: {
        conferenceContent: "",
      },
      rules: {
        conferenceTitle: [
          { required: true, message: "标题不能为空", trigger: "blur" },
        ],
        conferenceContent: [
          { required: true, message: "内容不能为空", trigger: "blur" },
        ],
        cooperativeUnitId: [
          { required: true, message: "协作单位不能为空", trigger: "blur" },
        ],

        type: [{ required: true, message: "状态不能为空", trigger: "blur" }],
        collaborativeContent: [
          { required: true, message: "协作要求不能为空", trigger: "blur" },
        ],
        date: [
          { required: true, message: "限期时间不能为空", trigger: "blur" },
        ],
      },

      deptListSele: [],
      seleValue: [],
    };
  },
  created() {
    if (this.param.id) {
      getJOINTCONFERENCE(this.param.id).then((response) => {
        this.form = response.data;
        this.seleValue = this.form.deptIds;
        let arr = eval("(" + this.form.deptIds + ")");
        this.seleValue = arr.toString();
        console.log(this.seleValue);
        this.deptId = JSON.parse(this.form.deptIds);
      });
    }

    listDept(this.queryParams).then((response) => {
      this.deptListSele = this.handleTrees(response.data, "deptId");
      this.deptListSele.forEach((item, index) => {
        if (item.children) {
          item.children.forEach((it, i) => {
            it.children = null;
          });
        }
      });
    });

    // if (this.user.type == 1) {
      getDept(2).then((res) => {
        this.deptList = res.data;
      });
    // }else{
    //   getDept(1).then((res) => {
    //     this.deptList = res.data;
    //   });
    // }
  },
  updated() {
    this.form.conferenceContent = this.$refs["test"].innerText;
  },

  methods: {
    getDeptList() {},
    handleTrees(data, id, parentId, children) {
      let config = {
        id: id || "id",
        parentId: parentId || "parentId",
        childrenList: children || "children",
      };

      var childrenListMap = {};
      var nodeIds = {};
      var tree = [];

      for (let d of data) {
        let parentId = d[config.parentId];
        if (childrenListMap[parentId] == null) {
          childrenListMap[parentId] = [];
        }
        nodeIds[d[config.id]] = d;
        d.label = d.deptName;
        d.value = d.deptId;
        childrenListMap[parentId].push(d);
      }

      for (let d of data) {
        let parentId = d[config.parentId];
        if (nodeIds[parentId] == null) {
          tree.push(d);
        }
      }

      for (let t of tree) {
        adaptToChildrenList(t);
      }

      function adaptToChildrenList(o) {
        if (childrenListMap[o[config.id]] !== null) {
          o[config.childrenList] = childrenListMap[o[config.id]];
        }
        if (o[config.childrenList]) {
          for (let c of o[config.childrenList]) {
            adaptToChildrenList(c);
          }
        }
      }
      return tree;
    },
    selectChange(val) {
      // console.log(val);
    },
    handleChange(val) {
      this.form.cooperativeUnitId = this.seleValue;
    },
    /** 提交按钮 */
    submitForm() {
      console.log(this.form);
      this.$refs["form"].validate((valid) => {
        if (this.seleValue.length == 0) {
          this.$message.error("请选择协作单位");
          return;
        }
        if (valid) {
          if (this.form.id != null) {
            this.form.cooperativeUnitId = [this.seleValue];
            this.form.deptNum = this.form.cooperativeUnitId.length;
            updateJOINTCONFERENCE(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.$layer.close(this.layerid);
              this.$parent.getList();
            });
          } else {
            // this.form.deptName = [];
            // for (const index in this.form.deptId) {
            //   for (const i in this.deptList) {
            //     if (this.form.deptId[index] == this.deptList[i].deptId) {
            //       console.log(
            //         this.form.deptId[index],
            //         this.deptList[i].deptName
            //       );
            //       this.form.deptName[index] = this.deptList[i].deptName;
            //     }
            //   }
            // }
            //俊豪像甲方一样，老是让我改传参
            //↑好家伙你还改错了，是seleValue不是deptId
            //↑啥玩意？

            this.form.cooperativeUnitId = [+this.seleValue];
            this.form.deptNum = this.form.cooperativeUnitId.length;
            addJOINTCONFERENCE(this.form).then((response) => {
              this.$modal.msgSuccess("发布成功");
              this.$layer.close(this.layerid);
              this.$parent.getList();
            });
          }
        }
      });
    },
  },
};
</script>

<style></style>
