<script lang="ts" setup>
/**
 * @Name: EditDrawer.vue
 * @Description: 价格审批（直接销售）- 编辑抽屉
 * @Author: xinyan
 */

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';


const countryOptions = <Ref>inject('countryOptions');
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 {
  sku: string;
  weight: string;
  platform: string;
  country_code: string;
  currency_code: string;
  estimated_cost: string;
  price_show: string;
  price_daily: string;
  price_min: string;
  final_cost: string;
  sales_mode: string;
  cost: string;
}

const ruleFormRef = ref<FormInstance>();
const ruleForm = reactive<RuleForm>({
  sku: rowData.sku,
  weight: rowData.weight,
  platform: rowData.platform,
  country_code: rowData.country_code,
  currency_code: rowData.currency_code,
  estimated_cost: rowData.estimated_cost,
  price_show: rowData.price_show,
  price_daily: rowData.price_daily,
  price_min: rowData.price_min,
  final_cost: rowData.final_cost,
  sales_mode: rowData.sales_mode,
  cost: rowData.cost
});

const rules = reactive<FormRules<RuleForm>>({});

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="30%"
    >
      <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="SKU" prop="sku">
          <el-input v-model="ruleForm.sku" placeholder="请输入SKU" />
        </el-form-item>
        <el-form-item class="font-medium" label="重量(KG)" prop="weight">
          <el-input v-model="ruleForm.weight" placeholder="请输入重量"></el-input>
        </el-form-item>
        <el-form-item class="font-medium" label="平 台" prop="platform">
          <el-input v-model="ruleForm.platform" placeholder="请输入平台"></el-input>
        </el-form-item>
        <el-form-item class="font-medium" label="国 家" prop="country_code">
          <el-select v-model="ruleForm.currency_code" placeholder="请选择货币代码">
            <el-option v-for="item in countryOptions" :key="item" :label="item" :value="item"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="font-medium" label="货币代码" prop="currency_code">
          <el-input v-model="ruleForm.currency_code" placeholder="请输入货币代码"></el-input>
        </el-form-item>
        <el-form-item class="font-medium" label="预估硬件成本" prop="estimated_cost">
          <el-input v-model="ruleForm.estimated_cost" placeholder="请输入预估硬件成本"></el-input>
        </el-form-item>
        <el-form-item class="font-medium" label="展示价格(外币)" prop="price_show">
          <el-input v-model="ruleForm.price_show" placeholder="请输入展示价格(外币)"></el-input>
        </el-form-item>
        <el-form-item class="font-medium" label="日常活动售价(外币)" prop="price_daily">
          <el-input v-model="ruleForm.price_daily" placeholder="请输入展示价格(外币)"></el-input>
        </el-form-item>
        <el-form-item class="font-medium" label="最低售价(外币)" prop="price_min">
          <el-input v-model="ruleForm.price_min" placeholder="请输入最低售价(外币)"></el-input>
        </el-form-item>
        <el-form-item class="font-medium" label="尾程费用(外币)" prop="final_cost">
          <el-input v-model="ruleForm.final_cost" placeholder="请输入尾程费用(外币)"></el-input>
        </el-form-item>
        <el-form-item class="font-medium" label="销售模式" prop="sales_mode">
          <el-input v-model="ruleForm.sales_mode" placeholder="请输入销售模式"></el-input>
        </el-form-item>
        <el-form-item class="font-medium" label="成本" prop="cost">
          <el-input v-model="ruleForm.cost" placeholder="请输入成本"></el-input>
        </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>
