<template>
  <div>
    <!-- 查询 -->
    <div class='tabox'>
      <div class="headBox3">
        <div class="mr10">
          <el-input placeholder="请输入内部编号进行查询" v-model="pageData.listQuery.nbbh"
            style='width:250px;margin-right:10px;'></el-input>
          <el-button type="primary" @click="Searchdata">
            <el-icon>
              <Search />
            </el-icon>
            <span>查询</span>
          </el-button>
        </div>
        <el-button type="primary" @click="addUserHander" v-if='props.obj.saveBtnShow'>
          <el-icon>
            <CirclePlus />
          </el-icon>
          <span>新增</span>
        </el-button>
      </div>
      <MOSTY.Table :table-columns="pageData.tableColumns" :tableData="pageData.tableData" :tableloading="tableloading"
        :tableHeight="'40vh'">
        <template #jczq="{ row }">
          {{ row.jczq ? row.jczq + '月' : '' }}
        </template>
        <template #responsibilitypersonname="{ row }">
          <span>{{ row.responsibilitypersonname ? row.responsibilitypersonname.replace(/（null）/g, '') : '' }}</span>
        </template>
        <template #actions="{ row, $index }">
          <el-popconfirm confirm-button-text="是" cancel-button-text="否" icon-color="red" title="复制该条数据并进行数据编辑?"
            @confirm="handlcopy(row, $index)" v-if='props.obj.saveBtnShow'>
            <template #reference>
              <span class="operecopy" size="small">
                <el-icon>
                  <CopyDocument />
                </el-icon>
                复制</span>
            </template>
          </el-popconfirm>
          <span class="operedit" @click="handlexg(row, $index)" v-if='props.obj.saveBtnShow'>
            <el-icon>
              <EditPen />
            </el-icon>修改</span>
          <el-popconfirm v-if='props.obj.saveBtnShow' confirm-button-text="是" cancel-button-text="否" icon-color="red"
            title="确定要删除?" @confirm="handledelete(row, $index)">
            <template #reference>
              <span class="operdel" size="small">
                <el-icon>
                  <Delete />
                </el-icon>
                删除</span>
            </template>
          </el-popconfirm>
          <span class="operesee" @click="handleSee(row)">
            <el-icon>
              <Compass />
            </el-icon>查看</span>
        </template>
        <template #fenye>
          <div class="fenye" :style="{ top: tableHeight + 'px' }">
            <el-pagination class="pagination" @size-change="handleSizeChange" @current-change="handleCurrentChange"
              :current-page="pageData.listQuery.pageCurrent" background :page-sizes="[10, 20, 50, 100]"
              :page-size="pageData.listQuery.pageSize" layout="total,  prev, pager, next, sizes, jumper"
              :total="total"></el-pagination>
          </div>
        </template>
      </MOSTY.Table>
    </div>
  </div>
  <!--安全装置-->
  <div v-if="showdialogjzw" class="dialog-box">
    <Addaqzz :formData="formData" :obj="props.obj" @saveSuccess="saveSuccess" :type="props.obj.type" :opt="props.opt" />
  </div>
