<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="商品编号" prop="productNo">
              <el-input v-model="queryParams.productNo" placeholder="请输入商品编号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="商品名称" prop="productName">
              <el-input v-model="queryParams.productName" placeholder="请输入商品名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="报备状态" prop="status">
              <el-select v-model="queryParams.status" placeholder="请选择报备状态" clearable>
                <el-option label="待审核" value="0" />
                <el-option label="已通过" value="1" />
                <el-option label="已驳回" value="2" />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="handleQuery">搜索</el-button>
              <el-button @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="hover">
      <template #header>
        <el-row :gutter="10">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd">新增报备</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain :disabled="single" icon="Edit" @click="handleUpdate()">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain :disabled="multiple" icon="Delete" @click="handleDelete()">删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport">导出</el-button>
          </el-col>
          <right-toolbar v-model:show-search="showSearch" @query-table="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" border :data="productList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="商品编号" align="center" prop="productNo" width="150" />
        <el-table-column label="商品名称" align="center" prop="productName" :show-overflow-tooltip="true" width="180" />
        <el-table-column label="商品分类" align="center" prop="categoryName" width="120" />
        <el-table-column label="品牌" align="center" prop="brandName" width="120" />
        <el-table-column label="规格型号" align="center" prop="specification" width="150" />
        <el-table-column label="报备价格" align="center" prop="reportPrice" width="120">
          <template #default="scope">
            <span>¥{{ scope.row.reportPrice }}</span>
          </template>
        </el-table-column>
        <el-table-column label="报备状态" align="center" prop="status" width="100">
          <template #default="scope">
            <el-tag v-if="scope.row.status === 0" type="warning">待审核</el-tag>
            <el-tag v-else-if="scope.row.status === 1" type="success">已通过</el-tag>
            <el-tag v-else-if="scope.row.status === 2" type="danger">已驳回</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="报备人" align="center" prop="reportUser" width="120" />
        <el-table-column label="报备时间" align="center" prop="reportTime" width="180" />
        <el-table-column label="审核人" align="center" prop="auditUser" width="120" />
        <el-table-column label="审核时间" align="center" prop="auditTime" width="180" />
        <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" />
        <el-table-column label="操作" align="center" width="180" fixed="right" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" @click="handleUpdate(scope.row)">详情</el-button>
            <el-button link type="primary" @click="handleAudit(scope.row)" v-if="scope.row.status === 0">审核</el-button>
            <el-button link type="primary" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total" @pagination="getList" />
    </el-card>

    <el-dialog v-model="dialog.visible" :title="dialog.title" width="900px" append-to-body @close="cancel">
      <el-form ref="productFormRef" :model="form" :rules="rules" label-width="120px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="商品编号" prop="productNo">
              <el-input v-model="form.productNo" placeholder="请输入商品编号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="商品名称" prop="productName">
              <el-input v-model="form.productName" placeholder="请输入商品名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="商品分类" prop="categoryName">
              <el-input v-model="form.categoryName" placeholder="请输入商品分类" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="品牌" prop="brandName">
              <el-input v-model="form.brandName" placeholder="请输入品牌" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="规格型号" prop="specification">
              <el-input v-model="form.specification" placeholder="请输入规格型号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="报备价格" prop="reportPrice">
              <el-input-number v-model="form.reportPrice" :min="0" :precision="2" placeholder="请输入报备价格" style="width: 100%" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="供应商" prop="supplierName">
              <el-input v-model="form.supplierName" placeholder="请输入供应商" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="生产厂家" prop="manufacturer">
              <el-input v-model="form.manufacturer" placeholder="请输入生产厂家" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="商品描述" prop="description">
              <el-input v-model="form.description" type="textarea" :rows="3" placeholder="请输入商品描述" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" :rows="2" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
          <el-button type="primary" @click="submitForm">确 定</el-button>
        </div>
      </template>
    </el-dialog>

    <el-dialog v-model="auditDialog.visible" title="商品报备审核" width="600px" append-to-body @close="cancelAudit">
      <el-form ref="auditFormRef" :model="auditForm" :rules="auditRules" label-width="100px">
        <el-form-item label="审核结果" prop="status">
          <el-radio-group v-model="auditForm.status">
            <el-radio :label="1">通过</el-radio>
            <el-radio :label="2">驳回</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审核意见" prop="auditRemark">
          <el-input v-model="auditForm.auditRemark" type="textarea" :rows="4" placeholder="请输入审核意见" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="cancelAudit">取 消</el-button>
          <el-button type="primary" @click="submitAudit">确 定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="ProductReport" lang="ts">
import { getCurrentInstance, reactive, ref, toRefs } from 'vue';

