<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="visable = true">
        添加货种
      </el-button>
    </div>
    <el-dialog
      v-model="visable"
      :title="currentCargoId ? '修改货种' : '添加货种'"
      width="550px"
      @close="resetForm()"
    >
      <template v-slot:default>
        <div class="df jcc">
          <el-form
            :model="ruleForm"
            :rules="rules"
            ref="form"
            label-width="110px"
            label-position="left"
          >
            <el-form-item prop="cargo" label="货种名称">
              <el-input
                style="width: 200px"
                v-model="ruleForm.cargo"
                placeholder="请填写货种名称"
              ></el-input>
            </el-form-item>
            <el-form-item prop="proxyIds" label="启用代理">
              <RemoteSelect
                api="getAgencySelect"
                v-model="ruleForm.proxyIds"
                multiple
                @selectItem="selectAgency($event)"
                class="mb10"
              ></RemoteSelect>
            </el-form-item>
          </el-form>
        </div>
      </template>
      <template v-slot:footer>
        <div class="dialog-footer">
          <el-button @click="resetForm">取 消</el-button>
          <el-button type="primary" @click="addCargo(ruleForm)">
            确 定
          </el-button>
        </div>
      </template>
    </el-dialog>
    <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">
          <template v-slot="scope">
            <el-switch v-model="scope.disabled"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="80" align="center">
          <template v-slot="scope">
            <el-button
              size="small"
              @click="showUpdate(scope.row)"
              type="primary"
            >
              修改
            </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"
        ></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 currentCargoId = ref(0);
let form = ref(null);
let visable = ref(false);
let ruleForm = ref({
  cargo: "",
  proxyIds: "",
});
function resetForm() {
  visable.value = false;
  form.value.resetFields();
}
const rules = ref({
  cargo: [
    {
      required: true,
      message: "请填写货种",
      trigger: "blur",
    },
  ],
  proxyIds: [
    {
      required: true,
      message: "请选择代理",
      trigger: "blur",
    },
  ],
});
function selectAgency(e) {
  console.log(e);
}

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();
}
async function addCargo() {
  console.log(ruleForm.value);
  let { cargo, proxyIds } = ruleForm.value;
  let arr = [];
  for (let i of proxyIds) {
    arr.push(i.value);
  }
  let res = await api.addCargo({
    cargo,
    proxyIds: arr.join(","),
  });
  if (res.data.status == 0) {
    ElNotification({
      title: "成功",
      duration: 1500,
      message: res.data.msg,
      type: "success",
    });
    resetForm();
    getCargoList();
  } else {
    ElNotification({
      title: "失败",
      duration: 1500,
      message: res.data.msg,
      type: "error",
    });
  }
}

async function showUpdate(item) {
  console.log(item);
  visable.value = true;
  let { id: cargoId } = item;
  currentCargoId.value = cargoId;
  ruleForm.value = {
    proxyIds: [],
  };
  let res = await api.getAgencySelected({
    cargoId,
  });
  ruleForm.value.proxyIds = [];
  for (let i of res.data.result) {
    ruleForm.value.proxyIds.push({
      value: i.proxyName,
      key: i.proxyId,
    });
  }
  console.log(res);
}

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>
