Newer
Older
HuangJiPC / src / pages / views / publicService / wenJuan.vue
@zhangdeliang zhangdeliang on 21 Jun 8 KB update
<template>
  <!-- 问卷调查 -->
  <div class="wenjuan">
    <div class="searchBoxs">
      <n-space>
        <n-input v-model:value="searchVal1" clearable placeholder="请输入问卷调查标题" />
        <n-button type="primary" @click="handleClick('search')">
          <template #icon>
            <n-icon><Search /></n-icon>
          </template>
          搜索
        </n-button>
        <n-button type="primary" @click="handleClick('add')">
          <template #icon>
            <n-icon><Add /></n-icon>
          </template>
          新增问题
        </n-button>
      </n-space>
    </div>
    <!-- 表格 -->
    <div class="tableBox">
      <n-data-table
        :bordered="false"
        :max-height="700"
        striped
        :columns="columns"
        :data="tableData"
        :loading="tableLoading"
        :remote="true"
        :pagination="pagination"
      >
      </n-data-table>
    </div>
    <!-- 新增修改弹窗 -->
    <n-modal
      :title="modalTitle"
      :mask-closable="false"
      preset="dialog"
      :show-icon="false"
      :style="{ width: '600px' }"
      v-model:show="modalShow"
    >
      <n-form ref="formRef" :label-width="120" :rules="formInfo.rules" :model="formInfo.data" label-placement="left">
        <n-form-item label="问题标题:" path="title">
          <n-input v-model:value="formInfo.data.title" placeholder="请输入问题标题" />
        </n-form-item>
        <n-form-item label="问题类型:" path="quesType">
          <n-select v-model:value="formInfo.data.quesType" :options="linkTypes" placeholder="请选择" />
        </n-form-item>
        <n-form-item label="答案A(10分):" v-if="formInfo.data.quesType == 0" path="a">
          <n-input v-model:value="formInfo.data.a" placeholder="请输入" />
        </n-form-item>
        <n-form-item label="答案B(7分):" v-if="formInfo.data.quesType == 0" path="b">
          <n-input v-model:value="formInfo.data.b" placeholder="请输入" />
        </n-form-item>
        <n-form-item label="答案C(4分):" v-if="formInfo.data.quesType == 0" path="c">
          <n-input v-model:value="formInfo.data.c" placeholder="请输入" />
        </n-form-item>
        <!-- <n-form-item label="答案D:"
                     path="d">
          <n-input v-model:value="formInfo.data.d"
                   placeholder="请输入" />
        </n-form-item> -->
      </n-form>
      <template #action>
        <n-space>
          <n-button @click="() => (modalShow = false)">取消</n-button>
          <n-button type="primary" @click="handleClick('submit')">确定 </n-button>
        </n-space>
      </template>
    </n-modal>
  </div>
</template>

<script>
import { toRefs, onMounted, reactive, h, ref } from 'vue';
import { Search, Add } from '@vicons/ionicons5';
import { NButton, NImage } from 'naive-ui';
import { wjdcSearch, wjdcSave, wjdcUpdate, wjdcDelete } from '@/services';
import { resetForm } from '@/utils/util';

