<template>
  <div>
    <div class="headClass" style="">
      <h3>集会历史</h3>
      <el-button type="primary" @click="openHistory"  v-if="showBut">新增</el-button>
    </div>
    <MyTable :tableData="pageData.tableData" :tableColumn="pageData.tableColumn" :tableHeight="pageData.tableHeight"
      :key="pageData.keyCount" :tableConfiger="pageData.tableConfiger" :controlsWidth="pageData.controlsWidth">
      <template #timeQuantum="{ row }">
        {{ `${row.kssj}至${row.jssj}` }}
      </template>
      <template #ssry="{ row }">
        <span v-for="(item, index) in row.ssry" :key="index">{{ item.ryXm }}<span
            v-if="index < row.ssry.length - 1">,</span></span>
      </template>
      <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="modification(row)">修改</el-link>
        <el-link type="danger" @click="delDictItem(row.id)">删除</el-link>
      </template>
    </MyTable>
  </div>
  <History v-model="historyShow" @comfirm="onComfirm" :data="dataAmend" />
</template>
<script setup>
import { ref, reactive, watch,  getCurrentInstance } from "vue";
import MyTable from "@/components/aboutTable/MyTable.vue";
import { ElMessageBox } from 'element-plus'
import { tbZdqtLsjhAdd, tbZdqtLsjhDelete, tbZdqtLsjhSelectPage, tbZdqtLsjhUpdate } from '@/api/qt.js'
import History from "../component/history.vue";
const props = defineProps({
  dataList: {
    type: Object,
    default: () => { },
  }, disabled: {
    type: Boolean,
    default: false
  },
  showBut: {
    type: Boolean,
    default: false
  },
})
const { proxy } = getCurrentInstance();
const pageData = reactive({
  tableData: [],
  tableColumn: [{
    prop: 'timeQuantum',
    label: '活动时间',
    showSolt: true
  }, {
    prop: 'gjxw',
    label: '过激行为',
  }, {
    prop: 'hdcs',
    label: '活动场所',
  }, {
    prop: 'ssry',
    label: '涉事人员',
    showSolt: true
  }, {
    prop: 'fj',
    label: '附件',
    showSolt: true
  }],
  tableHeight: '200px',
  keyCount: 0,
  tableConfiger: {
    border: true,
    stripe: true,
    showHeader: true,
    showIndex: true,
    indexLabel: '序号',
    indexWidth: 60,
    align: 'center',
    showOverflowTooltip: true,
  },
  controlsWidth: 120,

})
const listData = ref({})
const butShow = ref(true)
watch(() => props.dataList, (val) => {
  if (val) {
    listData.value = val
    gettbZdqtLsjhSelectPage()

  }
}, { deep: true })

const historyShow = ref(false);
const dataAmend = ref({});
const onComfirm = (val) => {
  let promes = {
    kssj: val.kssj,
    jssj: val.jssj,
    gjxw: val.gjxw,
    hdcs: val.hdcs,
    ssry: JSON.stringify(val.ssry),
    fj: val.fileList.length > 0 ? JSON.stringify(val.fileList) : '',
    qtid: listData.value.id
  }
  if (butShow.value) {
    tbZdqtLsjhAdd(promes).then((res) => {
      gettbZdqtLsjhSelectPage()
      proxy.$message({
        message: '添加成功',
        type: 'success'
      })
    })
  } else {
    promes.id=val.id
    tbZdqtLsjhUpdate(promes).then((res) => {
      gettbZdqtLsjhSelectPage()
      proxy.$message({
        message: '修改成功',
        type: 'success'
      })
    })
  }

}
const openHistory = () => {
  butShow.value=true
  historyShow.value = true;
}
// 列表
const gettbZdqtLsjhSelectPage = () => {
  const promes = {
    qtid: listData.value.id,
    pageSize: 1000,
    pageCurrent: 1
  }
  tbZdqtLsjhSelectPage(promes).then((res) => {
    pageData.tableData = res.records.map(item => {
      return {
        ...item,
        ssry: item.ssry ? JSON.parse(item.ssry) : [],
        fj: item.fj ? JSON.parse(item.fj) : [],
      }
    })
  })
}
// 删除
const delDictItem = (val) => {

  ElMessageBox.confirm(
    '是否删除该记录?',
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(() => {
      const promes = { ids: [val] }
      tbZdqtLsjhDelete(promes).then((res) => {
        gettbZdqtLsjhSelectPage()
        proxy.$message({
          message: '删除成功',
          type: 'success'
        })
      })
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '取消删除',
      })
    })



}
// 修改
const modification = (val) => {
  butShow.value=false
  historyShow.value = true;
  dataAmend.value = val
}
</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>
