<template>
  <div class="app-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
          <el-form :model="queryParams" ref="queryForm" :inline="true" class="queryFormInline">
            <el-row :span="24" type="flex" align="middle" justify="space-between">
              <el-col :span="20">
                <el-form-item label="商品Id" prop="goodsId">
                  <el-input
                    size="small"
                    placeholder="请输入商品Id"
                    v-model="queryParams.goodsId"
                  ></el-input>
                </el-form-item>
                <el-form-item label="商品名称" prop="goodsName">
                  <el-input
                    size="small"
                    placeholder="请输入商品名称"
                    v-model="queryParams.goodsName"
                  ></el-input>
                </el-form-item>
                <el-form-item>
                  <el-button
                    size="mini"
                    type="primary"
                    icon="el-icon-search"
                    @click="handleQuery"
                  >搜索</el-button>
                  <el-button
                    size="mini"
                    icon="el-icon-refresh"
                    @click="resetQuery('queryForm')"
                  >重置</el-button>
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item style="float: right">
                  <el-button
                    type="primary"
                    icon="el-icon-plus"
                    size="mini"
                    @click="handleAdd"
                  >新增
                  </el-button>
                  <el-button
                    type="success"
                    icon=""
                    size="mini"
                    @click="handleOffGoods"
                  >商品下架
                  </el-button>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
      </div>

      <el-table v-loading="loading" :data="goodsList" :height="tableHeight" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column label="商品主图" align="center" width="100" prop="defaultImage">
          <template slot-scope="scope">
            <el-image
              style="width: 60px; height: 50px"
              :src="scope.row.defaultImage"
            ></el-image>
          </template>
        </el-table-column>
        <el-table-column label="商品ID" align="center" prop="goodsId"/>
        <el-table-column label="商品名称" align="center" prop="goodsName"/>
        <el-table-column label="协议价" align="center" :formatter="formatePrice" prop="jsPrice"/>
        <el-table-column label="物流运费" align="center" :formatter="formatePrice" prop="wlPrice"/>
        <el-table-column label="市场售价" align="center" :formatter="formatePrice" prop="scPrice"/>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="handleOffGoods(scope.row)"
            >下架
            </el-button>
            <el-divider direction="vertical"></el-divider>
            <el-button
              size="mini"
              type="text"
              @click="handleUpdate(scope.row)"
            >编辑
            </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"
      />
      <!-- 添加或修改goods对话框 -->
      <el-dialog :title="title" :visible.sync="open" :modal-append-to-body="false" width="90%"
                 style="height:90%;overflow:hidden;" append-to-body>
        <IndexBtn :option="form"/>
      </el-dialog>
    </el-card>
  </div>
</template>
<script>
  import IndexBtn from '../add'
  import {listGoods, getGoods, delGoods, addGoods, updateGoods, Onsale} from '@/api/module/goods'

  export default {
    name: "goods",
    data() {
      return {
        fullHeight: 0,
        tableHeight: 0,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // goods表格数据
        goodsList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          isOnsale: 1,
          goodsId: '',
          goodsName: ''
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          goods_name: [
            {required: true, message: "商品名称不能为空", trigger: "blur"}
          ],
        }
      };
    },
    components: {
      IndexBtn
    },
    created() {
      this.getList();
      this.$nextTick(() => {
        this.fullHeight = document.getElementsByClassName('box-card')[0].clientHeight
      })
    },
    watch: {
      fullHeight(val) {
        let formHeight = document.getElementsByClassName('clearfix')[0].clientHeight
        this.tableHeight = val - formHeight - 100;
      }
    },
    methods: {
      /*商品下架*/
      handleOffGoods(row) {
        const rowIds = row.goodsId || this.ids;
        let params = {"ids": rowIds, "status": 0};
        if (!rowIds || rowIds.length === 0) {
          this.$message({message: '请先选择要下架的商品数据', type: 'warning'});
          return;
        }
        this.$confirm('是否确认下架商品ID为"' + rowIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return Onsale(params);
        }).then(() => {
          this.getList();
          this.msgSuccess("下架成功");
        }).catch(() => {

        });
      },
      //格式化价格
      formatePrice(row, s, value, i) {
        var nm = 0
        var money = value
        if (money > 0) {
          nm = money / 100
        }
        return nm
      },
      /** 查询goods列表 */
      getList() {
        this.loading = true;
        listGoods(this.queryParams).then(res => {
          if(res.code === 1 && res.data) {
            this.goodsList = res.data.list;
            this.total = res.data.count;
          }else if(res.code === 0) {
            if(res.msg) {
              this.$message({type: 'error',message: res.msg});
            }else {
              this.$message({type: 'error',message: '数据出错啦！'});
            }
          }
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {};
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.goodsId)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 下架操作 */
      handleOnsale() {
        Onsale({"ids": this.ids, "status": 0}).then(response => {
          this.msgSuccess("成功申请" + response.data.length + "个商品");
        });
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.$router.push({
          path: '/system/goods/add'
        });
      },
      /** 详情按钮操作 */
      handleUpdate(row) {
        const goods_id = row.goodsId
        this.$router.push({
          path: '/system/goods/add', query: {goodsId: goods_id}

        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.goodsId != null) {
              updateGoods(this.form).then(response => {
                if (response.code === 0) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                }
              });
            } else {
              addGoods(this.form).then(response => {
                if (response.code === 0) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                }
              });
            }
          }
        });
      },
      // 重置
      resetQuery(form) {
        this.resetForm("queryForm");
        this.handleQuery();
      },
    } //methods结束
  };
</script>
<style scoped>
  .app-container {
    height: 100%;
  }

  /deep/ .el-dialog {
    height: 100%;
  }

  /deep/ .el-dialog__body {
    height: 80%;
    overflow-y: scroll;
  }

  .box-card {
    height: 100%;
  }

  .box-card /deep/ .el-card__body {
    height: 100%;
    overflow: hidden;
  }

  .el-table {
    height: calc(100% - 120px);
  }
</style>
