<!--
 * @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="客户" prop="customerNickname">
            <el-input v-model='formData.customerNickname' disabled type="text" placeholder='点击选择客户'>
              <template #append>
                <div @click="handleChooseCustomer">选择</div>
              </template>
            </el-input>
          </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="formData.customerAddressAreaId" @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>
        <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="8" placeholder="请输入详细地址" />
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>

    <template #footer>
      <div key="dialog-footer">
        <el-button type="primary" @click="handleAddClick(formRef)">添加</el-button>
        <el-button @click="handleResetClick(formRef)">重置</el-button>
      </div>
    </template>
  </el-dialog>

  <!-- 选择客户 -->
  <ChooseCustomerDialog v-model="ChooseCustomerDialogVisible" :data="ChooseCustomerDialogRow"></ChooseCustomerDialog>
</template>


<script setup>
import { reactive, ref, watch } from "vue";
import { ElMessage } from 'element-plus'
import modal from '@/plugins/modal.js'
import { addOrUpdateCustomerAddress } from '@/api/business/Custom/CustomerAddresses/customerAddress.js';
import { regionTreeList } from '@/api/business/Custom/Regions/region.js';
import ChooseCustomerDialog from './ChooseCustomerDialog.vue';


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

}

// -业务参数
const dataList = ref([])
const ChooseCustomerDialogVisible = ref(false);
const ChooseCustomerDialogRow = ref({});


// -业务方法

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

// 打开选择客户弹窗
const handleChooseCustomer = () => {
  ChooseCustomerDialogVisible.value = true
  ChooseCustomerDialogRow.value = formData
}

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


// -基础参数
const labelWidth = 100;
const formRef = ref();
const { proxy } = getCurrentInstance()
const emits = defineEmits(["update:modelValue"]);
const formData = reactive({
});
const props = defineProps({
  modelValue: Boolean,
  done: Function,
});
const imgData = ref({
  fileDir: "CustomerAddress"
})

// 验证
const rules = reactive({
  customerNickname: [{ required: true, message: "客户不能为空", trigger: "blur" }],
  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 handleAddClick = async (formEl) => {
  if (!formEl) return;
  formEl.validate(async (valid) => {
    if (!valid) {
      return;
    }


    const { code } = await addOrUpdateCustomerAddress(formData);
    if (code == 200) {
      modal.msgSuccess('添加成功')
      closeDialog();
    }
  });
};
const closeDialog = () => {
  formData.customerNickname = ""
  handleResetClick(formRef.value);
  props.done();
  emits("update:modelValue", false);
};
const handleResetClick = async (formEl) => {
  if (!formEl) return;
  formEl.resetFields();
};
</script>