Newer
Older
KaiFengPC / src / views / preassess / removalRate / tableDalgo.vue
@zhangdeliang zhangdeliang on 23 May 10 KB 初始化项目
<template>
  <div class="btns" v-show="opts !== 'view'">
    <el-button type="primary" plain icon="Plus" @click="openDialog({}, 'add')">新增</el-button>
    <el-button type="primary" plain icon="Check" @click="submit2">保存</el-button>
  </div>
  <el-table class="table" :data="tableData" v-loading="loading" max-height="680" element-loading-text="数据加载中...">
    <el-table-column type="index" width="55" label="序号" />
    <el-table-column label="海绵设施" prop="itemName" show-overflow-tooltip />
    <el-table-column label="设施类型" prop="itemType" show-overflow-tooltip width="100">
      <template #default="{ row }">
        <span>{{ findItemTypeText(row.itemType) }}</span>
      </template>
    </el-table-column>
    <el-table-column label="下限值" prop="downValue" show-overflow-tooltip />
    <el-table-column label="上限值" prop="upValue" show-overflow-tooltip />
    <el-table-column label="造价下限值" prop="costLower" show-overflow-tooltip width="90" />
    <el-table-column label="造价上限值" prop="costUpper" show-overflow-tooltip width="90" />
    <el-table-column label="单位" prop="units" show-overflow-tooltip />
    <el-table-column label="是否下沉式绿地" prop="sinkGreenFlag" width="120">
      <template #default="{ row }">
        <span>{{ row.sinkGreenFlag === '1' ? '是' : '否' }}</span>
      </template>
    </el-table-column>
    <template v-if="opts !== 'add'">
      <el-table-column label="修改时间" prop="updateTime" show-overflow-tooltip width="160" />
      <el-table-column label="修改人" prop="updateBy" show-overflow-tooltip />
    </template>
    <el-table-column label="排序" prop="orderNo" show-overflow-tooltip />
    <template v-if="opts !== 'view'">
      <el-table-column label="操作" show-overflow-tooltip width="150">
        <template #default="{ row, index }">
          <el-button type="primary" link size="small" @click="openDialog(row, 'view')">查看</el-button>
          <el-button type="primary" link size="small" @click="openDialog(row, 'edit')">修改</el-button>
          <el-button type="danger" link size="small" @click="del(row, index)">删除</el-button>
        </template>
      </el-table-column>
    </template>
  </el-table>
  <el-dialog
    v-model="visible"
    :title="`${configOpts.text}海绵设施系数`"
    :close-on-click-modal="false"
    width="50%"
    :before-close="closeDialog"
    class="innerDialog"
  >
    <el-form ref="ruleForm" :model="form" :rules="rules">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="海绵设施名称:" prop="itemName" class="formItem">
            <el-input v-model="form.itemName" :disabled="configOpts.type === 'view'" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设施类型:" prop="itemType" class="formItem">
            <el-select v-model="form.itemType" placeholder="请选择" style="width: 100%" :disabled="configOpts.type === 'view'">
              <el-option v-for="dict in sponge_facilities_type" :key="dict.value" :label="dict.label" :value="dict.value" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="径流系数下限:" prop="downValue" class="formItem">
            <el-input v-model="form.downValue" placeholder="请输入0-1的值" :disabled="configOpts.type === 'view'" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="径流系数上限:" prop="upValue" class="formItem">
            <el-input v-model="form.upValue" placeholder="请输入0-1的值" :disabled="configOpts.type === 'view'" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="造价下限值:" prop="costLower" class="formItem">
            <el-input v-model="form.costLower" placeholder="请输入造价下限值" :disabled="configOpts.type === 'view'" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="造价上限值:" prop="costUpper" class="formItem">
            <el-input v-model="form.costUpper" placeholder="请输入造价上限值" :disabled="configOpts.type === 'view'" /> </el-form-item
        ></el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="单位:" prop="units" class="formItem">
            <el-select v-model="form.units" placeholder="请选择" style="width: 100%" :disabled="configOpts.type === 'view'">
              <el-option label="m" value="m" />
              <el-option label="㎡" value="㎡" />
              <el-option label="m³" value="m³" />
            </el-select>
            <!-- <el-input v-model="form.units" placeholder="请输入单位" :disabled="configOpts.type === 'view'" /> -->
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否下沉式绿地:" prop="sinkGreenFlag" class="formItem">
            <el-select v-model="form.sinkGreenFlag" placeholder="请选择" style="width: 100%" :disabled="configOpts.type === 'view'">
              <el-option label="是" value="1" />
              <el-option label="否" value="0" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="排序:" prop="orderNo" class="formItem">
            <el-input v-model="form.orderNo" placeholder="请输入排序" :disabled="configOpts.type === 'view'" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer v-if="configOpts.type !== 'view'">
      <div class="dialog-footer">
        <el-button type="primary" @click="submit">保存</el-button>
        <el-button @click="closeDialog">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import {
  getFacilitiesPolluteRemoveConfigItemList,
  facilitiesPolluteRemoveConfigItemAdd,
  facilitiesPolluteRemoveConfigAddWithItem,
  facilitiesPolluteRemoveConfigItemEdit,
  facilitiesPolluteRemoveConfigItemDel,
} from '@/api/preassess/removalRate.js';
import { ref, reactive, onMounted } from 'vue';
import { inheritAttr } from '@/utils/v3';
import { optTextMap } from '@/utils/map';
import { validate, orderNoValidate } from './validate';
import { isNumber, required } from '@/utils/validate-helper';
const { proxy } = getCurrentInstance();

