<template>
		<el-dialog v-model="state.dialogVisible" width="800px" draggable :title="dialogTitle" @close="close">
      <el-form :model="state.ruleForm" label-width="90px" ref="ruleFormRef">
        <el-row :gutter="10">
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="标签名称" prop="name" :rules="[{ required: true, message: '请填写标签名称', trigger: 'blur' }]">
              <el-input v-model="state.ruleForm.name" placeholder="请填写标签名称" clearable :disabled="state.isDisabled"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="标签类型" prop="type" :rules="[{ required: true, message: '请选择标签类型', trigger: 'change' }]">
              <el-select v-model="state.ruleForm.type" placeholder="请选择标签类型" class="w100" @change="selectType" :disabled="state.isDisabled">
                <el-option v-for="item in tagType" :value="item.key" :key="item.key" :label="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" v-if="isClassify">
            <el-form-item label="分类名称" prop="classify" :rules="[{ required: false, 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-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" v-else>
            <el-form-item label="分类名称" prop="classify" :rules="[{ required: false, message: '请填写分类名称', trigger: 'blur' }]">
              <el-input v-model="state.ruleForm.classify" placeholder="请填写分类名称" clearable :disabled="state.isDisabled"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="业务类型" prop="businessType" :rules="[{ required: true, message: '请选择业务类型', trigger: 'change' }]">
              <el-select v-model="state.ruleForm.businessType" placeholder="请选择业务类型" class="w100" :disabled="state.isDisabled">
                <el-option v-for="item in businessTagType" :value="item.key" :key="item.key" :label="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
			<template #footer v-if="!state.isDisabled">
				<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="businessTagEdit">
import { computed, reactive, ref } from 'vue';
import { ElMessage, FormInstance } from 'element-plus';
import { throttle } from '@/utils/tools';
import {businessTagUpdate, businessTagDetail, businessTagList} from '@/api/auxiliary/businessTag';

const props = defineProps({
  businessTagType: {
    type: Array,
    default: () => <EmptyArrayType>[],
  },
  tagType: {
    type: Array,
    default: () => <EmptyArrayType>[],
  },
})
// 定义子组件向父组件传值/事件
const emit = defineEmits(['updateList']);

// 定义变量内容
const state = reactive<any>({
	dialogVisible: false,
	ruleForm: {
    name: '', // 标签名
    type: '', // 标签类型
    businessType: '', // 标签业务类型
    classify: '', // 分类名称
	},
	isDisabled: false, // 是否禁用
});
let loading = ref<boolean>(false);  // 加载状态
const dialogTitle = computed(() => {
	return state.isDisabled ? '查看业务标签' : '编辑业务标签';
});
// 打开弹窗
const ruleFormRef = ref<RefType>();
const openDialog = async (id: string, isDisabled?: boolean) => {
	try {
		const res: any = await businessTagDetail(id);
		state.ruleForm = res.result ?? <EmptyObjectType>{};
    if(state.ruleForm.type === 2){
      getClassify();
      isClassify.value = true;
    }
		state.isDisabled = isDisabled ?? false;
		state.dialogVisible = true;
	} catch (error) {
		console.log(error);
	}
};
// 关闭弹窗
const closeDialog = () => {
	state.dialogVisible = false;
};
const close = () => {
  ruleFormRef.value?.resetFields();
  ruleFormRef.value?.resetFields();
};
const classifyList = ref<EmptyArrayType>([]);// 分类列表
const isClassify = ref<boolean>(false);// 是否显示分类
const getClassify =()=>{
  businessTagList({type:1,PageIndex:1,PageSize:999}).then((res:any)=>{
    classifyList.value = res.result?.items;
  })
}
const selectType = (val:any)=>{
  ruleFormRef.value.resetFields('classify');
  if(val===2){// 如果是分类
    getClassify();
    isClassify.value = true;
  }else{
    isClassify.value = false;
  }
}
// 新增
const onSubmit = throttle(async (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	await formEl.validate((valid: boolean) => {
		if (!valid) return;
		loading.value = true;
    businessTagUpdate(state.ruleForm)
			.then(() => {
				ElMessage({
					message: '操作成功',
					type: 'success',
				});
				loading.value = false;
				closeDialog();
				emit('updateList');
			})
			.catch(() => {
			}).finally(() => {
        loading.value = false;
        closeDialog();
      });
	});
}, 300);
// 暴露变量
defineExpose({
	openDialog,
	closeDialog,
});
</script>
