<template>
  <div class="guns-layout">
    <guns-split-layout v-show="!showViewConfig" width="0px" :resizable="false" :allowCollapse="false">
      <div class="guns-layout-sidebar width-100 p-t-12" v-if="false">
        <div class="sidebar-content">
          <BuinessTree @treeSelect="treeSelect" />
        </div>
      </div>
      <template #content>
        <div class="guns-layout-content" v-show="!showViewConfig">
          <div class="guns-layout">
            <div class="guns-layout-content-application">
              <div class="content-mian">
                <div class="content-mian-body">
                  <div class="table-content">
                    <common-table
                      :columns="columns"
                      :where="where"
                      size="middle"
                      :isInit="false"
                      rowId="viewId"
                      ref="tableRef"
                      url="/devopsView/page"
                      :showToolTotal="false"
                    >
                      <template #toolLeft>
                        <a-input
                          v-model:value="where.searchText"
                          allowClear
                          placeholder="视图名称、编码"
                          @pressEnter="reload"
                          :bordered="false"
                          style="width: 300px"
                          class="search-input"
                        >
                          <template #prefix>
                            <icon-font iconClass="icon-opt-search"></icon-font>
                          </template>
                        </a-input>
                      </template>
                      <template #toolRight>
                        <a @click="openAddEdit()">新建</a>
                        <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>更多</a>
                        </a-dropdown>
                      </template>
                      <template #bodyCell="{ column, record }">
                        <template v-if="column.dataIndex == 'viewName'">
                          <a @click="viewConfig(record)">{{ record.viewName }}</a>
                        </template>
                        <template v-if="column.dataIndex == 'viewType'">
                          <a-tag v-if="record.viewType == 1" color="blue">列表视图</a-tag>
                          <a-tag v-if="record.viewType == 2" color="orange">其他视图</a-tag>
                        </template>
                        <!-- 操作 -->
                        <template v-if="column.key == 'action'">
                          <a-space :size="16">
                            <icon-font
                              iconClass="icon-opt-bianji"
                              font-size="24px"
                              color="#60666b"
                              title="编辑"
                              @click="openAddEdit(record)"
                            ></icon-font>
                            <icon-font
                              iconClass="icon-opt-shezhi"
                              font-size="24px"
                              color="#60666b"
                              title="视图设计"
                              @click="viewConfig(record)"
                            ></icon-font>
                            <icon-font
                              iconClass="icon-opt-shanchu"
                              font-size="24px"
                              color="#60666b"
                              title="删除"
                              @click="remove(record)"
                            ></icon-font>
                          </a-space>
                        </template>
                      </template>
                    </common-table>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </template>
    </guns-split-layout>

    <!-- 视图配置 -->
    <ViewConfig v-if="showViewConfig" :data="current" @back="showViewConfig = false" />

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

    <!-- 新增编辑弹框 -->
    <ViewAddEdit
      v-model:visible="showEdit"
      v-if="showEdit"
      :data="current"
      @done="refresh"
      :businessCategoryId="where.businessCategoryId"
      :businessId="where.businessId"
    />
  </div>
</template>

<script setup name="BuinessViewList">
import { useGeneratorStore } from '@/store/modules/generator';
import { onMounted, computed, ref, createVNode } from 'vue';
import { useRouter } from 'vue-router';
import { SheetViewApi } from './api/SheetViewApi';
import { message, Modal } from 'ant-design-vue/es';
import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
import { CustomApi } from '@/components/common/Custom/api/CustomApi';
import BuinessTree from '../list/components/list-type/buiness-tree.vue';
import ViewAddEdit from './components/view-add-edit.vue';
import ViewConfig from './components/view-config.vue';

defineOptions({
  name: 'BuinessViewList'
});

const props = defineProps({
  getBusinessStatCount: Function
});

const router = useRouter();

const generatorStore = useGeneratorStore();

// 当前选中应用数据
const buinessSelectAppData = computed(() => generatorStore.buinessSelectAppData ?? {});

// 搜索条件
const where = ref({
  searchText: '',
  businessId: '',
  businessCategoryId: ''
});

// ref
const tableRef = ref();

// 表格配置
const columns = ref([
  {
    key: 'index',
    title: '序号',
    width: 60,
    align: 'center',
    isShow: true,
    hideInSetting: true
  },
  {
    dataIndex: 'viewName',
    title: '视图名称',
    ellipsis: true,
    width: 150,
    isShow: true
  },
  {
    dataIndex: 'viewCode',
    title: '视图编码',
    width: 100,
    isShow: true
  },
  {
    dataIndex: 'viewType',
    title: '视图类型',
    ellipsis: true,
    width: 100,
    isShow: true
  },
  {
    dataIndex: 'systemCodeShow',
    title: '系统编码',
    ellipsis: true,
    width: 100,
    isShow: true
  },
  {
    dataIndex: 'fldSort',
    title: '排序',
    ellipsis: true,
    width: 100,
    isShow: true
  },
  {
    dataIndex: 'createTime',
    title: '创建时间',
    ellipsis: true,
    width: 150,
    isShow: true
  },
  {
    key: 'action',
    title: '操作',
    width: 80,
    isShow: true
  }
]);
// 当前行数据
const current = ref(null);
// 是否显示新增编辑弹框
const showEdit = ref(false);
// 是否显示自定义列
const isShowCustom = ref(false);
// 业务标识的编码
const fieldBusinessCode = ref('BASE_VIEW_BUSINESS');

// 是否显示视图配置
const showViewConfig = ref(false);

onMounted(() => {
  if (!buinessSelectAppData.value?.businessId) {
    return goHome();
  }
  getColumnData();
});

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

// 返回应用列表页
const goHome = () => {
  router.push(generatorStore.appBackUrl);
};

// 树选中
const treeSelect = node => {
  where.value.businessId = node.businessId;
  where.value.businessCategoryId = node.businessCategoryId;
  reload();
};

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

const refresh = () => {
  reload();
  if (props.getBusinessStatCount) props.getBusinessStatCount();
};

// 新增编辑点击
const openAddEdit = record => {
  if (!where.value.businessId) return message.warn('请选择一个业务');
  current.value = record;
  showEdit.value = true;
};

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

// 批量删除
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 SheetViewApi.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 viewConfig = record => {
  current.value = record;
  showViewConfig.value = true;
};
</script>

<style scoped lang="less">
.table-tool-top-right a {
  font-size: 16px;
}
</style>
