<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 { DictionaryStore } from '/@/stores/dictionary';
import { useResponse } from '/@/utils/useResponse';
import * as api from '../api';

const { data: staticData } = DictionaryStore();

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 {
  asin: any,
  sku: any,
  country_code: any
  shop: any
  shop_id: any
  tag: any
  status: any
  freq: any
}

const ruleFormRef = ref<FormInstance>();
const ruleForm = reactive<RuleForm>({
  asin: rowData?.asin,
  sku: rowData?.goods.sku,
  country_code: rowData?.country_code,
  shop: rowData?.shop_name,
  shop_id: rowData?.shop_id,
  tag: rowData?.goods.tag,
  status: rowData?.status.toString(),
  freq: rowData?.freq,
});

const rules = reactive<FormRules<RuleForm>>({
  sku: [ { required: true, message: '请输入SKU', trigger: 'blur' } ],
  shop: [ { required: true, message: '请输入店铺', trigger: 'blur' } ],
  tag: [ { required: true, message: '请输入分组', trigger: 'blur' } ],
});

ruleForm.sku = [rowData.goods.sku, rowData.goods.sku2, rowData.goods.sku3]
    .filter(Boolean)
    .join(',');

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;

  const skuCount = ruleForm.sku.split(',').length;

  if (skuCount > 3) {
    ElMessage.warning('SKU 输入不能超过三个，请检查 SKU');
    return;
  }
  
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      try {
        const res = await useResponse(api.updateRow, { id: rowData?.id, ...ruleForm }, 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="ASIN" prop="asin">
          <el-input v-model="ruleForm.asin" :disabled="true" />
        </el-form-item>
        <el-form-item class="font-medium" label="SKU" prop="sku">
          <el-input v-model="ruleForm.sku" />
        </el-form-item>
        <el-form-item class="font-medium" label="店 铺" prop="shop">
          <el-input v-model="ruleForm.shop" />
        </el-form-item>
        <el-form-item class="font-medium" label="分 组" prop="tag">
          <el-input v-model="ruleForm.tag" />
        </el-form-item>
        <el-form-item class="font-medium" label="状 态" prop="status">
          <el-select v-model="ruleForm.status" :disabled="true">
            <el-option label="启用" value="1" />
            <el-option label="暂停" value="2" />
          </el-select>
        </el-form-item>
        <el-form-item class="font-medium" label="国 家" prop="country_code">
          <el-select v-model="ruleForm.country_code" placeholder="请选择国家" :disabled="true">
            <el-option
                v-for="item in staticData.country_code"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="font-medium" label="更新频率" prop="freq">
          <el-input-number v-model="ruleForm.freq" />
        </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>
