<template>
  <div class="dialog" v-if="dialogForm">
    <div class="head_box">
      <span class="title">{{ title }}</span>
      <div>
        <el-button type="primary" :loading="loading" @click="submit">保存</el-button>
        <el-button @click="close">关闭</el-button>
      </div>
    </div>
    <div class="form_cnt">
      <FormMessage v-model="listQuery" :formList="formData" ref="elform" :rules="rules">
        <template #gapdive>
          <div style="width: 100%;height: 10px;" class="mb20">
            <el-divider content-position="left">基础信息</el-divider>
          </div>
        </template>
        <template #gapline>
          <div style="width: 100%;height: 10px;" class="mb20">
            <el-divider content-position="left">线索内容</el-divider>
          </div>
        </template>
        <template #scfj>
          <div style="width: 100%;padding-left: 50px;">
            <div>上传附件:<span class="f12">（可附电子表格、Word文档、图像、音视频文件）</span> </div>
            <MOSTY.Upload :showBtn="true" :isAll="true" :isImg="false" :limit="10" v-model="fjdz" /> 
          </div>
        </template>
      </FormMessage>
      <el-divider content-position="left"><span class="mr20">相关人员</span>  <el-button type="primary" size="small" @click="addEdit('add',null)">添加人员</el-button></el-divider>
      <MyTable
        :tableData="pageForm.tableData"
        :tableColumn="pageForm.tableColumn"
        :tableHeight="pageForm.tableHeight"
        :key="pageForm.keyCount"
        :tableConfiger="pageForm.tableConfiger"
        :controlsWidth="pageForm.controlsWidth"
      >
      <template #xb="{row}">
        <DictTag :value="row.xb" :tag="false" :options="props.dic.D_BZ_XB" />
      </template>
      <template #bqList="{row}">
        <div v-if="row.bqList">
          <el-tag type="success" v-for="(it,idx) in row.bqList" :key="idx">{{ it.bqMc }}</el-tag >
        </div>
      </template>
        <!-- 操作 -->
        <template #controls="{ row,index }">
          <el-link size="small" type="success" @click="addEdit('edit', row,index)">编辑</el-link>
          <el-link size="small" type="danger" @click="deleteRow(index)">删除</el-link>
        </template>
      </MyTable>
    </div>
    <!-- 人员 -->
    <AddPeo ref="showAdd" v-model="showPeo" v-if="showPeo" :dic="props.dic" @change="getPeo"></AddPeo>
  </div>
</template>

<script setup>
import * as MOSTY from "@/components/MyComponents/index";
import AddPeo from '@/components/addPerson/index.vue'
import MyTable from "@/components/aboutTable/MyTable.vue";
import FormMessage from "@/components/aboutTable/FormMessage.vue";
import { qcckGet, qcckPost, qcckPut } from "@/api/qcckApi.js";
import { ref, defineExpose, reactive, onMounted, defineEmits, getCurrentInstance, nextTick } from "vue";
const emit = defineEmits(["change"]);
const props = defineProps({
  dic: Object
});
const showPeo = ref(false)
const { proxy } = getCurrentInstance();
const dialogForm = ref(false); //弹窗
const rules = reactive({
  xsMc: [{ required: true, message: "请输入线索名称", trigger: "blur" }],
  xlLx: [{ required: true, message: "请选择线索类型", trigger: "change" }],
  qbLy: [{ required: true, message: "请选择情报来源", trigger: "change" }],
});
const formData = ref([
  { prop: "gapdive", type: "slot",width:'100%' },
  { label: "线索名称", prop: "xsMc", type: "input" },
  { label: "线索类型", prop: "xlLx", type: "select", options:props.dic.D_GS_XS_LX },
  { label: "情报来源", prop: "qbLy", type: "select", options:props.dic.D_GS_XS_LY},
  { label: "指向开始时间", prop: "zxkssj", type: "datetime"},
  { label: "指向结束时间", prop: "zxjssj", type: "datetime"},
  { label: "指向地点", prop: "zxdz", type: "input"},
  { label: "所属专题", prop: "sszt", type: "select",options:props.dic.D_BZ_SSZT},
  { prop: "gapline", type: "slot",width:'100%' },
  { prop: "scfj", type: "slot",width:'100%'},
  { label: "线索内容", prop: "xsNr", type: "textarea",width:'100%'},
]);
const fjdz = ref([])
const listQuery = ref({}); //表单
const loading = ref(false);
const elform = ref();
const title = ref("");
const pageForm = reactive({
  tableData: [],
  keyCount: 0,
  tableConfiger: {
    rowHieght: 61,
    showSelectType: "checkBox",
    loading: false
  },
  controlsWidth: 220,
  tableColumn: [
    { label: "姓名", prop: "xm" },
    { label: "性别", prop: "xb",showSolt:true },
    { label: "身份证号", prop: "sfzh" },
    { label: "户籍地", prop: "hjdz" },
    { label: "户籍地派出所", prop: "hjdpcs" },
    { label: "标签", prop: "bqList",showSolt:true }
  ]
});
const showAdd = ref()
onMounted(()=>{
  tabHeightFn()
})


