<template>
  <div class="app-container">
    <el-card style="margin-bottom: 12px;" shadow="always" v-show="showSearch">
      <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="68px">
        <el-form-item label="样式名称" prop="styleName">
          <el-input v-model="queryParams.styleName" placeholder="请输入样式名称" clearable></el-input>
        </el-form-item>
        <el-form-item label="样式类型" prop="styleType">
          <el-select clearable v-model="queryParams.styleType" style="width: 200px" placeholder="请选择样式类型">
            <el-option v-for="type in styleTypeList" :key="type.value" :label="type.label" :value="type.value" />
          </el-select>
        </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>
    <el-card shadow="always" class="list-table-section">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="formDialogRef.show()">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="formDialogRef.show(ids[0])">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete">删除</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>
      <el-table v-loading="loading" :data="styleList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="样式名称" align="center" prop="styleName" />
        <el-table-column label="样式类型" align="center" prop="styleType">
          <template #default="{ row }">
            <!-- <el-tag type="primary" v-for="(type, index) in row.type.split(',')" :key="index">
							{{ styleTypeList.find(item => item.id === type) }}
						</el-tag> -->
            <el-tag v-for="(dict, index) in styleTypeList"
              v-show="row.styleType === dict.value" :key="index">{{ dict.label }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="描述信息" align="center" prop="description" />
        <el-table-column label="操作" width="300" align="center" class-name="small-padding fixed-width">
          <template #default="{ row }">
            <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="Edit" @click="formDialogRef.show(row.id)"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="Delete" @click="handleDelete(row)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <div class="page-wrap">
      <pagination class="list-page" v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize" @pagination="getList" />
    </div>
  </div>
  <style-dialog ref="formDialogRef" @refresh="getList()" />
</template>

<script setup name="configStyle">
import { getStyleType, listStyle, delStyle  } from "@/api/gisManagement/configStyle"
import styleDialog from "./styleDialog.vue"

const { proxy } = getCurrentInstance()

const styleTypeList = ref([])
const styleList = ref([])
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)

const formDialogRef = ref()
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  styleName: '',
  styleType: null
})

/* 初始化 */
function init() {
  // 获取样式类型
  getStyleType().then(response => {
    styleTypeList.value = response
  })

  getList()
}

/** 查询用户信息列表 */
function getList() {
  loading.value = true
  listStyle(queryParams.value).then(response => {
    styleList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

/** 搜索按钮操作 */
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 handleDelete(row) {
  const _userIds = row.id || ids.value
  proxy.$modal.confirm('是否确认删除勾选的数据项？').then(function () {
    return delStyle(_userIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => { })
}

init()
</script>

<style lang="scss" scoped>
.app-container {
  position: relative;
  height: calc(100vh - 84px);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.list-table-section {
  flex: auto;
}
</style>