<!--
 * @Descripttion: (基础资料/库位信息表/bas_bin)
 * @version: (1.0)
 * @Author: (admin)
 * @Date: (2023-04-10)
 * @LastEditors: (admin)
 * @LastEditTime: (2023-04-10)
-->
<template>
  <div>
    <el-form :model="queryParams" label-position="right" inline ref="queryRef" v-show="showSearch" @submit.prevent>
      <el-form-item>
        <el-button icon="search" type="primary" @click="handleQuery">{{ $t('btn.search') }}</el-button>
        <el-button icon="refresh" @click="resetQuery">{{ $t('btn.reset') }}</el-button>
      </el-form-item>
    </el-form>
    <!-- 工具区域 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" v-hasPermi="['business:basbin:add']" plain icon="plus" @click="handleAdd">
          {{ $t('btn.add') }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" :disabled="single" v-hasPermi="['business:basbin:edit']" plain icon="edit" @click="handleUpdate">
          {{ $t('btn.edit') }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" :disabled="multiple" v-hasPermi="['business:basbin:delete']" plain icon="delete" @click="handleDelete">
          {{ $t('btn.delete') }}
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <!-- 数据区域 -->
    <el-table 
      :data="dataList" 
      v-loading="loading" 
      ref="table" 
      border 
      highlight-current-row 
      @sort-change="sortChange" 
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center"/>
      <el-table-column prop="binId" label="库位Id 系统主键" align="center" v-if="columns.showColumn('binId')"/>
      <el-table-column prop="binCode" label="库区编码" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('binCode')"/>
      <el-table-column prop="binName" label="库区名称" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('binName')"/>
      <el-table-column prop="binType" label="库位类型（S存储库位，P拣选库位，V虚拟库位）" align="center" v-if="columns.showColumn('binType')">
        <template #default="scope">
          <dict-tag :options=" options.binTypeOptions" :value="scope.row.binType" />
        </template>
      </el-table-column>
      <el-table-column prop="regionId" label="库区编码" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('regionId')"/>
      <el-table-column prop="binRow" label="库位所在排" align="center" v-if="columns.showColumn('binRow')"/>
      <el-table-column prop="binColumn" label="库位所在列" align="center" v-if="columns.showColumn('binColumn')"/>
      <el-table-column prop="binLayer" label="库位所在层" align="center" v-if="columns.showColumn('binLayer')"/>
      <el-table-column prop="describe" label="描述" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('describe')"/>
      <el-table-column prop="visible" label="显示状态（0显示，1隐藏）" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('visible')"/>
      <el-table-column prop="status" label="使用状态（0正常，1使用）" align="center" v-if="columns.showColumn('status')">
        <template #default="scope">
          <dict-tag :options=" options.statusOptions" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column prop="remark" label="备注" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('remark')"/>
      <el-table-column label="操作" align="center" width="160">
        <template #default="scope">
          <el-button v-hasPermi="['business:basbin:edit']" type="success" icon="edit" title="编辑" @click="handleUpdate(scope.row)"></el-button>
          <el-button v-hasPermi="['business:basbin:delete']" type="danger" icon="delete" title="删除" @click="handleDelete(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination class="mt10" background :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改基础资料/库位信息表对话框 -->
    <el-dialog :title="title" :lock-scroll="false" v-model="open" >
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">
    
          <el-col :lg="12">
            <el-form-item label="库位Id 系统主键" prop="binId">
              <el-input v-model="form.binId" placeholder="请输入库位Id 系统主键" :disabled="opertype != 1"/>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="库区编码" prop="binCode">
              <el-input v-model="form.binCode" placeholder="请输入库区编码" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="库区名称" prop="binName">
              <el-input v-model="form.binName" placeholder="请输入库区名称" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="库位类型（S存储库位，P拣选库位，V虚拟库位）" prop="binType">
              <el-select v-model="form.binType" placeholder="请选择库位类型（S存储库位，P拣选库位，V虚拟库位）">
                <el-option 
                  v-for="item in  options.binTypeOptions" 
                  :key="item.dictValue" :label="item.dictLabel" 
                  :value="item.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="库区编码" prop="regionId">
              <el-input v-model="form.regionId" placeholder="请输入库区编码" />
            </el-form-item>
          </el-col>
    
          <el-col :lg="12">
            <el-form-item label="库位所在排" prop="binRow">
              <el-input v-model="form.binRow" placeholder="请输入库位所在排" />
            </el-form-item>
          </el-col>
    
          <el-col :lg="12">
            <el-form-item label="库位所在列" prop="binColumn">
              <el-input v-model="form.binColumn" placeholder="请输入库位所在列" />
            </el-form-item>
          </el-col>
    
          <el-col :lg="12">
            <el-form-item label="库位所在层" prop="binLayer">
              <el-input v-model="form.binLayer" placeholder="请输入库位所在层" />
            </el-form-item>
          </el-col>

          <el-col :lg="24">
            <el-form-item label="描述" prop="describe">
              <el-input type="textarea" v-model="form.describe" placeholder="请输入描述"/>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="显示状态（0显示，1隐藏）" prop="visible">
              <el-input v-model="form.visible" placeholder="请输入显示状态（0显示，1隐藏）" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="使用状态（0正常，1使用）" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio v-for="item in  options.statusOptions" :key="item.dictValue" :label="item.dictValue">{{item.dictLabel}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="创建者" prop="createBy">
              <el-input v-model="form.createBy" placeholder="请输入创建者" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="创建时间" prop="createTime">
              <el-date-picker v-model="form.createTime" type="datetime" :teleported="false" placeholder="选择日期时间"></el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="更新者" prop="updateBy">
              <el-input v-model="form.updateBy" placeholder="请输入更新者" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="更新时间" prop="updateTime">
              <el-date-picker v-model="form.updateTime" type="datetime" :teleported="false" placeholder="选择日期时间"></el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer v-if="opertype != 3">
        <el-button text @click="cancel">{{ $t('btn.cancel') }}</el-button>
        <el-button type="primary" @click="submitForm">{{ $t('btn.submit') }}</el-button>
      </template>
    </el-dialog>

  </div>
</template>

<script setup name="basbin">
import { listBasBin, addBasBin, delBasBin, updateBasBin, getBasBin, 
 
 } 
from '@/api/business/basbin.js'

const { proxy } = getCurrentInstance()
// 选中binId数组数组
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const loading = ref(false)
const showSearch = ref(true)
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  sort: '',
  sortType: 'asc',
})
const columns = ref([
  { visible: true, prop: 'binId', label: '库位Id 系统主键' },
  { visible: true, prop: 'binCode', label: '库区编码' },
  { visible: true, prop: 'binName', label: '库区名称' },
  { visible: true, prop: 'binType', label: '库位类型（S存储库位，P拣选库位，V虚拟库位）' },
  { visible: true, prop: 'regionId', label: '库区编码' },
  { visible: true, prop: 'binRow', label: '库位所在排' },
  { visible: true, prop: 'binColumn', label: '库位所在列' },
  { visible: true, prop: 'binLayer', label: '库位所在层' },
  { visible: false, prop: 'describe', label: '描述' },
  { visible: false, prop: 'visible', label: '显示状态（0显示，1隐藏）' },
  { visible: false, prop: 'status', label: '使用状态（0正常，1使用）' },
  { visible: false, prop: 'remark', label: '备注' },
])
const total = ref(0)
const dataList = ref([])
const queryRef = ref()
const defaultTime = ref([new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 2, 1, 23, 59, 59)])


