<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="width: 330px"
        ></el-input>
        <div class="seach-btn" @click="getTransPortsList(1)">查询</div>
      </div>
      <div class="cargo-owner-add" @click="dialogFormVisible = true">
        添加航期
      </div>
      <el-dialog
        title="添加航期"
        @closed="resetForm()"
        v-model="dialogFormVisible"
      >
        <template v-slot:default>
          <el-form
            :model="ruleForm"
            :rules="rules"
            ref="form"
            label-width="110px"
            label-position="left"
          >
            <el-form-item prop="cargoOwnerIds" label="货主名称">
              <RemoteSelect
                api="getCargoOwnerCompanySelect"
                v-model="cargoOwnerCompanyStr"
                multiple
                @selectItem="selectCargoOwnerCompany($event)"
                class="mb10"
              ></RemoteSelect>
            </el-form-item>
            <el-form-item prop="departurePortId" label="始发港">
              <RemoteSelect
                api="getPortsSelect"
                v-model="departurePortStr"
                @selectItem="selectDeparturePort($event)"
                class="mb10"
              ></RemoteSelect>
            </el-form-item>
            <el-form-item prop="destinationPortId" label="目的地港">
              <RemoteSelect
                api="getPortsSelect"
                v-model="destinationPortStr"
                @selectItem="selectDdestinationPort($event)"
                class="mb10"
              ></RemoteSelect>
            </el-form-item>
            <el-form-item prop="sailingPeriod" label="航期">
              <el-input
                style="width: 193px"
                v-model="ruleForm.sailingPeriod"
              ></el-input>
            </el-form-item>
          </el-form>
        </template>
        <template v-slot:footer>
          <div class="dialog-footer">
            <el-button @click="resetForm">取 消</el-button>
            <el-button type="primary" @click="addTransPort(ruleForm)">
              确 定
            </el-button>
          </div>
        </template>
      </el-dialog>
    </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="cargoOwnerName"
          label="货主名称"
          min-width="120"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="departurePort"
          label="始发港"
          min-width="120"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="destinationPort"
          label="目的地港"
          min-width="120"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="sailingPeriod"
          label="航期"
          min-width="120"
          align="center"
        >
          <template v-slot="scope"> {{ scope.row.sailingPeriod }} 天 </template>
        </el-table-column>
        <el-table-column
          prop="createTime"
          label="添加时间"
          min-width="200"
          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 size="mini" type="danger">删除</el-button>
          </template>
        </el-table-column> -->
      </el-table>
      <div style="width: 100%; text-align: right; margin-top: 43px">
        <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";

let currentPage = ref(1);
let term = ref("");
let tableData = ref([]);
let total = ref(0);
let dialogFormVisible = ref(false);
let form = ref(null);
const ruleForm = ref({
  cargoOwnerIds: [],
  departurePortId: "",
  destinationPortId: "",
  sailingPeriod: "",
});
const rules = ref({
  cargoOwnerIds: [
    { required: true, message: "请填写货主名称", trigger: "blur" },
  ],
  departurePortId: [
    { required: true, message: "请填写始发港", trigger: "blur" },
  ],
  destinationPortId: [
    { required: true, message: "请填写目的地港", trigger: "blur" },
  ],
  sailingPeriod: [{ required: true, message: "请填写航期", trigger: "blur" }],
});
async function getTransPortsList(page) {
  currentPage.value = page || currentPage.value;
  let res = await api.getTransPortsList({
    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;
  }
}
function resetForm() {
  dialogFormVisible.value = false;
  cargoOwnerCompanyStr.value = [];
  departurePortStr.value = "";
  destinationPortStr.value = "";
  form.value.resetFields();
}
async function addTransPort() {
  form.value.validate(async (valid) => {
    if (valid) {
      let { cargoOwnerIds, departurePortId, destinationPortId, sailingPeriod } =
        ruleForm.value;
      let res = await api.addTransPort({
        cargoOwnerIds,
        departurePortId,
        destinationPortId,
        sailingPeriod,
      });
      console.log(res);
      if (res.data.status == 0) {
        ElNotification.success({
          title: "添加成功",
          duration: 2000,
          message: `${cargoOwnerIds}:${res.data.msg}`,
          type: "success",
        });
        resetForm();
        getTransPortsList();
      } else {
        ElNotification.error({
          title: "失败",
          duration: 3000,
          message: res.data.msg,
        });
      }
    } else {
      return false;
    }
  });
}

async function changeSwitch(status, portId) {
  let res = await api.updatePortStatus({
    status,
    portId,
  });
  if (res.data.status == 0) {
    ElNotification.success({
      title: "成功",
      duration: 2000,
      message: res.data.msg,
    });
  }
}

async function cargoOwnerCompanyDetail(id) {
  router.push({
    path: "/cargoOwnerManage/cargoOwnerCompanyDetail",
    query: {
      id,
    },
  });
}
function pageChange(e) {
  currentPage.value = e;
  getTransPortsList();
}

let cargoOwnerCompanyStr = ref([]);
function selectCargoOwnerCompany(e) {
  console.log(e);
  let arr = [];
  for (let i of e) {
    arr.push(i.value);
  }
  ruleForm.value.cargoOwnerIds = arr.join(",");
}

let departurePortStr = ref("");
function selectDeparturePort(e) {
  ruleForm.value.departurePortId = e.value;
}

let destinationPortStr = ref("");
function selectDdestinationPort(e) {
  ruleForm.value.destinationPortId = e.value;
}
let options = ref([
  {
    value: "武汉",
    key: "1",
  },
  {
    value: "南京",
    key: "2",
  },
  {
    value: "郑州",
    key: "3",
  },
]);

let cargoOwnerId = ref("");

async function remoteMethod(term) {
  let res = await api.getCargoOwnerCompanySelect({
    term,
  });
  options.value = res.data.result;
  console.log(res);
}

function selectCargoOwner(e) {
  console.log(e);
}
let loading = ref(false);

onMounted(() => {
  getTransPortsList();
});
</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: 15px;
  cursor: pointer;
  box-sizing: border-box;
}

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

: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>
