<template>
  <div class="content-box">
    <div class="top">报修区域</div>
    <div class="left">
      <!-- <el-icon :size="23" class="camera"><VideoCameraFilled /></el-icon> -->
      <span class="cameratxt" @click="getList(1)" :class="themeIndex == 1 ? 'is_active' : ''">黄家湖校区</span>
      <span class="cameratxt" @click="getList(2)" :class="themeIndex == 2 ? 'is_active' : ''">墨轩湖校区</span>
    </div>
    <div v-if="themeIndex == 1">
      <div class="middle">
        <!-- 按钮列表 -->
        <div class="gongneng">
          <el-button type="primary" color="rgba(111, 182, 184, 1)" @click="addlist"><img src="@/assets/add.png"
              style="width: 14px; height: 14px; margin-right: 4px" alt="" /><span>添加区域</span></el-button>
        </div>
      </div>
      <div class="footer" :key="keyIndex">
        <el-table v-loading="loading" :expand-row-keys="mexpandId" :row-key="row => row.id" :data="tableData.list"
          @expand-change="mhandleExpandChange" :header-cell-style="{
            background: 'rgba(240, 243, 247, 1)',
          }" style="width: 100%; margin-bottom: 20px">
          <el-table-column type="expand">
            <template #default="props">
              <div m="4" class="Inside">
                <!-- 排序 -->
                <el-table :data="props.row.children" :row-key="row => row.id"
                  :cell-style="{ background: 'rgba(0,0,0,0.1)' }">
                  <el-table-column label="报修物品" prop="name" />
                  <el-table-column label="操作">
                    <template #default="scope">
                      <div class="options">
                        <div class="look edit" @click="editAreaH(scope.row)">修改</div>
                        <el-popconfirm width="220" confirm-button-text="确认" cancel-button-text="取消" :icon="InfoFilled"
                          icon-color="#f89626" title="是否删除?" @confirm="delAreaH(scope.row)" @cancel="cencelAreaH">
                          <template #reference>
                            <div class="look delete">删除</div>
                          </template>
                        </el-popconfirm>
                      </div>
                    </template>
                  </el-table-column>

                  <!-- <el-table-column label="拖拽排序" align="center" width="120">
                    <template #default="props">
                      <el-icon class="allowDragInside">
                        <Rank />
                      </el-icon>
                    </template>
                  </el-table-column> -->
                </el-table>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="报修区域">
            <template #default="scope">
              <img v-if="scope.row.flag == 1" src="@/assets/areaTable.png" alt="" />
              <span>{{ scope.row.name }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="address" align="center" label="操作">
            <template #default="scope">
              <div class="options">
                <div class="look add" @click="addAreaH(scope.row)">
                  添加
                </div>
                <div class="look edit" @click="editAreaH(scope.row)">修改</div>
                <el-popconfirm width="220" confirm-button-text="确认" cancel-button-text="取消" :icon="InfoFilled"
                  icon-color="#f89626" title="是否删除?" @confirm="delAreaH(scope.row)" @cancel="cencelAreaH">
                  <template #reference>
                    <div class="look delete">删除</div>
                  </template>
                </el-popconfirm>
              </div>
            </template>
          </el-table-column>

          <!-- <el-table-column label="拖拽排序" align="center" width="120">
            <template #default="props">
              <el-icon color="rgb(26, 145, 255)" class="allowDrag">
                <Rank />
              </el-icon>
            </template>
          </el-table-column> -->
        </el-table>
        <!-- 添加区域 -->
        <el-dialog class="addAreaH" v-model="addAreaHvisible" :close-on-click-modal="false" :close-on-press-escape="false"
          :title="addAreaHTitle" align-center width="609" :before-close="addAreaHclose">
          <el-form ref="addAreas" :model="addAreaForm" :rules="addAreaRules" label-width="120px" class="demo-ruleForm"
            :size="formSize" label-position="left" status-icon>
            <el-form-item label="区域名称 :" prop="content" style="
                padding-bottom: 40px;
                border-bottom: 1px solid rgba(230, 230, 230, 1);
              ">
              <el-input clearable v-model="addAreaForm.content" placeholder="请输入区域名称" />
            </el-form-item>
            <el-form-item class="options">
              <el-button color="rgba(111, 182, 184, 1)" class="queding" type="primary" @click="addAreaSub(addAreas)">
                确定
              </el-button>
              <el-button class="congzhi" @click="addAreaHclose">取消</el-button>
            </el-form-item>
          </el-form>
        </el-dialog>
        <!-- 添加耗材弹窗 -->
        <!-- <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="handleVideoClose"
        >
          <el-form
            ref="ruleFormRef"
            :model="ruleForm"
            :rules="rules"
            label-width="120px"
            class="demo-ruleForm"
            :size="formSize"
            label-position="left"
            status-icon
          >
            <el-form-item label="耗材名称 :" prop="buslicence">
              <el-input
                clearable
                v-model="ruleForm.buslicence"
                placeholder="请输入耗材名称"
              />
            </el-form-item>
            <el-form-item label="单价 :" prop="buslicence">
              <el-input
                clearable
                v-model="ruleForm.buslicence"
                placeholder="请输入单价"
              />
            </el-form-item>
            <el-form-item
              label="关联物品种类 :"
              prop="volume"
              class="checkbox"
              style="
                padding-bottom: 40px;
                border-bottom: 1px solid rgba(230, 230, 230, 1);
              "
            >
              <el-checkbox
                v-model="checkAll"
                :indeterminate="isIndeterminate"
                @change="handleCheckAllChange"
                >全选</el-checkbox
              >
              <el-checkbox-group
                v-model="checkedCities"
                @change="handleCheckedCitiesChange"
              >
                <el-checkbox v-for="city in cities" :key="city" :label="city">{{
                  city
                }}</el-checkbox>
              </el-checkbox-group>
            </el-form-item>

            <el-form-item class="options">
              <el-button
                color="rgba(111, 182, 184, 1)"
                class="queding"
                type="primary"
                @click="submitAdd(ruleFormRef)"
              >
                确定
              </el-button>
              <el-button class="congzhi" @click="cancelAdd(ruleFormRef)"
                >取消</el-button
              >
            </el-form-item>
          </el-form>
        </el-dialog> -->
      </div>
    </div>
    <div v-else-if="themeIndex == 2">
      <div class="middle">
        <!-- 按钮列表 -->
        <div class="gongneng">
          <el-button type="primary" color="rgba(111, 182, 184, 1)" @click="addlistM"><img src="@/assets/add.png"
              style="width: 14px; height: 14px; margin-right: 4px" alt="" /><span>添加区域</span></el-button>
        </div>
      </div>
      <div class="footer" :key="keyIndex">
        <el-table v-loading="loading" :expand-row-keys="mexpandId" :row-key="row => row.id" :data="tableData.list"
          @expand-change="mhandleExpandChange" :header-cell-style="{
            background: 'rgba(240, 243, 247, 1)',
          }" style="width: 100%; margin-bottom: 20px">
          <el-table-column type="expand">
            <template #default="props">
              <!-- 排序 -->
              <el-table :data="props.row.children" :row-key="row => row.id"
                :cell-style="{ background: 'rgba(0,0,0,0.1)' }">
                <el-table-column label="报修物品" prop="name" />
                <el-table-column label="操作">
                  <template #default="scope">
                    <div class="options">
                      <div class="look edit" @click="editAreaM(scope.row)">修改</div>
                      <el-popconfirm width="220" confirm-button-text="确认" cancel-button-text="取消" :icon="InfoFilled"
                        icon-color="#f89626" title="是否删除?" @confirm="delAreaM(scope.row)" @cancel="cencelAreaH">
                        <template #reference>
                          <div class="look delete">删除</div>
                        </template>
                      </el-popconfirm>
                    </div>
                  </template>
                </el-table-column>

                <!-- <el-table-column label="拖拽排序" align="center" width="120">
                  <template #default="props">
                    <el-icon class="allowDragInside">
                      <Rank />
                    </el-icon>
                  </template>
                </el-table-column> -->
              </el-table>
            </template>
          </el-table-column>

          <el-table-column prop="date" label="报修区域">
            <template #default="scope">
              <img v-if="scope.row.flag == 1" src="@/assets/areaTable.png" alt="" />
              <span>{{ scope.row.name }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="address" align="center" label="操作">
            <template #default="scope">
              <div class="options">
                <div class="look add" @click="addAreasM(scope.row)">
                  添加
                </div>
                <div class="look edit" @click="editAreaM(scope.row)">修改</div>
                <el-popconfirm width="220" confirm-button-text="确认" cancel-button-text="取消" :icon="InfoFilled"
                  icon-color="#f89626" title="是否删除?" @confirm="delAreaM(scope.row)" @cancel="cencelAreaM">
                  <template #reference>
                    <div class="look delete">删除</div>
                  </template>
                </el-popconfirm>
              </div>
            </template>
          </el-table-column>

          <!-- <el-table-column label="拖拽排序" align="center" width="120">
            <template #default="props">
              <el-icon color="rgb(26, 145, 255)" class="allowDrag">
                <Rank />
              </el-icon>
            </template>
          </el-table-column> -->
        </el-table>
        <!-- 添加区域 -->
        <el-dialog class="addAreaM" v-model="addAreaMvisible" :close-on-click-modal="false" :close-on-press-escape="false"
          :title="addAreaMTitle" align-center width="609" :before-close="addAreaMclose">
          <el-form ref="addAreaM" :model="addAreaMForm" :rules="addAreaMRules" label-width="120px" class="demo-ruleForm"
            :size="formSize" label-position="left" status-icon>
            <el-form-item label="区域名称 :" prop="content" style="
                padding-bottom: 40px;
                border-bottom: 1px solid rgba(230, 230, 230, 1);
              ">
              <el-input clearable v-model="addAreaMForm.content" placeholder="请输入区域名称" />
            </el-form-item>
            <el-form-item class="options">
              <el-button color="rgba(111, 182, 184, 1)" class="queding" type="primary" @click="addAreaMSub(addAreaM)">
                确定
              </el-button>
              <el-button class="congzhi" @click="addAreaMclose">取消</el-button>
            </el-form-item>
          </el-form>
        </el-dialog>
      </div>
    </div>
  </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";
import { useStore } from "vuex";
const store = useStore();
const api = ref("");
const router = useRouter();
// 表格数据
const tableData = reactive({
  list: [
    // {
    //   id: 1,
    //   flag: 1,
    //   date: "2016-05-02",
    // },
    // {
    //   id: 2,
    //   flag: 1,
    //   date: "2016-05-04",
    // },
    // {
    //   id: 3,
    //   flag: 1,
    //   date: "2016-05-01",
    //   children: [
    //     {
    //       id: 31,
    //       date: "2016-05-01",
    //     },
    //     {
    //       id: 32,
    //       date: "2016-05-01",
    //     },
    //   ],
    // },
  ],
});
const themeIndex = ref(1); // 切换 耗材管理 和 耗材记录
const loading = ref(false)
const keyIndex = ref(1) // 切换时将key改变使组件重新加载

// 添加耗材 弹窗数据 (------------------------------------)
const dialongTitle = ref("添加耗材"); // 弹窗标题
const checkAll = ref(false); // 判断物品种类是否全选
const isIndeterminate = ref(true); // 不确定状态，表示只选择一部分
const checkedCities = ref(["泥工", "木工"]); // 选中的数据
const cities = ["水电类", "泥工", "木工", "其他"]; //  种类列表

const searchInput = reactive({
  carnumber: "",
  createTime: "",
}); // 搜索按钮数据
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({
  serial: "",
  buslicence: "",
  volume: "",
  id: "",
});
// 表单验证
const rules = reactive({
  // serial: [{ required: true, message: "序列号不能为空", trigger: "blur" }],
  buslicence: [
    { required: true, message: "请输入车牌号", trigger: "blur" },
    {
      pattern:
        /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$/,
      message: "请输入合法车牌号",
      trigger: "blur",
    },
  ],
  volume: [
    {
      required: true,
      message: "容量不能为空",
      trigger: "blur",
    },
    {
      pattern: /^(0|[1-9][0-9]*)$/,
      message: "请输入正确数字",
      trigger: "blur",
    },
  ],

  // desc: [{ required: true, message: "Please input activity form", trigger: "blur" }],
});
// 表格中添加区域表单数据（----------黄家湖-----------------------------）
const addAreaHvisible = ref(false); // 表格中添加功能弹窗
const addAreas = ref();
const addAreaHTitle = ref("添加区域");
const addAreaForm = reactive({
  content: "",
  id: "",
});

// 表格中添加区域表单数据（----------墨轩湖-----------------------------）
const addAreaMvisible = ref(false); // 表格中添加功能弹窗
const addAreaM = ref();
const addAreaMTitle = ref("添加区域");
const addAreaMForm = reactive({
  content: "",
  id: "",
});

const mexpandId = ref([]) // 树形表格默认展开项

// 表单验证
const addAreaRules = reactive({
  content: [{ required: true, message: "请输入区域名称", trigger: "blur" }],
});
const addAreaMRules = reactive({
  content: [{ required: true, message: "请输入区域名称", trigger: "blur" }],
});
// 查看报修区域列表
const getList = async (flag) => {
  loading.value = true
  let data = {};
  if (flag == 1) {
    themeIndex.value = 1;
    data = {
      schoolId: 1,
    };
    // let res = await axios({
    //   method: "get",
    //   url: api.value + "/repairArea/queryRepairAreaTree",
    //   headers: {
    //     token: sessionStorage.getItem("token"),
    //     user_head: sessionStorage.getItem("userhead"),
    //   },
    //   params: data,
    // });
    // console.log(res, "报修区域数据");
    // if (res.data.code == 200) {
    //   res.data.data.forEach((item) => {
    //     item.flag = 1;
    //   });
    //   tableData.list = res.data.data;
    //   // if (flag == 1) {
    //   //   sessionStorage.setItem("busSelect", JSON.stringify(tableData.list));
    //   // }
    //   // currentPage.value = res.data.currentPage;
    //   // total.value = res.data.total;
    // } else {
    //   ElMessage({
    //     type: "error",
    //     showClose: true,
    //     message: res.data.message,
    //     center: true,
    //   });
    // }
  } else if (flag == 2) {
    themeIndex.value = 2;
    data = {
      schoolId: 2,
    };
  }
  let res = await axios({
    method: "get",
    url: api.value + "/repairArea/queryRepairAreaTree",
    headers: {
      token: sessionStorage.getItem("token"),
      user_head: sessionStorage.getItem("userhead"),
    },
    params: data,
  });
  console.log(res, `${themeIndex.value}` + "报修区域数据");
  if (res.data.code == 200) {
    loading.value = false
    res.data.data.forEach((item) => {
      item.flag = 1;
    });
    tableData.list = res.data.data;
  } else {
    ElMessage({
      type: "error",
      showClose: true,
      message: res.data.message,
      center: true,
    });
    if (res.data.code == 570) {
      sessionStorage.removeItem("token")
      router.push({
        path: `/login`,
      });
    }
  }
};

//  当用户对某一行展开或者关闭的时候会触发该事件
const mhandleExpandChange = (rows, expand) => {
  // console.log(rows, expand, "树形展开回调");
  const isExpend = expand.some(r => r.id === rows.id) // 判断当前行展开状态
  if (isExpend) {
    // Do some things
    if (mexpandId.value.length == 0) {
      mexpandId.value.push(rows.id)
      console.log("当前行展开", mexpandId.value);
    } else {
      mexpandId.value = [rows.id]
      console.log("当前行展开", mexpandId.value);
    }

  } else {
    mexpandId.value = mexpandId.value.filter(item => {
      return rows.id != item
    })
    console.log("当前行关闭", mexpandId.value);
  }

  // nextTick(() => {
  //   let tbodyInside = document.querySelector(".Inside .el-table__body-wrapper tbody");
  //   // console.log(tbodyInside);
  //   if (tbodyInside) {
  //     Sortable.create(tbodyInside, {
  //       handle: '.allowDragInside',
  //       //方式一：字符串
  //       group: 'itxst', //直接设置组名称
  //       //方式二：object
  //       group: {
  //         name: 'itxst',//分组名称itxst的组之间可以相互拖拽
  //         pull: false,//是否允许拖入当前组
  //         put: false,//是否允许拖出当前组
  //       },
  //       // chosenClass: "chosenClass",// :ghost-class="hostClass" 被选中目标的样式，你的自定义样式可能需要加!important才能生效，并把forceFallback属性设置成true
  //       // dragClass: "dragClass",// :drag-class="dragClass"拖动元素的样式，你的自定义样式可能需要加!important才能生效，并把forceFallback属性设置成true
  //       // forceFallback: true,
  //       animation: 120,
  //       async onEnd({ newIndex, oldIndex }) {
  //         console.log(newIndex, oldIndex);
  //         const list = JSON.parse(JSON.stringify(tableData.list))

  //         let data = []
  //         data = list.filter(item => {
  //           return item.id == mexpandId.value[0]
  //         })
  //         console.log(data);

  //         // 内部改变的表格数据
  //         let datas = data[0].children
  //         const targetRow = datas.splice(oldIndex, 1)[0]
  //         console.log(targetRow, "被截取的数据");
  //         datas.splice(newIndex, 0, targetRow)
  //         console.log(datas, "改变后数组数据");

  //         tableData.list.forEach(item => {
  //           if (item.id == mexpandId.value[0]) {
  //             item.children = datas
  //           }
  //         })

  //         let articles = []
  //         datas.forEach((item, index) => {
  //           let aug = {
  //             id: "",
  //             orderNum: ""
  //           }
  //           aug.id = item.id
  //           aug.orderNum = index + 1
  //           articles.push(aug)
  //         })
  //         console.log(articles, "排序参数");

  //         let res = await axios({
  //           method: "post",
  //           url: api.value + "/repairArticleType/dragArticle",
  //           headers: {
  //             token: sessionStorage.getItem("token"),
  //             user_head: sessionStorage.getItem("userhead"),
  //           },
  //           data: { articles: articles },
  //         });
  //         console.log(res, "排序数据");

  //         if (res.data.code == 200) {
  //           getList(themeIndex.value);
  //           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(tableData.list, "改变后数组数据");
  //       }
  //     })
  //   }
  // })
}

// （黄家湖-------------------------------------------------------）
// 黄家湖-页面添加按钮
const addlist = () => {
  addAreaHvisible.value = true;
  addAreaHTitle.value = "添加区域";
  addAreaForm.content = "";
  addAreaForm.id = 1;
};
// 黄家湖-表格中添加功能
const addAreaH = async (row) => {
  console.log(row);
  addAreaHvisible.value = true;
  addAreaHTitle.value = "添加区域";
  addAreaForm.content = "";
  addAreaForm.id = row.id;
};
// 黄家湖-表格中修改功能
const editAreaH = async (row) => {
  console.log(row);
  addAreaHvisible.value = true;
  addAreaHTitle.value = "修改区域";
  addAreaForm.content = row.name;
  addAreaForm.id = row.id;
};

// 黄家湖-表格中确定添加
const addAreaSub = async (formEl) => {
  if (!formEl) return;
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      if (addAreaHTitle.value == "添加区域") {
        let data = {
          name: addAreaForm.content,
          parentId: addAreaForm.id,
        };
        // let res = await admin.adminAdd(data);
        let res = await axios({
          method: "post",
          url: api.value + "/repairArea/insertRepairArea",
          headers: {
            token: sessionStorage.getItem("token"),
            user_head: sessionStorage.getItem("userhead"),
          },
          data: data,
        });
        console.log(res, "添加区域");
        if (res.data.code == 200) {
          getList(1);
          ElMessage({
            type: "success",
            showClose: true,
            message: res.data.message,
            center: true,
          });
          addAreaHvisible.value = false;
          addAreas.value.resetFields();
        } else {
          ElMessage({
            type: "error",
            showClose: true,
            message: res.data.message,
            center: true,
          });
        }
      } else if (addAreaHTitle.value == "修改区域") {
        let data = {
          name: addAreaForm.content,
          id: addAreaForm.id,
        };
        // let res = await admin.adminAdd(data);
        let res = await axios({
          method: "post",
          url: api.value + "/repairArea/updateRepairAreaById",
          headers: {
            token: sessionStorage.getItem("token"),
            user_head: sessionStorage.getItem("userhead"),
          },
          data: data,
        });
        // console.log(res, "修改账号");
        if (res.data.code == 200) {
          getList(1);
          ElMessage({
            type: "success",
            showClose: true,
            message: res.data.message,
            center: true,
          });
          addAreaHvisible.value = false;
          addAreas.value.resetFields();
        } else {
          ElMessage({
            type: "error",
            showClose: true,
            message: res.data.message,
            center: true,
          });
        }
      }
    } else {
      console.log("error submit!", fields);
    }
  });
};
// 黄家湖-关闭添加功能
const addAreaHclose = () => {
  addAreaHvisible.value = false;
  addAreas.value.resetFields();
};
// 黄家湖-删除区域
const delAreaH = async (row) => {
  let data = {
    id: row.id,
  };
  // let res = await adminApi.adminLogin(data);
  let res = await axios({
    method: "get",
    url: api.value + "/repairArea/deleteRepairAreaById",
    headers: {
      token: sessionStorage.getItem("token"),
      user_head: sessionStorage.getItem("userhead"),
    },
    params: data,
  });
  if (res.data.code == 200) {
    getList(1);
    ElMessage({
      type: "success",
      showClose: true,
      message: res.data.message,
      center: true,
    });
  } else {
    ElMessage({
      type: "error",
      showClose: true,
      message: res.data.message,
      center: true,
    });
  }
};
// 黄家湖-取消删除区域
const cencelAreaH = () => {
  ElMessage({
    type: "info",
    showClose: true,
    message: "取消删除",
    center: true,
  });
};

