<template>
    <el-dialog v-model="showDialog" :append-to-body="true" :destroy-on-close="true" :title="title" :close-on-click-modal="false">
        <FormMessage v-model="listQuery" :disabled="title == '反馈' ? false : true" :formList="formData" labelWidth="160px" ref="elform" :rules="rules">
          <template #mbzrmjxm>
            <MOSTY.Other width="100%" @click="handleChoose('mbzrmjxm')" clearable v-model="listQuery.mbzrmjxm"  placeholder="请选择民警" :readonly="true" />
          </template>
          <template #czzrmj>
            <MOSTY.Other width="100%" @click="handleChoose('czzrmj')" clearable v-model="listQuery.czzrmj"  placeholder="请选择民警" :readonly="true" />
          </template>
        </FormMessage>
        <template #footer>
            <div class="flex just-center">
                <el-button @click="close">取消</el-button>
                <el-button type="primary" @click="submitForm" :loading="loading" v-if="title == '反馈'">确认</el-button>
            </div>
        </template>
  </el-dialog>
  <ChooseUser v-model="chooseUserVisible" v-if="chooseUserVisible" @choosedUsers="handleUserSelected" :roleIds="roleIds" :Single="true" />
</template>

<script setup>
import ChooseUser from "@/components/ChooseList/ChooseUser/index.vue"
import * as MOSTY from "@/components/MyComponents/index";
import { qcckGet, qcckPost } from "@/api/qcckApi.js";
import emitter from "@/utils/eventBus.js";
import FormMessage from "@/components/aboutTable/FormMessage.vue";
import { onMounted, reactive, ref ,getCurrentInstance} from 'vue';
const emit = defineEmits(['change'])
const { proxy } = getCurrentInstance();
const { D_BZ_SF ,D_YJXX_CZCSLX,D_YJXX_CZSSXZ,D_YJXX_CKCZJG,D_YJXX_CKZYLX } = proxy.$dict('D_YJXX_CKZYLX','D_BZ_SF','D_YJXX_CZCSLX','D_YJXX_CZSSXZ','D_YJXX_CKCZJG')
const chooseUserVisible = ref(false)
const roleIds = ref([])
const elform = ref()
const showDialog = ref(false)
const loading = ref(false)
const choosetype = ref('')
const listQuery = ref({})
const formData = ref([
  { label: "发现目标状态", prop: "mbzt", type: "select",options: D_BZ_SF },
  { label: "发现目标责任单位", prop: "mbzrdwdm", type: "department" ,depMc:'mbzrdw'},
  { label: "发现目标责任民警", prop: "mbzrmjxm", type: "slot"},
  { label: "是否本人", prop: "sfbr", type: "select",options: D_BZ_SF },
  { label: "处置时间", prop: "czsj", type: "datetime" },
  { label: "处置地址", prop: "czdz", type: "textarea",width:'100%'},
  { label: "详细地址", prop: "xxdz", type: "textarea",width:'100%' },
  { label: "处置责任单位", prop: "czzrdwdm", type: "department",depMc:'czzrdw' },
  { label: "处置责任民警", prop: "czzrmj", type: "slot" },
  { label: "常控处置措施类型", prop: "ckczcslx", type: "select" ,options: D_YJXX_CZCSLX},
  { label: "常控处置措施细类", prop: "ckczcsxl", type: "select",options: D_YJXX_CZSSXZ},
  { label: "常控处置结果", prop: "ckczjg", type: "select",options: D_YJXX_CKCZJG },
  { label: "常控立线侦察评估", prop: "cklxzcpg", type: "input" },
  { label: "常控立线侦察依据", prop: "cklxzcpgyj", type: "input" },
  { label: "常控从事职业类型", prop: "ckzylx", type: "select",options: D_YJXX_CKZYLX },
  { label: "是否尿检", prop: "sfnj", type: "radio",options:  D_BZ_SF  },
  { label: "常控不尿检理由", prop: "ckbnjly", type: "textarea",width:'100%' },
  { label: "常控处置反馈补充信息", prop: "ckczbcxx", type: "textarea",width:'100%' },
])

const rules = reactive({
  mbzt: [{ required: true, message: "请选择发现目标状态", trigger: "change" }],
  fxmbzedw: [{ required: true, message: "请选择发现目标责任单位", trigger: "change" }],
  mbzrmjxm: [{ required: true, message: "请选择发现目标责任民警", trigger: "change" }],
  sfbr:[{ required: true, message: "请选择是否本人", trigger: "change" }],
  czsj:[{ required: true, message: "请选择处置时间", trigger: "change" }],
  czdz:[{ required: true, message: "请选择处置地址", trigger: "blur" }],
  xxdz:[{ required: true, message: "请选择详细地址", trigger: "blur" }],
  czzrdwdm:[{ required: true, message: "请选择处置责任单位", trigger: "change" }],
  czzrmj:[{ required: true, message: "请选择处置责任民警", trigger: "change" }],
  ckczcslx:[{ required: true, message: "请选择常控处置措施类型", trigger: "change" }],
  ckczcsxl:[{ required: true, message: "请选择常控处置措施细类", trigger: "change" }],
  ckczjg:[{ required: true, message: "请输入常控处置结果", trigger: "blur" }],
  cklxzcpg:[{ required: true, message: "请输入常控立线侦察评估", trigger: "blur" }],
  cklxzcpgyj:[{ required: true, message: "请输入常控立线侦察依据", trigger: "blur" }],
  ckzylx:[{ required: true, message: "请选择常控从事职业类型", trigger: "change" }],
  sfnj:[{ required: true, message: "请选择是否尿检", trigger: "change" }],
})

const title = ref('')
onMounted(() => {
  emitter.on("openFkDialog", (val) => {
    showDialog.value = true;
    listQuery.value = {yjid:val.id}
    title.value = val.type;
    if(val.type == '查看反馈'){
      qcckGet({}, "/mosty-gsxt/tbYjxx/getInfo/"+val.id).then((res) => {
        let list = res.fkList || []
        listQuery.value =  list.length > 0 ? list[0] : {};
      });
    }
  })
})

const handleChoose = (type)=>{
  chooseUserVisible.value = true,
  choosetype.value = type;

}

// 选取角色
const handleUserSelected = (val) => {
  switch(choosetype.value){
    case 'mbzrmjxm':
      listQuery.value.mbzrmjxm = val[0].userName
      listQuery.value.mbzrmjsfzh = val[0].inDustRialId
      break;
      case 'czzrmj':
      listQuery.value.czzrmj = val[0].userName
      listQuery.value.czzrmjsfzh = val[0].inDustRialId
      break;
  }
  
}

const submitForm = () =>{
    elform.value.submit((val)=>{
    loading.value = true;
    qcckPost(val, "/mosty-gsxt/tbYjxx/yjfk").then(() => {
      loading.value = false;
      proxy.$message({ type: "success", message: "反馈成功" });
      emit('change')
      close()
    }).catch(() => {
      loading.value = false;
    })
       
  })
}

const close = () => {
  elform.value.reset();
  listQuery.value = {}
  showDialog.value = false;
}
</script>

<style lang="scss" scoped>
.el-form{
    max-height: 60vh;
    overflow: hidden;
    overflow-y: auto;
}
</style>