<template>
  <div class="app-container">
    <el-row justify="space-between">
      <el-col :span="18">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="标签名称" prop="labelName">
            <el-input v-model="queryParams.labelName" 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-col>
      <el-col :span="6">
        <div class="right-box">
          <el-button type="primary" plain icon="Plus" @click="handleAdd"
            v-hasPermi="['member:label:add']">新增标签</el-button>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </div>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="labelList" @selection-change="handleSelectionChange" :border="true" stripe>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="排序" align="center" prop="orderNum" width="60" />
      <el-table-column label="ID" align="center" prop="id" width="60" />
      <el-table-column label="图标" align="center" prop="iconColor">
        <template #default="scope">
          <el-row justify="center">
            <span class="icon-color-text" :style="{ 'background': scope?.row?.iconColor }">{{ scope?.row?.labelName }}</span>
          </el-row>
        </template>
      </el-table-column>
      <el-table-column label="标签名称" align="center" prop="labelName" />
      <el-table-column label="描述" align="center" prop="remark" show-overflow-tooltip min-width="300" />
      <el-table-column label="该标签人数" align="center">
        <template #default="scope">
          <span>0</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['member:label:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['member:label:remove']">删除</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作人" align="center" prop="updateBy">
        <template #default="scope">
          <span>{{ scope?.row?.updateBy || scope?.row?.createBy || '--' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作时间" align="center" prop="updateTime">
        <template #default="scope">
          <span>{{ scope?.row?.updateTime || scope?.row?.createTime || '--' }}</span>
        </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="labelRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="排序" prop="orderNum">
          <el-input v-model="form.orderNum" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="标签名称" prop="labelName">
          <el-input v-model="form.labelName" placeholder="请输入标签名称" maxlength="16" show-word-limit />
        </el-form-item>
        <el-form-item label="图标" prop="iconColor">
          <el-radio-group v-model="form.iconColor">
            <el-row justify="space-between">
              <el-col :span="8" v-for="dict in ff_member_label_icon" :key="dict.value">
                <el-radio :value="dict.value">
                  <span v-if="dict.value === 'NO_ICON'">{{ dict.label }}</span>
                  <span v-else class="icon-color-text" :style="{ 'background': dict.value }">{{ form.labelName }}</span>
                </el-radio>
              </el-col>
            </el-row>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="描述" prop="remark">
          <el-input v-model="form.remark" maxlength="90" placeholder="请输入描述" show-word-limit type="textarea"
            :rows="4" />
        </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="Label">
import { listLabel, getLabel, delLabel, addLabel, updateLabel } from "@/api/member/label";

const { proxy } = getCurrentInstance();
const { ff_member_label_icon } = proxy.useDict("ff_member_label_icon");

const labelList = 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 data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderNum: null,
    labelName: null,
    iconColor: null,
  },
  rules: {
    labelName: [{ required: true, message: '请输入标签名称' }],
    iconColor: [{ required: true, message: '请选择图标' }],
  }
});

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

/** 查询会员标签列表 */
function getList() {
  loading.value = true;
  listLabel(queryParams.value).then(response => {
    labelList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

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

// 表单重置
function reset() {
  form.value = {
    id: null,
    orderNum: null,
    labelName: null,
    iconColor: 'NO_ICON',
    remark: null
  };
  proxy.resetForm("labelRef");
}

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

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

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

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加会员标签";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getLabel(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改会员标签";
  });
}

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

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除会员标签ID为"' + _ids + '"的数据项？').then(function () {
    return delLabel(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

getList();
</script>

<style lang="scss" scoped>
.icon-color-text {
  width: 70px;
  height: 30px;
  line-height: 30px;
  display: block;
  text-align: center;
  color: #fff;
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
  padding: 0 3px
}
</style>