<template>
  <div class="detail_container">
    <bg-breadcrumb></bg-breadcrumb>
    <div class="flex_row">
      <div class="flex_left">
        <div class="box">
          <el-date-picker style="width: 100%" type="date" placeholder="请选择日期" />

          <div class="type_station bg-scroll">
            <div
              class="type-box"
              :class="{ 'current-type': nodeClassifyId == item.classify_id }"
              @click="nodeClick(item)"
              v-for="(item, index) in typeList"
              :key="'type' + index + 200">
              {{ item.classify_name }}
            </div>
          </div>
        </div>
      </div>
      <div class="flex_right">
        <div class="form-filter">
          <el-form inline>
            <el-form-item label="级别" prop="force">
              <el-select placeholder="全部" style="width: 160px">
                <el-option label="选项1" value="1"> </el-option>
                <el-option label="选项2" value="2"> </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="关键字" prop="force">
              <el-input placeholder="请输入" maxlength="100" style="width: 240px"></el-input>
            </el-form-item>
          </el-form>
          <div class="operate_btns">
            <el-button type="primary"> 查询 </el-button>
            <el-button type="default"> 重置 </el-button>
          </div>
        </div>
        <div class="search-result"></div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref, onBeforeMount, toRefs, computed, watch, nextTick } from "vue";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";
import axios from "../../../../request/http.js";
import { Search } from "@element-plus/icons-vue";
import bgBreadcrumb from "@/components/bg-breadcrumb.vue";
const bgForm = ref(null);
const state = reactive({
  bgForm,
  typeList: [], // 分类数据
  typeKeyword: "", // 分类删选关键词
  nodeClassifyId: null, // 当前选中分类的uuid  用于新增字典
  nodeId: null, // 当前选中分类的id  用于请求列表
  timer: null, // 定时器
  headers: [
    {
      label: "名称",
      prop: "name",
    },
    {
      label: "描述",
      prop: "describe",
      minWidth: 360,
    },
    {
      label: "更新时间",
      prop: "updated_time",
      width: 220,
    },
    {
      label: "是否启用",
      prop: "state",
    },
    {
      label: "操作",
      prop: "action",
      width: 176,
      fixed: "right",
    },
  ], // 表格数据表头
  tableRows: [], // 表格数据
  tableTotal: 0, // 表格数据条数
  filter: {
    state: "",
    search: "",
    page: 1,
    limit: 10,
  }, // 表格筛选项
  stateOptions: [
    {
      name: "全部",
      value: "",
    },
    {
      name: "启用",
      value: "1",
    },
    {
      name: "禁用",
      value: "0",
    },
  ], // 启用禁用
  actionRow: null, // 当前操作的数据
  dialogDelete: false, // 删除弹窗
  addType: 0, //
  addDialog: false,
  formData: {
    name: "",
    describe: "",
    state: 1,
  },
  rules: {
    name: [{ required: true, message: "请输入名称", trigger: "blur" }],
    describe: [
      { required: true, message: "请输入描述", trigger: "blur" },
      { max: 200, message: "描述最大为200字", trigger: "blur" },
    ],
    state: [{ required: true, message: "请选择是否启用", trigger: "change" }],
  },
});

const nodeClick = (item) => {
  state.nodeId = item.id;
  state.nodeClassifyId = item.classify_id;
  state.filter = {
    state: "",
    search: "",
    page: 1,
    limit: 10,
  };
  changePage(1);
}; // 切换字典分类

const getTypeList = () => {
  let params = {
    name: state.typeKeyword,
  };
  axios
    .get(`/apaas/system/v5/dictionary/classify/list`, { params })
    .then((res) => {
      if (res.data.code == 200) {
        state.typeList = res.data.data || [];
        state.nodeClassifyId = state.typeList[0].classify_id || null;
        state.nodeId = state.typeList[0].id || null;
        if (state.nodeId) {
          getTableRows();
        }
      } else {
        ElMessage.error(res.data.data);
      }
    })
    .catch((err) => {
      console.log(err);
    });
}; // 获取字典分类

const changeSearch = (val) => {
  state.filter.search = val;
  changePage(1);
}; // 表格关键字筛选

const filterAction = () => {
  changePage(1);
}; // 查询按钮

const filterClear = () => {
  state.filter = {
    state: "",
    search: "",
    limit: 10,
    page: 1,
  };
  changePage(1);
}; // 重置筛选项

const getTableRows = () => {
  let params = { ...state.filter };
  params.id = state.nodeId;
  axios
    .get(`/apaas/system/v5/dictionary/list`, {
      params,
    })
    .then((res) => {
      if (res.data.code == 200) {
        state.tableRows = res.data.data || [];
        state.tableTotal = res.data.total;
        if (state.tableRows.length > 0) {
          state.tableRows.forEach((e) => {
            e.canMoveUp = true;
            e.canMoveDown = true;
          });
          state.tableRows[0].canMoveUp = false;
          state.tableRows[state.tableRows.length - 1].canMoveDown = false;
        }
      } else {
        ElMessage.error(res.data.data);
      }
    });
}; // 获取表格数据

const changeUseRow = (row) => {
  axios.put(`/apaas/system/v5/dictionary/state?id=${row.id}&state=${row.state}`).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success(res.data.msg);
      changePage(1);
    } else {
      ElMessage.error(res.data.data);
      row.state = row.state == 0 ? 1 : 0;
    }
  });
}; // 启用禁用

const changePage = (page) => {
  state.filter.page = page;
  getTableRows();
}; // 改变页码