var dictParams = [
]


function getList(){
  loading.value = true
  listBasBin(queryParams).then(res => {
    const { code, data } = res
    if (code == 200) {
      dataList.value = data.result
      total.value = data.totalNum
      loading.value = false
    }
  })
}

// 查询
function handleQuery() {
  queryParams.pageNum = 1
  getList()
}

// 重置查询操作
function resetQuery(){
  proxy.resetForm("queryRef")
  handleQuery()
}

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

// 自定义排序
function sortChange(column) {
  var sort = undefined
  var sortType = undefined

  if (column.prop != null && column.order != null) {
    sort = column.prop
    sortType = column.order

  }
  queryParams.sort = sort
  queryParams.sortType = sortType
  handleQuery()
}

/*************** form操作 ***************/
const formRef = ref()
const title = ref("")
// 操作类型 1、add 2、edit 3、view
const opertype = ref(0)
const open = ref(false)
const state = reactive({
  form: {},
  rules: {
    binId: [{ required: true, message: "库位Id 系统主键不能为空", trigger: "blur", type: "number" }],
    binCode: [{ required: true, message: "库区编码不能为空", trigger: "blur" }],
    binType: [{ required: true, message: "库位类型（S存储库位，P拣选库位，V虚拟库位）不能为空", trigger: "change" }],
    regionId: [{ required: true, message: "库区编码不能为空", trigger: "blur" }],
    binRow: [{ required: true, message: "库位所在排不能为空", trigger: "blur", type: "number" }],
    binColumn: [{ required: true, message: "库位所在列不能为空", trigger: "blur", type: "number" }],
    binLayer: [{ required: true, message: "库位所在层不能为空", trigger: "blur", type: "number" }],
    visible: [{ required: true, message: "显示状态（0显示，1隐藏）不能为空", trigger: "blur" }],
    status: [{ required: true, message: "使用状态（0正常，1使用）不能为空", trigger: "blur" }],
  },
  options: {
    // 库位类型（S存储库位，P拣选库位，V虚拟库位） 选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0'}
    binTypeOptions: [],
    // 使用状态（0正常，1使用） 选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0'}
    statusOptions: [],
  }
})

