<template>
  <div class="content-box">
    <div class="left">
      <!-- <el-icon :size="23" class="camera"><VideoCameraFilled /></el-icon> -->
      <span class="cameratxt">车次管理</span>
    </div>
    <div class="middle">
      <div class="filter">
        <div class="condition">
          <span>车牌 : </span>
          <el-input
            clearable
            v-model="searchInput.carnumber"
            class="w-50 m-2"
            placeholder="请输入车牌号"
            style="width: 150px"
          />
        </div>
        <div class="condition">
          <span>创建时间 : </span>
          <el-date-picker
            v-model="searchInput.createTime"
            type="date"
            format="YYYY-MM-DD"
            value-format="YYYY-MM-DD"
            :prefix-icon="Calendar"
            placeholder="请选择时间"
          />
        </div>

        <el-button
          style="margin-left: 20px"
          color="rgba(61, 81, 232, 1)"
          type="primary"
          class="search"
          @click="searchBtn"
          ><el-icon><Search /></el-icon> <span>查询</span></el-button
        >
        <el-button
          style="margin-left: 20px !important"
          color="rgba(61, 81, 232, 1)"
          type="primary"
          class="search"
          @click="searchRefresh"
          ><el-icon><Refresh /></el-icon><span>重置</span></el-button
        >
      </div>
      <!-- 按钮列表 -->
      <div class="gongneng">
        <el-button type="primary" color="rgba(61, 81, 232, 1)" @click="addlist"
          ><el-icon><CirclePlus /></el-icon><span>新增车次</span></el-button
        >
        <!-- <el-button
          color="rgba(61, 81, 232, 1)"
          class="import"
          type="primary"
          @click="resetInput"
          ><img
            src="@/assets/import.png"
            style="width: 14px; height: 14px; margin-right: 4px"
            alt=""
          />
          <span>导出表单</span></el-button
        > -->
      </div>
    </div>
    <div class="footer">
      <el-table
        :row-class-name="tableRowClassName"
        :data="tableData.list"
        @selection-change="handleSelectionChange"
        style="width: 100%"
        :header-cell-style="{
          background: 'rgba(240, 243, 247, 1)',
          height: '50px',
          border: 0,
        }"
      >
        <!-- <el-table-column align="center" type="selection" width="80" /> -->
        <el-table-column
          align="center"
          prop="car_number"
          width="180"
          label="车牌"
        />
        <el-table-column
          align="center"
          width="100"
          prop="contain"
          label="容量"
        />
        <el-table-column align="center " prop="ci_time" label="发车时间" />
        <el-table-column align="center" prop="route" label="路线" width="200" />

        <el-table-column
          align="center"
          prop="create_time"
          width="250"
          label="创建时间"
        />
        <el-table-column
          align="center"
          prop="ci_time"
          label="截止时间"
          width="120"
        />
        <el-table-column
          align="center"
          width="250"
          prop="work_duration"
          label="生效日期"
        />
        <el-table-column
          align="center"
          prop="notice_time"
          label="提前通知时间"
          width="100"
        />
        <el-table-column align="center" label="操作" width="220">
          <template #default="scope">
            <el-button link type="primary" @click="edit(scope.row)"
              ><div class="look">编辑</div></el-button
            >
            <el-button link type="primary" @click="del(scope.row)"
              ><div class="look">删除</div></el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 添加车辆弹窗 -->
      <el-dialog
        class="addStaff"
        v-model="addDialogVisible"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :title="dialongTitle"
        align-center
        width="609"
        :before-close="cancelAdd"
      >
        <el-form
          ref="ruleFormRef"
          :model="ruleForm"
          :rules="rules"
          label-width="100px"
          class="demo-ruleForm"
          :size="formSize"
          label-position="left"
          status-icon
        >
          <el-form-item label="车牌 :" prop="carnumber">
            <el-select
              v-model="ruleForm.carnumber"
              class="m-2"
              placeholder="请输入车牌"
            >
              <el-option label="赣A11111" value="赣A11111" />
              <el-option label="赣A22222" value="赣A22222" />
              <el-option label="赣A33333" value="赣A33333" />
            </el-select>
          </el-form-item>
          <el-form-item label="容量 :" prop="contain">
            <el-input
              clearable
              v-model="ruleForm.contain"
              placeholder="请输入容量"
            />
          </el-form-item>
          <el-form-item label="生效日期 :" prop="workduration">
            <el-date-picker
              v-model="ruleForm.workduration"
              type="daterange"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
              :prefix-icon="Calendar"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
            />
          </el-form-item>
          <el-form-item label="发车时间 :" prop="startytime">
            <!-- <el-time-picker
              v-model="ruleForm.startytime"
              format="HH:mm"
              value-format="HH:mm"
              placeholder="请选择发车时间"
            /> -->
            <el-select
              v-model="ruleForm.startytime"
              class="m-2"
              placeholder="请选择发车时间"
            >
              <el-option label="14:30" value="14:30" />
              <el-option label="16:30" value="16:30" />
            </el-select>
          </el-form-item>
          <el-form-item label="路线 :" prop="route">
            <el-select
              v-model="ruleForm.route"
              class="m-2"
              placeholder="请选择路线"
            >
              <el-option label="墨轩湖-黄家湖" value="墨轩湖-黄家湖" />
              <el-option label="南昌校区" value="南昌校区" />
            </el-select>
          </el-form-item>
          <el-form-item label="截止时间 :" prop="citime">
            <el-select
              v-model="ruleForm.citime"
              class="m-2"
              placeholder="请选择截止时间"
            >
              <el-option label="16:30" value="16:30" />
              <el-option label="18:30" value="18:30" />
            </el-select>
          </el-form-item>
          <el-form-item
            label="提前通知时间 :"
            prop="noticetime"
            type="Number"
            style="
              padding-bottom: 40px;
              border-bottom: 1px solid rgba(230, 230, 230, 1);
            "
          >
            <div class="informTime">
              <el-input v-model="ruleForm.noticetime" clearable />
              <span> 分钟</span>
            </div>
          </el-form-item>

          <el-form-item class="options">
            <el-button class="congzhi" @click="cancelAdd(ruleFormRef)"
              >取消</el-button
            >
            <el-button
              color="rgba(61, 81, 232, 1)"
              class="queding"
              type="primary"
              @click="submitAdd(ruleFormRef)"
            >
              确定
            </el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
    </div>

    <!-- 分页组件 -->
    <el-pagination
      background
      :current-page="currentPage"
      :page-size="pageSize"
      layout="total, prev, pager, next, jumper, slot"
      :total="total"
      @update:current-page="handleCurrentChange"
    />
  </div>
