<template>
  <div class="app-container">
    <div class="card-change" >
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="90px">
        <el-form-item label="" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入商品名称"
            clearable
            style="width: 240px;"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>

        <el-form-item label="请选择分类" prop="categoryName">
          <el-select v-model="queryParams.categoryName" style="width: 150px" placeholder="全部">
            <el-option
              v-for="dict in dict.type.hardware_type"
              :key="dict.label"
              :label="dict.label"
              :value="dict.label"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="请选择状态" prop="status">
          <el-select v-model="queryParams.status" style="width: 150px" placeholder="全部">
            <el-option
              v-for="dict in dict.type.hardware_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item style="float: right">
          <el-button type="primary" icon="el-icon-search"  @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh"  @click="resetQuery">重置</el-button>
          <el-button
            type="primary"
            icon="el-icon-plus"
            @click="handleAdd"
            v-hasPermi="['setting:hardware:add']"
          >新增商品</el-button>
        </el-form-item>
      </el-form>

      <div style="overflow: auto">
      <el-table :data="tableData" style="width: 100%" border>
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column prop="name" label="商品名称"></el-table-column>
        <el-table-column prop="categoryName" label="商品分类"></el-table-column>
        <el-table-column prop="image" label="商品图片">
          <template slot-scope="scope">
            <ImagePreview :src="scope.row.image"></ImagePreview>
          </template>
        </el-table-column>
        <el-table-column prop="formStore" label="采购商家"></el-table-column>
        <el-table-column prop="content" label="商品介绍"></el-table-column>
        <el-table-column prop="status" label="商品状态">
          <template slot-scope="scope">
            <div v-if="scope.row.status==0">
              未上架
            </div>
            <div v-else>
              已上架
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="createName" label="创建人"></el-table-column>
        <el-table-column prop="createTime" label="创建时间"></el-table-column>
        <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="edit(scope.row)"
              v-hasPermi="['setting:hardware:edit']"
            >编辑</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-top"
              v-if="scope.row.status == 0 "
              @click="shangjia(scope.row)"
              v-hasPermi="['setting:hardware:grounding']"
            >上架</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-bottom"
              v-if="scope.row.status == 1 "
              @click="shangjia(scope.row)"
              v-hasPermi="['setting:hardware:offShelf']"
            >下架</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="del(scope.row)"
              v-hasPermi="['setting:hardware:delete']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      </div>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.page"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

    </div>

    <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="open" width="40%" append-to-body>
      <el-form ref="form" :rules="rules" :model="form" label-width="80px">

          <el-form-item label="商品名称" prop="name">
            <el-input v-model="form.name" style="width: 100%" placeholder="请输入商品名称"></el-input>
          </el-form-item>
          <el-form-item label="商品分类" prop="categoryName">
            <el-select v-model="form.categoryName" style="width: 100%" placeholder="请选择商品分类">
              <el-option
                v-for="dict in dict.type.hardware_type"
                :key="dict.label"
                :label="dict.label"
                :value="dict.label"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="采购商家" prop="formStore">
            <el-input v-model="form.formStore" style="width: 100%" placeholder="请输入采购商家名称"></el-input>
          </el-form-item>
          <el-form-item label="图片" prop="image">
           <ImageUpload v-model="form.image"></ImageUpload>
          </el-form-item>

          <el-form-item label="商品介绍" prop="content">
            <el-input type="textarea" v-model="form.content" style="width: 100%;" placeholder="请输入商品介绍" :rows="5"></el-input>
          </el-form-item>
        </el-form>
        <div style="text-align: center; margin-top: 50px;">
          <el-button style="margin-right: 20px;" @click="handleReset">取消</el-button>
          <el-button type="primary" @click="submitForm('form')">保 存</el-button>
        </div>
    </el-dialog>

  </div>
</template>

<script>

import {deleteApi, getListApi, saveApi, updateApi} from "@/api/setting/hardware";
import { getToken } from '@/utils/auth'

