<template>
  <div class="page_container">
    <bg-breadcrumb></bg-breadcrumb>
    <div class="main_container">
      <bg-filter-group @search="changeSearch" v-model="filter.keyword" placeholder="请输入业务系统名称、账号或所属机构">
        <template v-slot:filter_group>
          <div class="left-filter filter_list">
            <div class="filter_item">
              <span class="filter_title">审批状态</span>
              <el-select v-model="filter.state" placeholder="请选择" style="width: 300px">
                <el-option
                  v-for="(item, index) in stateOptions"
                  :key="'pushOptions' + index"
                  :label="item.name"
                  :value="item.value">
                </el-option>
              </el-select>
            </div>
            <div class="filter_item">
              <span class="filter_title">提交时间</span>
              <el-date-picker
                v-model="filter.time"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="YYYY-MM-DD"
                style="width: 300px" />
            </div>
          </div>
          <div class="right-action apaas_button">
            <el-button type="primary" @click="filterAction"> 查询 </el-button>
            <el-button type="default" @click="filterClear"> 重置 </el-button>
          </div>
        </template>
      </bg-filter-group>
      <div class="table_container">
        <bg-table ref="bgTable" :headers="headers" :rows="tableRows" :isIndex="true" :stripe="true">
          <template v-slot:ability_name="{ row }">
            <span @click="goDetail(row)" class="can_click_text">
              {{ row.ability_name }}
              <span v-if="row.include_mock_service" class="mock_tip"> mock </span>
            </span>
          </template>
          <template v-slot:ability_state="{ row }">
            <span class="circle" :class="'bgc_' + row.ability_state"></span>
            {{ ["待上架", "已上架", "已下架", "上架中", "下架中"][row.ability_state] }}
          </template>
          <template v-slot:action="{ row }">
            <bg-table-btns2 :limit="3" :key="row.id">
              <bg-table-btn @click="approve(row)">审批</bg-table-btn>
              <bg-table-btn @click="approveDetail(row)">审批详情</bg-table-btn>
            </bg-table-btns2>
          </template>
        </bg-table>
        <bg-pagination
          :page="filter.page"
          :size="filter.size"
          :total="tableTotal"
          @change-page="changePage"
          @change-size="changeSize">
        </bg-pagination>
      </div>
    </div>

    <el-dialog class="dialog_box" title="删除" v-model="dialogDelete" width="400px">
      <div style="text-align: left">
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;确定要删除数据吗？数据删除后不能恢复，请谨慎操作
      </div>
      <template v-slot:footer>
        <div class="apaas_button">
          <el-button type="default" @click="dialogDelete = false">取 消</el-button>
          <el-button type="primary" @click="deleteData">确 定</el-button>
        </div>
      </template>
    </el-dialog>

    <el-dialog class="dialog_box" title="提示" v-model="cacheDialog" width="400px">
      <div style="font-size: 16px; color: #404a62">你有未提交的服务，是否继续编辑该服务？</div>
      <template v-slot:footer>
        <div class="apaas_button">
          <el-button type="default" @click="cancelCache">取 消</el-button>
          <el-button type="primary" @click="confirmCache">确 定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { reactive, toRefs, computed, onBeforeMount } from "vue";
import { useRouter } from "vue-router";
import bgBreadcrumb from "@/components/bg-breadcrumb.vue";
import store from "@/store";
import axios from "@/request/http.js";
import { ElMessage } from "element-plus";

const router = useRouter();
const state = reactive({
  filter: {
    state: "",
    keyword: "",
    page: 1,
    size: 10,
    abilityType: 0,
    time: [],
  },
  stateOptions: [
    {
      name: "全部",
      value: "",
    },
    {
      name: "已上架",
      value: "1",
    },
    {
      name: "已下架",
      value: "2",
    },
    {
      name: "上架中",
      value: "3",
    },
    {
      name: "下架中",
      value: "4",
    },
    {
      name: "待上架",
      value: "0",
    },
  ],
  headers: [
    {
      label: "业务系统名称",
      prop: "ability_name",
      minWidth: 280,
    },
    {
      label: "账号",
      prop: "synopsis",
      minWidth: 360,
    },
    {
      label: "所属机构",
      prop: "created_time",
      width: 200,
    },
    {
      label: "审批状态",
      prop: "ability_state",
      width: 120,
    },
    {
      label: "提交时间",
      prop: "ability_state",
      width: 120,
    },
    {
      label: "操作",
      prop: "action",
      width: 270,
      fixed: "right",
    },
  ],
  tableRows: [],
  tableTotal: 0,
  actionRow: {},
  dialogDelete: false,
  cacheDialog: false,
});