</template>

<script setup>
import { ref, reactive, nextTick, onBeforeMount, onUnmounted } from "vue";
import { useRouter } from "vue-router";
import { ElMessage, ElMessageBox } from "element-plus";
import { Calendar } from "@element-plus/icons-vue";
import vidiconsApi from "@/api/vidicons.js";
import { dayjs } from "element-plus";
import lodash from "lodash";
import axios from "axios";
const router = useRouter();
// 表格数据
const tableData = reactive({ list: [] });
const activeIndex = ref(); // 默认跳转路由
const dialongTitle = ref("新增车次"); // 弹窗标题

const searchInput = reactive({
  carnumber: "",
  createTime: "",
  route: "",
  citime: "",
}); // 搜索按钮数据
5;

const currentPage = ref(1); // 当前页
const pageSize = ref(10);
const total = ref(); // 当前总数
const selectData = reactive({ list: [] }); // 多选框选择的数据

const addDialogVisible = ref(false); // 控制添加员工弹窗

// 表单数据
const formSize = ref("default");
const ruleFormRef = ref();
const ruleForm = reactive({
  carnumber: "",
  contain: "",
  citime: "",
  route: "",
  startytime: "",
  workduration: "",
  noticetime: "10",
  id: "",
});
// 表单验证
const rules = reactive({
  workduration: [
    { required: true, message: "生效日期不能为空", trigger: "blur" },
    // { min: 3, max: 5, message: "Length should be 3 to 5", trigger: "blur" },
  ],
  carnumber: [
    {
      required: true,
      message: "车牌号不能为空",
      trigger: "change",
    },
  ],
  contain: [
    {
      required: true,
      message: "容量不能为空",
      trigger: "blur",
    },
  ],
  startytime: [
    {
      required: true,
      message: "发车时间不能为空",
      trigger: "change",
    },
  ],
  route: [
    {
      required: true,
      message: "路线不能为空",
      trigger: "change",
    },
  ],
  citime: [
    {
      required: true,
      message: "截止时间不能为空",
      trigger: "change",
    },
  ],
  noticetime: [
    {
      required: true,
      message: "提前通知时间不能为空",
      trigger: "blur",
    },
    {
      pattern: /^[1-720]*$/,
      message: "请填写正确的时间 (整数)",
      trigger: "blur",
    },
  ],
});
// 获取车次信息
const getList = async () => {
  let data = new FormData();
  if (searchInput.createTime == null) {
    searchInput.createTime = "";
  }
  data.set("car_number", searchInput.carnumber);
  data.set("ci_time", searchInput.citime);
  data.set("route", searchInput.route);
  data.set("create_time", searchInput.createTime);
  data.set("page", currentPage.value);
  data.set("rows", pageSize.value);
  let res = await axios({
    method: "post",
    url: "/api/carBook/cclist.action",
    headers: {
      token: sessionStorage.getItem("token"),
    },
    data: data,
  });
  console.log(res);
  if (res.data.code == 200) {
    // ElMessage({
    //   type: "success",
    //   showClose: true,
    //   message: "请求数据成功",
    //   center: true,
    // });
    tableData.list = res.data.rows;
    currentPage.value = res.data.currentPage;
    total.value = res.data.total;
  } else {
    tableData.list = res.data.rows;
    currentPage.value = 1;
    total.value = res.data.total;
    ElMessage({
      type: "error",
      showClose: true,
      message: "暂无数据",
      center: true,
    });
  }
};

