<template>
  <div>
    <div class="headClass" style="">
      <h3>研判记录</h3>
      <el-button type="primary" v-if="showBut" @click="openJudgment">新增</el-button>
    </div>
    <MyTable :tableData="pageData.tableData" :tableColumn="pageData.tableColumn" :tableHeight="pageData.tableHeight"
      :key="pageData.keyCount" :tableConfiger="pageData.tableConfiger" :controlsWidth="pageData.controlsWidth">
      <template #fj="{row}">
        <span v-for="(item,index) in row.fj" :key="index">{{ item.originalName }}<span v-if="index < row.fj.length - 1">、</span></span>
      </template>
      <!-- 操作 -->
      <template #controls="{ row }">
        <el-link type="danger" @click="updeteRecord(row)">修改</el-link>
        <el-link type="danger" @click="delDictItem(row.id)">删除</el-link>
      </template>
    </MyTable>
  </div>
  <JudgmentRecord v-model="JudgmentShow" @comfirm="onComfirm" :data="dataAmend" />
</template>

<script setup>
import { ref, reactive, watch, toRaw, getCurrentInstance, onMounted, onUnmounted } from "vue";
import MyTable from "@/components/aboutTable/MyTable.vue";
import { tbZdqtYpAdd, tbZdqtYpDelete, tbZdqtYpSelectPage, tbZdqtYpUpdate } from '@/api/qt.js'
import JudgmentRecord from '../component/judgmentRecord.vue'
const { proxy } = getCurrentInstance();
const props = defineProps({
  dataList: {
    type: Object,
    default: () => { },
  }, disabled: {
    type: Boolean,
    default: false
  },
  showBut: {
    type: Boolean,
    default: false
  },
})
const pageData = reactive({
  tableData: [],
  tableColumn: [{
    prop: 'ypnr',
    label: '研判内容',
  }, {
    prop: 'fj',
    label: '附件',
    showSolt: true,
  }],
  tableHeight: '300px',
  keyCount: 0,
  tableConfiger: {
    border: true,
    stripe: true,
    showHeader: true,
    showIndex: true,
    indexLabel: '序号',
    indexWidth: 60,
    align: 'center',
    showOverflowTooltip: true,
  },
  controlsWidth: 200,
})
const listData = ref({})
watch(() => props.dataList, (val) => {
  if (val) {
    listData.value = val
    gettbZdqtYpSelectPage()
  }
}, { deep: true })

const JudgmentShow = ref(false)
const modifyShow = ref(true)
const dataAmend = ref()
const onComfirm = (val) => {
  if (modifyShow.value) {
    gettbZdqtYpAdd(val)
    gettbZdqtYpSelectPage()
  } else {
    const promes = { ...val,fj:JSON.stringify(val.fileList) }
    amendantRecord(promes)
  }
}
const openJudgment = (val) => {
  JudgmentShow.value = true;
  modifyShow.value = true
}
const delDictItem = (val) => {
  proxy.$confirm('确定删除吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    deletbZdqtYpDelete(val)
  }).catch(() => {
  });
}

const updeteRecord = (val) => {
  dataAmend.value = val
  JudgmentShow.value = true;
  modifyShow.value = false
}

//查询列表
const gettbZdqtYpSelectPage = () => {
  const promes = {
    pageCurrent: 1,
    pageSize: 1000,
    qtid: props.dataList.id
  }
  tbZdqtYpSelectPage(promes).then((res) => {
    pageData.tableData = res.records.map(item => {
      return {
        ...item,
        fj: item.fj ? JSON.parse(item.fj) : []
      }
    })
  }).catch((err) => {
  });
}
// 新增
const gettbZdqtYpAdd = (val) => {
  const params = {
    qtid: listData.value.id,
    ypnr: val.ypnr,
    fj: val.fileList.length > 0 ? JSON.stringify(val.fileList) : ''
  }
  tbZdqtYpAdd(params).then(res => {
    gettbZdqtYpSelectPage()
    proxy.$message({
      message: '新增成功',
      type: 'success',
    })
  })
}
// 删除
const deletbZdqtYpDelete = (id) => {
  tbZdqtYpDelete({
    ids: [id]
  }).then(res => {
    gettbZdqtYpSelectPage()
    proxy.$message({
      message: '删除成功',
      type: 'success',
    })
  })
}
// 修改
const amendantRecord = (val) => {
  tbZdqtYpUpdate(val).then(res => {
    gettbZdqtYpSelectPage()
    proxy.$message({
      message: '修改成功',
      type: 'success',
    })
  })
}
</script>

<style lang="scss" scoped>
@import "~@/assets/css/layout.scss";
@import "~@/assets/css/element-plus.scss";

.backinfo-container {
  padding: 10px;
}

.left_box {
  width: 200px;
  border: 1px solid #c8c8c89a;
  border-radius: 5px;
  padding: 5px;
}

.right_box {
  width: calc(100% - 230px);
  overflow-y: auto;
  padding: 5px;
}

::v-deep .el-form-item__content {
  display: block !important;
}

.headClass {
  font-size: 18px;
  font-weight: 600;
  color: #303133;
  margin: 0 0 10px 0;
  padding-bottom: 10px;
  border-bottom: 2px solid #409eff;
  position: relative;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.headClass::after {
  content: '';
  position: absolute;
  left: 0;
  bottom: -2px;
  width: 60px;
  height: 2px;
  background-color: #409eff;
}

h3 {
  margin: 0;
}
</style>