const { form, rules, options } = toRefs(state)

// 关闭dialog
function cancel(){
  open.value = false
  reset()
}

// 重置表单
function reset() {
  form.value = {
    binId: undefined,
    binCode: undefined,
    binName: undefined,
    binType: undefined,
    regionId: undefined,
    binRow: undefined,
    binColumn: undefined,
    binLayer: undefined,
    describe: undefined,
    visible: undefined,
    status: undefined,
    createBy: undefined,
    createTime: undefined,
    updateBy: undefined,
    updateTime: undefined,
    remark: undefined,
  };
  proxy.resetForm("formRef")
}

// 添加按钮操作
function handleAdd() {
  reset();
  open.value = true
  title.value = '添加'
  opertype.value = 1
}

// 修改按钮操作
function handleUpdate(row) {
  reset()
  const id = row.binId || ids.value
  getBasBin(id).then((res) => {
    const { code, data } = res
    if (code == 200) {
      open.value = true
      title.value = "修改数据"
      opertype.value = 2

      form.value = {
        ...data,
      }
    }
  })
}

// 添加&修改 表单提交
function submitForm() {
  proxy.$refs["formRef"].validate((valid) => {
    if (valid) {
      if (form.value.binId != undefined && opertype.value === 2) {
        updateBasBin(form.value).then((res) => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
        .catch(() => {})
      } else {
        addBasBin(form.value).then((res) => {
            proxy.$modal.msgSuccess("新增成功")
            open.value = false
            getList()
          })
          .catch(() => {})
      }
    }
  })
}

// 删除按钮操作
function handleDelete(row) {
  const Ids = row.binId || ids.value

  proxy
    .$confirm('是否确认删除参数编号为"' + Ids + '"的数据项？')
    .then(function () {
      return delBasBin(Ids)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess("删除成功")
    })
    .catch(() => {})
}




handleQuery()
</script>