<template>
  <div>
    <div class="bigBox">
      <MOSTY.FromPage :title="props.title" @closeDialog="CloseEvent">
        <template #content>
          <MOSTY.Assort :title="'基本信息'" />
          <el-form :model="dialogForm" :rules="rules" ref="ruleFormRef" label-width="150px"
            :disabled="!props.saveBtnShow">
            <el-form-item label="标题：" prop="title">
              <el-input v-model="dialogForm.title" />
            </el-form-item>
            <el-form-item label="分类：" prop="type">
              <MOSTY.Select v-model="dialogForm.type" clearable placeholder="请选择分类" :options="D_TB_YWLX">
                <!-- <el-option v-for="item in D_TB_YWLX" :key="item" :label="item.label" :value="item.value" /> -->
              </MOSTY.Select>
            </el-form-item>
            <el-form-item label="讨论范围：" style="display: flex" prop="deptname">
              <el-input v-model="dialogForm.deptname" readonly autosize type="textarea">
              </el-input>
              <el-button @click="ChooesDdld('01')" class='cursor'>选择</el-button>
            </el-form-item>
            <el-form-item label="内容：" prop="content">
              <el-input v-model="dialogForm.content" :rows="5" type="textarea" />
            </el-form-item>
            <el-form-item label="附件：">
              <MOSTY.Upload v-model="dialogForm.files" :delrow="delrow" :isAutoWidth="true" :width="'100%'"
                :placeholder="'请上传pdf,ofd, doc, docx,wps'" :gsgfList="['pdf', 'ofd', 'doc', 'docx', 'wps']"
                :isEdit="fileEdit" :ShowfileList="true" :showinput="true" :limit="5" :nopicer="true" :isImg="false" />
            </el-form-item>
          </el-form>
        </template>
        <template #footer>

          <el-button type="primary" v-loading='loading' @click="save(ruleFormRef)" v-if="props.saveBtnShow">
            <el-icon>
              <CirclePlus />
            </el-icon>
            <span>新增讨论组</span>
          </el-button>
          <el-button type="primary" @click="CloseEvent">
            <el-icon>
              <DocumentDelete />
            </el-icon>
            <span>关闭</span>
          </el-button>
        </template>
      </MOSTY.FromPage>
    </div>
    <MOSTY.ChoosebmTree v-if='showCjbm' :formInfo="fromObj" :showDdld="showCjbm" :orgLevels='orgLevels'
      @closezzjg="closedept" @listdata="getSonValue" :zzlx="zzlx" :parentId='null' />
    <ChooseDept v-if='showCjdw' :formInfo="fromObj" :showDdld="showCjdw" :searchType="searchType" @closezzjg="closedept"
      @listdata="getSonValue" :zzlx="zzlx" />
  </div>
</template>

<script setup>
// 引入
import { reactive, ref, defineProps, getCurrentInstance } from "vue";
import { postApi } from "@/api/tobAcco_api.js";
import { ElMessage } from "element-plus";
import { filterqx } from "@/utils/auth.js";
import ChooseDept from "@/components/MyComponents/ChooseDept/Depart.vue";
import * as MOSTY from "@/components/MyComponents/index";
const { proxy } = getCurrentInstance();
const { D_TB_YWLX } = proxy.$dict("D_TB_YWLX");
const emits = defineEmits(["close"]);
const ruleFormRef = ref();
const props = defineProps({
  saveBtnShow: {
    default: true,
    type: Boolean
  },
  title: String,
});
const fromObj = ref([])
const loading = ref(false);
// 数据
const rules = reactive({
  title: [{ required: true, message: "请输入标题", trigger: "change" }],
  deptname: [{ required: true, message: "请选择讨论范围", trigger: "change" }],
  type: [{ required: true, message: "请选择分类", trigger: "change" }],
  content: [{ required: true, message: "请输入讨论内容", trigger: "change" }],
});
const searchType = ref(null)
const dialogForm = ref({});
const fileEdit = ref(true);
const showCjdw = ref(false);
const showCjbm = ref(false);
const orgLevels = ref('02');
const zzlx = ref(null);
// 选择单位
function ChooesDdld() {
  fromObj.value = dialogForm.value.orgcodes ? dialogForm.value.orgcodes : null
  if (filterqx('isSgs') == '1') {
    searchType.value = '02'
    showCjdw.value = true;
  } else if (filterqx('isgs') == '1') {
    // 市级：本级加县单位
    searchType.value = '03'
    showCjdw.value = true;
  } else {
    // 县都展示部门
    // orgLevels.value = '31'
    orgLevels.value = '31,40' // 增加站点
    showCjbm.value = true
  }
}
// 获取部门信息
const getSonValue = (value) => {
  dialogForm.value.deptname = value.map((item) => {
    return item.orgName
  }).toString();
  dialogForm.value.orgcodes = value.map((item) => {
    return item.id.replace('XN', '')
  })
};

// 关闭弹窗
function closedept() {
  showCjbm.value = false
  showCjdw.value = false
}
//关闭综合配置表
const CloseEvent = () => {
  if (props.saveBtnShow) {
    proxy.$modal.confirm('离开会丢失页面中修改的内容，确认离开吗？')
      .then(() => {
        // 关闭上一次的表单校验
        ruleFormRef.value.clearValidate();
        emits("close");
      })
      .catch(() => {
        // catch error
      })
  } else {
    // 关闭上一次的表单校验
    ruleFormRef.value.clearValidate();
    emits("close");
  }

};

// 接收外层新增保存
const save = async (ruleFormRef) => {
  if (!ruleFormRef) return;
  await ruleFormRef.validate((valid) => {
    if (valid) {
      loading.value = true;
      dialogForm.value.files = dialogForm.value.files ? dialogForm.value.files.toString() : null;
      // dialogForm.value.orgcodes = dialogForm.value.orgcodes ? dialogForm.value.orgcodes.toString() : null;
      let params = dialogForm.value;
      postApi(params, "/mosty-jcgl/discuss/addDiscuss").then(() => {
        loading.value = false;
        ElMessage.success("新增成功");
        emits("close");
      }).catch(() => {
        loading.value = false;
      })
    } else {
      ElMessage.error("请输入必填项!");
    }
  });
};
</script>

<style lang="scss" scoped>
::v-deep .el-form-item__content {
  flex-wrap: nowrap !important;
}

.bigBox {
  width: 100%;
  box-sizing: border-box;
}

.el-form {
  display: block !important;

  .el-form-item {
    width: 50%;
    margin: 0 auto;
    margin-top: 20px;
  }
}
</style>
