<template>
  <el-card shadow="never" class="page page-wrapper">
    <el-form :model="tableData.param" ref="queryRef" inline>
      <el-form-item>
        <el-button @click="onOpenEdit('')">
          <el-icon>
            <ele-Plus />
          </el-icon>
          添加
        </el-button>
        <!-- <el-button disabled type="primary" class="ml10" @click="onOpenAdd" v-auth="'add'">
          <el-icon>
            <ele-FolderAdd />
          </el-icon>
          批量执行
        </el-button> -->
      </el-form-item>
    </el-form>
    <el-table :data="tableData.data" style="width: 100%" v-loading="tableData.loading">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="序号" align="center" prop="id" width="100" />
      <el-table-column label="标题" prop="title" show-overflow-tooltip />
      <el-table-column label="说明" prop="description" show-overflow-tooltip />
      <el-table-column prop="status" label="类型" width="100" align="center" >
        <template #default="scope">
          <div v-if="scope.row.types == 1">手动</div>
          <div v-if="scope.row.types == 2">自动</div>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" width="80" align="center" >
        <template #default="scope">
          <el-tag type="success" size="small" v-if="scope.row.status == 1">启用</el-tag>
          <el-tag type="info" size="small" v-if="scope.row.status == 2">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="220" align="center" fixed="right">
        <template #default="scope">
          <el-button size="small" text type="info" v-if="scope.row.status === 1" @click="setStatus(scope.row, 2)">禁用</el-button>
          <el-button size="small" text type="primary" v-else @click="setStatus(scope.row, 1)">启用</el-button>
          <el-button size="small" text type="primary" @click="onOpenRecord(scope.row)">执行</el-button>
          <el-button size="small" text type="primary" @click="onOpenPolicyRecord(scope.row)">策略</el-button>
          <el-button size="small" text type="warning" @click="onOpenEdit(scope.row)">修改</el-button>
          <el-button size="small" text type="info" @click="onRowDel(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="tableData.total > 0" :total="tableData.total" v-model:page="tableData.param.pageNum" v-model:limit="tableData.param.pageSize" @pagination="typeList" />
    <Execute @getList="updeteList()" ref="executeRef" />
    <AddPolicy @getList="updeteList()" ref="addPolicyRef" />
  </el-card>
</template>

<script lang="ts" setup>
import { ref, onMounted, defineAsyncComponent } from 'vue';
import api from '/@/api/modules/policy';
import { ElMessage, ElMessageBox } from "element-plus";
import { getToken } from "/@/utils/auth";

const Execute = defineAsyncComponent(() => import("/@/views/modules/policy/index/components/execute.vue"));
const AddPolicy = defineAsyncComponent(() => import("/@/views/modules/policy/index/components/addItem.vue"));

const  tableData =  ref({
  data: [],
  total: 0,
  loading: false,
  param: {
    pageNum: 1,
    pageSize: 20,
  }
})
const executeRef = ref();
const addPolicyRef = ref();

const onOpenEdit = (row:any) => {
  addPolicyRef.value.openDialog(row);
};
const onOpenRecord = (row: any) => {
  executeRef.value.openDialog(row);
};

const onRowDel = (row: any) => {
  let msg = '你确定要删除所选数据？';
  ElMessageBox.confirm(msg, '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    api.del(row.id).then(() => {
      ElMessage.success('删除成功');
      updeteList();
    });
  }).catch(() => { });
};

const onOpenPolicyRecord = async (row: any) => {
  localStorage.setItem("auth-tokens", `{"access_token":"${getToken()}"}`);
  const params:any = {};
  row.paramData.forEach((item: any) => {
    params[item.key] = item.value || '';
  });
  const url = `/plugin/rule/index.html?${(new URLSearchParams(params)).toString()}#${row.flowId}`;
  window.open(url);
};

const setStatus = (row: any, status: number) => {
  api.setStatus({
    id: row.id,
    status: status
  }).then(() => {
    ElMessage.success('操作成功');
    updeteList();
  });
};

const updeteList = ( ) => {
  if(tableData.value.param.pageNum !== 1) tableData.value.param.pageNum = 1;
  getList();
}

const getList = () => {
  tableData.value.loading = true;
  api.getList(tableData.value.param).then((res: any) => {
    tableData.value.data = res.list;
    tableData.value.total = res.Total;
  }).finally(() => {
    tableData.value.loading = false;
  });
};

// 页面加载时
onMounted(() => {
  getList();
});
</script>