<template>
  <div class="banner-box" v-loading="loading > 0">
    <Header :title="title"></Header>
    <div class="container">
      <div class="content">
        <!-- 表格 -->
        <div class="btn" @click="handleBannerModal('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="name"
            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="handleBannerModal('edit', scope.row)"
                >
                  编辑
                </button>
              </div>
              <div>
                <button
                  size="mini"
                  class="table-btn"
                  @click="deleteBanner(scope.row.id)"
                >
                  删除
                </button>
              </div>
            </div>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <el-dialog
      :visible.sync="addBannerShow"
      class="dialog"
      width="560px"
      top="25vh"
    >
      <div slot="title" class="dialog-title">
        {{ (type === "add" ? "发布" : "编辑") + "文章分类" }}
      </div>
      <el-form
        ref="form"
        :rules="rules"
        :model="addBannerForm"
        class="banner-form"
        label-width="auto"
      >
        <el-form-item
          label="分类名称："
          prop="link"
          class="form-item"
        >
          <el-input
            size="small"
            placeholder="请输入分类名称"
            class="add-renovation-input"
            v-model="addBannerForm.name"
            style="width: 254px"
          ></el-input>
        </el-form-item>
        <el-form-item label="排序：" prop="sort" class="form-item">
          <el-input
            size="small"
            placeholder="请输入序号"
            class="add-renovation-input"
            style="width: 160px"
            v-model.number="addBannerForm.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="addBannerShow = false"
          >取 消</el-button
        >
        <el-button class="confirm-btn btn" @click="handleBanner"
          >确 定</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, // 加载中
      title: {
        // 页面标题
        firstTitile: '商城设置',
        secondTitle: '文章分类'
      },
      list: [], // 轮播列表
      rules: {},
      wayMap: {
        1: '商品',
        2: '文章',
        3: '其他'
      },
      sortShow: false,
      currentSort: 0,
      addBannerForm: {
        name: '',
        orderBy: null
      },
      addBannerShow: false,
      bannerId: null,
      type: ''
    };
  },
  mounted() {
    // 获取列表
    this.getList();
  },
  computed: {},
  methods: {
    // 表格序号
    tableIndex(index) {
      return ++index;
    },
    // 获取轮播列表
    getList() {
      this.loading++;
      this.httpGet(this.$root.getArticleCategoryList, {}).then(
        (res) => {
          this.loading--;
          this.list = res;
          this.total = res.total;
        },
        (res) => {
          this.loading--;
          this.$message.error(res);
        }
      );
    },
    // 编辑轮播弹窗
    handleBannerModal(type, rowData) {
      this.type = type;
      if (type === 'edit') {
        this.bannerId = rowData.id;
        this.addBannerForm = {
          name: rowData.name,
          orderBy: rowData.orderBy,
        };
      } else {
        this.addBannerForm = {
          name: '',
          orderBy: null
        };
        this.$refs.form && this.$refs.form.clearValidate();
      }
      this.addBannerShow = true;
    },
    changeWay() {
      this.addBannerForm.link = '';
    },
    // 删除轮播图
    deleteBanner(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.deleteArticleCategory, {
          id
        }).then(
          (res) => {
            this.$message({
              message: '操作成功',
              type: 'success'
            });
            this.loading--;
            this.getList();
          },
          (res) => {
            this.loading--;
            this.$message.error(res);
          }
        );
      });
    },
    // 轮播
    handleBanner() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          let url =
            this.type === 'add'
              ? this.$root.addArticleCategory
              : this.$root.updateArticleCategory;
          let method =
            this.type === 'add'
              ? 'httpPost'
              : 'httpPut';
          let inputData =
            this.type === 'add'
              ? {
                  ...this.addBannerForm
                }
              : {
                  ...this.addBannerForm,
                  id: this.bannerId
                };
          this.loading++;
          this[method](url, inputData).then(
            (res) => {
              this.$message({
                message: '操作成功',
                type: 'success'
              });
              this.loading--;
              this.addBannerShow = false;
              this.getList();
            },
            (res) => {
              this.loading--;
              this.$message.error(res);
            }
          );
        }
      });
    },
    changeSortModal() {
      // this.sortShow = true;
    },
    // 图片改变
    addChangeImg(e) {
      this.upPic(e, (res) => {
        this.$set(this.addBannerForm, '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: 24px;

    // 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 10px;
        height: 32px;
        line-height: 32px;
        confirm-btn();
        font-size: 14px;
        margin-bottom: 16px;
        // margin: 16px 0;
      }

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

      // 表格按钮
      .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: 80px;

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