<template>
  <div>
    <MOSTY.PageTree :pageTitle="'安全荣誉列表'" :searchType="'03'" @getDeptId="getdeptId">
      <template #serach>
        <el-divider />
        <MOSTY.Search :searchArr="searchArr" @submit="onSearch" />
        <el-divider />
        <div class="btnBox">
          <el-button type="primary" @click="addUserHander">
            <el-icon>
              <Plus />
            </el-icon>
            <span>新增</span>
          </el-button>
        </div>
      </template>
      <template #table>
        <section class="eltab9">
          <MOSTY.Table :table-columns="tableColumn" :tableData="tableData" :tableloading="tableloading"
            :isSelect="false" :isSort="true" :actionsW="250" @SortChange="SortChange" style="width: 100%;">
            <template #level="{ row }">
              <dict-tag :options="D_BZ_LWJB" :value="row.level" :tag="false" />
            </template>
            <template #actions="{ row }">
              <span style="margin-left: 0;" class="operedit" @click="editClick(row)"><el-icon>
                  <EditPen />
                </el-icon>修改</span>
              <el-popconfirm 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="seeClick(row)"><el-icon>
                  <View />
                </el-icon>查看
              </span>
              <el-popconfirm confirm-button-text="是" cancel-button-text="否" icon-color="red" :title="`确定${row.findVerifyFlag == '1'?'取消':''}查证?`"
                @confirm="sheckClick(row)">
                <template #reference>
                  <span class="operedown" size="small">
                    {{ row.findVerifyFlag == '1' ? '取消' : '' }}查证</span>
                </template>
              </el-popconfirm>
            </template>
          </MOSTY.Table>
        </section>
        <div class="fenye">
          <MOSTY.Pages :pageConfiger="pageConfiger" @changeSize="handleSizeChange" @changeNo="handleCurrentChange">
          </MOSTY.Pages>
        </div>
      </template>
    </MOSTY.PageTree>

    <!-- 修改 -->
    <div v-if="comprehensiveShow" class="dialog-box">
      <EditAqry @close="close" :title="title" :orgcode="orgcode" :rowId="rowId" :saveBtnShow="saveBtnShow" />
    </div>
  </div>
</template>

<script setup>
// 引入
import { onMounted, reactive, ref, getCurrentInstance } from "vue";
import { filterYear } from "@/utils/auth.js";
import { ElMessage } from "element-plus";
import EditAqry from "./commponents/EditAqry.vue";
import * as MOSTY from "@/components/MyComponents/index";
import { getApi, deleteApi, putApi, postApi } from "@/api/tobAcco_api.js";
const { proxy } = getCurrentInstance();
const { D_BZ_LWJB } = proxy.$dict("D_BZ_LWJB");
const searchArr = reactive([
  {
    showType: "select",
    prop: "year",
    width: '250px',
    defaultVal: new Date().getFullYear(),
    options: filterYear(7),
  },
  {
    showType: "input",
    prop: "username",
    width: '250px',
    placeholder: '请输入人员姓名查询'
  }
]);
const tableColumn = ref([
  {
    title: "年度",
    prop: "year",
    sortable: true
  },
  {
    title: "荣誉名称",
    prop: "titlename",
    sortable: true
  },
  {
    title: "获奖等级",
    prop: "level",
    slot: true,
    sortable: true
  },
  {
    title: "获奖对象",
    prop: "hjdx",
    sortable: true
  },
  {
    title: "获奖时间",
    prop: "time",
    sortable: true
  },
  {
    title: "颁发单位",
    prop: "fbjg",
    sortable: true
  },
  {
    title: "操作",
    prop: "actions",
    slot: true
  }
])
const pageConfiger = reactive({
  pageSize: 20,
  pageCurrent: 1,
  total: 0
})
const comprehensiveShow = ref(false);
const listQuery = reactive({
  pageCurrent: 1,
  pageSize: 20,
  type: '2',
  year: new Date().getFullYear(),
});
const rowId = ref(null);
const saveBtnShow = ref(true);
const title = ref("新增");
const tableData = ref([
]);

function onSearch(obj) {
  listQuery.pageCurrent = 1
  pageConfiger.pageCurrent = 1
  listQuery.year = obj.year
  listQuery.username = obj.username
  getListData();
}
/** 查证 */
const sheckClick = (row) => {
  let findVerifyFlag = row.findVerifyFlag == '1' ? '2' : '1' // 查证 1 是 2 否
  postApi({ id: row.id, findVerifyFlag }, `/mosty-jcgl/safetypaperCommend/findVerify`).then(
    (res) => {
      let successTxt = row.findVerifyFlag == '1' ? '取消查证成功':'查证成功'
      ElMessage.success(successTxt);
      getListData();
    }
  );
};
function close() {
  rowId.value = null
  comprehensiveShow.value = false
  getListData()
}
//获取列表数据
const tableloading = ref(false);
function getListData() {
  tableloading.value = true;
  postApi(listQuery, "/mosty-jcgl/safetypaperCommend/getPageList").then((res) => {
    if (res.records) {
      tableloading.value = false;
      for (let i = 0; i < res.records.length; i++) {
        res.records[i].hjdx = res.records[i].honorOrgname ? res.records[i].honorOrgname : res.records[i].username
      }
      tableData.value = res.records;
      pageConfiger.total = res.total;
    } else {
      tableData.value = [];
      tableloading.value = false;
    }
  }).finally(()=>{
    tableloading.value = false;
  });
}

//分页
const handleSizeChange = (currentSize) => {
  listQuery.pageSize = currentSize;
  pageConfiger.pageSize = currentSize;
  getListData();
};
const handleCurrentChange = (currentPage) => {
  listQuery.pageCurrent = currentPage;
  pageConfiger.pageCurrent = currentPage;
  getListData();
};
//选择右侧部门
const getdeptId = (val) => {
  listQuery.orgcode = val.id;
  listQuery.pageCurrent = 1;
  getListData();
};
// 方法

//新增
const addUserHander = () => {
  title.value = "安全荣誉新增";
  orgcode.value = listQuery.orgcode
  comprehensiveShow.value = true;
  rowId.value = null;
  saveBtnShow.value = true;
};

// 编辑
const orgcode = ref("")
const editClick = (row) => {
  title.value = "安全荣誉修改";
  orgcode.value = listQuery.orgcode
  comprehensiveShow.value = true;
  saveBtnShow.value = true;
  rowId.value = row.id
};
// 查看
const seeClick = (row) => {
  title.value = "安全荣誉详情";
  comprehensiveShow.value = true;
  rowId.value = row.id
  saveBtnShow.value = false;
};
//删除
const handledelete = (row) => {
  postApi({}, `/mosty-jcgl/safetypaperCommend/del/${row.id}`).then(
    (res) => {
      ElMessage.success("删除成功");
      getListData();
    }
  );
};



onMounted(() => {
});
</script>

<style lang="scss" scoped>
.btnBox {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  margin-bottom: 12px;
}
</style>