// （墨轩湖-------------------------------------------------------）
// 墨轩湖-页面添加按钮
const addlistM = () => {
  addAreaMvisible.value = true;
  addAreaMTitle.value = "添加区域";
  addAreaMForm.content = "";
  addAreaMForm.id = 2;
};
// 墨轩湖-表格中添加功能
const addAreasM = async (row) => {
  console.log(row);
  addAreaMvisible.value = true;
  addAreaMTitle.value = "添加区域";
  addAreaMForm.content = "";
  addAreaMForm.id = row.id;
};
// 墨轩湖-表格中修改功能
const editAreaM = async (row) => {
  console.log(row);
  addAreaMvisible.value = true;
  addAreaMTitle.value = "修改区域";
  addAreaMForm.content = row.name;
  addAreaMForm.id = row.id;
};
// 墨轩湖-表格中确定添加
const addAreaMSub = async (formEl) => {
  if (!formEl) return;
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      if (addAreaMTitle.value == "添加区域") {
        let data = {
          name: addAreaMForm.content,
          parentId: addAreaMForm.id,
        };
        let res = await axios({
          method: "post",
          url: api.value + "/repairArea/insertRepairArea",
          headers: {
            token: sessionStorage.getItem("token"),
            user_head: sessionStorage.getItem("userhead"),
          },
          data: data,
        });
        console.log(res, "添加区域墨轩湖");
        if (res.data.code == 200) {
          getList(2);
          ElMessage({
            type: "success",
            showClose: true,
            message: res.data.message,
            center: true,
          });
          addAreaMvisible.value = false;
          addAreaM.value.resetFields();
        } else {
          ElMessage({
            type: "error",
            showClose: true,
            message: res.data.message,
            center: true,
          });
        }
      } else if (addAreaMTitle.value == "修改区域") {
        let data = {
          name: addAreaMForm.content,
          id: addAreaMForm.id,
        };
        // let res = await admin.adminAdd(data);
        let res = await axios({
          method: "post",
          url: api.value + "/repairArea/updateRepairAreaById",
          headers: {
            token: sessionStorage.getItem("token"),
            user_head: sessionStorage.getItem("userhead"),
          },
          data: data,
        });
        // console.log(res, "修改账号");
        if (res.data.code == 200) {
          getList(2);
          ElMessage({
            type: "success",
            showClose: true,
            message: res.data.message,
            center: true,
          });
          addAreaMvisible.value = false;
          addAreaM.value.resetFields();
        } else {
          ElMessage({
            type: "error",
            showClose: true,
            message: res.data.message,
            center: true,
          });
        }
      }
    } else {
      console.log("error submit!", fields);
    }
  });
};
// 墨轩湖-关闭添加功能
const addAreaMclose = () => {
  addAreaMvisible.value = false;
  addAreaM.value.resetFields();
};
// 墨轩湖-删除区域
const delAreaM = async (row) => {
  let data = {
    id: row.id,
  };
  // let res = await adminApi.adminLogin(data);
  let res = await axios({
    method: "get",
    url: api.value + "/repairArea/deleteRepairAreaById",
    headers: {
      token: sessionStorage.getItem("token"),
      user_head: sessionStorage.getItem("userhead"),
    },
    params: data,
  });
  if (res.data.code == 200) {
    getList(2);
    ElMessage({
      type: "success",
      showClose: true,
      message: res.data.message,
      center: true,
    });
  } else {
    ElMessage({
      type: "error",
      showClose: true,
      message: res.data.message,
      center: true,
    });
  }
};
// 墨轩湖-取消删除区域
const cencelAreaM = () => {
  ElMessage({
    type: "info",
    showClose: true,
    message: "取消删除",
    center: true,
  });
};

