<template>
  <div>
    <el-form inline :model="queryParams" class="-mb-15px" label-width="0">
      <el-form-item>
        <el-select
          v-model="queryParams.productId"
          placeholder="选择产品"
          clearable
          filterable
          @change="changeProd"
        >
          <el-option
            v-for="item in opts.product"
            :key="item.productId"
            :label="item.productName"
            :value="item.productId"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select
          v-model="queryParams.specsId"
          placeholder="选择规格"
          clearable
          filterable
          :disabled="!queryParams.productId"
        >
          <el-option
            v-for="item in opts.spec"
            :key="item.specsId"
            :label="item.specsName"
            :value="item.specsId"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select v-model="queryParams.supplier" placeholder="供应商" clearable filterable>
          <el-option
            v-for="item in opts.supplier"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="queryParams.applyTime"
          type="daterange"
          value-format="YYYY-MM-DD"
          format="YYYY-MM-DD"
          range-separator="-"
          start-placeholder="申请时间"
          end-placeholder="申请时间"
        />
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="queryParams.checkTime"
          type="daterange"
          value-format="YYYY-MM-DD"
          format="YYYY-MM-DD"
          range-separator="-"
          start-placeholder="审核时间"
          end-placeholder="审核时间"
        />
      </el-form-item>
      <el-form-item>
        <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>
      </el-form-item>
    </el-form>
    <!-- 列表 -->
    <el-table v-loading="loading" class="mt-20px" :data="tableList" border>
      <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"
        :formatter="item.formatter"
        showOverflowTooltip
      />
      <el-table-column label="操作" width="240px" fixed="right">
        <template #default="{ row }">
          <el-button
            type="primary"
            v-if="row.auditStatus == 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>
    </el-table>
    <!-- 分页 -->
    <Pagination
      v-model:limit="queryParams.pageSize"
      v-model:page="queryParams.pageNo"
      :total="total"
      @pagination="getList"
    />

    <DialogAdd :opts="opts" ref="creatPurchase" />
    <DialogAudit ref="auditPurchase" />
  </div>
</template>

<script setup name="Purchase">
import * as ProductApi from '@/api/mall/product'
import { allSchemas } from './index.data.js'
import DialogAdd from './Comp/DialogAdd.vue'
import DialogAudit from './Comp/DialogAudit.vue'
import * as PurchaseApi from '@/api/mall/purchase'
import { getDictOptions } from '@/utils/dict'

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

const opts = ref({
  product: [],
  spec: [],
  supplier: []
})

const tableList = ref([])
const loading = ref(false)
const total = ref(0)

const queryParams = ref({
  productId: undefined,
  specsId: undefined,
  supplier: undefined,
  applyTime: [],
  checkTime: [],
  pageNo: 1,
  pageSize: 20
})

function getOptions() {
  ProductApi.getSimpleProductList().then((data) => {
    opts.value.product = data
  })
  opts.value.supplier = getDictOptions('erp_supplier')
}

function changeProd(val) {
  if (val) {
    opts.value.spec = opts.value.product.find((it) => it.productId == val).productSpecList
  } else {
    opts.value.spec = []
  }
  queryParams.value.specsId = undefined
}

function resetQuery() {
  queryParams.value = {
    productId: undefined,
    specsId: undefined,
    supplier: undefined,
    applyTime: [],
    checkTime: [],
    pageNo: 1,
    pageSize: 20
  }
  getList()
}

const getList = async function () {
  loading.value = true
  try {
    const data = await PurchaseApi.getPurchasePage(queryParams.value)
    tableList.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

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)
}

onMounted(() => {
  getOptions()
  getList()
})
</script>

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