<template>
  <div class="full-container-p24">
    <div class="df jcsb">
      <div style="display: flex">
        <el-input
          placeholder="请输入模板名称"
          prefix-icon="el-icon-search"
          v-model="term"
          clearable
          size="large"
          class="mr30"
          style="width: 300px"
        ></el-input>
        <el-button
          size="large"
          type="primary"
          @click="getSecurityTemplateList(1)"
        >
          查询
        </el-button>
      </div>
      <el-button size="large" type="primary" @click="showDialog">
        创建安检模板
      </el-button>
    </div>
    <div style="margin-top: 24px">
      <el-table border :data="tableData" stripe style="width: 100%">
        <el-table-column
          type="index"
          label="序号"
          width="80"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="securityCheckName"
          label="船东名称"
          min-width="120"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="shipLen"
          label="适用船型"
          min-width="120"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="createTime"
          label="创建时间"
          min-width="120"
          align="center"
        >
          <template v-slot="scope">
            {{ subTimeStr(scope.row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="80" align="center">
          <template v-slot="scope">
            <el-button
              @click="getSecurityTemplateDetail(scope.row.id)"
              type="primary"
              text
              size="small"
            >
              编辑
            </el-button>
            <el-button
              @click="
                deleteSecurityTemplate(
                  scope.row.id,
                  scope.row.securityCheckName
                )
              "
              type="danger"
              text
              size="small"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="df aic jcfe mt40 mr20">
        <el-pagination
          background
          layout="prev, pager, next"
          :current-page="currentPage"
          :total="total"
          @current-change="pageChange"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>
<script setup>
import { ref, h, reactive, toRefs, onMounted } from "vue";
import { ElNotification, ElMessageBox, ElMessage } from "element-plus";
import store from "../../store";
import router from "../../router";
import md5 from "md5";
import api from "../../apis/fetch";
import { subTimeStr } from "../../utils/utils";

function showDialog() {
  router.push("/shipSecurityManage/shipCheckTemplateDetail");
}
let currentPage = ref(1);
let term = ref("");
let tableData = ref([]);
let total = ref(0);
async function getSecurityTemplateList(page) {
  currentPage.value = page || currentPage.value;
  let res = await api.getSecurityTemplateList({
    currentPage: currentPage.value,
    size: 10,
    term: term.value,
  });
  if (res.data.status == 0) {
    tableData.value = res.data.result;
    total.value = res.data.total;
  } else {
    tableData.value = [];
    total.value = 0;
  }
}

async function getSecurityTemplateDetail(id) {
  router.push({
    path: "/shipSecurityManage/shipCheckTemplateDetail",
    query: {
      id,
    },
  });
}

function deleteSecurityTemplate(id, securityCheckName) {
  ElMessageBox.confirm(`确认删除安检模板: ${securityCheckName} ?`, "提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(async () => {
      let { data } = await api.deleteSecurityTemplate({
        id,
      });
      if (data.status == 0) {
        ElMessage({
          type: "success",
          message: data.msg,
          duration: 1500,
        });
      }
      getSecurityTemplateList();
    })
    .catch(() => {});
}

function pageChange(e) {
  currentPage.value = e;
  getSecurityTemplateList();
}
onMounted(() => {
  getSecurityTemplateList();
});
</script>
<style scoped>
.seach-btn {
  display: inline-block;
  width: 60px;
  height: 38px;
  background: #0094fe;
  border-radius: 2px;
  font-size: 14px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #ffffff;
  text-align: center;
  line-height: 38px;
  margin-left: 10px;
  cursor: pointer;
}

.cargo-owner-add {
  width: 80px;
  height: 32px;
  border-radius: 2px;
  border: 1px solid #0094fe;
  font-size: 14px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #0094fe;
  line-height: 32px;
  text-align: center;
  cursor: pointer;
}

:deep().el-dialog {
  width: 560px;
  padding: 20px 50px;
  border-radius: 6px;
}

:deep() .el-dialog__title {
  font-size: 18px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #0094fe;
}
</style>
