<template>
  <div class="w-full h-full bg-white p-4">
    <p class="font-bold text-lg leading-10 border-b-2">产品类别管理</p>
    <!-- <el-form class="pt-4" :inline="true" :model="formState">
      <el-form-item label="颜色名称">
        <el-input v-model="formState.colorName" placeholder="颜色名称" clearable />
      </el-form-item>
      <el-form-item label="颜色编码">
        <el-input v-model="formState.colorCode" placeholder="颜色编码" clearable />
      </el-form-item>
      <el-form-item label="颜色描述">
        <el-input v-model="formState.colorRemark" placeholder="颜色描述" clearable />
      </el-form-item>

      <el-form-item>
        <el-button type="default" @click="onReset">重置</el-button>
        <el-button type="primary" @click="onQuery">查询</el-button>
      </el-form-item>
    </el-form> -->

    <div class="">
      <vxe-toolbar>
        <template #buttons>
          <!-- <vxe-button @click="allAlign = 'left'">居左</vxe-button> -->
          <el-button type="primary" @click="showEdit = true">新增</el-button>
          <el-button type="danger" @click="removeRows">删除</el-button>
        </template>
      </vxe-toolbar>
      <vxe-table
        ref="xTable"
        size="small"
        min-height="460"
        border
        :loading="loading"
        :data="tableData"
      >
        <vxe-column type="checkbox" width="50"></vxe-column>
        <vxe-column field="colorId" title="id" width="200"></vxe-column>
        <vxe-column field="colorName" title="颜色名称" width="200"></vxe-column>
        <vxe-column field="colorCode" title="颜色编码" width="120">
          <template #default="{ row }">
            <div class="flex items-center">
              <span class="w-4 h-4 mr-2" :style="{ background: row.colorCode }"></span>
              <span>{{ row.colorCode }}</span>
            </div>
          </template>
        </vxe-column>
        <vxe-column field="colorRemark" title="颜色描述" show-overflow></vxe-column>
        <vxe-column field="action" title="操作" width="100">
          <template #default="{ row }">
            <el-button type="primary" text size="small" @click="editRow(row)">修改</el-button>
          </template>
        </vxe-column>
      </vxe-table>

      <vxe-pager
        size="small"
        background
        v-model:current-page="formState.currentPage"
        v-model:page-size="formState.pageSize"
        :total="formState.total"
        @page-change="onQuery"
        :layouts="['PrevPage', 'JumpNumber', 'NextPage', 'Sizes', 'FullJump', 'Total']"
      >
      </vxe-pager>
    </div>

    <vxe-modal v-model="showEdit" title="编辑&保存" destroy-on-close esc-closable @hide="onHide">
      <template #default>
        <el-form ref="formRef" label-width="90px" :model="formEdit" :rules="formRules">
          <el-form-item label="上级类别" prop="pid">
            <el-input v-model="formEdit.pid"></el-input>
          </el-form-item>
          <el-form-item label="类别名称" prop="productCategoryName">
            <el-input v-model="formEdit.productCategoryName"></el-input>
          </el-form-item>
          <el-form-item label="类别编码" prop="productCategoryCode">
            <el-input v-model="formEdit.productCategoryCode" />
          </el-form-item>
          <el-form-item label="编码前缀" prop="productPrefix">
            <el-input v-model="formEdit.productPrefix"></el-input>
          </el-form-item>
          <el-form-item label="状态" prop="productCategoryStatus">
            <el-select v-model="formEdit.productCategoryStatus">
              <template
                v-for="item in dictOptions['productCategoryStatus']"
                key="item.dictItemValue"
              >
                <el-option :label="item.dictItemText" :value="item.dictItemValue"></el-option>
              </template>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit" :loading="saveLoading">提交</el-button>
            <el-button @click="showEdit = false">取消</el-button>
          </el-form-item>
        </el-form>
      </template>
    </vxe-modal>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref } from 'vue'
import { getProductCategoryTree, saveProductCategory, modifyProductCategory } from '@/api/product'
import { apiDictFindById } from '@/api/common'
import { ElMessage } from 'element-plus'
import type { VxeTableInstance } from 'vxe-table'
import { deleteColors } from '@/api/configure/colors'

const loading = ref(false)
const saveLoading = ref(false)
const showEdit = ref(false)
const formState = reactive({
  colorName: '',
  colorCode: '',
  colorRemark: '',
  currentPage: 1,
  pageSize: 10,
  total: 0
})
const dictOptions = reactive({})

const formRef = ref()
const xTable = ref<VxeTableInstance>()
const formRules = reactive({
  // colorName: [{ required: true, message: '请输入颜色名称', trigger: 'change' }]
  // colorCode: [{ required: true, message: '请输入颜色编码', trigger: 'change' }]
})
const formEdit = reactive({
  productCategoryId: '',
  productCategoryName: '',
  productCategoryStatus: '',
  productCategoryCode: '',
  productPrefix: '',
  pid:''
})

const tableData = ref([])
const onHide = () => {
  // 重置表单
  Object.assign(formEdit, {
    productCategoryId: '',
    productCategoryName: '',
    productCategoryStatus: '',
    productCategoryCode: '',
    productPrefix: '',
    pid:''
  })
}

const onReset = () => {}

const editRow = (row) => {
  Object.assign(formEdit, row)
  showEdit.value = true
}

const removeRows = async () => {
  const list = xTable.value?.getCheckboxRecords()
  if (list?.length) {
    const ids = list.map((item) => item.productCategoryId).join(',')
    await deleteColors(ids)
    ElMessage.success('删除成功！')
    onQuery()
  }
}

// 表单提交
const onSubmit = async () => {
  await formRef.value.validate()
  saveLoading.value = true
  try {
    formEdit.colorId ? await modifyProductCategory(formEdit) : await saveProductCategory(formEdit)
    ElMessage.success('操作成功！')
    showEdit.value = false
    onQuery()
  } catch {}
  saveLoading.value = false
}

const onQuery = async () => {
  loading.value = true
  try {
    const { data } = await getProductCategoryTree()
    // tableData.value = data.result.records
    // formState.total = +data.result.total
  } catch {}
  loading.value = false
}

const getDict = async (code: string) => {
  const { result } = await apiDictFindById(code)
  dictOptions[code] = result
}

onMounted(() => {
  onQuery()
  getDict('productCategoryStatus')
})
</script>
<style lang="scss" scoped></style>