// 搜索功能
const searchBtn = lodash.debounce(async () => {
  getList();
}, 300);

// 添加车次
const addlist = () => {
  dialongTitle.value = "新增车次";
  addDialogVisible.value = true;
  ruleForm.carnumber = "";
  ruleForm.contain = "";
  ruleForm.citime = "";
  ruleForm.route = "";
  ruleForm.startytime = "";
  ruleForm.workduration = "";
  ruleForm.noticetime = "";
};
//编辑按钮
const edit = (row) => {
  dialongTitle.value = "编辑车次";
  addDialogVisible.value = true;
  ruleForm.carnumber = row.car_number;
  ruleForm.contain = row.contain;
  ruleForm.citime = row.ci_time;
  ruleForm.route = row.route;
  ruleForm.startytime = row.starty_time;
  ruleForm.workduration = row.work_duration.split("~");
  ruleForm.noticetime = row.notice_time;
  ruleForm.id = row.id;
};
// 取消添加车次
const cancelAdd = () => {
  addDialogVisible.value = false;
  ruleFormRef.value.resetFields();
};
// 重置搜索
const searchRefresh = lodash.debounce(async () => {
  searchInput.carnumber = "";
  searchInput.createTime = "";
  currentPage.value = 1;
  getList();
}, 300);
// 确认新增车次
const submitAdd = async (formEl) => {
  if (!formEl) return;
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      if (dialongTitle.value == "新增车次") {
        let data = {
          car_number: ruleForm.carnumber,
          contain: ruleForm.contain,
          ci_time: ruleForm.citime,
          route: ruleForm.route,
          starty_time: ruleForm.startytime,
          work_duration:
            ruleForm.workduration[0] + "~" + ruleForm.workduration[1],
          notice_time: ruleForm.noticetime,
        };
        // let res = await admin.adminAdd(data);
        let res = await axios({
          method: "post",
          url: "/api/carBook/ccinsert.action",
          headers: {
            "Content-Type": "application/json;charset=utf-8",
            token: sessionStorage.getItem("token"),
          },
          data: data,
        });
        // console.log(res, "添加账号");
        if (res.data.code == 200) {
          getList();
          ElMessage({
            type: "success",
            showClose: true,
            message: res.data.message,
            center: true,
          });
          addDialogVisible.value = false;
          ruleFormRef.value.resetFields();
        } else {
          ElMessage({
            type: "error",
            showClose: true,
            message: res.data.message,
            center: true,
          });
        }
      } else {
        let data = {
          car_number: ruleForm.carnumber,
          contain: ruleForm.contain,
          ci_time: ruleForm.citime,
          route: ruleForm.route,
          starty_time: ruleForm.startytime,
          work_duration:
            ruleForm.workduration[0] + "~" + ruleForm.workduration[1],
          notice_time: ruleForm.noticetime,
          id: ruleForm.id,
        };
        // let res = await admin.adminAdd(data);
        let res = await axios({
          method: "post",
          url: "/api/carBook/ccupdate.action",
          headers: {
            "Content-Type": "application/json;charset=utf-8",
            token: sessionStorage.getItem("token"),
          },
          data: data,
        });
        // console.log(res, "修改账号");
        if (res.data.code == 200) {
          getList();

          ElMessage({
            type: "success",
            showClose: true,
            message: res.data.message,
            center: true,
          });
          addDialogVisible.value = false;
          ruleFormRef.value.resetFields();
        } else {
          ElMessage({
            type: "error",
            showClose: true,
            message: res.data.message,
            center: true,
          });
        }
      }
    } else {
      console.log("error submit!", fields);
    }
  });
};