// 初始化数据
const init = (type, row) => {
  fjdz.value = []
  tabHeightFn()
  dialogForm.value = true;
  title.value = type == "edit" ? "编辑" : "详情"
  // 初始化表单数据，并根据详情页设置禁用状态
  if (row) getDataById(row.id);
};
// 根据id查询详情
const getDataById = (id) => {
  qcckGet({id}, "/mosty-gsxt/qbcj/selectByid").then((res) => {
    fjdz.value = res.ossList || [];
    listQuery.value = res;
    pageForm.tableData = res.ryList || [];
  });
};

// 打开弹窗
const addEdit = (type,row,index) =>{
  showPeo.value = true;
  nextTick(()=>{
    showAdd.value.init(type,row,index)
  })
}

// 新增人员
const getPeo = (val) =>{
  if(val.type == '新增'){
    pageForm.tableData.push(val.data);
  }else{
    pageForm.tableData.splice(val.index, 1, val.data); // 在索引2的位置删除一个元素，并插入newValue
  }
}

const deleteRow = (index) =>{
  pageForm.tableData.splice(index,1)
}

// 提交
const submit = () => {
  elform.value.submit((data) => {
    let params = { ...data ,ryList:pageForm.tableData,cjLx:'2'};
    let fjids = [];
    fjdz.value.forEach(item=>{
      if(Object.prototype.toString.call(item) === '[object Object]'){
        fjids.push(item.id)
      }else{
        fjids.push(item)
      }
    })
    params.fjdz = fjids.length > 0 ? fjids.join(','):'';
    loading.value = true;
    qcckPost(params, "/mosty-gsxt/qbcj/update").then((res) => {
      loading.value = false;
      proxy.$message({ type: "success", message: title.value + "成功" });
      emit("change");
      close();
    }).catch(() => {
      loading.value = false;
    });
  });
};

// 关闭
const close = () => {
  fjdz.value = []
  listQuery.value = {};
  dialogForm.value = false;
  loading.value = false;
};

// 表格高度计算
const tabHeightFn = () => {
  pageForm.tableHeight = window.innerHeight  - 720;
  window.onresize = function () {
    tabHeightFn();
  };
};

defineExpose({ init });
</script>

<style lang="scss" scoped>
@import "~@/assets/css/layout.scss";
@import "~@/assets/css/element-plus.scss";
::v-deep .el-tabs--card > .el-tabs__header .el-tabs__item.is-active {
  color: #0072ff;
  background: rgba(0, 114, 255, 0.3);
}
.boxlist {
  width: 99%;
  height: 225px;
  margin-top: 10px;
  overflow: hidden;
}

::v-deep .avatar-uploader{
  display: flex;
  align-items: center;
}

::v-deep .el-upload-list{
  margin-left: 20px;
  display: flex;
  align-items: center;
}
::v-deep .el-upload-list__item-name .el-icon{
  top: 3px;
}
</style>