const { proxy } = getCurrentInstance() as any;

const productList = 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 data = reactive({
  form: {} as any,
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    productNo: undefined,
    productName: undefined,
    status: undefined
  },
  rules: {
    productNo: [{ required: true, message: '商品编号不能为空', trigger: 'blur' }],
    productName: [{ required: true, message: '商品名称不能为空', trigger: 'blur' }],
    categoryName: [{ required: true, message: '商品分类不能为空', trigger: 'blur' }],
    reportPrice: [{ required: true, message: '报备价格不能为空', trigger: 'blur' }]
  },
  dialog: {
    visible: false,
    title: ''
  },
  auditDialog: {
    visible: false
  },
  auditForm: {
    id: undefined,
    status: 1,
    auditRemark: ''
  },
  auditRules: {
    status: [{ required: true, message: '请选择审核结果', trigger: 'change' }],
    auditRemark: [{ required: true, message: '审核意见不能为空', trigger: 'blur' }]
  }
});

const { queryParams, form, rules, dialog, auditDialog, auditForm, auditRules } = toRefs(data);

const queryFormRef = ref();
const productFormRef = ref();
const auditFormRef = ref();

function getList() {
  loading.value = true;
  setTimeout(() => {
    productList.value = [
      {
        id: 1,
        productNo: 'PRD202401210001',
        productName: '测试商品A',
        categoryName: '电子产品',
        brandName: '品牌A',
        specification: '型号X1',
        reportPrice: 1200.00,
        status: 0,
        reportUser: '张三',
        reportTime: '2024-01-21 10:00:00',
        auditUser: '',
        auditTime: '',
        supplierName: '供应商A',
        manufacturer: '厂家A',
        description: '高品质电子产品',
        remark: '测试商品报备'
      },
      {
        id: 2,
        productNo: 'PRD202401210002',
        productName: '测试商品B',
        categoryName: '家电',
        brandName: '品牌B',
        specification: '型号Y2',
        reportPrice: 3500.00,
        status: 1,
        reportUser: '李四',
        reportTime: '2024-01-20 14:30:00',
        auditUser: '王五',
        auditTime: '2024-01-20 16:00:00',
        supplierName: '供应商B',
        manufacturer: '厂家B',
        description: '智能家电产品',
        remark: '重要商品'
      }
    ] as any;
    total.value = 2;
    loading.value = false;
  }, 500);
}

function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

function resetQuery() {
  queryFormRef.value?.resetFields();
  handleQuery();
}

function reset() {
  form.value = {
    id: undefined,
    productNo: undefined,
    productName: undefined,
    categoryName: undefined,
    brandName: undefined,
    specification: undefined,
    reportPrice: undefined,
    supplierName: undefined,
    manufacturer: undefined,
    description: undefined,
    remark: undefined
  };
  productFormRef.value?.resetFields();
}

function handleAdd() {
  reset();
  dialog.value.visible = true;
  dialog.value.title = '新增商品报备';
}

function handleUpdate(row?: any) {
  reset();
  const id = row?.id || ids.value[0];
  dialog.value.visible = true;
  dialog.value.title = '修改商品报备';
  form.value = { ...row };
}

function submitForm() {
  productFormRef.value?.validate((valid: boolean) => {
    if (valid) {
      proxy?.$modal.msgSuccess('提交成功');
      dialog.value.visible = false;
      getList();
    }
  });
}

function cancel() {
  dialog.value.visible = false;
  reset();
}

function handleDelete(row?: any) {
  const productIds = row?.id || ids.value;
  proxy?.$modal
    .confirm('是否确认删除商品报备编号为"' + productIds + '"的数据项？')
    .then(() => {
      proxy?.$modal.msgSuccess('删除成功');
      getList();
    })
    .catch(() => {});
}

function handleExport() {
  proxy?.$modal.confirm('是否确认导出所有商品报备数据项？').then(() => {
    proxy?.$modal.msgSuccess('导出成功');
  });
}

function handleSelectionChange(selection: any[]) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
}

function handleAudit(row: any) {
  auditForm.value.id = row.id;
  auditForm.value.status = 1;
  auditForm.value.auditRemark = '';
  auditDialog.value.visible = true;
}

function submitAudit() {
  auditFormRef.value?.validate((valid: boolean) => {
    if (valid) {
      proxy?.$modal.msgSuccess('审核成功');
      auditDialog.value.visible = false;
      getList();
    }
  });
}

function cancelAudit() {
  auditDialog.value.visible = false;
  auditForm.value = {
    id: undefined,
    status: 1,
    auditRemark: ''
  };
}

getList();
</script>

<style scoped lang="scss"></style>