export default {
  name: 'wenjuan',
  components: { Search, Add, NButton },
  setup() {
    const allData = reactive({
      searchVal1: null,
      modalTitle: '新增',
      modalShow: false,
      linkTypes: [
        { value: 0, label: '单选' },
        { value: 1, label: '简答题' },
      ],
      formInfo: {
        data: {
          a: '',
          b: '',
          c: '',
          d: '',
          quesType: 0,
          title: '',
        },
        rules: {
          title: {
            required: true,
            trigger: ['blur'],
            message: '请输入',
          },
        },
      },

      tableLoading: true,
      tableData: [],
      columns: [
        {
          title: '序号',
          align: 'center',
          width: '80',
          render(row, index) {
            return (paginationReactive.page - 1) * paginationReactive.pageSize + index + 1;
          },
        },
        { title: '问题标题', align: 'center', key: 'title' },
        {
          title: '问题类型',
          align: 'center',
          key: 'quesType',
          render(row) {
            return row.quesType == 0 ? '单选' : '简答题';
          },
        },
        { title: '创建时间', align: 'center', key: 'createTime' },
        { title: '修改时间', align: 'center', key: 'updateTime' },
        {
          title: '操作',
          key: 'actions',
          width: '220',
          align: 'center',
          render(row) {
            const btn = allData.actionColumn.map((item, index) => {
              return h(
                NButton,
                {
                  text: true,
                  size: item.size,
                  style: {
                    margin: '10px',
                  },
                  type: item.btnType,
                  onClick: () => handleClick(item.type, row),
                },
                { default: () => item.default }
              );
            });
            return btn;
          },
        },
      ],
      actionColumn: [
        {
          size: 'small',
          btnType: 'primary',
          type: 'edit',
          default: '修改',
        },
        {
          size: 'small',
          btnType: 'error',
          type: 'delete',
          default: '删除',
        },
      ],
    });
    //分页
    const paginationReactive = reactive({
      page: 1,
      pageSize: 10,
      showSizePicker: true,
      pageSizes: [10, 20, 50],
      showQuickJumper: true,
      pageCount: 0,
      itemCount: 0,
      prefix: () => {
        return '共 ' + paginationReactive.itemCount + ' 项';
      },
      onChange: (page) => {
        paginationReactive.page = page;
        getTableData();
      },
      onPageSizeChange: (pageSize) => {
        paginationReactive.pageSize = pageSize;
        paginationReactive.page = 1;
        getTableData();
      },
    });
    const getTableData = async () => {
      allData.tableLoading = true;
      let pramas = {
        current: paginationReactive.page,
        size: paginationReactive.pageSize,
        title: allData.searchVal1,
      };
      let res = await wjdcSearch(pramas);
      if (res && res.code == 200) {
        allData.tableData = res.data.records;
        paginationReactive.pageCount = res.data.pages;
        paginationReactive.itemCount = res.data.total;
      }
      allData.tableLoading = false;
    };
    const formRef = ref(null);
    // 点击事件
    const handleClick = async (type, row) => {
      switch (type) {
        case 'search':
          paginationReactive.page = 1;
          getTableData();
          break;
        case 'add':
          allData.modalTitle = '新增';
          resetForm(allData.formInfo.data);
          allData.modalShow = true;
          allData.formInfo.data.quesType = 0;
          break;
        case 'edit':
          allData.modalTitle = '修改';
          allData.formInfo.data = { ...row };
          allData.modalShow = true;
          break;
        case 'submit':
          formRef.value.validate((errors) => {
            if (!errors) {
              submitData();
            } else {
              $message.error('验证失败,请检查必填项');
            }
          });
          break;
        case 'delete':
          $dialog.info({
            title: '提示',
            content: `确定删除该数据吗?`,
            positiveText: '确定',
            negativeText: '取消',
            onPositiveClick: () => {
              let ids = [row.id];
              dataDel(ids);
            },
          });
          break;
      }
    };
    // 删除数据
    async function dataDel(ids) {
      let res = await wjdcDelete({ ids: ids });
      if (res && res.code === 200) {
        $message.success('删除成功');
        getTableData();
      }
    }
    // 提交数据
    async function submitData() {
      let params = { ...allData.formInfo.data };
      if (allData.modalTitle == '新增') {
        let res = await wjdcSave(params);
        if (res && res.code == 200) {
          $message.success('操作成功');
          getTableData();
          allData.modalShow = false;
        }
      } else {
        let res = await wjdcUpdate(params);
        if (res && res.code == 200) {
          $message.success('操作成功');
          getTableData();
          allData.modalShow = false;
        }
      }
    }
    onMounted(() => {
      getTableData();
    });
    return {
      ...toRefs(allData),
      pagination: paginationReactive,
      handleClick,
      getTableData,
      formRef,
    };
  },
};
</script>

<style lang="less" scoped>
.wenjuan {
  width: 100%;
  .searchBoxs {
    margin: 10px;
  }
}
</style>