<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="运单号" prop="waybillNo">
        <el-input
          v-model="queryParams.waybillNo"
          placeholder="请输入运单号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="调账类型" prop="adjustmentType">
        <el-select v-model="queryParams.adjustmentType" placeholder="请选择">
          <el-option
              v-for="item in adjustment_type"
              :key="item.value"
              :label="item.label"
              :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="Plus"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['system:adjustmentRecord:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="Edit"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['system:adjustmentRecord:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="Delete"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['system:adjustmentRecord:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:adjustmentRecord:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="adjustmentRecordList" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->
<!--      <el-table-column label="调账流水id" align="center" prop="financialAdjustId" />-->
      <el-table-column label="运单号" align="center" prop="waybillNo" width="160" />
<!--      <el-table-column label="费用明细id" align="center" prop="waybillCostDetailId" />-->
      <el-table-column label="调账类型" align="center" prop="adjustmentType" >
        <template #default="scope">
          <dict-tag :options="adjustment_type" :value="scope.row.adjustmentType" />
        </template>
      </el-table-column>
      <el-table-column label="原金额" align="center" prop="originalAmount" />
      <el-table-column label="调整后金额" align="center" prop="adjustedAmount" />
      <el-table-column label="调整金额" align="center" prop="adjustmentAmount" />
      <el-table-column label="调账原因" align="center" prop="adjustmentReason" />
<!--      <el-table-column label="备注" align="center" prop="remark" />-->
      <el-table-column label="部门" align="center" prop="deptName" />
      <el-table-column label="操作人" align="center" prop="userName" />
<!--      <el-table-column label="部门ID" align="center" prop="deptId" />-->
<!--      <el-table-column label="用户ID" align="center" prop="userId" />-->
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">-->
<!--        <template #default="scope">-->
<!--          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:adjustmentRecord:edit']">修改</el-button>-->
<!--          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:adjustmentRecord:remove']">删除</el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改调账流水记录对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="adjustmentRecordRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="运单号" prop="waybillNo">
          <el-input v-model="form.waybillNo" placeholder="请输入运单号" />
        </el-form-item>
        <el-form-item label="费用明细id" prop="waybillCostDetailId">
          <el-input v-model="form.waybillCostDetailId" placeholder="请输入费用明细id" />
        </el-form-item>
        <el-form-item label="原金额" prop="originalAmount">
          <el-input v-model="form.originalAmount" placeholder="请输入原金额" />
        </el-form-item>
        <el-form-item label="调整后金额" prop="adjustedAmount">
          <el-input v-model="form.adjustedAmount" placeholder="请输入调整后金额" />
        </el-form-item>
        <el-form-item label="调整金额" prop="adjustmentAmount">
          <el-input v-model="form.adjustmentAmount" placeholder="请输入调整金额" />
        </el-form-item>
        <el-form-item label="调账原因" prop="adjustmentReason">
          <el-input v-model="form.adjustmentReason" placeholder="请输入调账原因" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="部门ID" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入部门ID" />
        </el-form-item>
        <el-form-item label="用户ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户ID" />
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="AdjustmentRecord">
import { listAdjustmentRecord, getAdjustmentRecord, delAdjustmentRecord, addAdjustmentRecord, updateAdjustmentRecord } from "@/api/logistics/adjustmentRecord"

const { proxy } = getCurrentInstance()
const { adjustment_type } = proxy.useDict("adjustment_type")

const adjustmentRecordList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    waybillNo: null,
    waybillCostDetailId: null,
    adjustmentType: null,
    originalAmount: null,
    adjustedAmount: null,
    adjustmentAmount: null,
    adjustmentReason: null,
    deptId: null,
    userId: null,
  },
  rules: {
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询调账流水记录列表 */
function getList() {
  loading.value = true
  listAdjustmentRecord(queryParams.value).then(response => {
    adjustmentRecordList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    financialAdjustId: null,
    waybillNo: null,
    waybillCostDetailId: null,
    adjustmentType: null,
    originalAmount: null,
    adjustedAmount: null,
    adjustmentAmount: null,
    adjustmentReason: null,
    createTime: null,
    updateTime: null,
    remark: null,
    deptId: null,
    userId: null,
    delFlag: null,
    createBy: null,
    updateBy: null
  }
  proxy.resetForm("adjustmentRecordRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.financialAdjustId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加调账流水记录"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _financialAdjustId = row.financialAdjustId || ids.value
  getAdjustmentRecord(_financialAdjustId).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改调账流水记录"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["adjustmentRecordRef"].validate(valid => {
    if (valid) {
      if (form.value.financialAdjustId != null) {
        updateAdjustmentRecord(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addAdjustmentRecord(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _financialAdjustIds = row.financialAdjustId || ids.value
  proxy.$modal.confirm('是否确认删除调账流水记录编号为"' + _financialAdjustIds + '"的数据项？').then(function() {
    return delAdjustmentRecord(_financialAdjustIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/adjustmentRecord/export', {
    ...queryParams.value
  }, `adjustmentRecord_${new Date().getTime()}.xlsx`)
}

getList()
</script>