export default {
  name: "printIndex",
  dicts: ['hardware_type','hardware_status'],
  data() {
    return {
      tableData: [],
      form: {

      },
      systemPositionList:[{
        value: '首页',
        label: '首页'
      }, {
        value: '系统首页',
        label: '系统首页'
      },],
      // 是否显示弹出层
      open: false,
      title:"",
      total:0,
      // 查询参数
      queryParams: {
        page: 1,
        pageSize: 10,
        bannerName:'',
        systemPosition:'',
        bannerStatus:'',
      },

      // 上传地址
      uploadAction: process.env.VUE_APP_BASE_API + 'backendApi/file/upload',
      uploadHeader: { 'Access-Token' : getToken() },  // 上传文件列表
      uploadFiles: [
        {name:"nihao",
          url:'http://localhost:8008/static/uploadImages/20231103/ffbbe7d3ee1441fdaf706802fa0f176a.png'}
      ],
      // 图片根目录
      imagePath: process.env.VUE_APP_BASE_API,
      // 隐藏上传
      hideUpload: false,
      // 表单校验
      rules: {
        name: [
          { required: true, message: "商品名称不能为空", trigger: "blur" }
        ],
        categoryName: [
          { required: true, message: "商品分类不能为空", trigger: "blur" }
        ],
        formStore: [
          { required: true, message: "采购商家不能为空", trigger: "blur" }
        ],
        image: [
          { required: true, message: "商品图片不能为空", trigger: "blur" }
        ],
        content: [
          { required: true, message: "商品介绍不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList()
  },
  methods: {
    getList(){
      getListApi(this.queryParams).then(res=>{
        this.tableData = res.data.records;
        this.total = res.data.total
      })
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          // 表单验证通过，可以提交数据
          this.submitDeviceInfo();
        } else {
          // 表单验证失败，不执行任何操作
          return false;
        }
      });
    },
    submitDeviceInfo() {
      // 在这里调用接口提交设备信息数据
      console.log('提交设备信息数据:', this.form);
      if (this.form.id) {
        updateApi(this.form).then(res=>{
          if(res.code === 200) {
            this.getList()
            this.open = false
          }

        })
      }else {
        saveApi(this.form).then(res=>{
          if(res.code === 200) {
            this.getList()
            this.open = false
          }
        })
      }
    },
    changeStatus(data){
      updateApi(data).then(res=>{
        if(res.code === 200) {
          this.$message({
            type: 'success',
            message: '修改状态成功!'
          });
          this.getList()
        }

      })
    },
    // 上传封面
    handleUploadSuccessCover(file) {
      this.form.productImage = file.data.fileName;
      this.form.productImage = file.data.fileName;
    },
    handleQuery(){
      this.getList()
    },
    resetQuery(){
      this.queryParams = {
        page: 1,
        pageSize: 10,
        deviceName:'',
      },
        this.getList()
    },
    handleAdd(e) {
      this.clean()
      this.open = true;
      this.title = "新增商品"
    },
    shangjia(data){

      if(data.status == 0){

        data.status =1
      }else {

        data.status =0
      }
      this.changeStatus(data)
    },
    edit(e) {
      this.clean()
      this.open = true;
      this.title = "修改商品"
      this.form = e
    },
    del(e) {
      this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteApi(e.id).then(res=>{
          if (res.code == 200) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.getList()
          }
        })

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    clean() {
      this.form= {
        id: null,            // 自增id
        bannerName: '',     // Banner名称
        productImage: '',           // 商品图片
        systemPosition: '',      // 系统位置
        sortOrder: null,        // 排序号
        bannerLink: '',          // Banner链接
        bannerStatus: false,       // Banner状态
        note: '',    // 备注
        createByName: '', //
      }
    },
    handleReset() {
      this.open = false
    },
  }
};
</script>
<style lang="scss" scoped>
.common-dialog >>> .el-upload--picture-card {
  width: 60px;
  height: 50px;
  line-height: 60px;
}
.d-dialog >>> .el-upload--picture-card {
  width: 100px;
  height: 100px;
  line-height: 100px;
}

.d-dialog >>> .avatar-uploader .el-upload {
  width: 100px;
  height: 100px;
}
.d-dialog >>> .el-upload-list--picture-card .el-upload-list__item {
  width: 100px;
  height: 100px;
}
.list-img{
  width: 98px;
  height: 98px;
}
</style>

