<!--
 * @Descripttion: (订单/tb_order 订单发货弹窗)
 * @version: (1.0)
 * @Author: (lwh)
 * @Date: (2023-09-03)
 * @LastEditors: (lwh)
 * @LastEditTime: (2023-09-03)
-->
<template>
  <el-dialog v-model="props.modelValue" title="订单发货" width="600px" @closed="closeDialog" @open="openDialog">
    <el-form ref="formRef" :model="formData" :rules="rules">
      <el-row :gutter="20">
        <el-col :lg="24">
          <el-form-item :label-width="labelWidth" label="物流公司" prop="logisticsCompany" >
            <el-select v-model="formData.logisticsCompany" placeholder="请选择物流公司" >
              <el-option v-for="item in logisticsCompanyDataList" :key="item.logisticsCompanyId"
                :label="item.logisticsCompanyName" :value="item.logisticsCompanyName" @click="handleCompanyChange(item)" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :lg="24">
          <el-form-item :label-width="labelWidth" label="物流单号" prop="logisticsTrackingNumber">
            <el-input v-model="formData.logisticsTrackingNumber" placeholder="请输入物流单号" />
          </el-form-item>
        </el-col>
      </el-row>


    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="handleEditClick(formRef)" :loading="loadingStatus">确认发货</el-button>
        <el-button @click="handleResetClick(formRef)">重置</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ElMessage } from 'element-plus'
import modal from '@/plugins/modal.js'
import { reactive, ref, watch } from "vue";
import { deliver } from "@/api/business/OrderManage/Orders/order.js";
import { logisticsCompanyList } from '@/api/business/LogisticsManage/LogisticsCompanys/logisticsCompany.js'


// 打开弹窗时回调
const openDialog = async () => {
  await getCompanyList()

}

const formData = ref({
  ...props.data,
});
watch(props, async (v) => {
  formData.value = v.data;

});

// 业务参数

//物流公司列表
const logisticsCompanyDataList = ref([]);



// -业务方法

/** 获取物流公司列表 */
async function getCompanyList() {
  logisticsCompanyList({
    pageNum: 1,
    pageSize: 10000
  }).then((res) => {
    if (res.code == 200) {
      logisticsCompanyDataList.value = res.data.result;
    }
  });
}


// -基础参数
const props = defineProps({
  modelValue: Boolean,
  data: Object,
  done: Function,
});

const loadingStatus = ref(false)
const labelWidth = 100;
const formRef = ref();
const { proxy } = getCurrentInstance()
const emits = defineEmits(["update:modelValue"]);
const imgData = ref({
  fileDir: "Order"
})

// 验证
const rules = reactive({
  logisticsCompany: [{ required: true, message: "请选择物流公司", trigger: "blur" }],
  logisticsTrackingNumber: [{ required: true, message: "请填写物流单号", trigger: "blur" }],
});

// -基础方法
// 提交
const handleEditClick = async (formEl) => {
  if (!formEl) return;
  formEl.validate(async (valid) => {
    if (!valid) {
      return;
    }
    loadingStatus.value = true


    const { code } = await deliver(formData.value);
    if (code == 200) {
      modal.msgSuccess('发货成功')
      props.done();
      closeDialog();
      loadingStatus.value = false
    }
  });
}

const handleCompanyChange = async (item) => {
  formData.value.logisticsCompanyGuid = item.logisticsCompanyGuid
}

const handleResetClick = async (formEl) => {
  if (!formEl) return;
  formEl.resetFields();
}
const closeDialog = () => {
  logisticsCompanyDataList.value.length = 0
  formData.value = null
  emits("update:modelValue", false);
};

</script>
