<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true"  label-width="68px">
      <el-form-item label="商品名称" prop="waresName">
        <el-input
          v-model="queryParams.waresName"
          placeholder="请输入商品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button type="info" icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="waresList" >
      <el-table-column label="商品id" align="center" prop="id"  v-if="false" />
      <el-table-column label="商品类别" align="center" prop="waresType"  v-if="false" />
      <el-table-column label="商品名称" align="center" prop="waresName" />
      <el-table-column label="消费积分" align="center" prop="saleIntegral" />
      <el-table-column label="商品图片" align="center" prop="waresPic" v-if="false" />
      <el-table-column label="商品详情" align="center" prop="waresDetail"  width="155" />
      <el-table-column label="油站ID" align="center" prop="stationId" v-if="false" />
      <el-table-column label="油站名称" align="center" prop="stationName" />
      <el-table-column label="库存" align="center" prop="waresCount" />
      <el-table-column label="商品状态" align="center" prop="waresStatus" :formatter="waresStatusFotmat" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <div  v-if="scope.row.waresStatus==2">
            <el-button
                       size="mini"
                       type="text"
                       icon="el-icon-edit"
                       @click="updateSJ(scope.row)"
            >上架</el-button>
          </div>
          <div  v-if="scope.row.waresStatus==1">
            <el-button size="mini"
                       type="text"
                       icon="el-icon-edit"
                       @click="updateSJ(scope.row)"
            >下架</el-button>
          </div>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['integral:wares:edit']"
          >修改</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="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商品图片" prop="waresPic">
          <el-upload
            class="avatar-uploader"
            :action="addressUrl"
            :headers="headers"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload">
            <img v-if="form.waresPic" :src="form.waresPic" style="width:150px;height: 150px;" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            <div class="el-upload__tip" slot="tip">点击图片重新上传，只能上传jpg/png文件，图片大小不超过2M</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="商品名称" prop="waresName">
          <el-input v-model="form.waresName" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="商品详情" prop="waresDetail">
          <textarea v-model="form.waresDetail" placeholder="请输入商品详情" rows="4" cols="50"></textarea>
        </el-form-item>
        <el-form-item label="消费积分" prop="saleIntegral">
          <el-input-number v-model="form.saleIntegral" :min="0" placeholder="请输入消费积分" />
        </el-form-item>
        <el-form-item label="库存"  prop="waresCount">
          <el-input-number v-model="form.waresCount" :min="0" placeholder="请输入库存数量" />
        </el-form-item>
      </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 { listWares, getWares, editInfoWares, addWares, updateWares, exportWares } from "@/api/integral/wares";
import {getToken} from "@/utils/auth";
import { getDept } from "@/api/system/dept";
export default {
  name: "Wares",
  data() {
    return {
      // 总条数
      total: 0,
      // 表格数据
      waresList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        waresName: null
      },
      deptInfo:{},
      deptId: null,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      headers: { Authorization: "Bearer " + getToken() },
      addressUrl: location.protocol+"//"+location.host+ process.env.VUE_APP_BASE_API+"/common/upload"
  
    };
  },
  created() {
    this.getList();
    
  },
  methods: {
    waresStatusFotmat(row, column) {
      if (row.waresStatus === '1') {
        return '已上架'
      } else if (row.waresStatus === '2') {
        return '已下架'
      }
    },
   
    /** 查询【请填写功能名称】列表 */
    getList() {
      listWares(this.queryParams).then(response => {
        this.waresList = response.rows;
        this.total = response.total;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        waresType: null,
        waresName: null,
        saleIntegral: null,
        waresPic: null,
        waresDetail: null,
        waresStatus: "1",
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        waresCount: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.deptId=this.$store.selectDeptId;
      if(this.deptId==null || this.deptId==""){
        this.deptId =this.$store.state.user.deptId;
      }
      getDept(this.deptId).then(response => {
          this.deptInfo = response.data;
          if(this.deptInfo.jiBie==2){
            this.open = true;
            this.title = "添加商品信息";
          }else{
              this.msgSuccess("请选择油站");
          }
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id 
      getWares(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商品信息";
      });
    },
    updateSJ(row){
      this.reset();
      this.form.id= row.id;
      if(row.waresStatus==1){
        this.form.waresStatus=2;
      }else{
        this.form.waresStatus = 1;
      }
      if (this.form.id != null) {
        editInfoWares(this.form).then(response => {
          this.msgSuccess("修改成功1");
          this.getList();
        });
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateWares(this.form).then(response => {
              this.msgSuccess("修改成功2");
              this.open = false;
              this.getList();
            });
          } else {
            addWares(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // 文件上传成功处理++ =
    handleAvatarSuccess(res, file) {
      let imgurl=location.protocol+"//"+location.host+process.env.VUE_APP_BASE_API+res .fileName;
      this.form.waresPic = imgurl;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'|| 'image/png';
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    }
  }
};
</script>
