<template>
<div class="app-container">
  <el-card > 
      <!-- <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
        <el-form-item label="分类名称" prop="categoryName">
          <el-input
            v-model="queryParams.categoryName"
            placeholder="请输入供应商名称"
            clearable
            style="width: 240px;"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-select v-model="queryParams.status" style="width: 217px" placeholder="全部" clearable
>
            <el-option
            v-for="dict in dict.type.zhzt"
                :key="dict.label"
                :label="dict.label"
                :value="dict.label"
            />
        </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search"  @click="handleQuery">搜索</el-button>
                          <el-button icon="el-icon-refresh"  @click="resetQuery">重置</el-button>
       </el-form-item>
      </el-form>  -->
    </el-card>  

    <el-card style="margin-top: 20px" >
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-plus"
            @click="giftAdd"
          >新增礼品</el-button>
        </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
      <el-table ref="tables"
                v-loading="loading"
                :data="dataList"
                :default-sort="defaultSort">
        <el-table-column label="分类" align="center" prop="id" width="50px"/>
        <el-table-column label="礼品信息" align="center">
          <el-table-column label="名称" align="center" prop="sort20"/>
          <el-table-column label="封面图" align="center" prop="sort634"/>
        </el-table-column>
        <el-table-column label="兑换信息" align="center">
          <el-table-column label="兑换方式" align="center" prop="sort65"/>
          <el-table-column label="兑换方式" align="center" prop="sort67"/>
        </el-table-column>
        <el-table-column label="配送类型" align="center">
          <el-table-column label="配送方式" align="center" prop="sort56"/>
          <el-table-column label="快递费用" align="center" prop="sort55"/>
        </el-table-column>
        <el-table-column label="礼品类型" align="center" prop="sort44" />
        <el-table-column label="礼品库存信息" align="center">
          <el-table-column label="总库存" align="center" prop="sort23"/>
          <el-table-column label="已用" align="center" prop="sort12"/>
          <el-table-column label="可用" align="center" prop="categoryName2"/>
        </el-table-column>
        <el-table-column label="排序" align="center" prop="sort" />
        <el-table-column label="状态" align="center" prop="status">
          <template slot-scope="scope">
            <div slot="reference" class="name-wrapper">
              <el-tag v-if ="scope.row.status == '启用'" size="medium">启用</el-tag>
              <el-tag v-if ="scope.row.status == '禁用'" type="danger" size="medium">禁用</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime"/>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
            >修改</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.page"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>

    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="dataForm" :rules="rules" label-width="80px">
        <el-row :gutter="24">
          <el-col :span="15">
            <el-form-item label="礼品名称" prop="categoryName">
              <el-input v-model="dataForm.categoryName" placeholder="分类名称" maxlength="30"/>
            </el-form-item>
          </el-col>
          <el-col :span="9">
            <el-form-item label="分类" prop="categoryName">
              <el-input v-model="dataForm.categoryName" placeholder="分类名称" maxlength="30"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="11">
            <el-form-item label="封面图" >
              <el-upload
                   :action="uploadAction"
                   list-type="picture-card"
                   :class="{hide:hideUpload}"
                   :file-list="uploadFiles"
                   :auto-upload="true"
                   :show-file-list="false"
                   :headers="uploadHeader"
                   :on-success="handleUploadSuccess">
                <img
                  v-if="this.form.logo"
                  :src="imagePath + this.form.logo"
                  class="list-img"
                />
                <i class="el-icon-plus"></i>
              </el-upload>
            </el-form-item>
          </el-col>

          <el-col :span="11">
            <el-form-item label="礼品图片" >
              <el-upload
                   :action="uploadAction"
                   list-type="picture-card"
                   :class="{hide:hideUpload}"
                   :file-list="uploadFiles"
                   :auto-upload="true"
                   :show-file-list="false"
                   :headers="uploadHeader"
                   :on-success="handleUploadSuccess">
                <img
                  v-if="this.form.logo"
                  :src="imagePath + this.form.logo"
                  class="list-img"
                />
                <i class="el-icon-plus"></i>
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col>
            <el-col :span="20">
            <el-form-item label="礼品类型" prop="status">
              <el-radio-group v-model="dataForm.status">
            <el-radio
              v-for="dict in dict.type.zhzt"
              :key="dict.value"
              :label="dict.label"
              @change="handleIsopenSelect"
              :value="dict.value"/>
            </el-radio-group>
            </el-form-item>
          </el-col>
          </el-col>
        </el-row>


        <el-row :gutter="24">
          <el-col :span="7.5">
            <el-form-item label="优惠卷" prop="supplierName">
              <el-input v-model="dataForm.sort" placeholder="分类名称" maxlength="30"/>
            </el-form-item>
          </el-col>
          <el-col :span="7.5">
            <el-form-item label="礼品数量" prop="supplierName">
              <el-input v-model="dataForm.sort" placeholder="分类名称" maxlength="30"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="24">
          <el-col :span="7.5">
            <el-form-item label="市场价格" prop="supplierName">
              <el-input v-model="dataForm.sort" placeholder="分类名称" maxlength="30"/>
            </el-form-item>
          </el-col>
          <el-col :span="7.5">
            <el-form-item label="礼品库存" prop="supplierName">
              <el-input v-model="dataForm.sort" placeholder="分类名称" maxlength="30"/>
            </el-form-item>
          </el-col>
        </el-row>


          
        <el-row>
          <el-col>
            <el-col :span="20">
            <el-form-item label="兑换方式" prop="status">
              <el-radio-group v-model="dataForm.status">
            <el-radio
              v-for="dict in dict.type.zhzt"
              :key="dict.value"
              :label="dict.label"
              @change="handleIsopenSelect"
              :value="dict.value"/>
            </el-radio-group>
            </el-form-item>
          </el-col>
          </el-col>
        </el-row>

        

        
        <el-row :gutter="24">
          <el-col :span="7.5">
            <el-form-item label="顿换积分" prop="supplierName">
              <el-input v-model="dataForm.sort" placeholder="分类名称" maxlength="30"/>
            </el-form-item>
          </el-col>
          <el-col :span="7.5">
            <el-form-item label="加钱比例" prop="supplierName">
              <el-input v-model="dataForm.sort" placeholder="分类名称" maxlength="30"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="24">
          <el-col :span="7.5">
            <el-form-item label="兑换次数" prop="supplierName">
              <el-input v-model="dataForm.sort" placeholder="分类名称" maxlength="30"/>
            </el-form-item>
          </el-col>
        </el-row>


        <el-row :gutter="24">
          <el-col :span="10">
            <el-form-item label="礼品排序" prop="contactPerson">
              <el-input-number v-model="dataForm.sort" :min="1" :max="10" label=""></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col>
            <el-col :span="20">
            <el-form-item label="礼品状态" prop="status">
              <el-radio-group v-model="dataForm.status">
            <el-radio
              v-for="dict in dict.type.zhzt"
              :key="dict.value"
              :label="dict.label"
              @change="handleIsopenSelect"
              :value="dict.value"/>
            </el-radio-group>
            </el-form-item>
          </el-col>
          </el-col>
        </el-row>

       <!-- <el-row :gutter="24">
          <el-col :span="23">
            <el-form-item label="图文详情" prop="contactPerson">
              <element-tiptap v-model="content" :extensions="extensions" :editor="editor" />

            </el-form-item>
          </el-col>
        </el-row>  -->



      </el-form>
      <div slot="footer" class="dialog-footer">
        <!-- <el-button type="primary" @click="submitForm">确 定</el-button> -->
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>




