<script lang="ts" setup>
/**
 * @Name: EditDrawer.vue
 * @Description: 属性枚举-行编辑
 * @Author: Cheney
 */

import { ElMessage, FormInstance, FormRules } from 'element-plus';
import { Close, Finished } from '@element-plus/icons-vue';
import { useResponse } from '/@/utils/useResponse';
import * as api from '../api';
import { updateRow } from '../api';


const btnLoading = ref(false);

const editOpen = defineModel({ default: false });
const editDrawer = <Ref>useTemplateRef('editDrawer');

const props = defineProps({
	editData: <any>Object,
  rowData: Object
});
const { editData , rowData } = props;

const emit = defineEmits([ 'refresh' ]);

interface RuleForm {
  label: any,
  value: any,
	status: boolean;
}

const ruleFormRef = ref<FormInstance>();
const ruleForm = reactive<RuleForm>({
  label: editData?.label,
  value: editData?.value,
	status: editData?.status,
});

const rules = reactive<FormRules<RuleForm>>({
	label: [{ required: true, message: '请输入名称', trigger: 'blur' }],
	value: [{ required: true, message: '请输入数据值', trigger: 'blur' }],
});

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      try {
				const query = {
					attr: rowData.id,
					id: editData.id,
					label: ruleForm.label,
					value: ruleForm.value,
					status: ruleForm.status,
				};
        const res = await useResponse(api.updateRow, { id: editData?.id, ...query }, btnLoading);
        if (res && res.code == 2000) {
          editOpen.value = false;
          ElMessage.success('编辑成功');
          emit('refresh');
        }
      } catch (error) {
        console.error('Error==>', error);
      }
    } else {
      console.log('error submit!', fields);
    }
  });
};

function closeDrawer() {
  editDrawer.value.handleClose();
}

</script>

<template>
  <div class="drawer-container">
    <el-drawer ref="editDrawer"
               v-model="editOpen"
               :close-on-click-modal="false"
               :close-on-press-escape="false"
               :title="`属性枚举- 编辑 `"
               size="25%">
      <el-form
          ref="ruleFormRef"
          :model="ruleForm"
          :rules="rules"
          class="mx-2.5 mt-7"
          label-position="top"
          label-width="auto"
          status-icon>
        <el-form-item class="font-medium" label="名称" prop="label">
          <el-input v-model="ruleForm.label" />
        </el-form-item>
        <el-form-item class="font-medium" label="数据值" prop="value">
          <el-input v-model="ruleForm.value" />
        </el-form-item>
				<el-form-item class="font-medium" label="状态" prop="value">
					<el-radio-group v-model="ruleForm.status">
						<el-radio :label="true">启用</el-radio>
						<el-radio :label="false">禁用</el-radio>
					</el-radio-group>
				</el-form-item>
        <el-form-item>
          <el-divider />
          <div class="flex flex-1 justify-end">
            <el-button :icon="Close" @click="closeDrawer">取 消</el-button>
            <el-button :icon="Finished" :loading="btnLoading" type="primary" @click="submitForm(ruleFormRef)">
              确 定
            </el-button>
          </div>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<style scoped>
.drawer-container :deep(.el-drawer__header) {
  border-bottom: none;
  font-weight: 500;
}

.drawer-container :deep(.el-drawer__title) {
  font-size: 18px;
}
</style>
