<template>
  <div class="guns-layout">
    <div class="guns-layout">
      <div class="guns-layout-sidebar p-t-12">
        <div class="sidebar-content">
          <FlowModelTree @treeSelect="treeSelect" />
        </div>
      </div>
      <div class="guns-layout-content">
        <div class="guns-layout">
          <div class="guns-layout-content-header">
            <a-button shape="circle" size="large" @click="back" style="margin-right: 10px"><left-outlined /></a-button>
            <a-tabs v-model:activeKey="where.linkType" @change="reload">
              <a-tab-pane :key="0" tab="全部"> </a-tab-pane>
              <a-tab-pane :key="10" tab="机构"> </a-tab-pane>
              <a-tab-pane :key="20" tab="人员"> </a-tab-pane>
              <a-tab-pane :key="30" tab="角色"> </a-tab-pane>
            </a-tabs>
          </div>
          <div class="guns-layout-content-application">
            <div class="content-mian">
              <div class="content-mian-header">
                <div class="header-content">
                  <div class="header-content-left">
                    <a-space :size="20">
                      <a-input
                        allowClear
                        v-model:value="where.searchText"
                        placeholder="名称、编码（回车搜索）"
                        @pressEnter="reload"
                        class="search-input"
                      >
                        <template #prefix>
                          <icon-font iconClass="icon-opt-search"></icon-font>
                        </template>
                      </a-input>
                    </a-space>
                  </div>
                  <div class="header-content-right">
                    <a-space>
                      <a-button type="primary" class="border-radius" @click="openAddEdit()" v-if="where.flowModelId && where.linkType != 0"
                        ><plus-outlined />新建</a-button
                      >
                      <a-dropdown>
                        <template #overlay>
                          <a-menu @click="moreClick">
                            <a-menu-item key="1">
                              <icon-font iconClass="icon-opt-zidingyilie" color="#60666b"></icon-font>
                              <span>自定义列</span>
                            </a-menu-item>
                            <div>
                              <a-menu-item key="2">
                                <icon-font iconClass="icon-opt-shanchu" color="#60666b"></icon-font>
                                <span>批量删除</span>
                              </a-menu-item>
                            </div>
                          </a-menu>
                        </template>
                        <a-button class="border-radius">
                          更多
                          <small-dash-outlined />
                        </a-button>
                      </a-dropdown>
                    </a-space>
                  </div>
                </div>
              </div>
              <div class="content-mian-body">
                <div class="table-content">
                  <common-table :columns="columns" :where="where" size="default" rowId="flowAuthId" ref="tableRef" url="/flowAuth/page">
                    <template #bodyCell="{ column, record }">
                      <template v-if="column.dataIndex == 'linkType'">
                        <span v-if="record.linkType == 0">全部</span>
                        <span v-if="record.linkType == 10">机构</span>
                        <span v-if="record.linkType == 20">人员</span>
                        <span v-if="record.linkType == 30">角色</span>
                      </template>
                      <template v-if="column.key == 'action'">
                        <a-space>
                          <icon-font
                            iconClass="icon-opt-shanchu"
                            font-size="24px"
                            title="删除"
                            color="#60666b"
                            @click="remove(record)"
                          ></icon-font>
                        </a-space>
                      </template>
                    </template>
                  </common-table>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 自定义列 -->
    <Custom
      v-model:visible="isShowCustom"
      v-if="isShowCustom"
      :data="columns"
      @done="val => (columns = val)"
      :fieldBusinessCode="fieldBusinessCode"
    />

    <!-- 选择弹框 -->
    <Selection
      v-model:visible="showEdit"
      v-if="showEdit"
      :data="selectedData"
      :showTab="showTab"
      :title="'选择'"
      :isRadio="false"
      @done="closeFlowPick"
    ></Selection>
  </div>
</template>

<script setup name="MidWorkFlowPower">
import { useRouter } from 'vue-router';
import { onMounted, ref, watch, createVNode } from 'vue';
import { FlowPowerApi } from './api/FlowPowerApi';
import { message, Modal } from 'ant-design-vue/es';
import FlowModelTree from './components/flow-mode-tree.vue';
import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
import { CustomApi } from '@/components/common/Custom/api/CustomApi';

defineOptions({
  name: 'MidWorkFlowPower'
});

const router = useRouter();

// ref
const tableRef = ref(null);