const props = defineProps({
  id: {
    type: [String, Number],
    default: '',
  },
  status: {
    type: String,
    default: ''
  },
  opts: {
    type: String,
    default: '',
  },
});
const emit = defineEmits(['close']);

const { id, status, opts } = props;
const loading = ref(false);
const tableData = ref([]);
const visible = ref(false);
const form = reactive({
  itemName: '',
  itemType: '',
  downValue: '',
  upValue: '',
  orderNo: '',
  sinkGreenFlag: '',
  costLower: '',
  costUpper: '',
  units: '',
});

const { sponge_facilities_type } = proxy.useDict('sponge_facilities_type');
const findItemTypeText = itemType => {
  const item = sponge_facilities_type.value.find(it => it.value === itemType);
  return item?.label || '';
};

const rules = reactive({
  itemName: [{ required: true, message: '海绵设施名称不能为空', trigger: ['blur', 'change'] }],
  itemType: [{ required: true, message: '设施类型不能为空', trigger: ['blur', 'change'] }],
  downValue: [{ required: true, validator: validate, trigger: ['blur', 'change'] }],
  upValue: [{ required: true, validator: validate, trigger: ['blur', 'change'] }],
  sinkGreenFlag: [{ required: true, message: '是否下沉式绿地不能为空', trigger: ['blur', 'change'] }],
  orderNo: [{ required: true, validator: orderNoValidate, trigger: ['blur', 'change'] }],
  costLower: isNumber('造价下限值', { required: true, min: 0 }),
  costUpper: isNumber('造价上限值', { required: true, min: 0 }),
  units: required('单位'),
});

let configOpts = reactive({
  type: '',
  text: '',
});
let row = {};

const getTableList = async () => {
  loading.value = true;
  const res = await getFacilitiesPolluteRemoveConfigItemList({
    configId: id,
  });
  loading.value = false;
  if (res?.code === 200) {
    tableData.value = res.data;
  }
};

const openDialog = (data, opt) => {
  row = data;
  configOpts.type = opt;
  configOpts.text = optTextMap.get(opt);
  visible.value = true;
  nextTick(() => {
    inheritAttr(data, form);
    console.log(form);
  });
};

const closeDialog = () => {
  proxy.$refs.ruleForm.resetFields();
  visible.value = false;
};

const submit = () => {
  proxy.$refs.ruleForm.validate(async (valid, fields) => {
    if (valid) {
      if (opts === 'add') {
        if (configOpts.type === 'add') {
          tableData.value.push({ ...form });
        } else {
          inheritAttr(form, row);
        }
        proxy.$modal.msgSuccess('操作成功!');
      } else {
        let method = configOpts.type === 'add' ? facilitiesPolluteRemoveConfigItemAdd : facilitiesPolluteRemoveConfigItemEdit;
        const params = { ...form, status };
        if (configOpts.type === 'edit') {
          params.id = row.id;
        } else {
          params.configId = id;
        }
        const res = await method(params);
        if (res?.code !== 200) return;
        proxy.$modal.msgSuccess('操作成功!');
        getTableList();
      }
      closeDialog();
    } else {
      console.log('error submit!', fields);
    }
  });
};

const submit2 = async () => {
  if (opts === 'add') {
    const res = await facilitiesPolluteRemoveConfigAddWithItem(tableData.value);
    if (res?.code !== 200) return;
    emit('close', opts);
  } else {
    emit('close', opts);
  }
};

const del = (row, index) => {
  proxy.$modal
    .confirm('是否确认删除?')
    .then(async () => {
      if (opts === 'add') {
        tableData.value.splice(index, 1);
      } else {
        const res = await facilitiesPolluteRemoveConfigItemDel(row.id);
        if (res?.code !== 200) return;
        proxy.$modal.msgSuccess('操作成功!');
        getTableList();
      }
    })
    .catch(() => {});
};

onMounted(() => {
  if (id) getTableList();
});
</script>

<style lang="scss" scoped>
.btns {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 15px;
}
:deep(.formItem) {
  label {
    width: 136px;
    word-break: keep-all;
  }
}
</style>