const { filter, stateOptions, headers, tableRows, tableTotal, dialogDelete, cacheDialog } = toRefs(state);
// 认证方案未确定，暂时使用假数据registerValid，后续更改
const registerValid = computed(() => {
  return store.state.registerValid;
});
const register = () => {
  // 请求认证校验接口
  // 未认证跳转认证页"/ability-register/add/valid"
  // 认证状态取用store中的registerValid，接口未出 暂时做假数据
  console.log("跳转能力注册页");
  // if (!registerValid.value) {
  //   router.push({
  //     path: "/ability-register/add/valid",
  //     query: {
  //       abilityType: 0,
  //     },
  //   });
  // } else {
  // 已认证情况
  // 1.请求缓存接口看是否有缓存，有则显示弹窗，没有则直接跳转注册页"/ability-register/add"
  axios.get(`/apaas/service/v5/ability/get/cach`).then((res) => {
    if (res.data.code == 200) {
      if (res.data.data) {
        // 有缓存打开弹窗
        state.cacheDialog = true;
      } else {
        // 无缓存
        router.push("/ability-register/add");
      }
    }
  });
  // }
};
const cancelCache = () => {
  axios
    .get(`/apaas/service/v5/ability/clear/cach?abilityType=0`)
    .then((res) => {
      if (res.data.code == 200) {
        state.cacheDialog = false;
        router.push("/ability-register/add");
      } else {
        ElMessage.error(res.data.data);
      }
    })
    .catch((err) => {
      router.push("/ability-register/add");
    });
}; // 取消按钮，清空缓存跳转注册页
const confirmCache = () => {
  state.cacheDialog = false;
  router.push("/ability-register/add");
}; // 确定按钮，直接跳转注册页
const changeSearch = (val) => {
  state.filter.keyword = val;
  changePage(1);
};
const changePage = (page) => {
  state.filter.page = page;
  getTableRows();
};
const getTableRows = () => {
  let params = { ...state.filter };
  params.registerStartTime = params.time ? params.time[0] || "" : "";
  params.registerEndTime = params.time ? params.time[1] || "" : "";
  delete params.time;
  axios
    .get(`/apaas/service/v5/ability/list`, {
      params,
    })
    .then((res) => {
      if (res.data.code == 200) {
        state.tableRows = res.data.data || [];
        state.tableTotal = res.data.total;
      } else {
        ElMessage.error(res.data.data);
      }
    });
};
const filterAction = () => {
  changePage(1);
};
const changeSize = (size) => {
  state.filter.size = size;
  changePage(1);
};
const filterClear = () => {
  state.filter = {
    state: "",
    keyword: "",
    page: 1,
    size: 10,
    abilityType: 0,
    time: [],
  };
  changePage(1);
};
const approve = (row) => {
  router.push(`/develop/systemApproval/approval`);
};
const approveDetail = (row) => {
  router.push(`/develop/systemApproval/detail`);
};
const deleteData = () => {
  console.log(state.actionRow.id);
  axios.delete(`/apaas/service/v5/ability/${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 goDetail = (row) => {
  router.push({
    path: "/ability-manage/real-list/detail",
    query: {
      id: row.id,
    },
  });
};
onBeforeMount(() => {
  getTableRows();
});
</script>

<style lang="scss" scoped>
.page_container {
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  align-items: stretch;
  width: 100%;
  padding: 0 24px;
  min-height: 100%;
  .main_container {
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    align-items: stretch;
    flex-grow: 1;
    width: 100%;
    background-color: #fff;
    box-shadow: 0px 1px 4px 0px rgba(0, 7, 101, 0.15);
    border-radius: 6px;
    padding: 0 0 16px 0;
    margin-bottom: 16px;
    position: relative;
    .filter-group {
      .left-filter {
        flex: 1;
        display: flex;
        justify-content: start;
        flex-wrap: wrap;
        .filter_item {
          display: flex;
          align-items: center;
          .filter_title {
            width: 84px;
            white-space: pre-wrap;
            font-weight: normal;
          }
        }
      }
      .right-action {
        width: 144px;
        padding-bottom: 16px;
        .el-button {
          width: 64px;
        }
      }
    }
    .table_container {
      padding: 0 16px;
      .el-table {
        flex: 1;
        .circle {
          display: inline-block;
          width: 6px;
          height: 6px;
          border-radius: 3px;
          margin-right: 8px;
          transform: translateY(-2px);
        }
        .bgc_1 {
          background-color: #48ad97;
        }
        .bgc_2 {
          background-color: #d75138;
        }
        .bgc_3 {
          background-color: #3759be;
        }
        .bgc_4 {
          background-color: #ea7d19;
        }
        .bgc_0 {
          background-color: #9e9e9e;
        }
        .mock_tip {
          display: inline-block;
          margin-left: 4px;
          width: 43px;
          height: 20px;
          font-size: 12px;
          color: #2b4695;
          text-align: center;
          background-color: #eff2fa;
          border-radius: 3px;
          border: solid 1px #b0bee8;
        }
      }
    }
  }
}
</style>