const changeSize = (size) => {
  state.filter.limit = size;
  changePage(1);
}; // 改变每页条数

const register = () => {
  state.formData = {
    name: "",
    describe: "",
    state: 1,
  };
  if (state.bgForm) {
    nextTick().then(() => {
      state.bgForm.validate((valid) => {
        if (!valid) {
          state.bgForm.clearValidate();
        }
      });
    });
  }
  state.addType = 1;
  state.addDialog = true;
}; // 新增字典按钮

const edit_row = (row) => {
  axios
    .get(`/apaas/system/v5/dictionary/${row.id}`)
    .then((res) => {
      if (res.data.code == 200) {
        state.actionRow = res.data.data;
        state.formData = {
          name: state.actionRow.name,
          describe: state.actionRow.describe,
          state: state.actionRow.state,
        };
      } else {
        ElMessage.error(res.data.data);
      }
    })
    .catch((err) => {
      console.log(err);
    });
  if (state.bgForm) {
    nextTick().then(() => {
      state.bgForm.validate((valid) => {
        if (!valid) {
          state.bgForm.clearValidate();
        }
      });
    });
  }
  state.addType = 2;
  state.addDialog = true;
}; // 编辑按钮

const addConfirm = () => {
  state.bgForm.validate((valid) => {
    if (valid) {
      if (state.addType == 1) {
        // 新增
        let params = {
          classify_id: state.nodeClassifyId,
          ...state.formData,
        };
        axios.post(`/apaas/system/v5/dictionary/add`, params).then((res) => {
          if (res.data.code == 200) {
            ElMessage.success(res.data.msg);
            state.addDialog = false;
            changePage(1);
          } else {
            ElMessage.error(res.data.data);
          }
        });
      } else {
        // 编辑
        let params = {
          id: state.actionRow.id,
          ...state.formData,
        };
        axios.put(`/apaas/system/v5/dictionary/update`, params).then((res) => {
          if (res.data.code == 200) {
            ElMessage.success(res.data.msg);
            state.addDialog = false;
            changePage(1);
          } else {
            ElMessage.error(res.data.data);
          }
        });
      }
    }
  });
}; // 确定新增/编辑

const delete_row = (row) => {
  state.dialogDelete = true;
  state.actionRow = row;
}; // 删除按钮

const deleteData = () => {
  axios.delete(`/apaas/system/v5/dictionary/${state.actionRow.id}`).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success(res.data.msg);
      state.dialogDelete = false;
      changePage(1);
    } else {
      ElMessage.error(res.data.data);
    }
  });
}; // 确定删除

const moveRow = (row, type) => {
  let index;
  state.tableRows.forEach((e, i) => {
    if (e.id == row.id) {
      index = i;
    }
  });
  let nextRow;
  if (type == 1) {
    // 上移
    nextRow = state.tableRows[index - 1];
  } else {
    // 下移
    nextRow = state.tableRows[index + 1];
  }
  let params = [
    {
      id: row.id,
      sort: nextRow.sort,
    },
    {
      id: nextRow.id,
      sort: row.sort,
    },
  ];
  axios
    .put(`/apaas/system/v5/dictionary/sort`, [...params])
    .then((res) => {
      if (res.data.code == 200) {
        ElMessage.success(res.data.msg);
        changePage(1);
      } else {
        ElMessage.error(res.data.data);
      }
    })
    .catch((err) => {
      console.log(err);
    });
};

onBeforeMount(() => {
  getTypeList();
});

const { typeList, typeKeyword, nodeClassifyId, headers, tableRows, tableTotal, filter, stateOptions } = toRefs(state);
</script>

<style lang="scss" scoped>
.detail_container {
  width: 100%;
  height: calc(100vh - 56px);
  padding: 0 24px;
  min-height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  align-items: stretch;
  .flex_row {
    // flex-grow: 1;
    width: 100%;
    display: flex;

    margin-bottom: 16px;
    position: relative;
    height: calc(100% - 62px);
    .flex_left {
      width: 240px;
      background-color: #fff;
      margin-right: 16px;
      border-radius: 6px;
      padding: 16px;
      height: 100%;
      box-shadow: 0px 1px 4px 0px rgba(0, 7, 101, 0.15);
      overflow: hidden;

      .box {
        width: 208px;
        height: 100%;
        overflow: hidden;
        .type_station {
          width: 100%;
          margin-top: 16px;
          height: calc(100% - 50px);
          .type-box {
            height: 32px;
            line-height: 32px;
            text-align: center;
            font-size: 14px;
            color: #404a62;
            cursor: pointer;
            &:hover {
              background-color: #f2f3f7;
            }
          }
          .current-type {
            background-color: #f2f3f7;
            border-radius: 4px;
          }
        }
      }
    }
    .flex_right {
      flex: 1;
      height: 100%;

      .form-filter {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 16px;
        height: 68px;
        width: 100%;
        padding: 16px 16px 16px 24px;
        background-color: #ffffff;
        box-shadow: 0px 1px 4px 0px rgba(0, 7, 101, 0.15);
        border-radius: 6px;
        .el-form {
          width: calc(100% - 144px);
        }
        .el-form-item {
          margin-bottom: 0px;
        }
        .operate_btns {
          width: 144px;
        }
      }
      .search-result {
        width: 100%;
        height: calc(100% - 84px);
        background-color: #1a1a1a;
        box-shadow: 0px 1px 4px 0px rgba(0, 7, 101, 0.1);
        border-radius: 6px;
      }
    }
  }
}
</style>