</div>
</template>


<script>
import { getToken } from '@/utils/auth'


export default {

  name: "gift",
  dicts: ['zhzt'],

  data() {
    return {
      dataList: [],

      dataForm:{
        id: null,
        categoryName: 1,
        sort: null,
        status: null
      },

      // 上传地址
      uploadAction: process.env.VUE_APP_SERVER_URL + 'backendApi/file/upload',
      uploadHeader: { 'Access-Token' : getToken() },
      // 隐藏上传
      hideUpload: false,
      // 上传文件列表
      uploadFiles: [],
           // 图片根目录
           imagePath: "",

      form:{logo:''},
      imageUrl: '',
      // 弹出框标题
      title:'',
      // 显示搜索条件
      showSearch: true,
      disableInput: false, // 默认不禁用
      // 是否显示弹出层
      open: true,
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        categoryName: 1,
        status: '',
        page:null,
        pageSize:null
      },
      dateRange:{},
      // 遮罩层
      loading: false,
      // 默认排序
      defaultSort: {prop: 'createTime', order: 'descending'},
      rules: {},

      /**
       * 表单
       */
       imageUrl: '',

    }
  },

  async created() {
    await this.getList();
  },
  methods: {
    getList() {
    },

    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.dataForm.id == null) {
            // 判断是否是新增还是修改
            insertGiftCategoryApi(this.dataForm).then(res => {
            this.getList()
            this.open = false
          })
          } else {
            // 判断是否是新增还是修改
            updateGiftCategoryApi(this.dataForm).then(res => {
              this.getList()
              this.open = false
          })
        }
      }})
    },
    handleIsopenSelect() {
      this.$forceUpdate()
    },
    // 新增
    giftAdd() {
      this.dataForm = {}
      this.dataForm.status = '启用'
      this.open = true
      this.title = '添加分类' 
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 搜索按钮操作
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    // 修改按钮
    handleUpdate(data) {
      this.dataForm = data
      this.open = true
      this.getList();
    },
    resetQuery() {

    },
    reset(){

    },



    /**
     * 表单
     */
    // 查询分类
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getGoodsCateInfo(id).then(response => {
        this.form = response.data.cateInfo;
        this.uploadFiles = [{ url: response.data.imagePath + this.form.logo, status: 'finished'}]
        this.open = true;
        this.title = "编辑商品分类";
      });
    },

    handleUploadSuccess(file) {
      this.form.logo = file.data.fileName;
    },

    handleAvatarSuccess(res, file) {
        this.imageUrl = URL.createObjectURL(file.raw);
      },
    beforeAvatarUpload(file) {
      const isJPG = file.type;
      const isLt2M = file.size / 1024 / 1024;

      // if (!isJPG) {
      //   this.$message.error('上传头像图片只能是 JPG 格式!');
      // }
      // if (!isLt2M) {
      //   this.$message.error('上传头像图片大小不能超过 2MB!');
      // }
      return isJPG && isLt2M;
    },

  }
}
</script>

<style scoped>

  .common-dialog >>> .el-upload--picture-card {
  width: 60px;
  height: 50px;
  line-height: 60px;
}
</style>
