<template>
  <div class="full-container-p24">
    <div style="display: flex; justify-content: space-between">
      <div style="display: flex">
        <el-input
          placeholder="请输入货种名称"
          prefix-icon="el-icon-search"
          v-model="term"
          clearable
          style="height: 32px; width: 330px; line-height: 32px"
          @keyup.enter="getCargoList"
        ></el-input>
        <div class="seach-btn" @click="getCargoList">查询</div>
      </div>
      <el-button v-auth="'ADDCARGO'" type="primary" @click="addCargo">
        添加货种
      </el-button>
    </div>

    <div style="margin-top: 24px">
      <el-table :data="tableData" stripe style="width: 100%">
        <el-table-column
          type="index"
          label="序号"
          min-width="80"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="cargo"
          label="货种名称"
          min-width="120"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="createTime"
          label="添加时间"
          min-width="160"
          align="center"
        >
          <template v-slot="scope">
            {{ subTimeStr(scope.row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="80" align="center">
          <el-button disabled size="small" type="danger">删除</el-button>
        </el-table-column>
      </el-table>
      <div style="width: 100%; text-align: right; margin-top: 43px">
        <el-pagination
          background
          layout="prev, pager, next"
          :total="total"
          @current-change="pageChange"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>

<script setup>
import api from "../../apis/fetch";
import { ref, onMounted } from "vue";
import { ElNotification, ElMessageBox } from "element-plus";
import { subTimeStr } from "../../utils/utils";

let tableData = ref([]);
let currentPage = ref(1);
let total = ref(0);
let term = ref("");
let loginAccountId = ref(0);
let cargo = ref("");

async function getCargoList() {
  let res = await api.getCargoList({
    term: term.value,
    currentPage: currentPage.value,
    size: 10,
    loginAccountId: loginAccountId.value,
  });
  if (res.data.status == 0) {
    tableData.value = res.data.result;
    total.value = res.data.total;
  } else {
    tableData.value = [];
    total.value = 0;
  }
}

function pageChange(e) {
  currentPage.value = e;
  getCargoList();
}
function addCargo() {
  ElMessageBox.prompt("请输入货种名称", "添加货种", {
    confirmButtonText: "添加",
    cancelButtonText: "取消",
    inputPattern: /^[\s\S]*.*[^\s][\s\S]*$/,
    inputErrorMessage: "请输入货种名称",
  })
    .then(async ({ value }) => {
      let res = await api.addCargo({
        loginAccountId: loginAccountId.value,
        cargo: value,
      });
      if (res.data.status == 0) {
        ElNotification({
          title: "成功",
          duration: 1500,
          message: res.data.msg,
          type: "success",
        });
        getCargoList();
      } else {
        ElNotification({
          title: "失败",
          duration: 1500,
          message: res.data.msg,
          type: "error",
        });
      }
    })
    .catch(() => {});
}

function closeModal() {
  cargo.value = "";
}

onMounted(() => {
  loginAccountId.value = localStorage.loginAccountId;
  getCargoList();
});
</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;
}
</style>
