<template>
    <div class="page">
        <LayoutTable :data="tableData" :config="tableConfig">
                    </LayoutTable>


        <drawer-show ref="drawerform" @ok="getData" />
    </div>
</template>

<script>
    /** 表单弹出框模式需引入 */
    import drawerShow from "./drawershow";
    import table from "@/assets/mixins/table";
    export default {
        name: "NewsList",
        components: {
            drawerShow
},
        mixins: [table],
        created() {
        },
        methods: {
            /** 重写新增方法 */
            toAdd(row) {
                this.$refs.drawerform.add(row);
            },
            /** 重写编辑方法 */
            toEdit(row) {
                this.$refs.drawerform.edit(row);
            },
            /** 重写查看方法 */
            toView(row) {
                this.$refs.drawerform.view(row);
            },

          handTop(row,top) {

              this.$post("/news/save",{id:row.id,top:top}).then((res) => {
                if (res.code == 1) {
                  this.$message.success("置顶成功！");
                  this.getData();
                }
              })
                  .catch((error) => {
                    this.$message.error(error.message);
                  });

          },

        },
        data() {
            return {
                config: {
                    search: [
                    ],
                    columns: [
                        {type: "selection", width: 60},
                        {type: "index",label: "序号",width: 50},

                      {label: "标题", prop: "title"},

                        {label: "新闻类型", prop: "categoryId",formatter: this.formatter},

                      {label: "创建时间", prop: "createDateTime", formatter: this.formatterDate},

                        {label: "数据",formatter:  row => {
                          let bool=false;
                            return (
                                <div><el-link underline={bool} type="primary">浏览({row.viewNums})</el-link></div>
                            );}},


                        {label: "创建用户", prop: "createUserId", formatter: this.formatter},
                        {
                            label: "操作",
                            width: 240,
                            formatter: row => {
                                return (
                                    <div>

                                      {row.top === 0 ? (
                                          <el-button
                                              size="mini"
                                              type="text"
                                              icon="el-icon-open"
                                              onClick={() => {
                                                this.handTop(row,1);
                                              }}
                                          >
                                            置顶
                                          </el-button>
                                      ) : (
                                          <el-button
                                              size="mini"
                                              type="text"
                                              icon="el-icon-open"
                                              onClick={() => {
                                                this.handTop(row,0);
                                              }}
                                          >
                                            取消置顶
                                          </el-button>
                                      )}
                                      <span> </span>
                                      <table-buttons
                                          noAdd
                                          row={row}
                                          onEdit={this.toEdit}
                                          onView={this.toView}
                                          onDel={this.toDel}
                                      />

                                    </div>
                            );
                            }
                        }
                    ]
                }
            };
        }
    };
</script>
