<template>
  <div class="page_container">
    <bg-breadcrumb></bg-breadcrumb>
    <div class="main_container">
      <bg-filter-group @search="changeSearch" v-model="filter.search" 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.status" 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>
              <bg-table-btn v-if="row.status === 0" @click="approve(row)">审批</bg-table-btn>
              <bg-table-btn v-else @click="approveDetail(row)">审批详情</bg-table-btn>
            </bg-table-btns2>
          </template>
        </bg-table>
        <bg-pagination
          :page="filter.page"
          :size="filter.limit"
          :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 axios from "@/request/http.js";
import { ElMessage } from "element-plus";

const router = useRouter();
const state = reactive({
  filter: {
    search: "",
    status: "",
    time: [],
    page: 1,
    limit: 10,
  },
  stateOptions: [
    {
      name: "全部",
      value: "",
    },
    {
      name: "待审批",
      value: 0,
    },
    {
      name: "通过",
      value: 1,
    },
    {
      name: "驳回",
      value: 2,
    },
  ],
  headers: [
    {
      label: "业务系统名称",
      prop: "business_name",
      minWidth: 120,
    },
    {
      label: "账号",
      prop: "system_account",
      minWidth: 120,
    },
    {
      label: "所属机构",
      prop: "organization_name",
      width: 200,
    },
    {
      label: "审批状态",
      prop: "status",
      width: 120,
    },
    {
      label: "提交时间",
      prop: "created_time",
      minWidth: 180,
    },
    {
      label: "操作",
      prop: "action",
      width: 180,
      fixed: "right",
    },
  ],
  tableRows: [],
  tableTotal: 0,
  actionRow: {},
  dialogDelete: false,
  cacheDialog: false,
});

const { filter, stateOptions, headers, tableRows, tableTotal, dialogDelete, cacheDialog } = toRefs(state);
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.search = val;
  changePage(1);
};
const changePage = (page) => {
  state.filter.page = page;
  getTableRows();
};
const getTableRows = () => {
  let params = { ...state.filter };
  params.start_at = params.time ? params.time[0] || "" : "";
  params.end_at = params.time ? params.time[1] || "" : "";
  delete params.time;
  if (!params.status.toString()) delete params.status;
  axios
    .get(`/apaas/system/v5/user/approval/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 = (limit) => {
  state.filter.limit = limit;
  changePage(1);
};
const filterClear = () => {
  state.filter = {
    search: "",
    status: 0,
    time: [],
    page: 1,
    limit: 10,
  };
  changePage(1);
};
const approve = (row) => {
  router.push({
    path: "/develop/systemApproval/approval",
    query: {
      id: row.id,
    },
  });
};
const approveDetail = (row) => {
  router.push({
    path: "/develop/systemApproval/detail",
    query: {
      id: row.id,
    },
  });
};
const deleteData = () => {
  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>