// 搜索条件
const where = ref({
  searchText: '',
  flowModelId: '',
  linkType: 0
});

// 表格配置
const columns = ref([
  {
    key: 'index',
    title: '序号',
    width: 60,
    align: 'center',
    isShow: true,
    hideInSetting: true
  },
  {
    dataIndex: 'linkType',
    title: '类型',
    ellipsis: true,
    width: 200,
    isShow: true
  },
  {
    dataIndex: 'businessName',
    title: '名称',
    width: 200,
    isShow: true
  },
  {
    dataIndex: 'flowModelName',
    title: '流程模型名称',
    width: 200,
    isShow: true
  },
  {
    dataIndex: 'createTime',
    title: '创建时间',
    ellipsis: true,
    width: 150,
    isShow: true
  },
  {
    key: 'action',
    title: '操作',
    width: 80,
    isShow: true
  }
]);
// 是否显示新增编辑弹框
const showEdit = ref(false);
// 是否显示自定义列
const isShowCustom = ref(false);
// 业务标识的编码
const fieldBusinessCode = ref('WORKFLOW_POWER');

// 显示的tab
const showTab = ref(['user']);

// 总数据
const selectedData = ref({
  selectRoleList: [],
  selectUserList: [],
  selectOrgList: []
});

onMounted(() => {
  getColumnData();
});

// 获取表格配置
const getColumnData = () => {
  CustomApi.getUserConfig({ fieldBusinessCode: fieldBusinessCode.value }).then(res => {
    if (res.tableWidthJson) {
      columns.value = JSON.parse(res.tableWidthJson);
    }
  });
  reload();
};

// 点击搜索
const reload = () => {
  tableRef.value?.reload();
};

// 树选择
const treeSelect = data => {
  where.value.flowModelId = data.modelId;
  reload();
};

// 新增编辑点击
const openAddEdit = () => {
  selectedData.value = {
    selectRoleList: [],
    selectUserList: [],
    selectOrgList: []
  };
  if (where.value.linkType == 10) {
    showTab.value = ['dept'];
  } else if (where.value.linkType == 20) {
    showTab.value = ['user'];
  } else if (where.value.linkType == 30) {
    showTab.value = ['role'];
  }
  showEdit.value = true;
};

// 关闭选择弹框
const closeFlowPick = data => {
  let businessIdList = [];
  if (where.value.linkType == 10) {
    businessIdList = data.selectOrgList.map(item => item.bizId);
  } else if (where.value.linkType == 20) {
    businessIdList = data.selectUserList.map(item => item.bizId);
  } else if (where.value.linkType == 30) {
    businessIdList = data.selectRoleList.map(item => item.bizId);
  }

  if (businessIdList?.length > 0) {
    let params = {
      businessIdList: businessIdList,
      flowModelId: where.value.flowModelId,
      linkType: where.value.linkType
    };
    FlowPowerApi.add(params).then(res => {
      message.success(res.message);
      reload();
    });
  }
};

// 删除单个
const remove = record => {
  Modal.confirm({
    title: '提示',
    content: '确定要删除选中数据吗?',
    icon: createVNode(ExclamationCircleOutlined),
    maskClosable: true,
    onOk: async () => {
      const res = await FlowPowerApi.delete({ flowAuthId: record.flowAuthId });
      message.success(res.message);
      reload();
    }
  });
};

// 批量删除
const batchDelete = () => {
  if (tableRef.value.selectedRowList && tableRef.value.selectedRowList.length == 0) {
    return message.warning('请选择需要删除的数据');
  }
  Modal.confirm({
    title: '提示',
    content: '确定要删除选中的数据吗?',
    icon: createVNode(ExclamationCircleOutlined),
    maskClosable: true,
    onOk: async () => {
      const res = await FlowPowerApi.batchDelete({ batchDeleteIdList: tableRef.value.selectedRowList });
      message.success(res.message);
      reload();
    }
  });
};

// 更多点击
const moreClick = ({ key }) => {
  if (key == '1') {
    isShowCustom.value = true;
  } else if (key == '2') {
    batchDelete();
  }
};

const back = () => {
  router.push('/mid/workflow/model');
};
</script>

<style scoped lang="less">
.guns-layout-content {
  padding-top: 20px;
}
:deep(.ant-tabs) {
  width: 100%;
}
:deep(.ant-tabs-tab-btn) {
  font-size: 17px;
}
</style>
