<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"
        ></el-input>
        <div class="seach-btn" @click="getShipOwnerList(1)">查询</div>
      </div>
      <div class="cargo-owner-add" @click="dialogFormVisible = true">
        添加船东
      </div>
      <el-dialog
        title="添加船东"
        v-model="dialogFormVisible"
        @closed="resetForm"
        distory
      >
        <template v-slot:default>
          <el-form
            :model="ruleForm"
            :rules="rules"
            ref="form"
            label-width="110px"
            label-position="left"
          >
            <el-form-item prop="userPhone" label="手机号">
              <el-input
                style="width: 280px"
                v-model="ruleForm.userPhone"
              ></el-input>
            </el-form-item>
            <el-form-item prop="userName" label="船东姓名">
              <el-input
                style="width: 280px"
                v-model="ruleForm.userName"
              ></el-input>
            </el-form-item>
            <el-form-item prop="shipMmsi" label="MMSI">
              <el-input
                style="width: 280px"
                v-model="ruleForm.shipMmsi"
                :disabled="!!ruleForm.shipId"
              ></el-input>
            </el-form-item>
            <el-form-item prop="shipName" label="船名">
              <el-input
                style="width: 280px"
                v-model="ruleForm.shipName"
                :disabled="!!ruleForm.shipId"
              ></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="addShipOwner(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="userName"
          label="船东名称"
          min-width="120"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="userPhone"
          label="手机号"
          min-width="160"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="shipName"
          label="船舶名称"
          min-width="160"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="cargo"
          label="常运货种"
          min-width="160"
          align="center"
        ></el-table-column>
        <!-- <el-table-column
          prop="createTime"
          label="入驻时间"
          min-width="200"
          align="center"
        ></el-table-column>
        <el-table-column label="操作" min-width="80" align="center">
          <template v-slot="scope">
            <el-button
              @click="shipOwnerDetail(scope.row.userId, tableData)"
              type="text"
              size="small"
            >
              查看详情
            </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"
          :total="total"
          @current-change="pageChange"
          :current-page="currentPage"
        ></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";
let dialogFormVisible = ref(false);
let form = ref(null);
let ruleForm = ref({
  userName: "",
  userPhone: "",
  shipName: "",
  shipMmsi: "",
});
async function resetForm() {
  dialogFormVisible.value = false;
  form.value.resetFields();
  ruleForm.value = {};
}

const checkShipOwnerPhone = async (rule, value, callback) => {
  if (value.length != 11) {
    return callback(new Error("请正确输入手机号"));
  }
  let res = await api.checkShipOwnerPhone({
    userPhone: value,
  });
  if (res.data.status == 0) {
    callback(new Error("此手机号已绑定船东!"));
  } else {
    callback();
  }
};

const checkShipMmsi = async (rule, value, callback) => {
  if (value?.length != 9) {
    return callback(new Error("请正确输入MMSI"));
  }
  if (ruleForm.value.shipId) return;

  let res = await api.checkShipMmsi({
    shipMmsi: value,
  });
  if (res.data.status == 0) {
    ElMessageBox.confirm("已查询到船舶信息,是否匹配?", "提示", {
      confirmButtonText: "匹配",
      cancelButtonText: "更换mmsi",
      type: "info",
    })
      .then(() => {
        ruleForm.value = {
          ...ruleForm.value,
          ...res.data.result,
        };
      })
      .catch(() => {
        ruleForm.value.shipMmsi = "";
        ruleForm.value.shipId = "";
        ruleForm.value.shipName = "";
      });
  } else {
    callback();
  }
};

const rules = ref({
  userName: [{ required: true, message: "请填写船东名称", trigger: "blur" }],
  shipName: [{ required: true, message: "请填写船名", trigger: "blur" }],
  shipMmsi: [{ validator: checkShipMmsi, trigger: "blur" }],
  userPhone: [{ validator: checkShipOwnerPhone, trigger: "blur" }],
});

async function addShipOwner() {
  form.value.validate(async (valid) => {
    if (valid) {
      let { userName, shipName, shipMmsi, userPhone } = ruleForm.value;
      let res = await api.addShipOwner(ruleForm.value);
      if (res.data.status == 0) {
        ElNotification.success({
          title: "添加成功",
          duration: 0,
          message: `${userName}:${res.data.msg}`,
          type: "success",
        });
        resetForm();
        getShipOwnerList();
      } else {
        ElNotification.error({
          title: "失败",
          duration: 3000,
          message: res.data.msg,
        });
      }
    } else {
      return false;
    }
  });
}
let currentPage = ref(1);
let term = ref("");
let tableData = ref([]);
let total = ref(0);
async function getShipOwnerList(page) {
  currentPage.value = page || currentPage.value;
  let res = await api.getShipOwnerList({
    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 shipOwnerDetail(userId) {
  router.push({
    path: "/shipOwnerManage/shipOwnerDetail",
    query: {
      userId,
    },
  });
}
function pageChange(e) {
  currentPage.value = e;
  getShipOwnerList();
}
onMounted(() => {
  getShipOwnerList();
});
</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>
