<!--
 * @Descripttion: (店铺地址/tb_shop_address 添加弹窗)
 * @version: (1.0)
 * @Author: (lwh)
 * @Date: (2023-06-15)
 * @LastEditors: (lwh)
 * @LastEditTime: (2023-06-15)
-->
<template>
  <el-dialog v-model="props.modelValue" title="添加店铺地址信息" width="900px" @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="shopAddressType">
            <el-radio-group v-model="formData.shopAddressType">
              <el-radio v-for="item in  shop_address_type " :key="item.dictValue" :label="parseInt(item.dictValue)">{{
                item.dictLabel }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :lg="12">
          <el-form-item :label-width="labelWidth" label="所在地区" prop="districtId">
            <el-cascader class="w100" :options="dataList"
              :props="{ checkStrictly: false, value: 'regionId', label: 'regionName', emitPath: true }"
              placeholder="请选择所在地区" clearable v-model="formData.districtId" @change="handleCascaderChange">
              <template #default="{ node, data }">
                <span>{{ data.regionName }}</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="shopAddressContactName">
            <el-input v-model="formData.shopAddressContactName" placeholder="请输入联系人姓名" />
          </el-form-item>
        </el-col>
        <el-col :lg="12">
          <el-form-item :label-width="labelWidth" label="联系电话" prop="shopAddressContactNumber">
            <el-input v-model="formData.shopAddressContactNumber" placeholder="请输入联系电话" />
          </el-form-item>
        </el-col>
        <el-col :lg="24">
          <el-form-item :label-width="labelWidth" label="详细地址" prop="shopAddressDetailedAddress">
            <el-input v-model="formData.shopAddressDetailedAddress" type="textarea" :rows="5" placeholder="请输入详细地址" />
          </el-form-item>
        </el-col>
        <el-col :lg="12">
          <el-form-item :label-width="labelWidth" label="排序" prop="shopAddressSort">
            <el-input-number v-model.number="formData.shopAddressSort" controls-position="right" :min="0" />
          </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>
</template>


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

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

  await getTreeList()
  await getshop_address_type()

}

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

// 地址类型字典选项列表
const shop_address_type = ref([]);


// -业务方法
// 字典获取
async function getshop_address_type() {
  await proxy.getDicts('shop_address_type').then((res) => {
    shop_address_type.value = res.data
  })
}

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

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


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

// 验证
const rules = reactive({
  shopAddressType: [{ required: true, message: "地址类型不能为空", trigger: "blur", type: "number" }],
  districtId: [{ required: true, message: "所在地区不能为空", trigger: "blur", type: "number" }],
  shopAddressContactName: [{ required: true, message: "联系人姓名不能为空", trigger: "blur" }],
  shopAddressContactNumber: [{ required: true, message: "联系电话不能为空", trigger: "blur" }],
  shopAddressDetailedAddress: [{ required: true, message: "详细地址不能为空", trigger: "blur" }],
  shopAddressSort: [{ required: true, message: "排序不能为空", trigger: "blur", type: "number" }],
});

// -基础方法

// 提交
const handleAddClick = async (formEl) => {
  if (!formEl) return;
  formEl.validate(async (valid) => {
    if (!valid) {
      return;
    }


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