//删除按钮
const del = (row) => {
  ElMessageBox.confirm(`是否删除 ${row.car_number} 车次?`, "提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(async () => {
      let data = new FormData();
      data.set("id", row.id);
      // let res = await adminApi.adminLogin(data);
      let res = await axios({
        method: "post",
        url: "/api/carBook/ccdel.action",
        headers: {
          token: sessionStorage.getItem("token"),
        },
        data: data,
      });
      if (res.data.code == 200) {
        if (tableData.list.length == 1 && currentPage.value != 1) {
          currentPage.value = currentPage.value - 1;
        }
        getList();
        ElMessage({
          type: "success",
          showClose: true,
          message: res.data.message,
          center: true,
        });
      } else {
        ElMessage({
          type: "error",
          showClose: true,
          message: res.data.message,
          center: true,
        });
      }
      console.log(res);
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "已取消删除",
      });
    });
};

// 多选框功能
const handleSelectionChange = (val) => {
  console.log(val);
  selectData.list = val;
};

// 表格斑马纹颜色修改
const tableRowClassName = ({ row, rowIndex }) => {
  if (rowIndex % 2 === 0) {
    return "even";
  } else if (rowIndex % 2 !== 0) {
    return "odd";
  }
  return "";
};
// 分页
const handleCurrentChange = (value) => {
  // console.log(value);
  currentPage.value = value;
  getList(value);
};

onBeforeMount(() => {
  getList(1);
});
onUnmounted(() => {
  // document.removeEventListener("keyup", Enters);
});
</script>

