<template>
  <div class="line-container-p24">
    <i class="el-icon-arrow-left"></i>
    <div
      class="dib go-back ml8"
      @click="router.replace('/cargoOwnerManage/cargoOwnerCompanyList')"
    >
      返回货主列表
    </div>
  </div>
  <div class="container-title">货主信息</div>
  <div class="line-container-p24 df aic">
    <div class="normal-label">货主名称</div>
    <div class="show-input">{{ cargoOwnerName }}</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="term"
          clearable
          style="width: 330px"
        ></el-input>
        <div class="seach-btn" @click="getCargoOwnerAccountList(1)">查询</div>
      </div>
      <div class="cargo-owner-add" @click="dialogFormVisible = true">
        添加账号
      </div>
      <el-dialog
        title="添加账号"
        v-model="dialogFormVisible"
        @closed="resetForm"
      >
        <template v-slot:default>
          <el-form
            :model="ruleForm"
            :rules="rules"
            ref="form"
            label-width="110px"
            label-position="left"
          >
            <el-form-item label="货主公司名称">
              {{ cargoOwnerName }}
            </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="phone" label="手机号">
              <el-input
                style="width: 280px"
                v-model="ruleForm.phone"
              ></el-input>
            </el-form-item>
            <el-form-item prop="password" label="密码">
              <el-input
                style="width: 280px"
                v-model="ruleForm.password"
              ></el-input>
            </el-form-item>
            <!-- <el-form-item prop="cargo" label="关联货种">
              <el-select
                v-model="ruleForm.cargo"
                placeholder="请选择"
                style="width: 280px"
              >
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.key"
                />
              </el-select>
            </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="addCargoOwnerAccount(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="phone"
          label="手机号"
          min-width="120"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="password"
          label="密码"
          min-width="160"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="deptName"
          label="部门"
          min-width="160"
          align="center"
        >
          <template v-slot="scope">
            {{ scope.row.deptName || "暂无部门" }}
          </template>
        </el-table-column>
        <el-table-column
          prop="roleName"
          label="职位"
          min-width="160"
          align="center"
        >
          <template v-slot="scope">
            {{ scope.row.roleName || "暂无职位" }}
          </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>
  <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="getAgencyCompanyByCargoOwnerCompany(1)">
          查询
        </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="proxyId" label="代理公司:">
              <RemoteSearch
                api="getAgencySelect"
                v-model="agencyStr"
                @selectItem="selectAgency($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="agencyName"
          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"
          :current-page="currentPage2"
          :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 cargoOwnerName = ref();
let contactName = ref();
let contactPhone = ref();
async function getCargoOwnerCompanyDetail() {
  let res = await api.getCargoOwnerCompanyDetail({
    cargoOwnerId: route.query.id,
  });
  if (res.data.status == 0) {
    cargoOwnerName.value = res.data.result.cargoOwnerName;
    contactName.value = res.data.result.contactName;
    contactPhone.value = res.data.result.contactPhone;
  } else {
    console.log(res);
  }
}

let tableData = ref([]);
let term = ref("");
let currentPage = ref(1);
let total = ref(0);
let dialogFormVisible = ref(false);
let form = ref(null);

async function getCargoOwnerAccountList(page) {
  currentPage.value = page || currentPage.value;
  let res = await api.getCargoOwnerAccountList({
    cargoOwnerId: route.query.id,
    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;
  }
}

const ruleForm = ref({
  userName: "",
  phone: "",
  password: "",
  cargo: "",
});
const rules = ref({
  userName: [{ required: true, message: "请填写姓名", trigger: "blur" }],
  phone: [
    { required: true, message: "请填写手机号", trigger: "blur" },
    { min: 11, max: 11, message: "请正确填写手机号", trigger: "blur" },
  ],
  password: [{ required: false, message: "请填写密码", trigger: "blur" }],
  cargo: [{ required: true, message: "请选择货种", trigger: "blur" }],
});

let options = ref([]);

async function getCargoList() {
  let res = await api.getCargoList({
    cargoOwnerId: route.query.id,
  });
  options.value = res.data.result;
}

function resetForm() {
  dialogFormVisible.value = false;

  form.value.resetFields();
}

function pageChange(e) {
  currentPage.value = e;
  getCargoOwnerAccountList();
}

async function addCargoOwnerAccount() {
  form.value.validate(async (valid) => {
    if (valid) {
      let { userName, phone, password, cargo } = ruleForm.value;

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

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

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

function selectAgency(item) {
  ruleForm2.value.proxyId = item.key;
}

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

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

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

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

onMounted(() => {
  getCargoOwnerCompanyDetail();
  getCargoOwnerAccountList();
  getAgencyCompanyByCargoOwnerCompany();
  // getCargoList();
});
</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>
