<template>
  <div class="pageBox">
    <div class="cardBox">
      <div class="searchBox">
        <div class="row">
          <span>商品名称：</span>
          <el-input v-model="filter.title" clearable></el-input>
        </div>
        <div class="row">
          <span>商品编码：</span>
          <el-input v-model="filter.external_sku_id" clearable></el-input>
        </div>
        <div class="row">
          <span>创建时间：</span>
          <el-date-picker
            v-model="addTime"
            type="datetimerange"
            range-separator="-"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            value-format="yyyy-MM-dd HH:mm:ss"
            style="width: 340px">
          </el-date-picker>
        </div>
        <div class="row">
          <span>审核状态：</span>
          <el-select v-model="filter.status" placeholder="请选择" clearable>
            <el-option v-for="item in statusList" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </div>
        <div class="row">
          <el-button type="primary" icon="el-icon-search" @click="handleSearch">筛选</el-button>
        </div>
      </div>
    </div>
    <el-card>
      <el-table v-loading="loading" :data="procureList" style="width: 100%" border>
        <el-table-column prop="goods_sku.id" label="商品id" width="80" align="center" />
        <el-table-column prop="goods_sku.name" label="商品名称" />
        <el-table-column prop="external_sku_id" label="商品编码" />
        <el-table-column prop="num" label="采购数量" />
        <el-table-column prop="cost" label="采购成本" />
        <el-table-column prop="buyer_name" label="采购人名称" />
        <!-- <el-table-column prop="status" label="状态" /> -->
        <el-table-column prop="expire_time" label="保质期时间" />
        <el-table-column prop="supplier_name" label="供应商">
          <template slot-scope="scope">
            <span v-if="scope.row.supplier_name">{{ scope.row.supplier_name }}（{{scope.row.supplier_id}}）</span>
          </template>
        </el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            <span v-if="scope.row.status == 0">待审核</span>
            <span v-else-if="scope.row.status == 1" style="color: #6DD230;">审核通过</span>
            <span v-else-if="scope.row.status == 2" style="color: #f00;">审核不通过</span>
          </template>
        </el-table-column>
        <el-table-column prop="date" label="采购时间" align="center" />
        <el-table-column label="操作" width="120" align="center">
          <template slot-scope="scope">
            <el-button type="primary" v-if="scope.row.status != 1" plain @click="toExamine(scope.row)" icon="el-icon-s-check" size="mini">审核</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="page-pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[15, 30, 50, 100]"
          :page-size="pageSize"
          layout="prev, pager, next, jumper, sizes, total"
          :total="total">
        </el-pagination>
      </div>
    </el-card>

    <el-dialog title="审核" :visible.sync="dialogVisible" width="500px">
      <el-form label-width="90px">
        <el-form-item label="审核状态：">
          <el-radio-group v-model="curInfo.status">
            <el-radio :label="1">审核通过</el-radio>
            <el-radio :label="2">审核不通过</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="commitCheck" :loading="commitloading">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import { getPurchaseLog, examinePurchase } from "@/api/supplyChain"

export default {
  data() {
    return {
      loading: false,
      page: 1,
      pageSize: 15,
      total: 0,
      procureList: [],
      filter: {
        title: '',
        external_sku_id: '',
        status: ''
      },
      addTime: [],
      curInfo: {},
      commitloading: false,
      dialogVisible: false,
      statusList: [
        { id: 0, name: '待审核' },
        { id: 1, name: '审核通过' },
        { id: 2, name: '审核不通过' }
      ]
    }
  },
  methods: {
    fetchList() {
      this.loading = true
      let params = {
        page: this.page,
        per_page: this.pageSize,
        ...this.filter,
        start_time: this.addTime ? this.addTime[0] : '',
        end_time: this.addTime ? this.addTime[1] : ''
      }
      getPurchaseLog(params).then((res) => {
        this.procureList = res.data.data
        this.total = res.data.meta.total
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    handleSizeChange(val) {
      this.page = 1
      this.pageSize = val
      this.fetchList()
    },
    handleCurrentChange(val) {
      this.page = val
      this.fetchList()
    },
    handleSearch() {
      this.page = 1
      this.fetchList()
    },
    toExamine(row) {
      this.curInfo = JSON.parse(JSON.stringify(row))
      this.dialogVisible = true
    },
    commitCheck() {
      if(!this.curInfo.status) {
        this.$message({ type: "error", message: "请选择审核状态" })
        return
      }
      this.commitloading = true
      let params = {
        purchaseOrders: [{
          id: this.curInfo.id,
          status: this.curInfo.status
        }]
      }
      examinePurchase(params).then((res) => {
        this.$message({ type: "success", message: "操作成功!" })
        this.commitloading = false
        this.dialogVisible = false
        this.fetchList()
      }).catch(() => {
        this.commitloading = false
      })
    }
  },
  mounted() {
    this.fetchList()
  }
}
</script>

<style lang="scss" scoped>
  .searchBox{
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    white-space: nowrap;
    .row{
      font-size: 14px;
      margin-bottom: 20px;
      margin-right: 15px;
      display: flex;
      align-items: center;
    }
  }
  .bgBox{
    position: relative;
    border-radius: 5px;
    background-color: #f5f2f2;
    margin-bottom: 15px;
    padding: 15px 30px 15px 0;
    .close{
      color: #f00;
      position: absolute;
      right: 5px;
      top: 5px;
      font-size: 20px;
      cursor: pointer;
    }
  }
  .btn{
    display: flex;
    align-items: center;
    font-size: 14px;
    color: #409eff;
    cursor: pointer;
    width: fit-content;
  }
</style>
