<template>
  <div class="app-container">
    <!-- 表单筛选 -->
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      inline
      v-show="showSearch"
      label-width="80px"
    >
      <el-form-item label="问题类型">
        <el-select
          v-model="queryParams.type"
          placeholder="请选择状态"
          clearable
        >
          <el-option
            v-for="dict in typeOption"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="问题描述">
        <el-input
          v-model="queryParams.descripiton"
          placeholder="请输入问题描述"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 100%"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @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="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="selectedRow.length < 1"
          @click="handleDelete"
          >删除</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-upload"
          size="mini"
          @click="handleImport"
          >导入</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getTableList"
      ></right-toolbar>
    </el-row>
    <!-- 表格 -->
    <el-table
      v-loading="loading"
      :data="tableList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="问题类型" align="center" prop="type">
        <template slot-scope="scope">
          {{ scope.row.type === "1" ? "单选" : "多选" }}
        </template>
      </el-table-column>
      <el-table-column label="问题描述" align="center" prop="descripiton" show-overflow-tooltip />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleDetail(scope.row)"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getTableList"
    />
    <!-- 新增编辑弹窗 -->
    <el-dialog
      :title="title"
      :visible.sync="dlgVisible"
      append-to-body
      width="40%"
    >
      <AddAndEdit
        :loading="dlgLoading"
        :data="formData"
        :dlgType="dlgType"
        @confirm="handleConfirm"
        @cancel="handleCancel"
      ></AddAndEdit>
    </el-dialog>

    <!-- 导入弹窗 -->
    <el-dialog
      title="导入"
      :visible.sync="uploadVisiable"
      width="400px"
      append-to-body
    >
      <ImportFiles />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary">确 定</el-button>
        <el-button @click="uploadVisiable = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getList,
  getDetail,
  add,
  update,
  del,
  exportData,
} from "@/api/exerciseManage/question";
import AddAndEdit from "./components/addAndEdit.vue";
import ImportFiles from "@/components/ImportFiles";
export default {
  name: "Question",
  components: {
    AddAndEdit,
    ImportFiles,
  },
  data() {
    return {
      // 显示搜索条件
      showSearch: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: "",
        descripiton: "",
      },
      // 时间
      dateRange: [],
      // 状态组
      typeOption: [
        { value: "1", label: "单选" },
        { value: "2", label: "多选" },
      ],
      // 表格相关
      loading: false,
      tableList: [],
      selectedRow: [],
      total: 0,
      //  弹窗相关
      title: "",
      dlgVisible: false,
      formData: {},
      dlgLoading: false,
      dlgType: 1, // 弹窗类型  1 - 新增   2 - 修改   3 - 查看
      // 导入弹窗相关
      uploadVisiable: false,
    };
  },
  created() {
    this.getTableList();
  },
  methods: {
    // 导入
    handleImport() {
      this.uploadVisiable = true;
    },
    // 导出
    handleExport() {
      let params = {
        type: this.queryParams.type,
        descripiton: this.queryParams.descripiton,
      };
      let newParams = this.addDateRange(params, this.dateRange);
      // exportData(newParams).then((res) => {
      //   console.log(res);
      // });
      this.download(
        "/question/info/export",
        {
          ...newParams,
        },
        `题目管理_${new Date().getTime()}.xlsx`
      );
    },
    // 搜索
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getTableList();
    },
    // 重置
    resetQuery() {
      this.dateRange = [];
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        type: "",
        descripiton: "",
      };
      this.getTableList();
    },
    // 新增
    handleAdd() {
      this.title = "新增";
      this.dlgVisible = true;
      this.dlgType = 1;
      this.formData = {};
    },
    // 修改
    handleUpdate(row) {
      this.title = "编辑";
      this.dlgType = 2;
      this.dlgVisible = true;
      this.dlgLoading = true;
      getDetail(row.id)
        .then((res) => {
          if (res.code === 200) {
            this.formData = { ...res.data, categoryName: row.categoryName };
          }
        })
        .finally(() => (this.dlgLoading = false));
    },
    // 查看
    handleDetail(row) {
      this.title = "查看";
      this.dlgType = 3;
      this.dlgVisible = true;
      this.dlgLoading = true;
      getDetail(row.id)
        .then((res) => {
          if (res.code === 200) {
            this.formData = { ...res.data, categoryName: row.categoryName };
          }
        })
        .finally(() => (this.dlgLoading = false));
    },
    // 弹窗确认
    handleConfirm(val) {
      if (val.id) {
        update(val).then((res) => {
          if (res.code === 200) {
            this.$modal.msgSuccess("编辑成功");
            this.handleCancel();
            this.getTableList();
          }
        });
      } else {
        add(val).then((res) => {
          if (res.code === 200) {
            this.$modal.msgSuccess("新增成功");
            this.handleCancel();
            this.getTableList();
          }
        });
      }
    },
    // 弹窗取消
    handleCancel() {
      this.dlgVisible = false;
    },
    // 删除
    handleDelete(row) {
      const selectIds = this.selectedRow.map((item) => item.id);
      const ids = row.id || selectIds;
      this.$modal
        .confirm("是否确认删除数据？")
        .then(function () {
          return del(ids);
        })
        .then(() => {
          this.getTableList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    // 表格选中
    handleSelectionChange(selection) {
      this.selectedRow = selection;
    },
    // 查询列表
    getTableList() {
      let params = this.addDateRange(this.queryParams, this.dateRange);
      this.loading = true;
      getList(params)
        .then((res) => {
          if (res.code === 200) {
            this.tableList = res.rows;
            this.total = res.total;
          }
        })
        .finally(() => (this.loading = false));
    },
  },
};
</script>

<style lang="scss" scoped></style>
