<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="文章标题" prop="newsTitle">
        <el-input
          v-model="queryParams.newsTitle"
          placeholder="请输入文章标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="文章状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择文章状态" clearable size="small">
          <el-option
            v-for="dict in dict.type.column_news"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="是否删除" prop="isDel">
        <el-select v-model="queryParams.status" placeholder="请选择是否删除" clearable size="small">
          <el-option
            v-for="dict in dict.type.is_del"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </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="['system:news: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="['system:news: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="['system:news: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="['system:news:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="newsList" @selection-change="handleSelectionChange" :max-height="tableMaxHeight">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="栏目名称" align="center" prop="columnName" />
      <el-table-column label="文章标题" align="center" prop="newsTitle" />
      <el-table-column label="文章内容" align="center" prop="newsContent" width="300px" :show-overflow-tooltip="true"/>
      <el-table-column label="文章状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.column_news" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="是否头条" align="center" prop="isTop">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.sys_yes_no"/>
        </template>
      </el-table-column>
      <el-table-column label="驳回原因" align="center" prop="reason" />
      <el-table-column label="备注" align="center" prop="remark" />
      <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="examine(scope.row)"
            v-hasPermi="['system:news:examine']"
          >通过审核</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="reject(scope.row)"
            v-hasPermi="['system:news:reject']"
          >驳回</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:news:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:news: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="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="文章标题" prop="newsTitle">
              <el-input v-model="form.newsTitle" placeholder="请输入文章标题" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="选择栏目" prop="columnId">
              <treeselect v-model="form.columnId" :options="columnOptions" :normalizer="normalizer"
                placeholder="选择栏目" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否头条" prop="isTop">
              <el-select v-model="form.isTop" placeholder="请选择" style="width: 100%;">
                <el-option v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入备注" />
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="文章内容" prop="newsContent">
              <editor v-model="form.newsContent" :min-height="192" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="文章缩略图" prop="newsImage">
              <!-- <editor v-model="form.newsImage" :min-height="192" /> -->
              <imageUpload v-model="form.newsImage" />
            </el-form-item>
          </el-col>
        </el-row>
<!--        <el-form-item label="文章状态">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.column_news"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="原因" prop="reason">
          <el-input v-model="form.reason" placeholder="请输入原因" />
        </el-form-item>-->
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </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 { listNews, getNews, delNews, addNews, updateNews,rejectNews,examineNews } from "@/api/system/news";
import { listColumn } from '@/api/system/column'
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "News",
  dicts: ['column_news','is_del','sys_yes_no'],
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 文章信息表格数据
      newsList: [],
      //栏目树选项
      columnOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        newsTitle: null,
        columnId: null,
        status: null,
        isDel: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        newsTitle: [
          { required: true, message: "文章标题不能为空", trigger: "blur" }
        ],
        columnId: [
          { required: true, message: "所属栏目不能为空", trigger: "blur" }
        ],
        isTop: [
          { required: true, message: "是否头条不能为空", trigger: "blur" }
        ]
      },
      boh:{},  //驳回原因
      tableMaxHeight:300
    };
  },
  created() {
    this.getList();
    window.onresize = () => {
          this.changeTableMaxHeight()
        }
        this.changeTableMaxHeight()
  },
  destroyed () {
      window.onresize = null
    },
  mounted() {
window.onresize = () => {
          this.changeTableMaxHeight()
        }
        this.changeTableMaxHeight()
  },
  methods: {
    /** 查询文章信息列表 */
    getList() {
      this.loading = true;
      listNews(this.queryParams).then(response => {
        this.newsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        newsId: null,
        newsTitle: null,
        columnId: null,
        newsContent: undefined,
        status: "0",
        isTop: null,
        isDel: null,
        reason: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.newsId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加文章信息";
      listColumn().then(response => {
        if (response.data != null) {
          this.columnOptions = this.handleTree(response.data, "columnId");
        }
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const newsId = row.newsId || this.ids
      getNews(newsId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改文章信息";
        listColumn().then(response => {
          if (response.data != null) {
            this.columnOptions = this.handleTree(response.data, "columnId");
          }
        });
      });
    },

    /** 审核通过按钮操作 */
    examine(row) {
      const newsId = row.newsId || this.ids
      examineNews(newsId).then(response => {
        this.$modal.msgSuccess("审核成功");
        this.open = false;
        this.getList();
      });
    },

    /** 驳回按钮操作 */
    reject(row) {
      const newsId = row.newsId || this.ids
	  var that = this
      this.$prompt('请输入驳回原因', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
		inputValue: '',
		inputErrorMessage: '输入不能为空',
		showCancelButton:false,
		inputValidator: (value) => {       // 点击按钮时，对文本框里面的值进行验证
		                        if(!value) {
		                            return '输入不能为空';
		                        }
				},

      }).then(({ value }) => {
        this.boh.reason = value
        this.boh.newsId = newsId
		// that.rejectNews(this.boh).then(response => {
		// 	console.log(8764)
		//   this.$modal.msgSuccess("驳回成功");
		//   // this.open = false;
		//   this.getList();
		// });
        return  that.nhde()
      })
	  .catch(() => {
		  // this.nhde()
      });
    },
	nhde(){
		rejectNews(this.boh).then(response => {
		  this.$modal.msgSuccess("驳回成功");
		  // this.open = false;
		  this.getList();
		});
	},
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.newsId != null) {
            updateNews(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            console.log(this.form,111)
            addNews(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const newsIds = row.newsId || this.ids;
      this.$modal.confirm('是否确认删除文章信息编号为"' + newsIds + '"的数据项？').then(function() {
        return delNews(newsIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/news/export', {
        ...this.queryParams
      }, `news_${new Date().getTime()}.xlsx`)
    },


    /** 转换栏目数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.columnId,
        label: node.columnName,
        children: node.children
      };
    },
    // 获取屏幕高度
    showFilterForm () {
          this.filterActive = !this.filterActive
          this.changeTableMaxHeight()
        },

        changeTableMaxHeight () {
          let height = document.body.offsetHeight // 网页可视区域高度
          // if (this.filterActive) {
          //   this.tableMaxHeight = height - 320
          // } else {
            this.tableMaxHeight = height - 250
          // }
          console.log(height)
        }
  }
};
</script>
