<template>
  <div class="app-container">

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-if="this.goodsCopy === 0 || this.$store.getters.roles == 'admin'"
        >新增</el-button>
      </el-col>
    </el-row>

    <el-table :data="goodsProductList">
<!--      <el-table-column type="selection" width="55" align="center" />-->
      <!--<el-table-column label="主键ID" align="center" prop="id" />-->
      <el-table-column label="阶梯数量" align="center" prop="productName" />
      <el-table-column label="规格数量" align="center" prop="num" />
      <el-table-column label="价格" align="center" prop="price" />
      <el-table-column label="vip价" align="center" prop="priceVip" />
      <el-table-column label="采购价" align="center" prop="purchasePrice" v-if="this.$store.getters.permissions.includes('mall:mallGoodsProduct:purchasePrice') || this.$store.getters.roles == 'admin'" />
<!--      <el-table-column label="状态" align="center" prop="status" >-->
<!--        <template slot-scope="scope">-->
<!--          <dict-tag :options="statusOptions" :value="scope.row.status"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="是否默认" align="center" prop="isDefault" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" v-if="this.goodsCopy === 0 || this.$store.getters.roles == 'admin'">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.$index)"
            v-hasPermi="['mall:mallGoodsProduct:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row,scope.$index)"
            v-hasPermi="['mall:mallGoodsProduct:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改商品规格对话框 -->
    <el-dialog :title="title" :visible.sync="openGoodsProductDetails" width="500px" append-to-body>
        <el-form ref="goodsProductForm" :model="goodsProductForm" :rules="rules" label-width="140px">
<!--        <el-form-item label="是否默认 0=不是,1=是" prop="isDefault">-->
<!--          <el-input v-model="form.isDefault" placeholder="请输入是否默认 0=不是,1=是" />-->
<!--        </el-form-item>-->
        <el-form-item label="阶梯数量" prop="productName">
          <el-input v-model="goodsProductForm.productName" placeholder="请输入阶梯数量" />
        </el-form-item>
        <el-form-item label="规格数量" prop="num">
          <el-input v-model="goodsProductForm.num" placeholder="请输入规格数量" type="number" :min="1" />
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="goodsProductForm.price" placeholder="请输入价格" type="number" :min="0" />
        </el-form-item>
          <el-form-item label="vip价" prop="priceVip">
            <el-input v-model="goodsProductForm.priceVip" placeholder="请输入vip价" type="number" :min="0" />
          </el-form-item>
          <el-form-item label="采购价" prop="purchasePrice" v-has-permi="['mall:mallGoodsProduct:purchasePrice']">
            <el-input v-model="goodsProductForm.purchasePrice" placeholder="请输入采购价" type="number" :min="0" />
          </el-form-item>
<!--        <el-form-item label="状态" prop="status">-->
<!--          &lt;!&ndash;          0 上架 1 下架 2 删除&ndash;&gt;-->
<!--          <el-radio-group v-model="goodsProductForm.status">-->
<!--            <el-radio-->
<!--              v-for="dict in statusOptions"-->
<!--              :key="dict.dictValue"-->
<!--              :label="dict.dictValue"-->
<!--            >{{dict.dictLabel}}</el-radio>-->
<!--          </el-radio-group>-->
<!--        </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>

export default {
  name: "goodsProduct",
  props: {
    // 商品规格列表
    goodsProductList: {
      type: Array
    },
    // 是否复制
    goodsCopy: {
      type: Number,
      default: 0,
    }
  },
  data() {
    return {
      // 临时
      tempObj: '',
      // 选中的下标
      indexFlag: -1,
      // 操作标识（0-修改，1-添加）
      submitFlag: 0,
      goodsProductForm:{},
      statusOptions: [],
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品 产品表格数据
      // goodsProductList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      openGoodsProductDetails: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productName: null,
        priceVip: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        productName: [
            { required: true, message: "请输入规格名称", trigger: "blur" }
        ],
        price: [
              { required: true, message: "请输入价格", trigger: "blur" }
          ],
        priceVip: [
              { required: true, message: "请输入vip价", trigger: "blur" }
          ],
        purchasePrice: [
              { required: true, message: "请输入采购价", trigger: "blur" }
          ],
        num: [
              { required: true, message: "请输入规格数量", trigger: "blur" }
          ],
      }
    };
  },
  created() {
    this.getDicts("mall_goods_status").then(response => {
      this.statusOptions = response.data;
    });

  },
  methods: {

    // 取消按钮
    cancel() {
        this.submitFlag = 0;
        this.openGoodsProductDetails = false;
        // 点击取消数据还原
        var obj = JSON.parse(this.tempObj)
        this.goodsProductList.splice(this.indexFlag,1, obj);
        this.indexFlag = -1;
        this.reset();
    },
    // 表单重置
    reset() {
      this.goodsProductForm = {
        id: null,
        goodsId: null,
        isDefault: null,
        productName: null,
        price: null,
        priceVip: null,
        priceN: null,
        priceM: null,
        stock: null,
        upAt: null,
        downAt: null,
        numSale: null,
        status: "0",
        createAt: null,
        delFlag: null,
        shopOneStatus: "0",
        shopTwoStatus: "0",
      };
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.submitFlag = 1;
      this.openGoodsProductDetails = true;
      this.title = "添加商品规格";
    },

    /** 修改按钮操作 */
    handleUpdate(index) {
      this.reset();
      this.title = "修改商品规格";
      this.indexFlag = index;
      this.submitFlag = 0;
      var obj = this.goodsProductList[index];
      this.goodsProductForm = {
          id: obj.id,
          goodsId: obj.goodsId,
          isDefault: obj.isDefault,
          productName: obj.productName,
          price: obj.price,
          priceVip: obj.priceVip,
          status: obj.status,
          stock: obj.stock,
          num: obj.num,
          integral: obj.integral,
          shopOneStatus: obj.shopOneStatus,
          shopTwoStatus: obj.shopTwoStatus,
      }
      this.openGoodsProductDetails = true
      // 临时数据
      this.tempObj = JSON.stringify(obj);
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["goodsProductForm"].validate(valid => {
          if (valid) {
              if (this.submitFlag === 0){
                  // 修改
                  this.goodsProductList.splice(this.indexFlag,1,this.goodsProductForm);
                  this.msgSuccess("修改成功");
                  this.openGoodsProductDetails = false;
                  this.indexFlag = -1;
              } else {
                  // 添加
                  this.goodsProductList.push(this.goodsProductForm);
                  this.msgSuccess("新增成功");
                  this.openGoodsProductDetails = false;
                  this.indexFlag = -1;
              }
          }
      });
      this.$emit('getGoodsProductList',this.goodsProductList);
    },
    /** 删除按钮操作 */
    handleDelete(row,index) {
      this.$confirm('是否确认删除？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
      }).then(() => {
          this.goodsProductList.splice(index,1);
          this.$message({
              type: 'success',
              message: '删除成功'
          });
      }).catch(() => {
      });
    },
  }
};
</script>
