<template>
  <div class="app-container calendar-list-container" v-loading.body="showLoadingBody">
    <div class="filter-container" ref="filter-container">
      <el-button class="filter-item" style="margin-left: 10px;" @click="handleCreate"
                 type="primary" icon="edit">添加
      </el-button>
    </div>
    <el-table :key='tableKey' :data="list" border fit highlight-current-row
              style="width: 600px">
      <el-table-column
        type="index"
        align="center" label="序号" width="98">
      </el-table-column>
      <el-table-column width="200" align="center" label="标题">
        <template scope="scope">
          <span>{{scope.row.title}}</span>
        </template>
      </el-table-column>
      <el-table-column width="100" align="center" label="标识">
        <template scope="scope">
          <span>{{scope.row.type}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" width="200" label="操作">
        <template scope="scope">
          <el-button size="small" type="success" @click="handleUpdate(scope.row)">编辑
          </el-button>
          <el-popover
            ref="popover5"
            placement="top"
            width="160"
            v-model="scope.row.visible2">
            <p>确定删除吗？</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="scope.row.visible2 = false">取消</el-button>
              <el-button type="primary" size="mini" @click="deleteHandler(scope.row)">确定</el-button>
            </div>
          </el-popover>

          <el-button type="danger" size="small" v-popover:popover5>删除</el-button>
          <!--<el-button size="small" type="danger" @click="deleteHandler(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>
    <!-- banner modal弹窗 -->
    <el-dialog :title="modalTitle" :visible.sync="dialogVisible">
      <el-form :model="dialogFrom" :rules="rules" ref="dialogFrom" label-width="90px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="dialogFrom.title" placeholder="请输入标题"></el-input>
        </el-form-item>
        <el-form-item label="标识" prop="type">
          <el-input type="number" v-model.number="dialogFrom.type" placeholder="1,2,3……"></el-input>
        </el-form-item>
        <el-form-item label="详情">
          <Editor @input="handelIncrease" :value="dialogFrom.value"></Editor>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelHandel">取 消</el-button>
        <el-button v-if="modalTitle=='创建'" type="primary" @click="create('dialogFrom')">确 定</el-button>
        <el-button v-else type="primary" @click="update('dialogFrom')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<style>
  .el-table__header{
    width: 100% !important;
  }
</style>
<script>
  import 'static/css/uploadImg.css';// 引入图片上传组件对话框
  import Editor from "../modal/editorTool";//富文本
  import {
    formatDate
  } from 'utils/dateFormattor';

  import {
    toEast8Date,
    deepCopyDate,
    newEast8Date,
    convertDate2Str
  } from 'utils/dateUtils';

  import {
    page,
    addGeneral,
    editGeneral,
    delGeneral
  } from 'api/base_info/general';

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

  import {mapGetters} from 'vuex';
  import {
    getToken
  } from '../../utils/auth';

  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";

  export default {
    name: 'tourManage',
    components: {
      ElCol,
      ElRow,
      Element1,
      Editor
    },
    data() {
      return {
        listQuery: {
          page: 1,
          limit: 20
        },
        tableKey:0,
        modalTitle: "创建",
        BASE_API: process.env.BASE_API,
        dialogVisible: false,//添加、编辑弹框
        showLoadingBody: false,
        dialogFrom:{
          title: "",
          type: undefined,
          value:""
        },
        rules: {
          title: {
            type: 'string',
            required: true,
            message: '请输入标题',
            trigger: 'blur'
          },
          type: {
            required: true,
            message: '请输入数字标识',
          },
        },
        list: null,
        total: null,
        listLoading: true,
        inline: true,
      }
    },
    created() {
      this.getList();
    },
    computed: {
      ...mapGetters([
        'elements',
        'belong2Type'
      ])
    },
    methods: {
      /**
       * 添加
       * */
      handleCreate() {
        this.cleanDialogFrom();
        this.modalTitle = '创建';
        this.dialogVisible = true;
      },
      /**
       * 操作-删除
       * */
      deleteHandler(row) {
        delGeneral(row.id).then(response => {
          if (response.status === 200) {
            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
            });
          }
        });

      },
      /**
       * 操作-编辑
       * */
      handleUpdate(row) {
        this.modalTitle = '编辑';
        this.dialogVisible = true;
        this.cleanDialogFrom();
        this.dialogFrom = row;
      },
      /**
       * 弹框-取消
       * */
      cancelHandel(){
        this.cleanDialogFrom();
        this.dialogVisible = false;
      },
      /**
       * 新建、编辑清空表单
       * */
      cleanDialogFrom(){
        this.dialogFrom = {
          title: "",
          type: undefined,
          value:""
        }
      },
      handleSizeChange(val) {
        this.listQuery.limit = val;
        this.getList();
      },
      handleCurrentChange(val) {
        this.listQuery.page = val;
        this.getList();
      },
      /**
       * 获取通用设置列表数据
       * */
      getList() {
        this.listLoading = true;
        page(this.listQuery).then(response => {
          let totalCountRs = undefined;
          let listRs = undefined;
          if (!this.$utils.isEmpty(response.data.rows) && this.$utils.isInteger(response.data.total)) {
            response.data.rows.map(function(item){
              item.visible2 = false;
            });
            listRs = response.data.rows;
            totalCountRs = response.data.total;
          }
          this.listLoading = false;
          this.list = listRs;
          this.total = totalCountRs;
        });
      },
      /**
       * 创建
       * */
      create(formName) {
        this.$refs.dialogFrom.validate(valid => {
          if (valid) {
            let params = {
              title: this.dialogFrom.title,
              type: this.dialogFrom.type,
              value: this.dialogFrom.value
            };
            addGeneral(params).then(response => {
              if (response.status === 200) {
                this.cleanDialogFrom();
                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;
          }
        });
      },
      /**
       * 编辑-更新
       * */
      update(formName) {
        this.$refs.dialogFrom.validate(valid => {
          if (valid) {
            let params = {
              id: this.dialogFrom.id,
              title: this.dialogFrom.title,
              type: this.dialogFrom.type,
              value: this.dialogFrom.value
            };
            editGeneral(params).then(response => {
              if (response.code === rsCode.RS_CODE_SUC) {
                this.cleanDialogFrom();
                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;
          }
        });
      },
      /**
       * 富文本内容
       * @param step
       */
      handelIncrease(step) {
        this.dialogFrom.value = step;
      }
    }
  }
</script>
