<template>
  <div class="line-container-p24">
    <i class="el-icon-arrow-left"></i>
    <div
      class="dib go-back ml8"
      @click="router.replace('/agencyManage/agencyCompanyList')"
    >
      返回代理公司列表
    </div>
  </div>
  <div class="container-title">代理信息</div>
  <div class="line-container-p24 df aic">
    <div class="normal-label">代理名称</div>
    <div class="show-input">{{ proxyName }}</div>
    <div class="normal-label">联系人</div>
    <div class="show-input">{{ contactName }}</div>
    <div class="normal-label">联系人手机号</div>
    <div class="show-input">{{ contactPhone }}</div>
  </div>
  <div class="container-title">货主信息</div>
  <div class="line-container-p24">
    <div style="display: flex; justify-content: space-between">
      <div style="display: flex">
        <el-input
          placeholder="公司名称/联系人/手机号"
          prefix-icon="el-icon-search"
          v-model="term2"
          clearable
          style="width: 330px"
        ></el-input>
        <div class="seach-btn" @click="getCargoOwnerCompanyByAgencyCompany">
          查询
        </div>
      </div>
      <div class="cargo-owner-add" @click="dialogFormVisible2 = true">
        关联货主公司
      </div>
      <el-dialog
        title="关联货主公司"
        v-model="dialogFormVisible2"
        @closed="resetForm2"
      >
        <template v-slot:default>
          <el-form
            :model="ruleForm2"
            :rules="rules2"
            ref="form2"
            label-width="110px"
            label-position="left"
          >
            <el-form-item prop="cargoOwnerId" label="货主公司">
              <RemoteSearch
                api="getCargoOwnerCompanySelect"
                v-model="cargoOwnerCompanyStr"
                placeholder="公司名称/联系人/手机号"
                @selectItem="selectCargoOwner($event)"
                class="mb10"
              ></RemoteSearch>
            </el-form-item>
          </el-form>
        </template>
        <template v-slot:footer>
          <div class="dialog-footer">
            <el-button @click="resetForm2">取 消</el-button>
            <el-button type="primary" @click="relateCargoAgency(ruleForm2)">
              确认关联
            </el-button>
          </div>
        </template>
      </el-dialog>
    </div>
    <div style="margin-top: 24px">
      <el-table :data="tableData2" stripe style="width: 100%">
        <el-table-column
          type="index"
          label="序号"
          min-width="80"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="proxyName"
          label="公司名称"
          min-width="120"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="contactName"
          label="联系人"
          min-width="120"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="contactPhone"
          label="联系人手机号"
          min-width="160"
          align="center"
        ></el-table-column>
      </el-table>
      <div style="width: 100%; text-align: right; margin-top: 43px">
        <el-pagination
          background
          layout="prev, pager, next"
          :total="total2"
          @current-change="pageChange2"
        ></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 { useRoute } from "vue-router";
import api from "../../apis/fetch";
import { subTimeStr } from "../../utils/utils";

const route = useRoute();
let proxyName = ref();
let contactName = ref();
let contactPhone = ref();
async function getAgencyCompanyDetail() {
  let res = await api.getAgencyCompanyDetail({
    proxyId: route.query.id,
  });
  if (res.data.status == 0) {
    proxyName.value = res.data.result.proxyName;
    contactName.value = res.data.result.contactName;
    contactPhone.value = res.data.result.contactPhone;
  } else {
    console.log(res);
  }
}

let tableData2 = ref([]);
let term2 = ref("");
let currentPage2 = ref(1);
let total2 = ref(0);
let dialogFormVisible2 = ref(false);
let form2 = ref(null);

async function getCargoOwnerCompanyByAgencyCompany() {
  tableData2.value = [];
  let res = await api.getCargoOwnerCompanyByAgencyCompany({
    proxyId: route.query.id,
    currentPage: currentPage2.value,
    size: 10,
    term: term2.value,
  });
  term2.value = "";
  if (res.data.status == 0) {
    tableData2.value = res.data.result;
    total2.value = res.data.total;
  }
}
let cargoOwnerCompanyStr = ref("");
const ruleForm2 = ref({
  cargoOwnerId: "",
});
const rules2 = ref({
  cargoOwnerId: [
    { required: true, message: "请选择货主公司", trigger: "blur" },
  ],
});

function selectCargoOwner(item) {
  ruleForm2.value.cargoOwnerId = item.key;
  console.log(ruleForm2.value);
}

function resetForm2() {
  dialogFormVisible2.value = false;
  cargoOwnerCompanyStr.value = "";
  form2.value.resetFields();
}

function pageChange2(e) {
  currenrPage2.value = e;
  getCargoOwnerCompanyByAgencyCompany();
}

async function relateCargoAgency() {
  form2.value.validate(async (valid) => {
    if (valid) {
      let { cargoOwnerId } = ruleForm2.value;

      let res = await api.relateCargoAgency({
        proxyId: route.query.id,
        cargoOwnerId,
      });
      console.log(res);
      if (res.data.status == 0) {
        ElNotification.success({
          title: "添加成功",
          duration: 2000,
          message: `${res.data.msg}`,
          type: "success",
        });
        resetForm2();
        getCargoOwnerCompanyByAgencyCompany();
      } else {
        ElNotification.error({
          title: "失败",
          duration: 3000,
          message: res.data.msg,
        });
      }
    } else {
      return false;
    }
  });
}

onMounted(() => {
  getAgencyCompanyDetail();
  getCargoOwnerCompanyByAgencyCompany();
});
</script>
<style scoped>
.go-back {
  font-size: 16px;
  font-family: PingFangSC-Medium, PingFang SC;
  font-weight: 500;
  color: #333d43;
  line-height: 100%;
  cursor: pointer;
}

.normal-label {
  font-size: 14px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #353a42;
  margin-right: 10px;
}

.show-input {
  width: 200px;
  height: 32px;
  background: #ffffff;
  border-radius: 2px;
  border: 1px solid #dee0e3;
  font-size: 14px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #333333;
  line-height: 32px;
  padding-left: 12px;
  margin-right: 40px;
}

.radio-btns {
  height: 38px;
  width: 103px;
  border: 1px solid #1486f9;
  line-height: 38px;
  text-align: center;
  font-size: 14px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #0094fe;
  cursor: pointer;
}

.left-radius {
  border-top-left-radius: 19px;
  border-bottom-left-radius: 19px;
}

.right-radius {
  border-top-right-radius: 19px;
  border-bottom-right-radius: 19px;
}
.currentbtn {
  background: #1486f9;
  color: #fff;
}

.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;
}

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