<!--
 * @Descripttion: (基础资料/库区信息表/bas_region)
 * @version: (1.0)
 * @Author: (admin)
 * @Date: (2023-04-11)
 * @LastEditors: (admin)
 * @LastEditTime: (2023-04-11)
-->
<template>
  <div>
    <el-form :model="queryParams" label-position="right" inline ref="queryRef" v-show="showSearch" @submit.prevent
      size="large">
      <el-row>
        <el-form-item label="库区编码" prop="regionCode">
          <el-input v-model="queryParams.regionCode" placeholder="请输入库区编码" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="库区名称" prop="regionName">
          <el-input v-model="queryParams.regionName" placeholder="请输入库区名称" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="显示状态" prop="visible">
          <!-- <el-select v-model="queryParams.visible" placeholder="请选择" clearable style="width:120px" @change="handleQuery">
            <el-option v-for="dict in options.visibleOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue">
              <dict-tag :options="options.visibleOptions" :value="dict.dictValue" />
            </el-option>
          </el-select> -->
          <el-radio-group v-model="queryParams.visible" @change="handleQuery">
            <el-radio-button>全部</el-radio-button>
            <el-radio-button v-for="dict in options.visibleOptions" :label="dict.dictValue">
              {{ dict.dictLabel }}
            </el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="使用标识" prop="status">
          <!-- <el-select v-model="queryParams.status" placeholder="请选择" clearable style="width:120px" @change="handleQuery">
            <el-option v-for="dict in options.statusOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue">
              <dict-tag :options="options.statusOptions" :value="dict.dictValue" />
            </el-option>
          </el-select> -->
          <el-radio-group v-model="queryParams.status" @change="handleQuery">
            <el-radio-button>全部</el-radio-button>
            <el-radio-button v-for="dict in options.statusOptions" :label="dict.dictValue">
              {{ dict.dictLabel }}
            </el-radio-button>
          </el-radio-group>
        </el-form-item>
        <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-row>
    </el-form>
    <!-- 工具区域 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" v-hasPermi="['business:basregion: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:basregion: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:basregion:delete']" plain icon="delete"
          @click="handleDelete">
          {{ $t('btn.delete') }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['business:region:export']">
          {{ $t('btn.export') }}
        </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" highlight-current-row @sort-change="sortChange"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column type="index" label="序号" width="50" align="center" />
      <el-table-column prop="regionId" label="ID" align="center" v-if="columns.showColumn('regionId')" />
      <el-table-column prop="regionCode" label="库区编码" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('regionCode')" />
      <el-table-column prop="regionName" label="库区名称" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('regionName')" />
      <el-table-column prop="describe" label="描述" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('describe')" />
      <el-table-column prop="visible" label="显示状态" align="center" width="80" v-if="columns.showColumn('visible')">
        <template #default="scope">
          <dict-tag :options="options.visibleOptions" :value="scope.row.visible" />
        </template>
      </el-table-column>
      <el-table-column prop="status" label="使用状态" align="center" width="80" 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="createBy" label="创建人" align="center" v-if="columns.showColumn('createBy')" />
      <el-table-column prop="createTime" label="创建时间" align="center" width="160"
        v-if="columns.showColumn('createTime')" />
      <el-table-column prop="updateBy" label="更新人" align="center" v-if="columns.showColumn('updateBy')" />
      <el-table-column prop="updateTime" label="更新时间" align="center" width="160"
        v-if="columns.showColumn('updateTime')" />
      <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:basregion:edit']" type="success" plain icon="edit" title="编辑"
            @click="handleUpdate(scope.row)"></el-button>
          <el-button v-hasPermi="['business:basregion:delete']" type="danger" plain 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" size="large">
        <el-row :gutter="20">

          <!-- <el-col :lg="12">
            <el-form-item label="ID" prop="regionId">
              <el-input v-model="form.regionId" disabled />
            </el-form-item>
          </el-col> -->

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

          <el-col :lg="12">
            <el-form-item label="库区名称" prop="regionName">
              <el-input v-model="form.regionName" 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="显示状态" prop="visible">
              <el-radio-group v-model="form.visible">
                <el-radio-button v-for="item in options.visibleOptions" :key="item.dictValue" :label="item.dictValue">{{
                  item.dictLabel }}</el-radio-button>
              </el-radio-group>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="使用状态" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio-button v-for="item in options.statusOptions" :key="item.dictValue" :label="item.dictValue">{{
                  item.dictLabel }}</el-radio-button>
              </el-radio-group>
            </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="basregion">
import {
  listBasRegion, addBasRegion, delBasRegion, updateBasRegion, getBasRegion, exportBasRegion
}
  from '@/api/business/basregion'
import { formatDate } from '@vueuse/shared';

const { proxy } = getCurrentInstance()

// 选中regionId数组数组
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: false, prop: 'regionId', label: 'ID' },
  { visible: true, prop: 'regionCode', label: '库区编码' },
  { visible: true, prop: 'regionName', label: '库区名称' },
  { visible: true, prop: 'describe', label: '描述' },
  { visible: true, prop: 'visible', label: '显示状态' },
  { visible: true, prop: 'status', label: '使用状态' },
  { visible: false, prop: 'remark', label: '备注' },
  { visible: true, prop: 'createBy', label: '创建人' },
  { visible: true, prop: 'createTime', label: '创建时间' },
  { visible: true, prop: 'updateBy', label: '更新人' },
  { visible: true, prop: 'updateTime', 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)])



function getList() {
  loading.value = true
  listBasRegion(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.regionId);
  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: {
    regionId: [{ required: true, message: "库区ID 系统主键不能为空", trigger: "blur", type: "number" }],
    regionCode: [{ required: true, message: "库区编码不能为空", trigger: "blur" }],
    visible: [{ required: true, message: "显示状态不能为空", trigger: "blur" }],
    status: [{ required: true, message: "使用状态不能为空", trigger: "blur" }],
  },
  options: {
    statusOptions: [],// 状态数据字典
    visibleOptions: [],// 显示数据字典
  }
})

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

var dictParams = [
  { dictType: 'sys_normal_disable', columnName: 'statusOptions' },
  { dictType: 'sys_show_hide', columnName: 'visibleOptions' },
]
proxy.getDicts(dictParams).then((response) => {
  response.data.forEach((element) => {
    state.options[element.columnName] = element.list
  })
})

// 关闭dialog
function cancel() {
  open.value = false
  reset()
}
// 重置表单
function reset() {
  form.value = {
    // regionId: parseInt(formatDate(new Date(), "YYYYMMDDHHmmss")),
    regionCode: undefined,
    regionName: undefined,
    describe: undefined,
    visible: "0",
    status: "0",
    // 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.regionId || ids.value
  getBasRegion(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.regionId != undefined && opertype.value === 2) {
        updateBasRegion(form.value).then((res) => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
          .catch(() => { })
      } else {
        addBasRegion(form.value).then((res) => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
          .catch(() => { })
      }
    }
  })
}

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

  proxy
    .$confirm('是否确认删除参数编号为"' + Ids + '"的数据项？')
    .then(function () {
      return delBasRegion(Ids)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess("删除成功")
    })
    .catch(() => { })
}
// 导出按钮操作
function handleExport() {
  proxy
    .$confirm('是否确认导出当前库区信息?', '警告', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    })
    .then(async () => {
      await exportBasRegion(queryParams)
    })
}
handleQuery()
</script>
