<template>
  <div class="app-container calendar-list-container" v-loading.body="showLoadingBody">
    <el-table
      :data="list"
      :row-key="getRowKeys" :expand-row-keys="expands"
      style="width: 100%;">
      <el-table-column
        type="index"
        align="center" label="序号" width="65">
      </el-table-column>
      <el-table-column width="250" align="center" label="板块">
        <template slot-scope="scope">
          <span>{{scope.row.modelName}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="标题">
        <template slot-scope="scope">
          <span>{{scope.row.header}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" width="100" label="操作" fixed="right">
        <template slot-scope="scope">
          <el-button size="small" class="el-button el-button--text el-button--small"
                     @click="handleUpdate(scope.row)">
            {{'编辑'}}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div v-show="!listLoading" class="pagination-container">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                     :current-page.sync="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit"
                     layout="total, sizes, prev, pager, next, jumper" :total="total"></el-pagination>
    </div>

    <!-- 编辑、创建 modal弹窗 -->
    <el-dialog :title="modalTitle" :visible.sync="dialogVisible">
      <el-form :model="form" :rules="formRules" ref="form" label-width="90px">
        <el-row>
          <el-col :span="15">
            <el-form-item label="模块" prop="modelName">
              <el-input v-model="form.modelName" disabled placeholder="模块"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="15">
            <el-form-item label="标题" prop="header">
              <el-input v-model="form.header" placeholder="标题"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="15">
            <el-form-item label="title" prop="title">
              <el-input v-model="form.title" placeholder="title"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="15">
            <el-form-item label="keywords" prop="keywords">
              <el-input v-model="form.keywords" placeholder="keywords"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="15">
            <el-form-item label="description" prop="description">
              <el-input v-model="form.description" placeholder="description"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>


      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelHandel">取 消</el-button>
        <el-button type="primary" @click="update('form')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<style>
  .el-upload-list {
    display: flex;
    width: 100px;
  }

  .el-upload-list li {
    margin-left: 10px;
  }
</style>
<script>
  import 'static/css/uploadImg.css';// 引入图片上传组件对话框
  import {
    list,
    save,
  } from 'src/api/officialWebsite/seoList';

  import rsCode from '../../utils/rsCode';

  import {mapGetters} from 'vuex';

  import Element1 from "../admin/menu/components/element";
  import ElRow from "element-ui/packages/row/src/row";
  import ElCol from "element-ui/packages/col/src/col";
  import ElIcon from "../../../node_modules/element-ui/packages/icon/src/icon.vue";

  export default {
    name: 'seoList',
    components: {
      ElIcon,
      ElCol,
      ElRow,
      Element1
    },
    data() {
      return {
        modalTitle: "创建",
        dialogVisible: false,//添加、编辑弹框
        showLoadingBody: false,
        expands: [],
        listQuery: {
          page: 1,
          limit: 10
        },
        total: 0,
        form: {
          id: undefined,
          modelId: undefined,
          modelName: undefined,
          title: '',
          keywords: '',
          description: '',
          header: ''
        },
        formRules: {
          header: {
            type: 'string',
            required: true,
            message: '请输入标题',
            trigger: 'blur'
          },
        },
        list: [],
        listLoading: true,
      }
    },
    created() {
      this.getList();
    },
    computed: {
      ...mapGetters([
        'elements',
      ])
    },
    methods: {
      getRowKeys(row) {
        return row.id
      },

      /**
       * 操作-编辑
       * */
      handleUpdate(row) {
        this.modalTitle = '编辑';
        this.cleanForm();
        this.form = row;
        this.dialogVisible = true;
      },
      /**
       * 弹框-取消
       * */
      cancelHandel() {
        this.dialogVisible = false;
        this.cleanForm();

      },
      /**
       * 清空表单
       * */
      cleanForm() {
        this.form = {
          id: undefined,
          modelId: undefined,
          modelName:'',
          title: '',
          keywords: '',
          description: '',
          header: ''
        };
      },
      /**
       * 获取列表数据
       * */
      getList() {
        list(this.listQuery).then(response => {
          let listRs = undefined;
          let total = undefined;
          if (!this.$utils.isEmpty(response.data)) {
            listRs = response.data.list;
            total = response.data.total
          }
          this.listLoading = false;
          this.list = listRs;
          this.total = total;
        });
      },
      /**
       * 编辑-更新
       * */
      update(formName) {
        const set = this.$refs;
        let form = this.form
        set[formName].validate(valid => {
          if (valid) {
            save(form).then(response => {
              if (response.status === 200) {
                this.dialogVisible = false;
                this.$notify({
                  title: '成功',
                  message: '创建成功',
                  type: 'success',
                  duration: 2000
                });
                this.getList();
              } else {
                this.$notify({
                  title: '失败',
                  message: rsCode.msg[response.code] ? rsCode.msg[response.code] : '操作失败！',
                  type: 'error',
                  duration: 2000
                });
              }
            });
          } else {
            return false;
          }
        });
      },
      handleSizeChange(val) {
        this.listQuery.limit = val;
        this.getList();
      },
      handleCurrentChange(val) {
        this.listQuery.page = val;
        this.getList();
      }

    }
  }
</script>
