<template>
  <div>
    {{goods}}
    <el-table
      :data="goodsList"
      style="width: 100%"
      @selection-change="handleSelectionChange">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        label="商品编号"
        width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.goodsId }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="商品名称"
        width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.goodsName }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="商品图片"
        width="180">
        <template slot-scope="scope">
          <img v-if="scope.row.goodsPic" :src="scope.row.goodsPic" class="avatar">
        </template>
      </el-table-column>
      <el-table-column
        label="商品价格"
        width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.goodsPrice }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        width="180">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['system:goods:edit']"
            :disabled="single"
            icon="el-icon-edit"
            plain
            size="mini"
            type="success"
            @click="handleEdit(scope.$index, scope.row)"
          >编辑
          </el-button>
          <el-button
            v-hasPermi="['system:goods:remove']"
            icon="el-icon-delete"
            size="mini"
            type="text"
            @click="handleDelete(scope.$index, scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-button
      icon="el-icon-delete"
      size="mini"
      type="text"
      @click="handleAdd()"
    >新增
    </el-button>
<!--    添加弹出框-->
    <el-dialog title="商品信息表单" :visible.sync="dialogFormVisible">
      {{imgUrl}}
      <el-form :model="goods">
        <el-form-item label="商品名称" :label-width="formLabelWidth">
          <el-input v-model="goods.goodsName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="商品价格" :label-width="formLabelWidth">
          <el-input v-model="goods.goodsPrice" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="商品图片" :label-width="formLabelWidth" :label-height="200">

            <el-upload
              style="height: 100%"
              class="avatar-uploader"
              action="http://localhost:9207/goods/uploadPicFile"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload">
              <img v-if="imgUrl" :src="imgUrl" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addGoods()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）,
//例如：import 《组件名称》 from '《组件路径》,
import {addGoods, deleteGoodsById, getGoodsList} from "@/api/system/goods";

export default {
    name: "index",
    //import引入的组件需要注入到对象中才能使用"
    components: {},
    props: {},
    data() {
      //这里存放数据"
      return {
        goods: {},
        dialogFormVisible: false,
        single: false,
        goodsList:[],
        queryParam: {
          pageNum: 1,
          pageSize: 10,
          goodsName: "",
          beginTime: null,
          endTime: null
        },
        ids: [],
        formLabelWidth: '120px',
        multipleSelection: [],
        imgUrl: ""
      };
    },
    //计算属性 类似于data概念",
    computed: {},
    //监控data中的数据变化",
    watch: {},
    //方法集合",
    methods: {
      getGoodsList() {
        getGoodsList(this.queryParam).then(
          res => {
            this.goodsList = res.data.rows
            console.log(res)
          }
        )
      },
      handleAdd(){
        this.dialogFormVisible = true
      },
      handleEdit(index, row) {
        console.log(index, row);
      },
      handleDelete(index, row) {
        console.log(row)
        this.ids.push(row.goodsId);
        deleteGoodsById(this.ids).then(
          res => {
            console.log(res)
            this.getGoodsList();
          }
        )
      },
      handleSelectionChange(val) {

        this.ids = val.map(item => item.goodsId);
        console.log(this.ids)
        this.multipleSelection = val;
      },
      addGoods() {
        this.goods.goodsPic = this.imgUrl;
        addGoods(this.goods).then(
          res => {
            this.goods = {}
            console.log(res);
            this.dialogFormVisible = false
          }
        )
      },
      handleAvatarSuccess(res, file) {
        this.imgUrl = res.data;
      },
      beforeAvatarUpload(file) {
        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isLt2M;
      }
    },
    //生命周期 - 创建完成（可以访问当前this实例）",
    created() {
      this.getGoodsList()
    },
    //生命周期 - 挂载完成（可以访问DOM元素）",
    mounted() {
    },
    beforeCreate() {
    }, //生命周期 - 创建之前",
    beforeMount() {
    }, //生命周期 - 挂载之前",
    beforeUpdate() {
    }, //生命周期 - 更新之前",
    updated() {
    }, //生命周期 - 更新之后",
    beforeDestroy() {
    }, //生命周期 - 销毁之前",
    destroyed() {
    }, //生命周期 - 销毁完成",
    activated() {
    } //如果页面有keep-alive缓存功能，这个函数会触发",
  };
</script>
<style scoped>

</style>
