<!--
 * @Descripttion: (店铺/tb_shop 添加弹窗)
 * @version: (1.0)
 * @Author: (黎文豪)
 * @Date: (2023-06-09)
 * @LastEditors: (黎文豪)
 * @LastEditTime: (2023-06-09)
-->
<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="12">
          <el-form-item :label-width="labelWidth" label="用户" prop="userName">
            <el-input v-model='formData.userName' disabled type="text" placeholder='点击选择用户'>
              <template #append>
                <div @click="handleChooseUser">选择</div>
              </template>
            </el-input>
          </el-form-item>
        </el-col>
        <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>
        <el-col :lg="12">
          <el-form-item :label-width="labelWidth" label="经营类目" prop="shopBusinessCategoryGuid">
            <el-cascader class="w100" :options="dataList"
              :props="{ checkStrictly: false, value: 'goodsCategoryGuid', label: 'goodsCategoryName', emitPath: false }"
              placeholder="请选择经营类目" clearable v-model="formData.shopBusinessCategoryGuid">
              <template #default="{ node, data }">
                <span>{{ data.goodsCategoryName }}</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="shopLogo">
            <UploadImage ref="uploadRef" v-model="formData.shopLogo" :data=imgData :limit="1" :fileSize="5"
              :drag="true" />
          </el-form-item>
        </el-col>
        <el-col :lg="12">
          <el-form-item :label-width="labelWidth" label="营业执照" prop="shopBusinessLicense">
            <UploadImage ref="uploadRef" v-model="formData.shopBusinessLicense" :data=imgData :limit="1" :fileSize="5"
              :drag="true" />
          </el-form-item>
        </el-col>
        <el-col :lg="12">
          <el-form-item :label-width="labelWidth" label="名称" prop="shopName">
            <el-input v-model="formData.shopName" placeholder="请输入名称" />
          </el-form-item>
        </el-col>
        <el-col :lg="24">
          <el-form-item :label-width="labelWidth" label="简介" prop="shopIntro">
            <el-input v-model="formData.shopIntro" type="textarea" :rows="5" placeholder="请输入简介" />
          </el-form-item>
        </el-col>
        <el-col :lg="12">
          <el-form-item :label-width="labelWidth" label="排序" prop="shopSort">
            <el-input-number v-model.number="formData.shopSort" controls-position="right" :min="0" />
          </el-form-item>
        </el-col>
        <el-col :lg="12">
          <el-form-item :label-width="labelWidth" label="销售单量" prop="shopSalesOrderCount">
            <el-input-number v-model.number="formData.shopSalesOrderCount" 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>

  <!-- 选择用户 -->
  <ChooseUserDialog v-model="ChooseUserDialogVisible" :data="ChooseUserDialogRow"></ChooseUserDialog>
  <!-- 选择客户 -->
  <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 { addOrUpdateShop, getFirstGoodsCategoryList } from '@/api/business/ShopManager/Shops/shop.js';
import { goodsCategoryTreeList } from '@/api/business/GoodsManager/GoodsCategorys/goodsCategory.js';
import ChooseCustomerDialog from './ChooseCustomerDialog.vue';
import ChooseUserDialog from './ChooseUserDialog.vue';


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

  await getTreeList()
  await getaudit_status()

}

// -业务参数
// 审核状态字典选项列表
const audit_status = ref([]);
// 经营类树形列表
const dataList = ref([])

// 选择用户弹窗参数
const ChooseUserDialogVisible = ref(false);
const ChooseUserDialogRow = ref({});
// 选择客户弹窗参数
const ChooseCustomerDialogVisible = ref(false);
const ChooseCustomerDialogRow = ref({});



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

// 获取经营类树形列表
async function getTreeList() {
  goodsCategoryTreeList().then((res) => {
    if (res.code == 200) {
      dataList.value = res.data
    }
  })
}

// 打开选择用户弹窗
const handleChooseUser = () => {
  ChooseUserDialogVisible.value = true
  ChooseUserDialogRow.value = formData
}

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


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

// 验证
const rules = reactive({
  userName: [{ required: true, message: "用户不能为空", trigger: "blur" }],
  customerNickname: [{ required: true, message: "客户不能为空", trigger: "blur" }],
  shopBusinessCategoryGuid: [{ required: true, message: "经营类目guid不能为空", trigger: "blur" }],
  shopLogo: [{ required: true, message: "图标不能为空", trigger: "blur" }],
  shopBusinessLicense: [{ required: true, message: "营业执照不能为空", trigger: "blur" }],
  shopName: [{ required: true, message: "名称不能为空", trigger: "blur" }],
  shopIntro: [{ required: true, message: "简介不能为空", trigger: "blur" }],
  shopSort: [{ required: true, message: "排序不能为空", trigger: "blur", type: "number" }],
  shopSalesOrderCount: [{ required: true, message: "销售单量不能为空", trigger: "blur", type: "number" }],
});

// -基础方法

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


    const { code } = await addOrUpdateShop(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>