<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.productName" 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-table
        ref="xTable"
        size="small"
        min-height="460"
        border
        show-overflow="tooltip"
        :loading="loading"
        :data="tableData"
      >
        <vxe-column type="checkbox" width="50"></vxe-column>
        <vxe-column field="productCode" title="产品编码" width="200"></vxe-column>
        <vxe-column field="productName" title="产品名称"></vxe-column>
        <vxe-column field="productCategoryId_dictText" title="产品类型" width="200"></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="productName">
            <el-input v-model="formEdit.productName"></el-input>
          </el-form-item>
          <el-form-item label="产品单位" prop="productUnit">
            <el-input v-model="formEdit.productUnit" />
          </el-form-item>
          <el-form-item label="品牌名称" prop="brandName">
            <el-input v-model="formEdit.brandName">
              <template #append>
                <el-button :icon="More" @click="brandVisible = true" />
              </template>
            </el-input>
          </el-form-item>
          <el-form-item label="状态" prop="productStatus">
            <el-select v-model="formEdit.productStatus">
              <template v-for="item in dictOptions['status']" 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>

    <BrandModal v-model:visible="brandVisible" @confirm="confirmBrand" />

  </div>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref } from 'vue'
import { More } from '@element-plus/icons-vue'
import { getProductList, saveProduct, modifyProduct } from '@/api/product'
import { apiDictFindById } from '@/api/common'
import { ElMessage } from 'element-plus'
import BrandModal from '@/components/BrandModal/BrandModal.vue'
import type { VxeTableInstance } from 'vxe-table'
import { queryParamsFilter } from '@/utils/query'

const brandVisible = ref(false)
const loading = ref(false)
const saveLoading = ref(false)
const showEdit = ref(false)
const formState = reactive({
  productName: '',
  currentPage: 1,
  pageSize: 10,
  total: 0
})
const dictOptions = reactive<Record<string, any>>({})

const formRef = ref()
const xTable = ref<VxeTableInstance>()
const formRules = reactive({
  productName: [{ required: true, message: '请输入产品名称', trigger: 'change' }]
})
const formEdit = reactive({
  productId: '',
  productName: '',
  productCategoryId: '',
  productStatus: '',
  productUnit: '',
  productSampleCode: '',
  brandName: '',
  prodcutType: ''
})

const tableData = ref([])
const onHide = () => {
  // 重置表单
  Object.assign(formEdit, {
    productId: '',
    productName: '',
    productCategoryId: '',
    productStatus: '',
    productUnit: '',
    productSampleCode: '',
    brandName: '',
    prodcutType: ''
  })
}

const onReset = () => {
  formState.productName = ''
}

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

const confirmBrand = (list) => {
  const [brand] = list
  formEdit.brandName = brand.brandName
}

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

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

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

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