// 种类是否全选按钮
const handleCheckAllChange = (val) => {
  console.log(val);
  checkedCities.value = val ? cities : [];
  isIndeterminate.value = false;
};
//单独勾选种类
const handleCheckedCitiesChange = (value) => {
  console.log(value);
  const checkedCount = value.length;
  checkAll.value = checkedCount === cities.length;
  isIndeterminate.value = checkedCount > 0 && checkedCount < cities.length;
};
//编辑按钮  （-------------------------------------------）
const edit = (row) => {
  dialongTitle.value = "编辑耗材";
  addDialogVisible.value = true;
  ruleForm.serial = row.device_number;
  ruleForm.buslicence = row.car_number;
  ruleForm.volume = row.contain;
  ruleForm.id = row.id;
};
// 取消新增车辆
const cancelAdd = () => {
  addDialogVisible.value = false;
  ruleFormRef.value.resetFields();
};
// 确认新增车辆
const submitAdd = async (formEl) => {
  if (!formEl) return;
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      if (dialongTitle.value == "新增车辆") {
        let data = {
          device_number: ruleForm.serial,
          car_number: ruleForm.buslicence,
          contain: ruleForm.volume,
        };
        // let res = await admin.adminAdd(data);
        let res = await axios({
          method: "post",
          url: api.value + "/carBook/cinfoinsert.action",
          headers: {
            "Content-Type": "application/json;charset=utf-8",
            token: sessionStorage.getItem("token"),
          },
          data: data,
        });
        // console.log(res, "添加账号");
        if (res.data.code == 200) {
          getList();
          store.dispatch("busNum");
          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 = {
          device_number: ruleForm.serial,
          car_number: ruleForm.buslicence,
          contain: ruleForm.volume,
          id: ruleForm.id,
        };
        // let res = await admin.adminAdd(data);
        let res = await axios({
          method: "post",
          url: api.value + "/carBook/cinfoupdate.action",
          headers: {
            "Content-Type": "application/json;charset=utf-8",
            token: sessionStorage.getItem("token"),
          },
          data: data,
        });
        // console.log(res, "修改账号");
        if (res.data.code == 200) {
          getList();
          store.dispatch("busNum");
          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.value + "/carBook/cinfodel.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();
        store.dispatch("busNum");
        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 importExcel = async () => {
  if (searchInput.createTime == null) {
    searchInput.createTime = "";
  }
  let data = new FormData();
  data.set("car_number", searchInput.carnumber);
  data.set("create_time", searchInput.createTime);
  let res = await axios({
    method: "post",
    url: api.value + "/carBook/cinfotoExcel.action",
    headers: {
      token: sessionStorage.getItem("token"),
    },
    data: data,
  });
  // console.log(res, "导出账号");
  if (res.data.code == 200) {
    // const elt = document.createElement("a");
    // elt.setAttribute(
    //   "href",
    //   "https://chtech.ncjti.edu.cn/carstop" + res.data.downurl
    // );
    // elt.setAttribute("download", "file.png");
    // elt.style.display = "none";
    // document.body.appendChild(elt);
    // elt.click();
    var downloadPath = "https://chtech.ncjti.edu.cn/carstop" + res.data.downurl;
    console.log("获得地址数据:", downloadPath);
    var downloadLink = document.createElement("a");
    downloadLink.style.display = "none"; // 使其隐藏
    downloadLink.href = downloadPath;
    downloadLink.download = "";
    downloadLink.click();
    document.body.appendChild(downloadLink);
    document.body.removeChild(downloadLink);
    ElMessage({
      type: "success",
      showClose: true,
      message: res.data.message,
      center: true,
    });
  } else {
    ElMessage({
      type: "error",
      showClose: true,
      message: res.data.message,
      center: true,
    });
  }
};
// 表格斑马纹颜色修改
const tableRowClassName = ({ row, rowIndex }) => {
  if (rowIndex % 2 === 0) {
    return "even";
  } else if (rowIndex % 2 !== 0) {
    return "odd";
  }
  return "";
};

onBeforeMount(() => {
  api.value = store.state.user.api;
  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;
  box-shadow: 0px 3px 10px rgba(213, 228, 252, 1);

  .top {
    display: flex;
    align-items: center;
    height: 60px;
    margin: 0 30px;
    border-bottom: 1px solid #ccc;
    color: rgb(0, 0, 0);
    font-size: 18px;
    font-weight: 600;
  }

  .left {
    // width: calc(100wh - 40px);
    display: flex;
    align-items: center;
    height: 40px;
    margin: 10px 30px 0px;
    color: rgb(0, 0, 0);
    font-size: 18px;
    font-weight: 600;

    span {
      margin-right: 20px;
      cursor: pointer;
    }

    .is_active {
      color: rgba(111, 182, 184, 1);
    }
  }

  .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 {
        color: #fff;
      }

      .condition {
        display: flex;
        align-items: center;
        margin: 10px 30px 10px 0;

        :deep(.el-input .el-input__inner) {
          font-size: 16px;
        }

        span {
          margin: 0 10px 0 0;
        }
      }
    }

    .gongneng {
      margin: 10px 0 20px 0;

      span {
        color: #fff;
      }
    }

    :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%;
      user-select: none;

      :deep(.el-table__header-wrapper) {
        background-color: #000;
        font-size: 16px;

        tr {
          color: #000;
        }

        .el-table__cell:nth-child(1) {
          height: 50px;
          font-size: 16px;
          color: #000;

          .cell {
            display: flex;
            align-items: center;
            margin-left: 40px;

            // justify-content: center;
            // 有子节点 且未展开

          }
        }


      }

      :deep(.el-table__row) {
        height: 50px;
        font-size: 16px;
        color: #000;

        .el-table__cell:nth-child(1) {
          // background-color: red;

          .cell {

            margin-left: 30px;

            // justify-content: center;
            // 有子节点 且未展开
            .el-table__expand-icon {
              display: flex;
              align-items: center;
              margin: 0;

              .el-icon {
                display: none;
              }
            }

            img {
              width: 20px;
              height: 20px;
              margin-right: 6px;
            }
          }
        }

        img {
          width: 20px;
          height: 20px;
          margin-right: 6px;
        }
      }

      :deep(.el-table__expanded-cell) {
        padding: 0;

        .el-table__header-wrapper {
          display: none;
        }

        .el-table__body-wrapper {
          .el-table__row {
            .el-table__cell:nth-child(1) {
              .cell {
                margin-left: 74px;
              }
            }
          }
        }
      }

      // :deep(.el-table__row--level-1) {
      //   .cell {
      //     margin-left: 30px;
      //   }
      // }
      // :deep(.el-table__row--level-2) {
      //   .cell {
      //     margin-left: 30px;
      //   }
      // }
      // :deep(.el-table__row--level-3) {
      //   .cell {
      //     margin-left: 30px;
      //   }
      // }
      :deep(.el-table__row td) {
        padding: 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(.options) {
        display: flex;
        align-items: center;
        justify-content: center;
        color: rgba(111, 182, 184, 1);
      }

      :deep(.look) {
        padding: 2px 10px;
        cursor: pointer;
        border-radius: 15px;
        margin-right: 10px;
      }

      .add {
        border: 1px solid rgba(30, 125, 251, 1);
        color: rgba(30, 125, 251, 1);
      }

      .edit {
        border: 1px solid rgba(111, 182, 184, 1);
        color: rgba(111, 182, 184, 1);
      }

      .delete {
        border: 1px solid rgba(212, 48, 48, 1);
        color: rgba(212, 48, 48, 1);
      }

      :deep(.add):hover {
        background-color: rgba(30, 125, 251, 1);
        color: #fff;
      }

      :deep(.edit):hover {
        background-color: rgba(111, 182, 184, 1);
        color: #fff;
      }

      :deep(.delete):hover {
        background-color: rgba(212, 48, 48, 1);
        color: #fff;
      }
    }

    :deep(.Inside) {
      // border: 1px solid red;

      // .chosenClass {
      //   background: #ccc
      // }

      // .dragClass {
      //   border: 1px solid red;
      //   display: flex;
      //   align-items: center;
      //   background: rgba(0, 0, 0, 0.1);


      //   .el-table__row {
      //     .el-table__cell:nth-child(1) {
      //       width: 503px;
      //     }

      //     .el-table__cell:nth-child(2) {
      //       width: 455px;
      //     }

      //     .el-table__cell:nth-child(3) {
      //       width: 120px;
      //     }
      //   }

      // }


    }

    // 添加区域弹窗样式
    :deep(.addAreaH) {
      //   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-input {
          width: 200px;
        }

        .checkbox {
          .el-form-item__content {
            display: flex;
            flex-direction: column;
            align-items: flex-start;

            .el-checkbox-group {
              display: flex;
              flex-direction: column;
            }
          }
        }

        .options {
          .el-form-item__content {
            .queding {
              margin-left: 20px;
              color: #fff;
            }

            display: flex;
            flex-direction: row-reverse;
          }
        }
      }
    }

    // 添加区域弹窗样式
    :deep(.addAreaM) {
      //   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-input {
          width: 200px;
        }

        .checkbox {
          .el-form-item__content {
            display: flex;
            flex-direction: column;
            align-items: flex-start;

            .el-checkbox-group {
              display: flex;
              flex-direction: column;
            }
          }
        }

        .options {
          .el-form-item__content {
            .queding {
              margin-left: 20px;
              color: #fff;
            }

            display: flex;
            flex-direction: row-reverse;
          }
        }
      }
    }

    // 添加员工弹窗样式
    :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-input {
          width: 200px;
        }

        .checkbox {
          .el-form-item__content {
            display: flex;
            flex-direction: column;
            align-items: flex-start;

            .el-checkbox-group {
              display: flex;
              flex-direction: column;
            }
          }
        }

        .options {
          .el-form-item__content {
            .queding {
              margin-left: 20px;
              color: #fff;
            }

            display: flex;
            flex-direction: row-reverse;
          }
        }
      }
    }
  }

  // 加号图片样式
  :deep(.el-table .el-table__expand-icon) {
    margin: 0 4px 0 0px;
  }

  // 有子节点 未展开
  :deep(.el-table .el-table__expand-icon:before) {
    background: url("@/assets/close.png") no-repeat;
    content: "";
    display: block;
    width: 16px;
    height: 16px;
    font-size: 16px;
    background-size: 16px;
    transform: translateY(-2px);
  }

  // 有子节点 且已展开
  :deep(.el-table .el-table__expand-icon--expanded:before) {
    background: url("@/assets/open.png") no-repeat;
    content: "";
    display: block;
    width: 16px;
    height: 16px;
    font-size: 16px;
    background-size: 16px;
  }

  // 没有子节点
  :deep(.el-table .el-table__placeholder) {
    content: "";
    display: block;
    width: 16px;
    height: 16px;
    font-size: 16px;
    background-size: 16px;
    // margin-right: 40px;
  }

  // 展开折叠时不进行动画，否则图标会旋转
  :deep(.el-table .el-table__expand-icon--expanded) {
    transform: none;
  }

  .pageSize {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin: 0 30px;

    span {
      color: #000;
    }

    .el-pagination {

      // width: 1600px;
      :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(111, 182, 184, 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>
