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


const groupOptions = <Ref>inject('groupOptions');

const { data: staticData } = DictionaryStore();

const btnLoading = ref(false);

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

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

const props = defineProps({
  rowData: Object
});
const { rowData } = props;
const emit = defineEmits([ 'refresh' ]);

interface RuleForm {
  asin: any,
  country_code: any
  shop_name: any
  tag: any
  status: any
  freq: any
}

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

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

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) 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({ message: '编辑成功', plain: true, icon: 'Operation' });
          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="店 铺" prop="shop">
          <el-input v-model="ruleForm.shop_name" placeholder="请输入店铺" />
        </el-form-item>
        <el-form-item class="font-medium" label="分 组" prop="tag">
          <el-select v-model="ruleForm.tag" allow-create filterable>
            <el-option v-for="item in groupOptions" :label="item.tag" :value="item.tag" />
          </el-select>
        </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-option label="暂停" value="3" />
						<el-option label="下架" value="10" />
          </el-select>
        </el-form-item>
        <el-form-item class="font-medium" label="国 家" prop="country_code">
          <el-select v-model="ruleForm.country_code" :disabled="true" placeholder="请选择国家">
            <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>
