<template>
  <div class="app-container">
    <!-- :logoSrc="imageUrl" -->
    <!-- <vue-qr text="https://www.baidu.com/" :size="200"></vue-qr> -->
    <el-card class="operate-container" shadow="never" style="margin-top:0">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
      <el-button class="btn-add" @click="handleAdd" size="small" type="success"
        >添加活动</el-button
      >
    </el-card>
    <el-table style="width: 100%" class="table-container" :data="dataList">
      <el-table-column
        label="序号"
        align="center"
        type="index"
      ></el-table-column>
      <el-table-column
        label="活动名称"
        align="center"
        prop="name"
      ></el-table-column>
      <el-table-column label="活动二维码" align="center" prop="pic">
        <template slot-scope="scope">
          <vue-qr :text="path + '?id=' + scope.row.id" :size="140"></vue-qr>
        </template>
      </el-table-column>
      <el-table-column label="商品图片" align="center" prop="pic">
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.pic"
            fit="cover"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column
        label="单位"
        align="center"
        prop="unit"
      ></el-table-column>
      <el-table-column
        label="详情"
        align="center"
        prop="note"
        show-overflow-tooltip
      >
        <!-- <template slot-scope="scope">
          <div v-html="scope.row.note"></div>
        </template> -->
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
      ></el-table-column>
      <el-table-column
        label="截止时间"
        align="center"
        prop="lastTime"
      ></el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button @click="handlePage(scope.row)" type="text" size="small">
            领取名单
          </el-button>
          <el-button @click="handleUpdate(scope.row)" type="text" size="small">
            编辑
          </el-button>
          <el-button
            @click="handleDelete(scope.row.id)"
            type="text"
            size="small"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getDataList"
    />
    <!-- 添加修改 -->
    <el-dialog :title="title" :visible.sync="dialogVisible" width="50%">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="活动名称" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model="form.unit"></el-input>
        </el-form-item>
        <el-form-item label="截止时间" prop="lastTime">
          <el-date-picker
            v-model="form.lastTime"
            type="datetime"
            value-format="yyyy-MM-dd hh:mm:ss"
            placeholder="选择截止时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="商品详情" prop="note">
          <div id="text">
            <editor v-model="form.note" :min-height="192" />
          </div>
        </el-form-item>
        <el-form-item label="商品图片" prop="pic">
          <multi-upload
            v-model="selectProductPics"
            :maxCount="1"
          ></multi-upload>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  qrList,
  qrCreate,
  qrUpdate,
  qrDelete,
  handleProduct,
  addProduct
} from '@/api/index';
import MultiUpload from '@/components/Upload/multiUpload';
import VueQr from 'vue-qr';
import Editor from '@/components/Editor';
export default {
  name: 'Index2',
  components: {
    MultiUpload,
    VueQr,
    Editor
  },
  data() {
    return {
      imageUrl: require('@/assets/logo/logo.png'), //icon路径
      path:
        process.env.NODE_ENV === 'development'
          ? 'http://192.168.0.125:1777'
          : process.env.VUE_APP_BASE_API,
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      total: 0,
      dataList: [],
      dialogVisible: false,
      title: '',
      form: {},
      rules: {
        name: [{ required: true, message: '请输入', trigger: 'blur' }],
        unit: [{ required: true, message: '请输入', trigger: 'blur' }],
        pic: [{ required: true, message: '请上传', trigger: 'blur' }],
        note: [{ required: true, message: '请输入', trigger: 'blur' }],
        lastTime: [
          {
            required: true,
            message: '请选择日期',
            trigger: 'change'
          }
        ]
      }
    };
  },
  computed: {
    //主图和画册图片
    selectProductPics: {
      get: function() {
        let pics = [];
        if (
          this.form.pic === undefined ||
          this.form.pic == null ||
          this.form.pic === ''
        ) {
          return pics;
        }
        pics.push(this.form.pic);
        if (
          this.form.albumPics === undefined ||
          this.form.albumPics == null ||
          this.form.albumPics === ''
        ) {
          return pics;
        }
        let albumPics = this.form.albumPics.split(',');
        for (let i = 0; i < albumPics.length; i++) {
          pics.push(albumPics[i]);
        }
        return pics;
      },
      set: function(newValue) {
        if (newValue == null || newValue.length === 0) {
          this.form.pic = null;
          this.form.albumPics = null;
        } else {
          this.form.pic = newValue[0];
          this.form.albumPics = '';
          if (newValue.length > 1) {
            for (let i = 1; i < newValue.length; i++) {
              this.form.albumPics += newValue[i];
              if (i !== newValue.length - 1) {
                this.form.albumPics += ',';
              }
            }
          }
        }
      }
    }
  },
  methods: {
    handleSearchList() {
      this.queryParams.pageNum = 1;
      this.getDataList();
    },
    getDataList() {
      qrList(this.queryParams).then(({ data }) => {
        this.dataList = data.list;
        this.total = data.total;
      });
    },
    // 添加
    handleAdd() {
      this.form = {};
      this.resetForm('form');
      this.dialogVisible = true;
      this.title = '添加商品';
    },
    // 编辑
    handleUpdate(row) {
      this.form = {};
      this.resetForm('form');
      this.form = Object.assign({}, row);
      this.dialogVisible = true;
      this.title = '修改活动';
    },
    // 提交
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.title == '修改活动') {
            qrUpdate(this.form).then(response => {
              this.msgSuccess('修改成功');
              this.dialogVisible = false;
              this.getDataList();
            });
          } else {
            qrCreate(this.form).then(response => {
              this.msgSuccess('新增成功');
              this.dialogVisible = false;
              this.getDataList();
            });
          }
        }
      });
    },
    // 跳转页面
    handlePage(row) {
      this.$router.push({
        path: '/index1',
        query: { row: JSON.stringify(row) }
      });
      // this.$router.push({ path: '/index1', query: { id: row.id } });
    },
    /** 删除按钮操作 */
    handleDelete(id) {
      this.$confirm('确认删除该数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(function() {
          return qrDelete({ id });
        })
        .then(() => {
          this.getDataList();
          this.msgSuccess('删除成功');
        });
    }
  },
  created() {
    this.getDataList();
    console.log(this.path);
  }
};
</script>
<style lang="scss" scoped>
#text {
  /deep/.ql-toolbar .ql-formats .ql-image {
    display: none;
  }
}
</style>
