<template>
  <div class="app-container">
    <el-card shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
      <el-button style="float: right" @click="open" size="mini" type="primary">
        添加轮播
      </el-button>
    </el-card>
    <el-table style="width: 100%" class="table-container" :data="rotationList">
      <el-table-column
        label="序号"
        align="center"
        type="index"
      ></el-table-column>
      <el-table-column label="轮播图片" prop="pic" align="center">
        <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="url"
      ></el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
      ></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.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="getList"
    />
    <el-dialog
      title="轮播图"
      :visible.sync="dialogVisible"
      width="30%">
        <el-form
        :model="ruleForm"
        ref="ruleForm"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item label="跳转链接" prop="url">
          <el-input placeholder="请输入" v-model="ruleForm.url"></el-input>
        </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('ruleForm')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { getRotationList,addRotation, deleteRotation, updateRotation } from '@/api/rotation';
import MultiUpload from '@/components/Upload/multiUpload';
export default {
   components: {
    MultiUpload
  },
  data() {
    return {
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      total: 0,
      rotationList: [],
      dialogVisible: false,
      ruleForm: {
        url: '',
        pic: ''
      },
      rules: {
        url: [{ required: true, message: '请输入', trigger: 'blur' }],
        pic: [{ required: true, message: '请上传', trigger: 'blur' }]
      }
    };
  },
  computed: {
    //主图和画册图片
    selectProductPics: {
      get: function() {
        let pics = [];
        if (
          this.ruleForm.pic === undefined ||
          this.ruleForm.pic == null ||
          this.ruleForm.pic === ''
        ) {
          return pics;
        }
        pics.push(this.ruleForm.pic);
        if (
          this.ruleForm.albumPics === undefined ||
          this.ruleForm.albumPics == null ||
          this.ruleForm.albumPics === ''
        ) {
          return pics;
        }
        let albumPics = this.ruleForm.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.ruleForm.pic = null;
          this.ruleForm.albumPics = null;
        } else {
          this.ruleForm.pic = newValue[0];
          // this.ruleForm.picList = newValue;
          this.ruleForm.albumPics = '';
          if (newValue.length > 1) {
            for (let i = 1; i < newValue.length; i++) {
              this.ruleForm.albumPics += newValue[i];
              if (i !== newValue.length - 1) {
                this.ruleForm.albumPics += ',';
              }
            }
          }
        }
      }
    }
  },
  methods: {
    getList() {
      getRotationList(this.queryParams).then(({ data }) => {
        this.rotationList = data.list;
        this.total = data.total
      });
    },
    open(){
      this.dialogVisible = true
    },
    handleUpdate(row){
      this.ruleForm = Object.assign({}, row)
      this.dialogVisible = true
    },
    submitForm(formName){
      this.$refs[formName].validate(valid=>{
        if(valid){
          if(this.ruleForm.id){
            updateRotation(this.ruleForm).then(res=>{
              this.msgSuccess(res.data)
              this.ruleForm = {
                url: '',
                pic: ''
              }
              this.getList()
              this.dialogVisible = false
            })
          }else{
            addRotation(this.ruleForm).then(res=>{
              this.msgSuccess(res.data)
              this.ruleForm = {
                url: '',
                pic: ''
              }
              this.getList()
              this.dialogVisible = false
            })
          }
        }
      })
      // this.dialogVisible = false
    },
    /** 删除按钮操作 */
    handleDelete(id) {
      this.$confirm('确认删除该轮播图?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(function() {
          return deleteRotation({id});
        })
        .then(() => {
          this.getList();
          this.msgSuccess('删除成功');
        });
    }
  },
  created() {
    this.getList();
  }
};
</script>
<style lang="scss" scoped>
// .el-tag + .el-tag {
//   margin-left: 10px;
// }
</style>
