<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="品牌名称" prop="brandName">
              <el-input v-model="queryParams.brandName" placeholder="请输入品牌名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="品牌编号" prop="brandNo">
              <el-input v-model="queryParams.brandNo" placeholder="请输入品牌编号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['product:brand:add']">新增品牌</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" border :data="brandList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="品牌编号" align="center" prop="brandNo" width="120" />
        <el-table-column label="品牌名称" align="center" prop="brandName" />
        <el-table-column label="是否有授权" align="center" prop="license" width="120">
          <template #default="scope">
            <span>{{ scope.row.license ? '是' : '无' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="授权区域" align="center" prop="position" show-overflow-tooltip>
          <template #default="scope">
            <span>{{ scope.row.position || '无' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="到期时间" align="center" prop="expireTime" width="120">
          <template #default="scope">
            <span>{{ scope.row.expireTime ? parseTime(scope.row.expireTime, '{y}-{m}-{d}') : '无' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
          <template #default="scope">
            <el-tooltip content="查看" placement="top">
              <el-button link type="primary" icon="View" @click="handleUpdate(scope.row)" v-hasPermi="['product:brand:query']">查看</el-button>
            </el-tooltip>
            <el-tooltip content="编辑" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['product:brand:edit']">编辑</el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['product:brand:remove']">删除</el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
    <!-- 添加或修改产品品牌信息对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body>
      <el-form ref="brandFormRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="品牌编号" prop="brandNo">
          <el-input v-model="form.brandNo" placeholder="请输入品牌编号" />
        </el-form-item>
        <el-form-item label="品牌中文名称" prop="brandName">
          <el-input v-model="form.brandName" placeholder="请输入品牌中文名称" />
        </el-form-item>
        <el-form-item label="品牌首字母缩写" prop="brandInitials">
          <el-input v-model="form.brandInitials" placeholder="请输入品牌首字母缩写" />
        </el-form-item>
        <el-form-item label="品牌英文名称" prop="brandEnglishName">
          <el-input v-model="form.brandEnglishName" placeholder="请输入品牌英文名称" />
        </el-form-item>
        <el-form-item label="推荐值" prop="recommendValue">
          <el-input v-model="form.recommendValue" placeholder="请输入推荐值" />
        </el-form-item>
        <el-form-item label="品牌Logo图片路径或URL" prop="brandLogo">
          <el-input v-model="form.brandLogo" placeholder="请输入品牌Logo图片路径或URL" />
        </el-form-item>
        <el-form-item label="品牌标题" prop="brandTitle">
          <el-input v-model="form.brandTitle" placeholder="请输入品牌标题" />
        </el-form-item>
        <el-form-item label="品牌大图" prop="brandBigImage">
          <image-upload v-model="form.brandBigImage"/>
        </el-form-item>
        <el-form-item label="品牌故事" prop="brandStory">
            <el-input v-model="form.brandStory" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="是否显示" prop="isShow">
          <el-input v-model="form.isShow" placeholder="请输入是否显示" />
        </el-form-item>
        <el-form-item label="品牌注册人" prop="brandRegistrant">
          <el-input v-model="form.brandRegistrant" placeholder="请输入品牌注册人" />
        </el-form-item>
        <el-form-item label="许可证编号" prop="license">
          <el-input v-model="form.license" placeholder="请输入许可证编号" />
        </el-form-item>
        <el-form-item label="注册证书编号" prop="registrationCertificate">
          <el-input v-model="form.registrationCertificate" placeholder="请输入注册证书编号" />
        </el-form-item>
        <el-form-item label="证书/许可过期时间" prop="expireTime">
          <el-date-picker clearable
            v-model="form.expireTime"
            type="datetime"
            value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="请选择证书/许可过期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="品牌描述" prop="brandDescribe">
            <el-input v-model="form.brandDescribe" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="展示位置" prop="position">
          <el-input v-model="form.position" placeholder="请输入展示位置" />
        </el-form-item>
        <el-form-item label="关注度/收藏数" prop="care">
          <el-input v-model="form.care" placeholder="请输入关注度/收藏数" />
        </el-form-item>
        <el-form-item label="数据来源" prop="dataSource">
          <el-input v-model="form.dataSource" placeholder="请输入数据来源" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Brand" lang="ts">
import { listBrand, getBrand, delBrand, addBrand, updateBrand } from '@/api/product/brand';
import { BrandVO, BrandQuery, BrandForm } from '@/api/product/brand/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const router = useRouter();

const brandList = ref<BrandVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const brandFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: BrandForm = {
  id: undefined,
  brandNo: undefined,
  brandName: undefined,
  brandInitials: undefined,
  brandEnglishName: undefined,
  recommendValue: undefined,
  brandLogo: undefined,
  brandTitle: undefined,
  brandBigImage: undefined,
  brandStory: undefined,
  isShow: undefined,
  brandRegistrant: undefined,
  license: undefined,
  registrationCertificate: undefined,
  expireTime: undefined,
  brandDescribe: undefined,
  position: undefined,
  care: undefined,
  dataSource: undefined,
  remark: undefined,
}
const data = reactive<PageData<BrandForm, BrandQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    brandNo: undefined,
    brandName: undefined,
    brandInitials: undefined,
    brandEnglishName: undefined,
    recommendValue: undefined,
    brandLogo: undefined,
    brandTitle: undefined,
    brandBigImage: undefined,
    brandStory: undefined,
    isShow: undefined,
    brandRegistrant: undefined,
    license: undefined,
    registrationCertificate: undefined,
    expireTime: undefined,
    brandDescribe: undefined,
    position: undefined,
    care: undefined,
    dataSource: undefined,
    platformCode: undefined,
    params: {
    }
  },
  rules: {
    brandInitials: [
      { required: true, message: "品牌首字母缩写不能为空", trigger: "blur" }
    ],
    brandEnglishName: [
      { required: true, message: "品牌英文名称不能为空", trigger: "blur" }
    ],
    recommendValue: [
      { required: true, message: "推荐值不能为空", trigger: "blur" }
    ],
    brandLogo: [
      { required: true, message: "品牌Logo图片路径或URL不能为空", trigger: "blur" }
    ],
    brandTitle: [
      { required: true, message: "品牌标题不能为空", trigger: "blur" }
    ],
    brandBigImage: [
      { required: true, message: "品牌大图不能为空", trigger: "blur" }
    ],
    brandStory: [
      { required: true, message: "品牌故事不能为空", trigger: "blur" }
    ],
    isShow: [
      { required: true, message: "是否显示不能为空", trigger: "blur" }
    ],
    brandRegistrant: [
      { required: true, message: "品牌注册人不能为空", trigger: "blur" }
    ],
    license: [
      { required: true, message: "许可证编号不能为空", trigger: "blur" }
    ],
    registrationCertificate: [
      { required: true, message: "注册证书编号不能为空", trigger: "blur" }
    ],
    expireTime: [
      { required: true, message: "证书/许可过期时间不能为空", trigger: "blur" }
    ],
    brandDescribe: [
      { required: true, message: "品牌描述不能为空", trigger: "blur" }
    ],
    position: [
      { required: true, message: "展示位置不能为空", trigger: "blur" }
    ],
    dataSource: [
      { required: true, message: "数据来源不能为空", trigger: "blur" }
    ],
    remark: [
      { required: true, message: "备注不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询产品品牌信息列表 */
const getList = async () => {
  loading.value = true;
  const res = await listBrand(queryParams.value);
  brandList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
  form.value = {...initFormData};
  brandFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: BrandVO[]) => {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
const handleAdd = () => {
  router.push({
    path: '/product/brand/edit',
    query: {
      type: 'add'
    }
  });
}

/** 修改按钮操作 */
const handleUpdate = async (row?: BrandVO) => {
  const _id = row?.id || ids.value[0];
  router.push({
    path: '/product/brand/edit',
    query: {
      id: _id,
      type: 'edit'
    }
  });
}

/** 提交按钮 */
const submitForm = () => {
  brandFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateBrand(form.value).finally(() =>  buttonLoading.value = false);
      } else {
        await addBrand(form.value).finally(() =>  buttonLoading.value = false);
      }
      proxy?.$modal.msgSuccess("操作成功");
      dialog.visible = false;
      await getList();
    }
  });
}

/** 删除按钮操作 */
const handleDelete = async (row?: BrandVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除产品品牌信息编号为"' + _ids + '"的数据项？').finally(() => loading.value = false);
  await delBrand(_ids);
  proxy?.$modal.msgSuccess("删除成功");
  await getList();
}

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('product/brand/export', {
    ...queryParams.value
  }, `brand_${new Date().getTime()}.xlsx`)
}

onMounted(() => {
  getList();
});
</script>
