<template>
  <FormMessage v-model="listQuery" :formList="formData" labelWidth="100px" ref="elform" :rules="rules">
    <template #zrSsbmdm>
      <MOSTY.Department filterable v-model="listQuery.zrSsbmdm" width="100%" @getDepValue="getDepValue" clearable
        placeholder="请选择所属部门" :multiple="true" />
    </template>
    <template #ry>
      <el-input readonly v-model="listQuery.ry" @click="chooseUserVisible = true" placeholder="请选择民警"></el-input>
    </template>
  </FormMessage>
  <ChooseUser v-model="chooseUserVisible" @choosedUsers="handleUserSelected" :roleIds="roleIds" :Single="false" />

</template>

<script setup>
import { reactive, ref, onMounted, watch } from "vue";
import { sendFqzl, ZdrfjSendFqzl ,qbcjSendFqzl} from '@/api/commit'
import { getItem } from '@/utils/storage'
import * as MOSTY from "@/components/MyComponents/index";
import ChooseUser from "@/components/ChooseList/ChooseUser/index.vue"
import FormMessage from "@/components/aboutTable/FormMessage.vue";
import { ElMessage } from "element-plus";
const props = defineProps({
  itemData: {
    type: Object,
    default: () => { }
  }, identification: {
    type: String,
    default: ""
  }, tacitly: {
    type: Object,
    default: () => { }
  }



})
const emit = defineEmits(['handleClose'])

// 表单数据
const listQuery = ref({}); //表单
// 选择人员
const ryStr = ref('')
const elform = ref()
const personnelEntity = ref()
const formData = ref([
  { label: "标题", prop: "title", type: "input", width: "40%" },
  { label: "接收单位", prop: "zrSsbmdm", type: "slot", width: "40%" },
  { label: "指令状态", prop: "status", type: "input", width: "40%" },
  { label: "人员选择", prop: "ry", type: "slot", width: "40%" },
  { label: "附件", prop: "attachmentPath", type: "upload" },
  { label: "指令内容", prop: "instructionContent", type: "textarea", width: "100%" },

])
const rules = reactive({
  title: [{ required: true, message: "请输入指令标题", trigger: "blur" }],
  zrSsbmdm: [{ required: true, message: "请选择接收单位", trigger: "blur" }],
  instructionContent: [{ required: true, message: "请输入指令内容", trigger: "blur" }],
  ry: [{ required: true, message: "请选择人员", trigger: "blur" }]
});
const deptId = getItem('deptId')
const getsendFqzl = () => {
  elform.value.submit(async (val) => {
    if (val) {
      const data = { ...listQuery.value }
      delete data.ry
      const promes = {
        instructionsEntity: {
          unitCode: deptId[0].deptCode,
          unitName: deptId[0].deptName,
          ...data,
          receivingUnitCode: listQuery.value.zrSsbmdm.toString(),
          receivingUnit: listQuery.value.receivingUnit.toString(),
          attachmentPath: JSON.stringify(listQuery.value.attachmentPath)
        }, id: props.itemData.id,
        personnelEntity: personnelEntity.value
      }
      try {
        let res
        switch (props.identification) {
          case 'yj':
            res = await sendFqzl(promes)
            break;
          case 'zdrfj':
            res = await ZdrfjSendFqzl(promes)
            break;
          case 'qbcj':
            res = await qbcjSendFqzl(promes)
            break;
        }
        const str = JSON.parse(res)
        if (str.code == 200) {
          ElMessage.success('发送成功')
          listQuery.value = {}
          listQuery.value.attachmentPath = ''
          emit('handleClose')
        } else {
          ElMessage.error(str.msg)
        }
      } catch (error) {
        console.log(error);

      }

    }
  })


}
console.log(props.itemData);

watch(() => props.itemData, (val) => {
  listQuery.value.title = val[props.tacitly['title']]
  if (props.tacitly['instructionContent']) {
    listQuery.value.instructionContent = val[props.tacitly['instructionContent']]
  }
}, { deep: true, immediate: true })
const chooseUserVisible = ref(false)
const roleIds = ref([])
// 选取角色
const handleUserSelected = (val) => {
  personnelEntity.value = val.map((item, index) => {
    return {
      name: item.userName,
      idNumber: item.idEntityCard,
      phoneNumber: item.mobile,
      personTypeId: "",
      personTypeName: "",
      domicilePlace: "",
      orderId: index + 1
    }
  })
  // ryStr.value
  listQuery.value.ry = personnelEntity.value.map(item => item.name)
}
// 选取部门
const getDepValue = (e) => {
  listQuery.value.receivingUnit = e.map(item => item.orgName)
}
const close = () => {
  listQuery.value = {}
  listQuery.value.attachmentPath = ''
}

defineExpose({
  getsendFqzl,
  close
})


</script>

<style scoped lang="scss"></style>
