<template>
  <div>
    <!-- 查询 -->
    <div class='tabox'>
      <div class="headBox3">
        <el-date-picker v-model="pageData.listQuery.yearly" type="year" format="YYYY" value-format="YYYY"
          placeholder="请选择年份进行查询" style="width: 337px">
        </el-date-picker>
        <el-button type="primary" @click="Searchdata">
          <el-icon>
            <Search />
          </el-icon>
          <span>查询</span>
        </el-button>
        <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 #installs="{ row }">
          <dict-tag v-if="row.installs" :options="D_BZ_SF" :value="row.installs" :tag="false" />
          <span v-else>无</span>
        </template>
        <template #actions="{ row }">
          <span class="operedit" @click="handlexg(row)" 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)">
            <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="[2, 5, 10, 20]"
              :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="addbpdShow" class="dialog-box">
    <Addfgzindex :formData="formData" @changePage="changePage" :type="type" />
  </div>
</template>
<script setup>
import * as MOSTY from "@/components/MyComponents/index";
import Addfgzindex from "@/components/dialog/Addfgzindex.vue";
import { reactive, ref, onMounted, getCurrentInstance } from "vue"
import { getApi, postApi } from "@/api/tobAcco_api.js";
import { ElMessage } from "element-plus";
const { proxy } = getCurrentInstance();
const { D_BZ_SF } = proxy.$dict("D_BZ_SF");
const emits = defineEmits(["handleChange", 'searchChange']);
const tableloading = ref(false);
const addbpdShow = ref(false);
const total = ref(0)
const formData = ref({
  title: "",
  saveBtnShow: true,
});
const props = defineProps({
  obj: {
    default: null,
    type: Object
  }
});
const pageData = reactive({
  listQuery: {
    pageCurrent: 1,
    pageSize: 5
  },
  tableColumns: [
    {
      title: "人员",
      prop: "username"
    },
    {
      title: "部门",
      prop: "deptname"
    },
    {
      title: "申请时间",
      prop: "time"
    },
    {
      title: "是否安装",
      prop: "installs",
      slot: true
    },
    {
      title: "措施内容",
      prop: "content"
    },

    {
      title: "操作",
      prop: "actions",
      slot: true
    }
  ],
  tableData: []
})
onMounted(() => {
  pageData.listQuery.buildid = props.obj.id
  gettablist()
})
//分页查看 防高坠措施
function gettablist() {
  tableloading.value = true
  postApi(pageData.listQuery, "/mosty-jcgl/tobaccoHigh/listTobaccoHigh").then((res) => {
    if (res) {
      tableloading.value = false
      pageData.tableData = res.records
      total.value = res.total
    }
  })
}
//新增
function addUserHander() {
  formData.value.title = '防高坠措施新增';
  formData.value.buildid = props.obj.id
  formData.value.buildname = props.obj.placename
  formData.value.saveBtnShow = true;
  addbpdShow.value = true
}
//修改
function handlexg(row) {
  formData.value.title = '防高坠措施修改';
  formData.value.id = row.id
  formData.value.saveBtnShow = true;
  addbpdShow.value = true
}
//详情
function handleSee(row) {
  formData.value.title = '防高坠措施详情';
  formData.value.id = row.id
  formData.value.saveBtnShow = false;
  addbpdShow.value = true
}
//注销
function handledelete(row) {
  postApi({}, `/mosty-jcgl/tobaccoHigh/deleteTobaccoHighOne/${row.id}`).then(res => {
    ElMessage.success("删除成功");
    emits("searchChange", 'fgzcs');
    gettablist()
  })
}
//关闭
const changePage = (val, obj) => {
  formData.value = {}
  addbpdShow.value = false
  emits("searchChange", 'fgzcs');
  if (obj) {
    //新增成功不刷新表格直接追加
    pageData.tableData.unshift(obj);
    total.value + 1
  } else {
    //修改不追加，直接刷新
    gettablist()
  }
}
//查询
function Searchdata() {
  gettablist()
}
//页码发生变化
const handleCurrentChange = (val) => {
  pageData.listQuery.pageCurrent = val;
  gettablist()
};
//查询条数发生变化
const handleSizeChange = (val) => {
  pageData.listQuery.pageSize = val;
  gettablist()
};
</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>
