<!--
 * @Descripttion: (店铺商品类目/tb_shop_goods_category 添加弹窗)
 * @version: (1.0)
 * @Author: (lwh)
 * @Date: (2023-06-15)
 * @LastEditors: (lwh)
 * @LastEditTime: (2023-06-15)
-->
<template>
  <el-dialog v-model="props.modelValue" title="添加商品类目信息" width="600px" @closed="closeDialog" @open="openDialog">
    <el-form ref="formRef" :model="formData" :rules="rules">

      <el-row v-if="userid == 1">
        <el-col :lg="24">
          <el-form-item :label-width="labelWidth" label="店铺" prop="shopGuid">
            <el-input v-model='formData.shopName' disabled type="text" placeholder='点击选择店铺'>
              <template #append>
                <div @click="handleChooseShop">选择</div>
              </template>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">

        <el-col :lg="24">
          <el-form-item :label-width="labelWidth" label="上级类目" prop="ShopGoodsCategoryParentGuid">
            <el-cascader class="w100" :options="dataList"
              :props="{ checkStrictly: true, value: 'shopGoodsCategoryGuid', label: 'shopGoodsCategoryName', emitPath: false }"
              placeholder="请选择上级类目" clearable v-model="formData.shopGoodsCategoryParentGuid">
              <template #default="{ node, data }">
                <span>{{ data.shopGoodsCategoryName }}</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="24">
          <el-form-item :label-width="labelWidth" label="名称" prop="shopGoodsCategoryName">
            <el-input v-model="formData.shopGoodsCategoryName" placeholder="请输入名称" />
          </el-form-item>
        </el-col>
        <el-col :lg="24">
          <el-form-item :label-width="labelWidth" label="显示状态" prop="shopGoodsCategoryDisplayStatus">
            <el-radio-group v-model="formData.shopGoodsCategoryDisplayStatus">
              <el-radio v-for="item in  display_status " :key="item.dictValue" :label="parseInt(item.dictValue)">{{
                item.dictLabel }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :lg="24">
          <el-form-item :label-width="labelWidth" label="排序" prop="shopGoodsCategorySort">
            <el-input-number v-model.number="formData.shopGoodsCategorySort" 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>

  <!-- 选择店铺 -->
  <ChooseShopDialog v-model="ChooseShopDialogVisible" :data="ChooseShopDialogRow"></ChooseShopDialog>
</template>


<script setup>
import { reactive, ref, watch } from "vue";
import { ElMessage } from 'element-plus'
import modal from '@/plugins/modal.js'
import { shopGoodsCategoryTreeList, addOrUpdateShopGoodsCategory } from '@/api/business/GoodsManager/ShopGoodsCategorys/shopGoodsCategory.js';
import useUserStore from '@/store/modules/user'
import ChooseShopDialog from './ChooseShopDialog.vue';


// 打开弹窗时回调
const openDialog = async () => {
  formData.shopName = props.data?.shopName
  formData.shopGuid = props.data?.shopGuid
  formData.shopGoodsCategoryParentGuid = props.data?.shopGoodsCategoryGuid

  await getdisplay_status()
  await getTreeList()
}

// -业务参数
const userid = useUserStore().userId
const dataList = ref([])
// 显示状态字典选项列表
const display_status = ref([]);
// 选择店铺弹窗参数
const ChooseShopDialogVisible = ref(false);
const ChooseShopDialogRow = ref({});

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

// 打开选择店铺弹窗
const handleChooseShop = () => {
  ChooseShopDialogVisible.value = true
  ChooseShopDialogRow.value = formData
}

// -基础参数
const labelWidth = 100;
const formRef = ref();
const { proxy } = getCurrentInstance()
const emits = defineEmits(["update:modelValue"]);
const props = defineProps({
  modelValue: Boolean,
  done: Function,
  data: Object
});
const formData = reactive({
  shopName: "",
  shopGuid: "",
  shopGoodsCategoryParentGuid: "",
  shopGoodsCategoryDisplayStatus: 1,
  shopGoodsCategorySort: 100
});
const imgData = ref({
  fileDir: "ShopGoodsCategory"
})

// 验证
const rules = reactive({
  shopGoodsCategoryParentGuid: [{ required: true, message: "上级菜单不能为空", trigger: "blur", type: "number" }],
  shopGoodsCategoryName: [{ required: true, message: "名称不能为空", trigger: "blur" }],
  shopGoodsCategoryDisplayStatus: [{ required: true, message: "显示状态不能为空", trigger: "blur", type: "number" }],
  shopGoodsCategorySort: [{ required: true, message: "排序不能为空", trigger: "blur", type: "number" }],
});

// -基础方法
async function getTreeList() {
  shopGoodsCategoryTreeList().then((res) => {
    if (res.code == 200) {
      dataList.value = res.data
    }
  })
}
watch(props, async (v) => {

});

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


    const { code } = await addOrUpdateShopGoodsCategory(formData);
    if (code == 200) {
      modal.msgSuccess('添加成功')
      closeDialog();
    }
  });
};
const closeDialog = () => {
  formData.shopName = ""
  formData.shopGuid = ""
  formData.shopGoodsCategoryParentGuid = ""
  console.log(formData);

  handleResetClick(formRef.value);
  props.done();
  emits("update:modelValue", false);
  emits("update:data", {});
};
const handleResetClick = async (formEl) => {
  if (!formEl) return;
  formEl.resetFields();
};
</script>