</template>
<script setup>
import * as MOSTY from "@/components/MyComponents/index";
import Addaqzz from "@/components/dialog/Addaqzz.vue";
import { reactive, ref, onMounted } from "vue"
import { postApi } from "@/api/tobAcco_api.js";
import { ElMessage } from "element-plus";
const emits = defineEmits(["handleChange", 'searchChange']);
const props = defineProps({
  obj: {
    default: null,
    type: Object
  },
  opt: {
    default: { type: '01' },
    type: Object
  }
});
const tableloading = ref(false);
const showdialogjzw = ref(false);
const total = ref(0)
const formData = ref({
  title: ""
});
/** type 对应的标题类型 */
const titleObj = {
  '01': '安全装置',
  '02': '防护装置',
  '03': '保护装置',
  '04': '安全附件'
}
const pageData = reactive({

  listQuery: {
    pageCurrent: 1,
    pageSize: 10,
    type: props.opt.type || '01', // 01 安全装置 02 防护装置 03 保护装置 04 安全附件
  },
  tableColumns: [
    {
      title: "装置名称",
      prop: "name"
    },
    {
      title: "内部编号",
      prop: "nbbh"
    },
    {
      title: "维修或更换时间",
      prop: "jczq",
      slot: true
    },
    {
      title: "责任人",
      prop: "responsibilitypersonname",
      slot: true

    },
    {
      title: "操作",
      prop: "actions",
      slot: true
    }
  ],
  tableData: []
})
onMounted(() => {
  pageData.listQuery.zbid = props.obj.id
  //获取子表
  getzblist()
});
//获取子表
function getzblist() {
  postApi(pageData.listQuery, '/mosty-jcgl/SbglAqzz/queryTbSbglAqzz').then(res => {
    pageData.tableData = res.records
      ? res.records : [];
    total.value = res.total
  })
}
//新增
function addUserHander() {
  formData.value.siteid = props.obj.siteid ? props.obj.siteid : null
  formData.value.zbid = props.obj.id ? props.obj.id : null
  formData.value.sitename = props.obj.sitename ? props.obj.sitename : null
  formData.value.buildingname = props.obj.buildingname ? props.obj.buildingname : null
  formData.value.buildingid = props.obj.buildingid ? props.obj.buildingid : null
  formData.value.floorid = props.obj.floorid ? props.obj.floorid : null
  formData.value.floorname = props.obj.floorname ? props.obj.floorname : null
  formData.value.partname = props.obj.partname ? props.obj.partname : null
  formData.value.partid = props.obj.partid ? props.obj.partid : null
  formData.value.publicarea = props.obj.publicarea ? props.obj.publicarea : null;
  formData.value.sfcq = props.obj.sfcq ? props.obj.sfcq : null;
  formData.value.checktype = props.obj.checktype ? props.obj.checktype : null;
  formData.value.qtmc = props.obj.qtmc ? props.obj.qtmc : null;
  formData.value.responsibilitydeptname = props.obj.responsibilitydeptname ? props.obj.responsibilitydeptname : null
  formData.value.responsibilitydeptid = props.obj.responsibilitydeptid ? props.obj.responsibilitydeptid : null
  formData.value.responsibilitypersonname = props.obj.responsibilitypersonname ? props.obj.responsibilitypersonname : null
  formData.value.responsibilityperson = props.obj.responsibilityperson ? props.obj.responsibilityperson : null
  formData.value.type = props.opt.type ? props.opt.type : '01'
  // formData.value.title = '安全装置新增'; //
  formData.value.title = titleObj[props.opt.type || '01'] + '新增';
  formData.value.saveBtnShow = true;
  showdialogjzw.value = true
  tableloading.value = false
}
//复制
function handlcopy(row) {
  formData.value.id = row.id;
  formData.value.publicarea = props.obj.publicarea ? props.obj.publicarea : null;
  formData.value.sfcq = props.obj.sfcq ? props.obj.sfcq : null;
  formData.value.checktype = props.obj.checktype ? props.obj.checktype : null;
  formData.value.qtmc = props.obj.qtmc ? props.obj.qtmc : null;
  // formData.value.title = '安全装置修改';
  formData.value.title = '装置复制';
  formData.value.saveBtnShow = true;
  showdialogjzw.value = true
}
//修改
function handlexg(row) {
  formData.value.id = row.id;
  formData.value.publicarea = props.obj.publicarea ? props.obj.publicarea : null;
  formData.value.sfcq = props.obj.sfcq ? props.obj.sfcq : null;
  formData.value.checktype = props.obj.checktype ? props.obj.checktype : null;
  formData.value.qtmc = props.obj.qtmc ? props.obj.qtmc : null;
  // formData.value.title = '安全装置修改';
  formData.value.title = titleObj[props.opt.type || '01'] + '修改';
  formData.value.saveBtnShow = true;
  showdialogjzw.value = true
}
//查看
function handleSee(row) {
  formData.value.id = row.id;
  formData.value.publicarea = props.obj.publicarea ? props.obj.publicarea : null;
  formData.value.sfcq = props.obj.sfcq ? props.obj.sfcq : null;
  formData.value.checktype = props.obj.checktype ? props.obj.checktype : null;
  formData.value.qtmc = props.obj.qtmc ? props.obj.qtmc : null;
  // formData.value.title = '安全装置详情';
  formData.value.title = titleObj[props.opt.type || '01'] + '详情';
  formData.value.saveBtnShow = false;
  showdialogjzw.value = true
}
//删除
function handledelete(row) {
  postApi({}, `/mosty-jcgl/SbglAqzz/deleteTbSbglAqzzOne/${row.id}`).then(res => {
    ElMessage.success("删除成功");
    emits("searchChange", 'mhxt');
    getzblist()
  })

}

const saveSuccess = async (val, obj) => {
  formData.value = {}
  showdialogjzw.value = false
  if (obj) {
    //新增成功不刷新表格直接追加
    pageData.tableData.unshift(obj);
    total.value + 1
  } else {
    //修改不追加，直接刷新
    getzblist()
  }
  emits("searchChange", 'aqzz');
}
//查询
function Searchdata() {
  getzblist()
}
//页码发生变化
const handleCurrentChange = (val) => {
  pageData.listQuery.pageCurrent = val;
  getzblist()
};
//查询条数发生变化
const handleSizeChange = (val) => {
  pageData.listQuery.pageSize = val;
  getzblist()
};
</script>

<style lang="scss" scoped>
.headBox3 {
  display: flex;
  justify-content: end;
  width: 100%;
  margin-bottom: 10px;
}

.top_ul {
  display: flex;
  justify-content: start;
  flex-wrap: wrap;

  >li {
    width: 15%;
    height: 83px;
    margin: 6px 32px;
  }
}
</style>
