<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="年份" prop="year">
        <el-input
          v-model="queryParams.year"
          placeholder="请输入年份"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学校编码" prop="collegeCode">
        <el-input
          v-model="queryParams.collegeCode"
          placeholder="请输入学校编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学校名称" prop="collegeName">
        <el-input
          v-model="queryParams.collegeName"
          placeholder="请输入学校名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否显示" prop="isShow">
        <el-input
          v-model="queryParams.isShow"
          placeholder="请输入是否显示"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开始日期" prop="createTime">
        <el-date-picker clearable
                        v-model="queryParams.createTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择开始日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束日期" prop="updateTime">
        <el-date-picker clearable
                        v-model="queryParams.updateTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="类型" prop="type">
		<el-select v-model="queryParams.type">
		  <el-option v-for="op in brochureTypes" :key="op.value" :value="op.value" :label="op.label"/>
		</el-select>
      </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"
          v-hasPermi="['syzy:bBusiWishUniversitiesEnrollBrochure:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['syzy:bBusiWishUniversitiesEnrollBrochure:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['syzy:bBusiWishUniversitiesEnrollBrochure:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['syzy:bBusiWishUniversitiesEnrollBrochure:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="bBusiWishUniversitiesEnrollBrochureList"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="ID" align="center" prop="id"/>
      <el-table-column label="年份" align="center" prop="year"/>
      <el-table-column label="学校编码" align="center" prop="collegeCode"/>
      <el-table-column label="类型" align="center" prop="typeName"/>
      <el-table-column label="学校名称" width="180" align="center" prop="collegeName"/>
      <el-table-column label="标题" align="center" width="300" prop="title"/>
      <el-table-column label="是否显示" align="center" prop="isShow"/>
      <el-table-column label="点击量" align="center" prop="hits"/>
      <el-table-column label="排名" align="center" prop="rank"/>
      <el-table-column label="创建日期" align="center" prop="createTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <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-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['syzy:bBusiWishUniversitiesEnrollBrochure:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['syzy:bBusiWishUniversitiesEnrollBrochure:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改院校招生简章对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="780px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="年份" prop="year">
              <el-input v-model="form.year" placeholder="请输入年份"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="标准类型" prop="type">
              <el-select v-model="form.type" @change="handleFormTypeChange">
                <el-option v-for="op in brochureTypes" :key="op.value" :value="op.value" :label="op.label"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="类型别名" prop="typeName">
              <el-input v-model="form.typeName" placeholder="请输类型别名"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="学校编码" prop="collegeCode">
              <el-input v-model="form.collegeCode" placeholder="请输入学校编码"/>
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item label="学校名称" prop="collegeName">
              <el-input v-model="form.collegeName" placeholder="请输入学校名称"/>
            </el-form-item>
          </el-col>
        </el-row>
        <!--        <el-form-item label="编辑" prop="editor">-->
        <!--          <el-input v-model="form.editor" placeholder="请输入编辑" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="summary" prop="summary">-->
        <!--          <el-input v-model="form.summary" placeholder="请输入summary" />-->
        <!--        </el-form-item>-->
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题"/>
        </el-form-item>
        <!--        <el-form-item label="isArt" prop="isArt">-->
        <!--          <el-input v-model="form.isArt" placeholder="请输入isArt" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="元数据描述" prop="metaDescription">-->
        <!--          <el-input v-model="form.metaDescription" placeholder="请输入元数据描述" />-->
        <!--        </el-form-item>-->
        <el-form-item label="简章内容">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
        <el-row>
          <el-col :span="8">
            <el-form-item label="是否显示" prop="isShow">
              <el-select v-model="form.isShow">
                <el-option :value="1" label="是" />
                <el-option :value="0" label="否" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="点击量" prop="hits">
              <el-input v-model="form.hits" placeholder="请输入点击量"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="排序" prop="rank">
              <el-input v-model="form.rank" placeholder="请输入排序"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addBBusiWishUniversitiesEnrollBrochure,
  delBBusiWishUniversitiesEnrollBrochure,
  getBBusiWishUniversitiesEnrollBrochure,
  listBBusiWishUniversitiesEnrollBrochure,
  updateBBusiWishUniversitiesEnrollBrochure
} from "@/api/syzy/bBusiWishUniversitiesEnrollBrochure";

export default {
  name: "BBusiWishUniversitiesEnrollBrochure",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 院校招生简章表格数据
      bBusiWishUniversitiesEnrollBrochureList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        year: null,
        collegeCode: null,
        collegeName: null,
        editor: null,
        summary: null,
        title: null,
        isArt: null,
        type: null,
        metaDescription: null,
        content: null,
        tags: null,
        isShow: null,
        hits: null,
        metaKeywords: null,
        rank: null,
        createTime: null,
        updateTime: null,
        createTime2: null,
        typeName: null
      },
      // 表单参数
      brochureTypes: [
        {label: '院校动态', value: 1},
        {label: '招生简章', value: 2},
        {label: '考试大纲', value: 3},
        {label: '就业情况', value: 4},
        {label: '其它资讯', value: 5}
      ],
      form: {},
      // 表单校验
      rules: {
        collegeCode: [
          {required: true, message: "学校编码不能为空", trigger: "blur"}
        ],
        collegeName: [
          {required: true, message: "学校名称不能为空", trigger: "blur"}
        ],
        editor: [
          {required: true, message: "编辑不能为空", trigger: "blur"}
        ],
        summary: [
          {required: true, message: "summary不能为空", trigger: "blur"}
        ],
        title: [
          {required: true, message: "标题不能为空", trigger: "blur"}
        ],
        type: [
          {required: true, message: "类型不能为空", trigger: "change"}
        ],
        content: [
          {required: true, message: "简章内容不能为空", trigger: "blur"}
        ],
        isShow: [
          {required: true, message: "是否显示不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询院校招生简章列表 */
    getList() {
      this.loading = true;
      listBBusiWishUniversitiesEnrollBrochure(this.queryParams).then(response => {
        this.bBusiWishUniversitiesEnrollBrochureList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      const currentYear = new Date().getFullYear()
      this.form = {
        id: null,
        year: currentYear,
        collegeCode: null,
        collegeName: null,
        editor: null,
        summary: null,
        title: null,
        isArt: null,
        type: 2,
        metaDescription: null,
        content: null,
        tags: null,
        isShow: 1,
        hits: null,
        metaKeywords: null,
        rank: null,
        createTime: null,
        updateTime: null,
        createTime2: null,
        typeName: null
      };
      this.handleFormTypeChange()
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加院校招生简章";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBBusiWishUniversitiesEnrollBrochure(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改院校招生简章";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          // 有些字段默认填充，减少用户输入
          this.form.editor = this.form.collegeName
          this.form.summary = this.form.title

          if (this.form.id != null) {
            updateBBusiWishUniversitiesEnrollBrochure(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBBusiWishUniversitiesEnrollBrochure(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除院校招生简章编号为"' + ids + '"的数据项？').then(function () {
        return delBBusiWishUniversitiesEnrollBrochure(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('syzy/bBusiWishUniversitiesEnrollBrochure/export', {
        ...this.queryParams
      }, `bBusiWishUniversitiesEnrollBrochure_${new Date().getTime()}.xlsx`)
    },
    handleFormTypeChange() {
      const matchName = this.brochureTypes.find(t => t.value == this.form.type)?.label
      this.form.typeName = matchName || this.form.typeName
    }
  }
};
</script>