<style scoped lang="scss">
.content-box {
  width: 97.5%;
  height: 89%;
  margin: 20px auto;
  background-color: #fff;
  color: #fff;
  display: flex;
  flex-direction: column;

  .left {
    // width: calc(100wh - 40px);
    display: flex;
    align-items: center;
    height: 60px;
    margin: 0 30px;
    border-bottom: 1px solid #ccc;
    color: #000;
    font-size: 18px;
    font-weight: 600;
    .camera {
      margin-right: 15px;
      color: #4392f7;
    }
  }
  .middle {
    width: 96%;
    margin: 0 auto;

    color: #000;
    // border-bottom: 1px solid rgb(231, 231, 231);
    .filter {
      display: flex;
      flex-wrap: wrap;
      align-items: center;
      margin: 10px 0 0 0;
      .search {
        margin-left: 0 !important;
      }
      .condition {
        display: flex;
        align-items: center;
        margin: 10px 30px 10px 0;
        span {
          margin: 0 10px 0 0;
        }
      }
    }
    .gongneng {
      margin: 10px 0;
    }
    :deep(.cont) {
      width: 60%;
      margin: 20px auto;
    }
    :deep(.download) {
      display: flex;
      align-items: center;
      margin: 10px;
    }
    :deep(.download span) {
      font-size: 16px;
      margin-left: 20px;
    }
    :deep(.cont .el-button) {
      margin-left: 60px;
      margin-bottom: 30px;
    }

    :deep(.cont .accomplish) {
      width: 100%;
      display: flex;
      justify-content: center;
    }
    :deep(.cont .accomplish .el-button) {
      width: 50%;
      margin: 0;
    }
  }
  .footer {
    width: 96%;
    height: 550px;
    margin: 10px auto 30px;
    .el-table--fit {
      height: 100%;
      :deep(.el-table__header-wrapper) {
        background-color: #000;
      }
      :deep(.el-table__row) {
        height: 50px;
      }
      :deep(.el-table__row td) {
        padding: 0;
        border: 0;
      }

      .el-button--primary {
        margin-left: 5px;
      }
      :deep(.el-table__body .even) {
        background-color: #fff;
      }
      :deep(.el-table__body .odd) {
        background-color: rgba(240, 243, 247, 1);
      }
      :deep(.look) {
        padding: 5px 10px;
        border-radius: 45px;
        border: 0.74px solid rgba(30, 125, 251, 1);
      }
    }

    // 添加员工弹窗样式
    :deep(.addStaff) {
      //   height: 420px;
      border-radius: 11px;
      .el-dialog__header {
        border-radius: 11px 11px 0 0;
        background: rgba(237, 241, 245, 1);
        font-weight: 600;
        margin: 0;
        .el-dialog__headerbtn {
          outline: none;
        }
      }
      .el-dialog__body {
        padding: 30px 20px 10px 20px;
        .el-form-item {
          label {
            width: 120px !important;
          }
          .el-form-item__label {
            width: 120px !important;
          }
          .informTime {
            display: flex;
            align-items: center;
            .el-input {
              width: 120px;
              margin-right: 10px;
              .el-input__inner {
                text-align: center;
              }
            }
          }
        }
        .el-form-item__content {
          flex: none;
          .el-input {
            width: 370px;
          }
        }
        .el-input {
          width: 200px;
        }
        .options {
          margin-left: 320px;
        }
      }
    }
  }
  .el-pagination {
    // width: 1600px;
    width: 96%;
    margin: 0 auto 18px;
    justify-content: flex-end;
    :deep(.el-pagination__total) {
      color: #000;
    }
    :deep(.el-pagination__goto) {
      color: #000;
    }
    :deep(.el-pagination__classifier) {
      color: #000;
    }
    :deep(.el-input__wrapper) {
      border: 1px solid rgba(0, 0, 0, 1);
      border-radius: 5px;
      box-shadow: none;
    }
    :deep(.el-pager li) {
      margin: 0 5px;
      border: 1px solid rgba(0, 0, 0, 1);
      border-radius: 5px;
      background-color: transparent;
    }
    :deep(.el-pager li.is-active) {
      background-color: rgba(30, 125, 251, 1);
    }
    :deep(.btn-prev) {
      margin-right: 5px;
      border: 1px solid rgba(0, 0, 0, 1);
      border-radius: 5px;
      background-color: transparent;
    }
    :deep(.btn-next) {
      margin-left: 5px;
      border: 1px solid rgba(0, 0, 0, 1);
      border-radius: 5px;
      background-color: transparent;
    }
  }
}
.el-input {
  width: 192px;
}
</style>
