<template>
  <el-dialog v-model="state.dialogVisible" width="500px" draggable title="批量绑定">
    <el-form :model="state.ruleForm" label-width="90px" ref="ruleFormRef">
      <el-form-item label="分类名称" prop="classify" :rules="[{ required: true, message: '请选择分类名称', trigger: 'change' }]">
        <el-select v-model="state.ruleForm.classify" placeholder="请选择分类名称" class="w100" :disabled="state.isDisabled">
          <el-option v-for="item in classifyList" :value="item.name" :key="item.id" :label="item.name" />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
				<span class="dialog-footer">
					<el-button @click="closeDialog" class="default-button">取 消</el-button>
					<el-button type="primary" @click="onSubmit(ruleFormRef)" :loading="loading">确 定</el-button>
				</span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="businessTagBinding">
import {reactive, ref} from 'vue';
import {ElMessage, FormInstance} from 'element-plus';
import {throttle} from '@/utils/tools';
import {businessTagBinding, businessTagList} from '@/api/auxiliary/businessTag';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['updateList']);

// 定义变量内容
const state = reactive<any>({
  dialogVisible: false,
  ruleForm: {
    name: '', // 标签名
    type: '', // 标签类型
    businessType: '', // 标签业务类型
    classify: '', // 分类名称
  },
});
let loading = ref<boolean>(false);  // 加载状态
// 打开弹窗
const ruleFormRef = ref<RefType>();
const idsArray = ref<EmptyArrayType>([]);
const openDialog = async (ids:any) => {
  idsArray.value = ids;
  ruleFormRef.value?.resetFields();
  try {
    getClassify();
    state.dialogVisible = true;
  } catch (error) {
    console.log(error);
  }
};
// 关闭弹窗
const closeDialog = () => {
  state.dialogVisible = false;
};
const classifyList = ref<EmptyArrayType>([]);// 分类列表
const getClassify =()=>{
  businessTagList({type:1,PageIndex:1,PageSize:999}).then((res:any)=>{
    classifyList.value = res.result?.items;
  })
}
// 新增
const onSubmit = throttle(async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid: boolean) => {
    if (!valid) return;
    loading.value = true;
    const request = {
      ids:idsArray.value,
      classify:state.ruleForm.classify,
    }
    businessTagBinding(request)
        .then(() => {
          ElMessage({
            message: '操作成功',
            type: 'success',
          });
          emit('updateList');
        })
        .catch((error) => {
        })
        .finally(() => {
          loading.value = false;
          closeDialog();
        });
  });
}, 300);
// 暴露变量
defineExpose({
  openDialog,
  closeDialog,
});
</script>
