<template>
  <div class="banner-box" v-loading="loading > 0">
    <Header :title="title"></Header>
    <div class="container">
      <div class="content">
        <!-- 表格 -->
        <div class="btn" @click="handleMessageModal('add')">+ 添加</div>
        <el-table
          :data="list"
          class="order-table table no-border-table"
          ref="orderTable"
          :header-cell-style="{
            border: 'none',
            color: '#333',
            background: '#f6f6f6',
            borderBottom: '1px solid #E8E8E8',
            height: '54px',
          }"
          :row-style="{
            height: '54px',
          }"
        >
          <el-table-column
            prop="title"
            align="center"
            label="文章标题"
          ></el-table-column>
          <el-table-column
            prop="link"
            align="center"
            label="文章链接"
          ></el-table-column>
          <el-table-column prop="orderBy" align="center" label="排序">
            <div slot-scope="scope" @click="changeSortModal(scope.row.id)">
              {{ scope.row.orderBy }}
            </div>
          </el-table-column>
          <el-table-column label="操作" align="center" width="180">
            <div slot-scope="scope" style="display: flex">
              <div>
                <button
                  size="mini"
                  class="table-btn"
                  @click="handleMessageModal('edit', scope.row)"
                >
                  编辑
                </button>
              </div>
              <div>
                <button
                  size="mini"
                  class="table-btn"
                  @click="deleteProblem(scope.row.id)"
                >
                  删除
                </button>
              </div>
            </div>
          </el-table-column>
        </el-table>
        <!-- 分页按钮 -->
      </div>
      <div class="footer">
        <el-pagination
          @current-change="handleCurrentChange"
          :current-page.sync="currentPage"
          background
          layout="total, prev, pager, next, jumper"
          :page-size="size"
          :total="total"
        ></el-pagination>
      </div>
    </div>
    <el-dialog
      :visible.sync="addProblemShow"
      class="dialog"
      width="560px"
      top="25vh"
    >
      <div slot="title" class="dialog-title">
        {{ type === "add" ? "发布" : "编辑" }}
      </div>
      <el-form
        ref="form"
        :rules="rules"
        :model="addProblemForm"
        class="banner-form"
        label-width="auto"
      >
        <el-form-item label="标题：" prop="link" class="form-item">
          <el-input
            size="mini"
            placeholder="请输入文章标题"
            class="add-renovation-input"
            v-model="addProblemForm.title"
            style="width: 254px"
          ></el-input>
        </el-form-item>
        <el-form-item label="输入文章链接：" prop="link" class="form-item">
          <el-input
            size="mini"
            placeholder="请输入公众号链接"
            class="add-renovation-input"
            v-model="addProblemForm.link"
            style="width: 254px"
          ></el-input>
        </el-form-item>
        <el-form-item label="排序：" prop="sort" class="form-item">
          <el-input
            size="mini"
            placeholder="请输入序号"
            class="add-renovation-input"
            style="width: 160px"
            v-model.number="addProblemForm.orderBy"
            type="number"
            onkeypress="return( /[\d]/.test(String.fromCharCode(event.keyCode)))"
          ></el-input>
          <div class="sort-tip">数值越大，排序越靠前</div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="cancel-btn btn" @click="addProblemShow = false"
          >取 消</el-button
        >
        <el-button class="confirm-btn btn" @click="handleProblem"
          >确 定</el-button
        >
      </div>
    </el-dialog>
    <!-- 修改排序弹窗 -->
    <el-dialog title="更改排序" width="400px" :visible.sync="sortShow">
      <el-form label-width="60px" size="mini" ref="editSort" inline-message>
        <el-form-item label="排序：" prop="tableData" style="margin-top: 15px">
          <el-input v-model="currentSort" type="number" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="btn cancel-btn" @click="sortShow = false"
          >取 消</el-button
        >
        <el-button class="btn confirm-btn" @click="saveSort">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script type="text/ecmascript-6">
