<!--
 * @Descripttion: (客户收货地址/tb_customer_address 编辑弹窗)
 * @version: (1.0)
 * @Author: (admin)
 * @Date: (2023-06-05)
 * @LastEditors: (admin)
 * @LastEditTime: (2023-06-05)
-->
<template>
  <el-dialog v-model="props.modelValue" title="修改客户收货地址信息" width="900px" @closed="closeDialog" @open="openDialog">
    <el-form ref="formRef" :model="formData" :rules="rules">
      <el-row>
        <el-col :lg="12">
          <el-form-item :label-width="labelWidth" label="客户">
            <div>{{ formData.customerName }}</div>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :lg="12">
          <el-form-item :label-width="labelWidth" label="所在地区" prop="customerAddressAreaId">
            <el-cascader class="w100" :options="dataList"
              :props="{ checkStrictly: false, value: 'regionId', label: 'regionName', emitPath: true }"
              placeholder="请选择所在地区" clearable v-model="region[2]" @change="handleCascaderChange">
              <template #default="{ node, data }">
                <span>{{ data.regionName }}</span>
                <!-- <span v-if="!node.isLeaf"> ({{ data.children.length }}) </span> -->
              </template>
            </el-cascader>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">

        <el-col :lg="12">
          <el-form-item :label-width="labelWidth" label="收货人名称" prop="customerAddressName">
            <el-input v-model="formData.customerAddressName" placeholder="请输入收货人名称" />
          </el-form-item>
        </el-col>

        <el-col :lg="12">
          <el-form-item :label-width="labelWidth" label="收货电话" prop="customerAddressPhone">
            <el-input v-model="formData.customerAddressPhone" placeholder="请输入收货电话" />
          </el-form-item>
        </el-col>

        <el-col :lg="24">
          <el-form-item :label-width="labelWidth" label="详细地址" prop="customerAddressDetailed">
            <el-input v-model="formData.customerAddressDetailed" type="textarea" :rows="5" placeholder="请输入详细地址" />
          </el-form-item>
        </el-col>






      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="handleEditClick(formRef)">编辑</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 { addOrUpdateCustomerAddress } from "@/api/business/Custom/CustomerAddresses/customerAddress.js";
import { regionTreeList } from '@/api/business/Custom/Regions/region.js';


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

  region.value.push(formData.value.customerAddressProvinceId)
  region.value.push(formData.value.customerAddressCityId)
  region.value.push(formData.value.customerAddressAreaId)
}

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

// 业务参数
const region = ref([])
const dataList = ref([])


// -业务方法

// 获取省市区数据
async function getTreeList() {
  regionTreeList().then((res) => {
    if (res.code == 200) {
      dataList.value = res.data
    }
  })
}

// 处理所在地区，省市区传值
function handleCascaderChange(selectedValues) {
  formData.value.customerAddressProvinceId = selectedValues[0]
  formData.value.customerAddressCityId = selectedValues[1]
  formData.value.customerAddressAreaId = selectedValues[2]
}


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

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

// 验证
const rules = reactive({
  customerAddressAreaId: [{ required: true, message: "所在地区不能为空", trigger: "blur" }],
  customerAddressName: [{ required: true, message: "收货人名称不能为空", trigger: "blur" }],
  customerAddressPhone: [{ required: true, message: "收货电话不能为空", trigger: "blur" }],
  customerAddressDetailed: [{ required: true, message: "详细地址不能为空", trigger: "blur" }],
});

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


    const { code } = await addOrUpdateCustomerAddress(formData.value);
    if (code == 200) {
      modal.msgSuccess('修改成功')
      closeDialog();
    }
  });
}

const handleResetClick = async (formEl) => {
  if (!formEl) return;
  formEl.resetFields();
}
const closeDialog = () => {
  props.done();
  emits("update:modelValue", false);
};

</script>
