<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>
      <!-- <p  v-for="(item, i) in deptList"> {{ item.deptId  + '----' + item.deptName }} </p> -->
      <el-form-item label="协作单位" prop="cooperativeUnitId">
        <el-select
          clearable
          filterable
          :multiple="user.type == 1"
          v-model="form.cooperativeUnitId"
          placeholder="请选择协作单位"
          @change="handleChange"
          no-data-text="超管无法指派单位"
        >
          <!-- style="width: 100%" -->
          <el-option
            :disabled="item.type == user.type && item.deptId == user.deptId"
            v-for="(item, i) in deptList"
            :key="item.deptId"
            :label="item.deptName"
            :value="item.deptId"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="协作要求" prop="collaborativeContent">
        <el-input
          :disabled="!(user.deptId == 100 && user.type == 1)"
          type="textarea"
          rows="4"
          placeholder="请输入协作要求"
          v-model="form.collaborativeContent"
        ></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="type">
        <template>
          <el-radio
            @input="radioChange"
            :disabled="!(user.deptId == 100 && user.type == 1)"
            v-model="form.type"
            label="1"
            >完结</el-radio
          >
          <el-radio
            @input="radioChange"
            :disabled="!(user.deptId == 100 && user.type == 1)"
            v-model="form.type"
            label="2"
            >长期</el-radio
          >
          <el-radio
            @input="radioChange"
            :disabled="!(user.deptId == 100 && user.type == 1)"
            v-model="form.type"
            label="3"
            >限期</el-radio
          >
        </template>
      </el-form-item>
      <el-form-item
        v-if="isShowDate"
        style="padding-bottom: 20px"
        label="限期日期"
        prop="date"
      >
        <el-date-picker
          :picker-options="pickerOptions"
          :disabled="!(user.deptId == 100 && user.type == 1)"
          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 { getInfo } from "@/api/login";

import {
  addJOINTCONFERENCE,
  updateJOINTCONFERENCE,
  getJOINTCONFERENCE,
} from "@/api/portal/JOINTCONFERENCE/JOINTCONFERENCE";
export default {
  props: {
    param: {
      type: Object,
      default: () => {
        return {};
      },
    },
    layerid: {
      type: String,
    },
  },
  data() {
    return {
      // 时间日期格式选择限制
      pickerOptions: {
        disabledDate(time) {
          const date = new Date();
          return time.getTime() < Date.now() - 8.64e7; //禁止选取当天之后的日期（不包括静止当天）
        },
      },
      isShowDate: false,
      user: {
        type: 1,
      },
      // 状态选择
      status: null,
      // 限期日期
      date: null,

      deptId: [],
      deptList: [],
      form: {
        conferenceContent: "",
        cooperativeUnitId: null,
      },
      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: [],
    };
  },
  created() {
    if (this.param.id) {
      getJOINTCONFERENCE(this.param.id).then((response) => {
        this.form = response.data;
        let arr = eval("(" + this.form.deptIds + ")");
        this.deptId = JSON.parse(this.form.deptIds);
      });
    }

    getInfo().then((res) => {
      this.user = res.user.dept;
      if (res.user.dept.type == 1 && res.user.userId == 14) {
        listDept().then((res) => {
          res.data.forEach(item=>{
            if(item.type!=3){
              this.deptList.push(item);
            }
          });
          
        });
       
      } else if (res.user.dept.type == 2) {
        this.deptList = [
          {
            searchValue: null,
            createBy: "admin",
            createTime: "2023-07-20 14:40:52",
            updateBy: "admin",
            updateTime: "2023-09-18 18:46:44",
            params: {},
            deptId: 100,
            parentId: 0,
            ancestors: "0",
            deptName: "广西壮族自治区北部湾办公室",
            type: 1,
            orderNum: 0,
            leader: "XXX",
            phone: "15888888888",
            biaoCang: "23",
            email: "ry@qq.com",
            status: "0",
            delFlag: "0",
            parentName: null,
            industryOrientation: null,
            planningArea: null,
            acceptanceArea: null,
            useArea: null,
            usageRate: null,
            warehouse: null,
            oilDepot: null,
            refrigeratory: null,
            workshop: null,
            approvalTime: null,
            children: [],
            createBy_dictText: "管理员",
          },
        ];
      }
    });
  },
  updated() {
    this.form.conferenceContent = this.$refs["test"].innerText;
  },

  methods: {
    radioChange(val) {
      if (val == 1 || val == 2) {
        this.isShowDate = false;
        return;
      }
      this.isShowDate = true;
    },

    handleChange(val) {
      console.log(typeof val);
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        let cooperativeUnitId;
        if (valid) {
          if (typeof this.form.cooperativeUnitId == "object") {
            cooperativeUnitId = this.form.cooperativeUnitId;
          } else {
            cooperativeUnitId = [this.form.cooperativeUnitId];
          }
          if (this.user.type == 1 && this.user.deptId == 100)
            cooperativeUnitId.push(100);
          console.log(cooperativeUnitId);

          if (this.form.id != null) {
            this.form.deptNum = 1;
            updateJOINTCONFERENCE({ ...this.form, cooperativeUnitId }).then(
              (response) => {
                this.$modal.msgSuccess("修改成功");
                this.$layer.close(this.layerid);
                this.$parent.getList();
              }
            );
          } else {
            //俊豪像甲方一样，老是让我改传参
            //↑好家伙你还改错了，是seleValue不是deptId
            //↑啥玩意？
            this.form.deptNum = cooperativeUnitId.length;
            addJOINTCONFERENCE({ ...this.form, cooperativeUnitId }).then(
              (response) => {
                this.$modal.msgSuccess("发布成功");
                this.$layer.close(this.layerid);
                this.$parent.getList();
              }
            );
          }
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
p {
  margin: 0;
}
</style>
