<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="门店编号" prop="shopNo">
        <el-input
          v-model="queryParams.shopNo"
          placeholder="请输入门店编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="门店名称" prop="shopName">
        <el-input
          v-model="queryParams.shopName"
          placeholder="请输入门店名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="门店地址" prop="shopAddress">
        <el-input
          v-model="queryParams.shopAddress"
          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-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['shop:info:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['shop:info:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['shop:info:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['shop:info:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList">
        <el-tooltip class="item" effect="dark" content="列表/平铺" placement="top">
          <el-button circle icon="Menu" @click="showGrid = !showGrid"/>
        </el-tooltip>
      </right-toolbar>
    </el-row>

    <div class="grid" style="width: 80vw;height: 75vh;" v-if="showGrid">
      <el-row :gutter="20" justify="space-around">
        <el-col  v-for="(item, key) in infoList" :key="key" :span="25">
          <div style="margin-top:10px">
            <el-card shadow="hover" :span="1" style="max-width: 400px;text-align: center;margin-bottom: 80px;">
              <el-image :src="getImgUrl(item.shopImg)" style="height: 180px;width: 200px;"  ></el-image>
              <div style="text-align: left;margin: 10px;white-space: nowrap;text-overflow:ellipsis;overflow:hidden;">
                <span style="margin-bottom: 5px;">门店名称：{{item.shopName}}</span><br>
                <span>门店编号：{{item.shopNo}}</span><br>
                <span>门店地址：{{item.shopAddress}}</span><br>
              </div>
            </el-card>
          </div>
        </el-col>
      </el-row>
    </div>

    <el-table v-else v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="门店编号" align="center" prop="shopNo" />
      <el-table-column label="门店名称" align="center" prop="shopName" />
      <el-table-column label="门店地址" align="center" prop="shopAddress" />
      <el-table-column label="门店图片" align="center" prop="shopImg" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.shopImg" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            type="text"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['shop:info:edit']"
          >修改</el-button>
          <el-button
            type="text"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['shop:info:remove']"
          >删除</el-button>
        </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-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="infoRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="门店编号" prop="shopNo">
          <el-input v-model="form.shopNo" placeholder="请输入门店编号" />
        </el-form-item>
        <el-form-item label="门店名称" prop="shopName">
          <el-input v-model="form.shopName" placeholder="请输入门店名称" />
        </el-form-item>
        <el-form-item label="门店地址" prop="shopAddress">
          <el-input v-model="form.shopAddress" placeholder="请输入门店地址" />
        </el-form-item>
        <el-form-item label="门店图片">
          <image-upload v-model="form.shopImg"/>
        </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 type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Info">
import { listInfo, getInfo, delInfo, addInfo, updateInfo } from "@/api/shop/info";
import {isExternal} from "@/utils/validate";

const { proxy } = getCurrentInstance();

const infoList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const showGrid = ref(false);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    shopNo: null,
    shopName: null,
    shopAddress: null,
    shopImg: null,
  },
  rules: {
    shopNo: [
      { required: true, message: "门店编号不能为空", trigger: "blur" }
    ],
    shopName: [
      { required: true, message: "门店名称不能为空", trigger: "blur" }
    ],
    shopAddress: [
      { required: true, message: "门店地址不能为空", trigger: "blur" }
    ],
    shopImg: [
      { required: true, message: "门店图片不能为空", trigger: "blur" }
    ],
  }
});

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

/** 查询门店信息列表 */
function getList() {
  loading.value = true;
  listInfo(queryParams.value).then(response => {
    infoList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

function getImgUrl(url) {
  let real_src = url.split(",")[0];
  if (isExternal(real_src)) {
    return real_src;
  }
  return import.meta.env.VITE_APP_BASE_API + real_src;
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    shopId: null,
    deptId: null,
    shopNo: null,
    shopName: null,
    shopAddress: null,
    shopImg: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  };
  proxy.resetForm("infoRef");
}

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

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.shopId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加门店信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _shopId = row.shopId || ids.value
  getInfo(_shopId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改门店信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["infoRef"].validate(valid => {
    if (valid) {
      if (form.value.shopId != null) {
        updateInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _shopIds = row.shopId || ids.value;
  proxy.$modal.confirm('是否确认删除门店信息编号为"' + _shopIds + '"的数据项？').then(function() {
    return delInfo(_shopIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('shop/info/export', {
    ...queryParams.value
  }, `info_${new Date().getTime()}.xlsx`)
}

getList();
</script>
