<template>
  <div>
    <!-- 搜索工作栏 -->
    <Search :schema="allSchemas.searchSchema" labelWidth="0">
      <template #actionMore>
        <el-button @click="getList" v-hasPermi="['mall:purchase:search']"> 搜索 </el-button>
        <el-button @click="resetQuery" v-hasPermi="['mall:purchase:reset']"> 重置 </el-button>
        <el-button type="primary" @click="handleAdd" v-hasPermi="['mall:purchase:add']">
          发起采购
        </el-button>
      </template>
    </Search>
    <!-- 列表 -->
    <SSTable
      class="mt-20px"
      v-model:tableObject="tableObject"
      :tableColumns="allSchemas.tableColumns"
      @get-list="getTableList"
    >
      <el-table-column
        v-for="item in allSchemas.tableColumns"
        :key="item.table?.field || item.field"
        :prop="item.table?.field || item.field"
        :label="item.label"
        :fixed="item.fixed"
        min-width="150px"
        showOverflowTooltip
      />
      <el-table-column label="操作" width="150px" fixed="right">
        <template #default="{ row }">
          <el-button
            type="primary"
            v-if="row.status == 1"
            link
            @click="handleAudit(row)"
            v-hasPermi="['mall:purchase:audit']"
          >
            采购审核
          </el-button>
          <el-button
            v-else
            type="primary"
            link
            @click="purchaseAgain(row)"
            v-hasPermi="['mall:purchase:readd']"
          >
            再次采购
          </el-button>
          <el-button
            type="primary"
            link
            @click="handleDetail(row)"
            v-hasPermi="['mall:purchase:detail']"
          >
            详情
          </el-button>
        </template>
      </el-table-column>
    </SSTable>
    <DialogAdd ref="creatPurchase" />
    <DialogAudit ref="auditPurchase" />
  </div>
</template>

<script setup>
import { allSchemas } from './index.data.js'
import DialogAdd from './Comp/DialogAdd.vue'
import DialogAudit from './Comp/DialogAudit.vue'

const creatPurchase = ref()
const auditPurchase = ref()

const tableObject = ref({
  tableList: [],
  loading: false,
  total: 1,
  pageSize: 20,
  currentPage: 1
})

function resetQuery() {
  queryParams.value = {
    productName: undefined,
    productBrand: undefined,
    productCategory: undefined,
    pageNo: 1,
    pageSize: 10
  }
  getList()
}

const getList = function () {
  tableObject.value.tableList = [
    { name: '测试', status: 1, statusName: '审核中', supplier: '林氏木业', purchaseCount: 10 },
    { name: '测试2', status: 2, statusName: '已通过', supplier: '张氏木业', purchaseCount: 1 },
    { name: '测试3', status: 3, statusName: '已驳回', supplier: '周氏木业', purchaseCount: 5 }
  ]
}

function getTableList() {
  tableObject.value.tableList = [
    { name: '测试', status: 1, statusName: '审核中', supplier: '林氏木业', purchaseCount: 10 },
    { name: '测试2', status: 2, statusName: '已通过', supplier: '张氏木业', purchaseCount: 1 },
    { name: '测试3', status: 3, statusName: '已驳回', supplier: '周氏木业', purchaseCount: 5 }
  ]
}

function purchaseAgain(row) {
  creatPurchase.value.open({ ...row })
}

function handleAudit(row) {
  auditPurchase.value.open({ ...row }, true)
}

function handleAdd() {
  creatPurchase.value.open(null)
}
function handleDetail(row) {
  auditPurchase.value.open({ ...row }, false)
}
</script>

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