<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品名称" prop="goodsName">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="请输入商品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核状态" prop="goodsStatus">
        <el-select v-model="queryParams.goodsStatus" placeholder="请选择审核状态" clearable size="small">
          <el-option
            v-for="dict in dict.type.hj_exa_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="审核时间" prop="goodsTime">-->
<!--        <el-date-picker clearable size="small"-->
<!--          v-model="queryParams.goodsTime"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="选择审核时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
      <el-form-item label="审核人" prop="reviewerName">
        <el-input
          v-model="queryParams.reviewerName"
          placeholder="请输入审核人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <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-hasPermi="['goods:goods:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['goods:goods:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['goods:goods:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->


<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleUpDateOK"-->
<!--        >批量通过</el-button>-->
<!--      </el-col>-->

<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleUpDateNO"-->
<!--        >批量不通过</el-button>-->
<!--      </el-col>-->



      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['goods:goods:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="goodsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="主键" align="center" prop="goodsId" />-->


      <el-table-column label="店铺名" align="center" prop="shopsName" />
      <el-table-column label="商品名称" align="center" prop="goodsName" />
<!--      <el-table-column label="商品原价" align="center" prop="goodsPrice" />-->
<!--      <el-table-column label="优惠价格" align="center" prop="goodsRatePrice" />-->

      <el-table-column label="审核状态" align="center" prop="goodsStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.hj_exa_status" :value="scope.row.goodsStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="审核时间" align="center" prop="goodsTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.goodsTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核人" align="center" prop="reviewerName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['goods:goods:edit']"-->
<!--          >修改</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['goods:goods:remove']"-->
<!--          >删除</el-button>-->



<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdateOK(scope.row)"-->
<!--            v-hasPermi="['shops:shops:edit']"-->
<!--          >审核通过</el-button>-->

<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdateNO(scope.row)"-->
<!--            v-hasPermi="['shops:shops:edit']"-->
<!--          >审核不通过</el-button>-->



          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['shops:shops: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="openshow" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item label="店铺名称" prop="goodsName">
          <el-input v-model="form.shopsName" placeholder="请输入商品名称" />
        </el-form-item>

        <el-form-item label="商品名称" prop="goodsName">
          <el-input v-model="form.goodsName" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="优惠价格" prop="goodsPrice">
          <el-input v-model="form.goodsPrice" placeholder="请输入优惠价格" />
        </el-form-item>
        <el-form-item label="商品原价" prop="goodsRatePrice">
          <el-input v-model="form.goodsRatePrice" placeholder="请输入商品原价" />
        </el-form-item>
        <el-form-item label="商品介绍" prop="goodsContent">
          <el-input v-model="form.goodsContent" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="商品图片">
          <imageUpload v-model="form.goodsPic"/>
        </el-form-item>
        <el-form-item label="审核状态">
          <el-radio-group v-model="form.goodsStatus">
            <el-radio
              v-for="dict in dict.type.hj_exa_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="审核理由" prop="remark">
          <el-input v-model="form.remark" type="textarea" 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>




    <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="remark">
          <el-input v-model="form.remark" placeholder="请输审核理由" type="textarea" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormOne">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="openhandleUpDateOK" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="审核理由" prop="remark">
          <el-input v-model="form.remark" placeholder="请输审核理由" type="textarea" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitHandleUpDateOKForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <el-dialog :title="title" :visible.sync="openhandleUpDateNO" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="审核理由" prop="remark">
          <el-input v-model="form.remark" placeholder="请输审核理由" type="textarea" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitHandleUpDateNOForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import { listGoods, getGoods, delGoods, addGoods, updateGoods, exportGoods,handleUpDateOKShops } from "@/api/goods/goodsindex";

export default {
  name: "Goods",
  dicts: ['hj_exa_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品信息表格数据
      goodsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openshow:false,
      openhandleUpDateOK:false,
      openhandleUpDateNO:false,

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodsName: null,
        goodsPic: null,
        goodsStatus: null,
        goodsTime: null,
        reviewerName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询商品信息列表 */
    getList() {
      this.loading = true;
      listGoods(this.queryParams).then(response => {
        this.goodsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openshow = false;
      this.openhandleUpDateOK= false;
      this.openhandleUpDateNO= false;
      this.openShow=false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        shopsName:null,
        goodsId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        goodsName: null,
        goodsPrice: null,
        goodsRatePrice: null,
        goodsContent: null,
        goodsPic: null,
        goodsStatus: "0",
        shopsId: null,
        goodsReviewer: null,
        goodsTime: null,
        reviewerName: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.goodsId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const goodsId = row.goodsId || this.ids
      getGoods(goodsId).then(response => {
        this.form = response.data;
        this.openshow = true;
        this.title = "查看商品详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.goodsId != null) {
            updateGoods(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGoods(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const goodsIds = row.goodsId || this.ids;
      this.$modal.confirm('是否确认删除商品信息编号为"' + goodsIds + '"的数据项？').then(function() {
        return delGoods(goodsIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有商品信息数据项？').then(() => {
        this.exportLoading = true;
        return exportGoods(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    },

    handleUpdateOK(row){
      this.reset();
      const goodsId = row.goodsId
      this.open = true;
      this.title = "审核通过商品信息";
      this.form.goodsId = goodsId;
      this.form.goodsStatus=2;
    },
    handleUpdateNO(row){
      this.reset();
      const goodsId = row.goodsId
      this.open = true;
      this.title = "审核不通过商品信息";
      this.form.goodsId = goodsId;
      this.form.goodsStatus=3;
    },
    handleUpDateOK(row){

      this.reset();
      this.openhandleUpDateOK = true;
      this.title = "批量审核通过";
    },
    handleUpDateNO(){

      this.reset();
      this.openhandleUpDateNO = true;
      this.title = "批量审核拒绝";
    },

    formSubmit(){
      const shopsIds =  this.ids;
      console.log(this.ids)
      handleUpDateOKShops(shopsIds,this.form).then(response => {
        this.$modal.msgSuccess("批量审核成功");
        this.open = false;
        this.openhandleUpDateOK= false;
        this.openhandleUpDateNO= false;
        this.openShow=false;
        this.reset();
        this.getList();
      });
    },
    submitHandleUpDateNOForm(){
      this.form.goodsStatus=3;
      this.formSubmit()
    },
    submitHandleUpDateOKForm(){
      this.form.goodsStatus=2;
      this.formSubmit()
    },
    submitFormOne(){
      handleUpDateOKShops(this.form.goodsId,this.form).then(response => {
        this.$modal.msgSuccess("审核成功");
        this.open = false;
        this.openhandleUpDateOK= false;
        this.openhandleUpDateNO= false;
        this.openShow=false;
        this.reset();
        this.getList();
      });
    }
  }
};
</script>
