<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="业务编码" prop="bizCode">
        <el-input v-model="queryParams.bizCode" placeholder="请输入业务编码" clearable style="width: 200px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="业务名称" prop="bizName">
        <el-input v-model="queryParams.bizName" placeholder="请输入业务名称" clearable style="width: 200px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="业务类型" prop="bizType">
        <el-select v-model="queryParams.bizType" placeholder="请选择" clearable style="width: 150px">
          <el-option label="水位" value="水位" />
          <el-option label="水质" value="水质" />
          <el-option label="调度" value="调度" />
          <el-option label="工程" value="工程" />
        </el-select>
      </el-form-item>
      <el-form-item label="提供单位" prop="bizUnit">
        <el-select v-model="queryParams.bizUnit" placeholder="请选择" clearable style="width: 200px">
          <el-option v-for="unit in unitList" :key="unit.unitCode" :label="unit.unitName" :value="unit.unitCode" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="bizState">
        <el-select v-model="queryParams.bizState" placeholder="请选择" clearable style="width: 120px">
          <el-option label="正常" value="0" />
          <el-option label="停用" value="1" />
        </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="['gx:biz:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['gx:biz:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['gx:biz:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="bizList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="排序" prop="orderNum" width="80" align="center" />
      <el-table-column label="业务编码" prop="bizCode" width="160" />
      <el-table-column label="业务名称" prop="bizName" :show-overflow-tooltip="true" />
      <el-table-column label="业务类型" prop="bizType" width="100" align="center" />
      <el-table-column label="提供单位" prop="bizUnit" :show-overflow-tooltip="true">
        <template #default="scope">
          {{ getUnitName(scope.row.bizUnit) }}
        </template>
      </el-table-column>
      <el-table-column label="数据频率" prop="dataFreq" width="100" align="center" />
      <el-table-column label="数据说明" prop="bizNote" :show-overflow-tooltip="true" />
      <el-table-column label="状态" prop="bizState" width="80" align="center">
        <template #default="scope">
          <el-tag :type="scope.row.bizState === '0' ? 'success' : 'danger'" size="small">
            {{ scope.row.bizState === '0' ? '正常' : '停用' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" prop="createTime" width="170" align="center" />
      <el-table-column label="操作" width="180" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['gx:biz:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['gx:biz: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="600px" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="业务编码" prop="bizCode">
          <el-input v-model="form.bizCode" placeholder="请输入业务编码(如TBA_SW_1H2H)" />
        </el-form-item>
        <el-form-item label="显示排序" prop="orderNum">
          <el-input-number v-model="form.orderNum" controls-position="right" :min="0" />
        </el-form-item>
        <el-form-item label="业务名称" prop="bizName">
          <el-input v-model="form.bizName" placeholder="请输入业务名称" />
        </el-form-item>
        <el-form-item label="业务类型" prop="bizType">
          <el-select v-model="form.bizType" placeholder="请选择">
            <el-option label="水位" value="水位" />
            <el-option label="水质" value="水质" />
            <el-option label="调度" value="调度" />
            <el-option label="工程" value="工程" />
          </el-select>
        </el-form-item>
        <el-form-item label="提供单位" prop="bizUnit">
          <el-select v-model="form.bizUnit" placeholder="请选择提供单位">
            <el-option v-for="unit in unitList" :key="unit.unitCode" :label="unit.unitName" :value="unit.unitCode" />
          </el-select>
        </el-form-item>
        <el-form-item label="实现方式" prop="bizImplType">
          <el-select v-model="form.bizImplType" placeholder="请选择">
            <el-option label="API接口" value="API" />
            <el-option label="文件传输" value="FILE" />
            <el-option label="数据库直连" value="DB" />
          </el-select>
        </el-form-item>
        <el-form-item label="接口地址" prop="bizImplUrl">
          <el-input v-model="form.bizImplUrl" placeholder="请输入接口地址" />
        </el-form-item>
        <el-form-item label="数据频率" prop="dataFreq">
          <el-select v-model="form.dataFreq" placeholder="请选择">
            <el-option label="实时" value="实时" />
            <el-option label="小时" value="小时" />
            <el-option label="日" value="日" />
            <el-option label="月" value="月" />
          </el-select>
        </el-form-item>
        <el-form-item label="数据说明" prop="bizNote">
          <el-input v-model="form.bizNote" type="textarea" placeholder="请输入数据说明" />
        </el-form-item>
        <el-form-item label="状态" prop="bizState">
          <el-radio-group v-model="form.bizState">
            <el-radio value="0">正常</el-radio>
            <el-radio value="1">停用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" 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="GxBiz">
import { listBiz, getBiz, addBiz, updateBiz, delBiz } from "@/api/gx/biz";
import { listUnit } from "@/api/gx/unit";

const { proxy } = getCurrentInstance();

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

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    bizCode: undefined,
    bizName: undefined,
    bizType: undefined,
    bizState: undefined,
  },
  rules: {
    bizCode: [{ required: true, message: "业务编码不能为空", trigger: "blur" }],
    bizName: [{ required: true, message: "业务名称不能为空", trigger: "blur" }],
    bizType: [{ required: true, message: "业务类型不能为空", trigger: "change" }],
  },
});

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

/** 查询业务域列表 */
function getList() {
  loading.value = true;
  listBiz(queryParams.value).then((response) => {
    bizList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

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

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

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

/** 重置表单 */
function reset() {
  form.value = {
    id: undefined,
    bizCode: undefined,
    orderNum: undefined,
    bizName: undefined,
    bizType: undefined,
    bizUnit: undefined,
    bizImplType: undefined,
    bizImplUrl: undefined,
    bizState: "0",
    dataFreq: undefined,
    bizNote: undefined,
    remark: undefined,
  };
  proxy.resetForm("formRef");
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "新增业务域";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const id = row.id || ids.value;
  getBiz(id).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改业务域";
  });
}

/** 取消按钮操作 */
function cancel() {
  open.value = false;
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["formRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != undefined) {
        updateBiz(form.value).then(() => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addBiz(form.value).then(() => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const deleteIds = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除业务域编号为"' + deleteIds + '"的数据项？').then(() => {
    return delBiz(deleteIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download("/gx/biz/export", { ...queryParams.value }, `biz_${new Date().getTime()}.xlsx`);
}

function getUnitName(unitCode) {
  const unit = unitList.value.find((u) => u.unitCode === unitCode);
  return unit ? unit.unitName : unitCode;
}

function getUnitList() {
  listUnit({}).then((response) => {
    unitList.value = response.rows;
  });
}

getList();
getUnitList();
</script>
