<template>
  <div class="app-container">
    <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"
        >添加商品</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="integral"
      ></el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button @click="handleUpdate(scope.row)" type="text" size="small"
            >编辑</el-button
          >
          <el-button @click="handleDelete(scope.row)" 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="25%">
      <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="integral">
          <el-input-number
            v-model="form.integral"
            :min="1"
            :step="100"
            step-strictly
          ></el-input-number>
        </el-form-item>
        <el-form-item label="图片" prop="pic">
          <multi-upload
            v-model="selectProductPics"
            :maxCount="1"
          ></multi-upload>
        </el-form-item>
        <el-form-item label="详细信息" prop="note">
          <el-input
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 10 }"
            v-model="form.note"
          ></el-input>
        </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 { getList2, handleProduct, addProduct } from '@/api/index';
import MultiUpload from '@/components/Upload/multiUpload';
export default {
  name: 'Index2',
  components: {
    MultiUpload
  },
  data() {
    return {
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        phone: ''
      },
      total: 0,
      dataList: [],
      dialogVisible: false,
      title: '',
      form: {},
      rules: {
        name: [{ required: true, message: '请输入', trigger: 'blur' }],
        integral: [{ required: true, message: '请输入', trigger: 'blur' }],
        pic: [{ required: true, message: '请上传', trigger: 'blur' }],
        note: [{ required: true, message: '请输入', trigger: 'blur' }]
      }
    };
  },
  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() {
      getList2(this.queryParams).then(({ data }) => {
        this.dataList = data.list;
        this.total = data.total;
      });
    },
    // 添加
    handleAdd() {
      this.form = { integral: 100 };
      this.resetForm('form');
      this.dialogVisible = true;
      this.title = '添加商品';
    },
    // 编辑
    handleUpdate(row) {
      this.form = {};
      this.resetForm('form');
      row.status = 0;
      this.form = Object.assign({}, row);
      this.dialogVisible = true;
      this.title = '修改商品';
    },
    // 提交
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.title == '修改商品') {
            handleProduct(this.form).then(response => {
              this.msgSuccess('修改成功');
              this.dialogVisible = false;
              this.getDataList();
            });
          } else {
            addProduct(this.form).then(response => {
              this.msgSuccess('新增成功');
              this.dialogVisible = false;
              this.getDataList();
            });
          }
        }
      });
    },
    // 跳转页面
    handlePage(id) {
      this.$router.push({ path: '/front/addUnscramble', query: { id } });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('确认删除该数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(function() {
          row.status = -1;
          return handleProduct(row);
        })
        .then(() => {
          this.getDataList();
          this.msgSuccess('删除成功');
        });
    }
  },
  created() {
    this.getDataList();
  }
};
</script>
