<script lang="ts" setup>
/**
 * @Name: AttributeManageEdit.vue
 * @Description:
 * @Author: Cheney
 */

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


const btnLoading = ref(false);

const editOpen = defineModel({ default: false });

const editDrawer = <Ref>useTemplateRef('editDrawer');

const props: any = defineProps({
  rowData: Object
});
const { rowData } = props;

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

interface RuleForm {
  section: any;
  name: any;
  order: any;
}

const ruleFormRef = ref<FormInstance>();
const ruleForm = reactive<RuleForm>({
  section: rowData?.section.toString(),
  name: rowData?.attr.id,
  order: rowData?.order
});

const rules = reactive<FormRules<RuleForm>>({
  section: [ { required: true, trigger: 'blur' } ],
  name: [ { required: true, trigger: 'blur' } ]
});

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      const query = {
        attr: ruleForm.name,
        id: rowData.id,
        kind: rowData.kind.id,
        order: ruleForm.order,
        section: Number(ruleForm.section),
        status: rowData.status
      };
      try {
        const res = await useResponse(api.updateAttributeRow, query, btnLoading);
        if (res && res.code == 2000) {
          editOpen.value = false;
          ElMessage.success({ message: '编辑成功', plain: true, icon: 'Operation' });
          emit('refresh');
        }
      } catch (error) {
        console.error('Error==>', error);
      }
    } else {
      console.log('error submit!', fields);
    }
  });
};

const attributeOptions: any = ref([]);

onBeforeMount(() => {
  fetchAttributeOptions();
});

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

async function fetchAttributeOptions() {
  const res = await useResponse(api.getAttributeOptions, { limit: 100 });
  attributeOptions.value = res.data;
}
</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="section">
          <el-select v-model="ruleForm.section">
            <el-option label="第1部分" value="1" />
            <el-option label="第2部分" value="2" />
            <el-option label="第3部分" value="3" />
          </el-select>
        </el-form-item>
        <el-form-item class="font-medium" label="属 性" prop="name">
          <el-select v-model="ruleForm.name">
            <el-option v-for="item in attributeOptions" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item class="font-medium" label="顺 序" prop="order">
          <el-input-number v-model="ruleForm.order" />
        </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>
