<template>
  <div class="system-business-tag-container layout-pd">
    <el-card shadow="never">
      <el-form :model="state.queryParams" ref="ruleFormRef" :inline="true" @submit.native.prevent>
        <el-form-item label="关键字" prop="Keyword">
          <el-input v-model="state.queryParams.Keyword" placeholder="标签名称 " clearable @keyup.enter="queryList" />
        </el-form-item>
        <el-form-item label="标签类型" prop="Type">
          <el-select v-model="state.queryParams.Type" placeholder="请选择标签类型">
            <el-option v-for="item in tagType" :value="item.key" :key="item.key" :label="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="业务类型" prop="BusinessType">
          <el-select v-model="state.queryParams.BusinessType" placeholder="请选择业务类型">
            <el-option v-for="item in businessTagType" :value="item.key" :key="item.key" :label="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="queryList" :loading="state.loading"> <SvgIcon name="ele-Search" class="mr5" />查询 </el-button>
          <el-button @click="resetQuery(ruleFormRef)" class="default-button"> <SvgIcon name="ele-Refresh" class="mr5" />重置 </el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card shadow="never">
      <div class="mb20">
        <el-button type="primary" @click="addParameter" v-auth="'system:systemBusinessTag:add'">
          <SvgIcon name="ele-Plus" class="mr5" />新增
        </el-button>
        <el-button type="primary" @click="businessTagRemove" v-auth="'system:systemBusinessTag:delete'" :disabled="!multipleSelection.length">
          <SvgIcon name="ele-Delete" class="mr5" />删除
         </el-button>
        <el-button type="primary" @click="businessTagBinding" v-auth="'system:systemBusinessTag:binding'" :disabled="!multipleSelection.length">
          <SvgIcon name="ele-Link" class="mr5" />批量绑定
        </el-button>
      </div>
      <!-- 表格 -->
      <el-table :data="state.tableData" v-loading="state.loading" row-key="id" ref="multipleTableRef" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55"  />
        <el-table-column prop="name" label="标签名称" show-overflow-tooltip></el-table-column>
        <el-table-column prop="typeText" label="标签类型" show-overflow-tooltip width="130"></el-table-column>
        <el-table-column prop="businessTypeText" label="业务类型" show-overflow-tooltip width="300"></el-table-column>
        <el-table-column prop="classify" label="分类名称" show-overflow-tooltip></el-table-column>
        <el-table-column prop="creationTime" label="创建时间" show-overflow-tooltip width="170">
          <template #default="{ row }">
            <span>{{ formatDate(row.creationTime, 'YYYY-mm-dd HH:MM:SS') }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="creatorName" label="创建人" show-overflow-tooltip></el-table-column>
        <el-table-column prop="creatorOrgName" label="创建人部门" show-overflow-tooltip></el-table-column>
        <el-table-column label="操作" width="120" fixed="right" align="center">
          <template #default="{ row }">
            <el-button link type="primary" @click="updateBusinessTah(row)" v-auth="'system:systemBusinessTag:edit'" title="修改参数"> 修改 </el-button>
            <el-button link type="info" @click="viewBusinessTag(row)" v-auth="'system:systemBusinessTag:detail'" title="查看详情"> 查看 </el-button>
          </template>
        </el-table-column>
        <template #empty>
          <Empty />
        </template>
      </el-table>
      <!-- 分页 -->
      <pagination
          :total="state.total"
          v-model:page="state.queryParams.PageIndex"
          v-model:limit="state.queryParams.PageSize"
          @pagination="queryList"
      />
    </el-card>
    <!--  参数新增  -->
    <business-tag-add ref="businessTagAddRef" @updateList="queryList" :businessTagType="businessTagType" :tagType="tagType"/>
    <!--  参数编辑  -->
    <business-tag-edit ref="businessTagEditRef" @updateList="queryList" :businessTagType="businessTagType" :tagType="tagType"/>
    <!--  批量绑定  -->
    <business-tag-binding-com ref="businessTagBindingRef" @updateList="queryList"/>
  </div>
</template>

<script lang="ts" setup name="systemBusinessTag">
import { reactive, ref, onMounted, defineAsyncComponent } from 'vue';
import { ElMessage, ElMessageBox, FormInstance } from 'element-plus';
import { formatDate } from '/@/utils/formatTime';
import {auth} from "/@/utils/authFunction";
import { businessTagList,businessTagBaseData,businessTagDelete } from '/@/api/system/businessTag';
// 引入组件
const BusinessTagBindingCom = defineAsyncComponent(() => import('/@/views/system/businessTag/component/Business-tag-binding.vue')); // 绑定标签
const BusinessTagAdd = defineAsyncComponent(() => import('/@/views/system/businessTag/component/Business-tag-add.vue')); // 参数标签
const BusinessTagEdit = defineAsyncComponent(() => import('/@/views/system/businessTag/component/Business-tag-edit.vue')); // 编辑标签

// 定义变量内容
const state = reactive<any>({
  loading: false, // 加载状态
  queryParams: {
    // 查询参数
    PageIndex: 1,
    PageSize: 10,
    Keyword: '', // 参数名称
    Type: null, // 标签类型
    BusinessType:null,// 业务类型
  },
  total: 0, // 总条数
  tableData: [], // 表格数据
});
const ruleFormRef = ref<any>(null); // 表单ref
const businessTagType = ref<EmptyArrayType>();// 业务类型
const tagType = ref<EmptyArrayType>();// 标签类型
// 获取基础数据
const getBaseData = async ()=>{
  try {
    const res = await businessTagBaseData();
    businessTagType.value = res.result?.businessTagType ?? [];
    tagType.value = res.result?.tagType ?? [];
  }catch (error){
    console.log(error)
  }
}
// 获取参数列表
const queryList = () => {
  state.loading = true;
  if (!auth('system:systemBusinessTag:query')) ElMessage.error('抱歉，您没有权限获取数据权限列表！');
  else {
    businessTagList(state.queryParams)
        .then((res) => {
          state.loading = false;
          state.tableData = res.result.items ?? [];
          state.total = res.result.total ?? 0;
        })
        .finally(() => {
          state.loading = false;
        });
  }
};
// 重置表单
const resetQuery = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
  queryList();
};
// 新增参数
const businessTagAddRef = ref<RefType>(); // 参数新增
const addParameter = () => {
  businessTagAddRef.value.openDialog();
};
// 修改参数
const businessTagEditRef = ref<RefType>(); // 参数编辑
const updateBusinessTah = (row: any) => {
  businessTagEditRef.value.openDialog(row.id);
};
// 查看详情
const viewBusinessTag = (row: any) => {
  businessTagEditRef.value.openDialog(row.id, true);
};
// 表格多选
const multipleTableRef = ref<RefType>();
const multipleSelection = ref<any>([]);
const handleSelectionChange = (val: any[]) => {
  multipleSelection.value = val;
};
// 删除参数
const businessTagRemove = (row: any) => {
  const ids = multipleSelection.value.map((item: any) => item.id);
  const names = multipleSelection.value.map((item: any) => item.name);
  ElMessageBox.confirm(`您确定要删除：【${names}】，是否继续?`, '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
    draggable: true,
    cancelButtonClass: 'default-button',
    autofocus: false,
  })
      .then(() => {
        businessTagDelete({ids}).then(() => {
          ElMessage.success('操作成功');
          queryList();
        });
      })
      .catch(() => {});
};
// 批量绑定
const businessTagBindingRef = ref<RefType>(); // 绑定ref
const businessTagBinding = ()=>{
  const ids = multipleSelection.value.map((item: any) => item.id);
  businessTagBindingRef.value.openDialog(ids);
}
// 页面加载时
onMounted(() => {
  getBaseData();
  queryList();
});
</script>

<style lang="scss" scoped>
.system-business-tag-container{

}
</style>
