<template>
  <div class="divBox">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item label="ck_bill_cargo表主键">
        <el-input v-model="dataForm.billCargoId" placeholder="请输入ck_bill_cargo表主键" clearable></el-input>
      </el-form-item>
      <el-form-item label="出入库类型">
        <el-input v-model="dataForm.inoutType" placeholder="请输入出入库类型" clearable></el-input>
      </el-form-item>
      <el-form-item label="库场ID">
        <el-input v-model="dataForm.stockId" placeholder="请输入库场ID" clearable></el-input>
      </el-form-item>
      <el-form-item label="库场代码">
        <el-input v-model="dataForm.stockCode" placeholder="请输入库场代码" clearable></el-input>
      </el-form-item>
      <el-form-item label="库场名称">
        <el-input v-model="dataForm.stockName" placeholder="请输入库场名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="单据编号">
        <el-input v-model="dataForm.billNumber" placeholder="请输入单据编号" clearable></el-input>
      </el-form-item>
      <el-form-item label="货物价值">
        <el-input v-model="dataForm.cargoValue" placeholder="请输入货物价值" clearable></el-input>
      </el-form-item>
      <el-form-item label="货物件数">
        <el-input v-model="dataForm.cargoNum" placeholder="请输入货物件数" clearable></el-input>
      </el-form-item>
      <el-form-item label="货物重量">
        <el-input v-model="dataForm.cargoWt" placeholder="请输入货物重量" clearable></el-input>
      </el-form-item>
      <el-form-item label="货物体积">
        <el-input v-model="dataForm.cargoVol" placeholder="请输入货物体积" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button @click="resetForm()">重置</el-button>
        <el-button v-hasPermi="['autogencode:ckbillstock:save']" type="primary" @click="addOrUpdateHandle()">新增数据</el-button>
        <el-button v-hasPermi="['autogencode:ckbillstock:delete']" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="id"
        header-align="center"
        align="center"
        label="主键">
      </el-table-column>
      <el-table-column
        prop="billCargoId"
        header-align="center"
        align="center"
        label="ck_bill_cargo表主键">
      </el-table-column>
      <el-table-column
        prop="inoutType"
        header-align="center"
        align="center"
        label="出入库类型">
      </el-table-column>
      <el-table-column
        prop="stockId"
        header-align="center"
        align="center"
        label="库场ID">
      </el-table-column>
      <el-table-column
        prop="stockCode"
        header-align="center"
        align="center"
        label="库场代码">
      </el-table-column>
      <el-table-column
        prop="stockName"
        header-align="center"
        align="center"
        label="库场名称">
      </el-table-column>
      <el-table-column
        prop="billNumber"
        header-align="center"
        align="center"
        label="单据编号">
      </el-table-column>
      <el-table-column
        prop="cargoValue"
        header-align="center"
        align="center"
        label="货物价值">
      </el-table-column>
      <el-table-column
        prop="cargoNum"
        header-align="center"
        align="center"
        label="货物件数">
      </el-table-column>
      <el-table-column
        prop="cargoWt"
        header-align="center"
        align="center"
        label="货物重量">
      </el-table-column>
      <el-table-column
        prop="cargoVol"
        header-align="center"
        align="center"
        label="货物体积">
      </el-table-column>
      <el-table-column
        prop="delFlag"
        header-align="center"
        align="center"
        label="删除标志（0代表存在 2代表删除）">
      </el-table-column>
      <el-table-column
        prop="createDept"
        header-align="center"
        align="center"
        label="创建部门">
      </el-table-column>
      <el-table-column
        prop="createBy"
        header-align="center"
        align="center"
        label="创建人">
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        label="创建时间">
      </el-table-column>
      <el-table-column
        prop="updateBy"
        header-align="center"
        align="center"
        label="修改人">
      </el-table-column>
      <el-table-column
        prop="updateTime"
        header-align="center"
        align="center"
        label="修改时间">
      </el-table-column>
      <el-table-column
        prop="tenantId"
        header-align="center"
        align="center"
        label="">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">{{ '修改' }}</el-button>
          <el-button v-hasPermi="['autogencode:ckbillstock:delete']" type="text" size="small" @click="deleteHandle(scope.row.id)" style="color: #f56c6c;">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 表单弹窗, 新增数据和修改数据 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './ckbillstock-add-and-update'
  import * as api from '@/api/ckbillstock.js'
  export default {
    data () {
      return {
        dataForm: {
          billCargoId: '',
          inoutType: '',
          stockId: '',
          stockCode: '',
          stockName: '',
          billNumber: '',
          cargoValue: '',
          cargoNum: '',
          cargoWt: '',
          cargoVol: '',
        },

        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
      this.getDataList()
    },
    mounted() {
      // 确保页面加载时自动查询
      this.getDataList()
    },
    methods: {
      // 重置表单
      resetForm() {
        this.dataForm = {
          billCargoId: '',
          inoutType: '',
          stockId: '',
          stockCode: '',
          stockName: '',
          billNumber: '',
          cargoValue: '',
          cargoNum: '',
          cargoWt: '',
          cargoVol: '',
        }
        this.getDataList()
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        // 根据后端CkBillStockController和PageParamRequest的期望格式构建参数
        const params = {
          page: this.pageIndex,
          limit: this.pageSize
        }

        // 将查询条件添加到参数中
        if (this.dataForm.billCargoId) params.billCargoId = this.dataForm.billCargoId;
        if (this.dataForm.inoutType) params.inoutType = this.dataForm.inoutType;
        if (this.dataForm.stockId) params.stockId = this.dataForm.stockId;
        if (this.dataForm.stockCode) params.stockCode = this.dataForm.stockCode;
        if (this.dataForm.stockName) params.stockName = this.dataForm.stockName;
        if (this.dataForm.billNumber) params.billNumber = this.dataForm.billNumber;
        if (this.dataForm.cargoValue) params.cargoValue = this.dataForm.cargoValue;
        if (this.dataForm.cargoNum) params.cargoNum = this.dataForm.cargoNum;
        if (this.dataForm.cargoWt) params.cargoWt = this.dataForm.cargoWt;
        if (this.dataForm.cargoVol) params.cargoVol = this.dataForm.cargoVol;

        api.ckbillstockListApi(params).then(res => {
          this.dataListLoading = false
          this.dataList = res.list || [];
          this.totalPage = res.total;
        }).catch(e => {
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除处理（支持单个删除和批量删除）
      deleteHandle (id) {
        let ids = []
        let idText = ''

        // 如果传入了单个id参数，则为单个删除
        if (id) {
          ids = [id]
          idText = id
        } else {
          // 否则为批量删除
          ids = this.dataListSelections.map(item => {
            return item.id
          })
          idText = ids.join(',')
        }

        this.$confirm(`确定进行删除操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          // 调用批量删除API
          api.ckbillstockDeleteApi(ids).then(res => {
            this.$message.success('删除成功')
            this.getDataList()
          })
        })
      },
    }
  }
</script>
