<template>
  <el-dialog title="深度研判" v-model="visible" v-if="visible" width="70%" @close="close">
    <el-form :model="formData" :rules="rules" ref="formRef" label-width="120px" label-position="top">
      <el-row :gutter="24">
        <el-col :span="12">
          <el-form-item label="研判议题" prop="ypyt" class="mt10">
            <el-input v-model="formData.ypyt" placeholder="请输入研判议题" clearable />
          </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
          <el-form-item label="研判时间" prop="wcsj" class="mt10">
            <el-date-picker v-model="formData.wcsj" type="datetime" placeholder="请选择研判时间" style="width: 100%;" />
          </el-form-item>
        </el-col> -->
        <el-col :span="12">
          <el-form-item label="报告类型" prop="bglx" class="mt10">
            <el-radio-group v-model="formData.bglx">
              <el-radio v-for="value in D_BZ_YPLX" :key="value.value" :label="value.value" :value="value.value">{{
                value.label }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="参与研判部门" prop="bmdm" class="mt10">
            <MOSTY.Department clearable v-model="formData.bmdm" multiple style="width: 100%;"
              @getDepValue="getdep($event)" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="研判要求" prop="ypyq" class="mt10">
            <el-input v-model="formData.ypyq" placeholder="请填写研判要求" type="textarea" rows="4" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item class="mt10" center>
            <div style="margin: 0 auto;">
              <el-button type="primary" :loading="loading" @click="submitForm">提交</el-button>
              <el-button @click="close">取消</el-button>
            </div>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script setup>
import * as MOSTY from "@/components/MyComponents/index";
import { ElMessage } from 'element-plus';
import { qcckPost } from '@/api/qcckApi'
import { ref, getCurrentInstance } from 'vue'
const emit = defineEmits(['change']);
const { proxy } = getCurrentInstance();
const { D_BZ_YPLX, D_BZ_YPFS } = proxy.$dict('D_BZ_YPLX', 'D_BZ_YPFS')
const visible = ref(false)
const loading = ref(false)
const formRef = ref()
const formData = ref({})

const rules = ref({
  ypyt: [{ required: true, message: '请输入研判议题', trigger: 'blur' }],
  // wcsj: [{ required: true, message: '请选择研判时间', trigger: 'change' }],
  bglx: [{ required: true, message: '请选择报告类型', trigger: 'change' }],
  ypfs: [{ required: true, message: '请选择研判方式', trigger: 'change' }],
  deptIds: [{ required: true, message: '请选择参与研判部门', trigger: 'change' }],
  ypyq: [{ required: true, message: '请输入研判内容', trigger: 'blur' }],
})

const init = (row) => {
  visible.value = true;
  formData.value = {
    sjly: '03',
    sjlyid: row.jjdbh,
  }
}
const close = () => {
  visible.value = false;
  formRef.value.resetFields();
}
const deptIds = ref([])
const getdep = (e) => {
  if (Array.isArray(e)) {
    deptIds.value = e
  } else {
    deptIds.value = [e]
  }
}
const submitForm = () => {
  formRef.value.validate((valid) => {
    if (!valid) return;
    loading.value = true;
    const promes = {
      ...formData.value,
      cyypList: deptIds.value.map((item) => {
        return {
          ypbmmc: item.orgName,
          ypbmdm: item.orgCode
        }
      }
      ),
    }
    qcckPost(promes, '/mosty-gsxt/lzJcjPjdb/sdyp').then(res => {
      loading.value = false;
      ElMessage.success('研判成功');
      emit('change');
      close();
    }).catch(() => {
      loading.value = false;
      ElMessage.error('研判失败');
    })
  })
}
defineExpose({
  init,
})
</script>