import Header from '../../components/common/header';
export default {
  components: {
    Header
  },
  name: 'banner',
  data() {
    return {
      loading: 0, // 加载中
      currentPage: 1, // 当前页码
      size: 20, // 每页条数
      total: 0, // 总页数
      title: {
        // 页面标题
        firstTitile: '商城设置',
        secondTitle: '常见问题'
      },
      list: [], // 轮播列表
      rules: {},
      sortShow: false,
      currentSort: 0,
      addProblemForm: {
        title: '',
        link: '',
        orderBy: null
      },
      addProblemShow: false,
      id: null,
      type: ''
    };
  },
  mounted() {
    // 获取列表
    this.getProblemList();
  },
  computed: {},
  methods: {
    // 表格序号
    tableIndex(index) {
      return ++index;
    },
    // 切换页码
    handleCurrentChange(page) {
      this.currentPage = page;
      this.getProblemList();
    },
    // 获取轮播列表
    getProblemList() {
      this.loading++;
      this.httpGet(this.$root.getProblemList, {
        page: this.currentPage,
        size: this.size
      }).then(
        (res) => {
          this.loading--;
          this.list = res.list;
          this.total = res.total;
        },
        (res) => {
          this.loading--;
          this.$message.error(res);
        }
      );
    },
    // 编辑轮播弹窗
    handleMessageModal(type, rowData) {
      this.type = type;
      if (type === 'edit') {
        this.id = rowData.id;
        this.addProblemForm = {
          title: rowData.title,
          link: rowData.link,
          orderBy: rowData.orderBy
        };
      } else {
        this.addProblemForm = {
          title: '',
          link: '',
          orderBy: null
        };
        this.$refs.form && this.$refs.form.clearValidate();
      }
      this.addProblemShow = true;
    },
    // 删除轮播图
    deleteProblem(id) {
      this.$confirm(
        `<p class='title'><i class='icon el-icon-question'></i>提醒</p><p class='text'>确定要删除该文章吗？</p>`,
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          cancelButtonClass: 'cancel-btn',
          confirmButtonClass: 'confirm-btn',
          customClass: 'confirm-box',
          dangerouslyUseHTMLString: true,
          showClose: false,
          type: 'none'
        }
      ).then(() => {
        this.loading++;
        this.httpDelete(this.$root.deleteProblem, {
          id
        }).then(
          (res) => {
            this.$message({
              message: '操作成功',
              type: 'success'
            });
            this.loading--;
            this.getProblemList();
          },
          (res) => {
            this.loading--;
            this.$message.error(res);
          }
        );
      });
    },
    // 轮播
    handleProblem() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          let url =
            this.type === 'add'
              ? this.$root.addProblem
              : this.$root.updateProblem;
          let method =
            this.type === 'add'
              ? 'httpPost'
              : 'httpPut';
          let inputData =
            this.type === 'add'
              ? {
                  ...this.addProblemForm
                }
              : {
                  ...this.addProblemForm,
                  id: this.id
                };
                if(!inputData.orderBy)  inputData.orderBy = 0;
          this.loading++;
          this[method](url, inputData).then(
            (res) => {
              this.$message({
                message: '操作成功',
                type: 'success'
              });
              this.loading--;
              this.addProblemShow = false;
              this.getProblemList();
            },
            (res) => {
              this.loading--;
              this.$message.error(res);
            }
          );
        }
      });
    },
    changeSortModal() {
      // this.sortShow = true;
    },
    // 图片改变
    addChangeImg(e) {
      this.upPic(e, (res) => {
        this.$set(this.addProblemForm, 'pic', res);
        // 清空input value，解决重复图片不触发change
        this.$refs.inputImg.value = '';
        this.$refs.form.clearValidate(['pic']);
      });
    },
    // 上传图片
    upPic(e, fun) {
      // 上传图片
      let file = e.target.files[0];
      if (!file) {
        return;
      }
      this.request.upPic({ prefix: 'other', file: file }).then(
        (res) => {
          fun(res);
        },
        (res) => {}
      );
    },
    saveSort() {}
  }
};
</script>
<style lang="stylus" rel="stylesheet/stylus">
@import '~assets/public.styl';
</style>
<style lang="stylus" rel="stylesheet/stylus" scoped>
@import '~assets/main.styl';

.banner-box {
  height: 100%;
  width: 100%;
  flex-y(flex-start, flex-start);
  overflow-y: hidden;
  background: bg-color;

  .container {
    box-sizing: border-box;
    height: 100%;
    width: 100%;
    overflow-y: auto;
    flex-y(flex-start, flex-start);
    padding: 0 20px 20px;

    // content 表格
    .content {
      flex: 1;
      width: 100%;
      font-size: 14px;
      background: white;
      box-sizing: border-box;
      padding: 24px 32px;
      border-radius: 2px;

      // 按钮
      .btn {
        // width: 100px;
        display: inline-block;
        padding: 0 30px;
        height: 32px;
        line-height: 32px;
        confirm-btn();
        font-size: 14px;
        margin-bottom: 16px;
        // margin: 16px 0;
      }

      .tip {
        margin-left: 32px;
        color: #40B98F;
      }

      // 表格按钮
      .table-btn {
        width: 65px;
        height: 24px;
        line-height: 24px;
        color: gray3;
        font-size: 12px;
        margin-right: 8px;
        cancel-btn();
      }

      .table-img {
        height: 70px;
        width: auto;
      }
    }
  }

  .dialog-content {
    flex-y();
  }

  .addImg {
    .icon {
      width: 270px;
      height: 108px;
    }
  }

  .banner-form {
    margin-top: 16px;
    margin-bottom: 80px;
    padding-left: 60px;

    .sort-tip {
      word(14px, #c8c8c8);
      line-height: 20px;
    }
  }
}
</style>
