<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page.sync="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               :upload-after="uploadAfter"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.goods_delete"
                   @click="handleDelete">删 除
        </el-button>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove} from "@/api/shopping/goods";
  import {mapGetters} from "vuex";

  export default {
    data() {
      return {
        form: {
          gallery: [],
        },
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          height:'auto',
          calcHeight: 30,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          dialogClickModal: false,
          labelWidth: 120,
          column: [
            {
              label: "商品ID",
              prop: "id",
              hide: true,
              display: false,
            },
            {
              label: '商品图片',
              prop: 'goodsImg',
              type: 'upload',
              drag: true,
              listType: 'picture-img',
              loadText: '模板上传中，请稍等',
              span: 24,
              hide: true,
              display: false,
              propsHttp: {
                res: 'data',
                url: 'link',
              },
              action: "/api/blade-resource/oss/endpoint/put-file-attach"
            },
            {
              label: "商品名称",
              prop: "goodsName",
              width: 120,
              span: 24,
              search: true,
              display: false,
              rules: [{
                required: true,
                message: "请输入商品名称",
                trigger: "blur"
              }]
            },
            {
              label: "货号",
              prop: "productNo",
              width: 120,
              search: true,
              display: false,
              rules: [{
                required: true,
                message: "请输入货号",
                trigger: "blur"
              }]
            },
            {
              label: "市场价",
              prop: "marketPrice",
              display: false,
            },
            {
              label: "分类",
              prop: "categoryId",
              type: "tree",
              dicUrl: "/api/shopping/category/tree",
              props: {
                label: "title",
                value: "key"
              },
              hide: true,
              display: false,
            },
            {
              label: "分类名称",
              prop: "categoryName",
              display: false,
            },
            {
              label: "品牌",
              prop: "brandName",
              display: false,
            },
            {
              label: "品牌",
              prop: "brandId",
              type: "select",
              dicUrl: "/api/shopping/brands/selectList",
              props: {
                label: "brandName",
                value: "id"
              },
              hide: true,
              search: true,
              display: false,
            },
            {
              label: "是否上架",
              prop: "isSaleDisplayName",
              display: false,
            },
            {
              label: "是否上架",
              prop: "isSale",
              type: "select",
              dicUrl: "/api/blade-system/dict/dictionary?code=yes_no",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              dataType: "number",
              hide: true,
              display: false,
              search: true,
              rules: [{
                required: true,
                message: "请选择是否上架",
                trigger: "blur"
              }]
            },
            {
              label: "销量",
              prop: "salesVolume",
              display: false,
              rules: [{
                required: true,
                message: "请输入总销量",
                trigger: "blur"
              }]
            },
            {
              label: "商品标签",
              prop: "goodsLabel",
              display: false,
              rules: [{
                required: true,
                message: "请输入商品标签",
                trigger: "blur"
              }]
            },
            {
              label: "是否推荐",
              prop: "isRecomDisplayName",
              display: false,
              hide: true,
            },
            {
              label: "是否推荐",
              prop: "isRecom",
              type: "select",
              dicUrl: "/api/blade-system/dict/dictionary?code=yes_no",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              dataType: "number",
              hide: true,
              display: false,
              rules: [{
                required: true,
                message: "请选择是否推荐",
                trigger: "blur"
              }]
            },
            {
              label: "是否热销",
              prop: "isHotDisplayName",
              display: false,
              hide: true,
            },
            {
              label: "是否热销",
              prop: "isHot",
              type: "select",
              dicUrl: "/api/blade-system/dict/dictionary?code=yes_no",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              dataType: "number",
              hide: true,
              display: false,
              rules: [{
                required: true,
                message: "请选择是否热销",
                trigger: "blur"
              }]
            },
            {
              label: "是否新品",
              prop: "isNewDisplayName",
              display: false,
            },
            {
              label: "是否新品",
              prop: "isNew",
              type: "select",
              dicUrl: "/api/blade-system/dict/dictionary?code=yes_no",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              dataType: "number",
              hide: true,
              display: false,
              rules: [{
                required: true,
                message: "请选择是否新品",
                trigger: "blur"
              }]
            },
            {
              label: "是否是积分专区",
              prop: "isTicketDisplayName",
              display: false,
            },
            {
              label: "成本价",
              prop: "costPrice",
              hide: true,
              display: false,
              rules: [{
                required: true,
                message: "请输入成本价",
                trigger: "blur"
              }]
            },
            {
              label: "销售价",
              prop: "goodsPrice",
              display: false,
              rules: [{
                required: true,
                message: "请输入销售价（购买价）",
                trigger: "blur"
              }]
            },
            {
              label: "酒票数",
              prop: "ticketNum",
              hide: true,
              display: false,
              rules: [{
                required: true,
                message: "请输入酒票数",
                trigger: "blur"
              }]
            },
            {
              label: "总库存",
              prop: "goodsStock",
              display: false,
              rules: [{
                required: true,
                message: "请输入商品总库存",
                trigger: "blur"
              }]
            },
            {
              label: "预警库存",
              prop: "warnStock",
              hide: true,
              display: false,
              rules: [{
                required: true,
                message: "请输入预警库存",
                trigger: "blur"
              }]
            },
            {
              label: "商品类型",
              prop: "goodsType",
              display: false,
              rules: [{
                required: true,
                message: "请输入商品类型",
                trigger: "blur"
              }]
            },
            {
              label: "是否包邮",
              prop: "isFreeShippingDisplayName",
              display: false,
              rules: [{
                required: true,
                message: "请输入是否包邮",
                trigger: "blur"
              }]
            },
            {
              label: "是否包邮",
              prop: "isFreeShipping",
              type: "select",
              dicUrl: "/api/blade-system/dict/dictionary?code=yes_no",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              dataType: "number",
              hide: true,
              display: false,
              rules: [{
                required: true,
                message: "请选择是否包邮",
                trigger: "blur"
              }]
            },
            {
              label: "运费",
              prop: "freePrice",
              hide: true,
              display: false,
            },
            {
              label: "是否有规格",
              prop: "isSpec",
              hide: true,
              display: false,
              rules: [{
                required: true,
                message: "请输入是否有规格",
                trigger: "blur"
              }]
            },
            {
              label: "商品描述",
              prop: "goodsDesc",
              hide: true,
              display: false,
              span: 24,
              type: "textarea",
              rules: [{
                required: true,
                message: "请输入商品描述",
                trigger: "blur"
              }]
            },
            {
              label: "商品状态",
              prop: "goodsStatusDisplayName",
              display: false,
            },
            {
              label: "商品状态",
              prop: "goodsStatus",
              type: "select",
              dicUrl: "/api/blade-system/dict-biz/dictionary?code=exam_status",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              dataType: "number",
              hide: true,
              search: true,
              display: false,
              rules: [{
                required: true,
                message: "请选择商品状态",
                trigger: "blur"
              }]
            },
            {
              label: "排序",
              prop: "sort",
              display: false,
              rules: [{
                required: true,
                message: "请输入排序",
                trigger: "blur"
              }]
            },
            {
              label: "上架时间",
              prop: "saleTime",
              display: false,
              width: 140,
              rules: [{
                required: true,
                message: "请输入上架时间",
                trigger: "blur"
              }]
            },
            {
              label: "创建时间",
              prop: "createTime",
              hide: true,
              display: false,
              rules: [{
                required: true,
                message: "请输入创建时间",
                trigger: "blur"
              }]
            },
            {
              label: "更新时间",
              prop: "updateTime",
              hide: true,
              display: false,
              rules: [{
                required: true,
                message: "请输入更新时间",
                trigger: "blur"
              }]
            },
          ],
          group: [
            {
              label: '基本信息',
              prop: 'baseInfo',
              column: [
                {
                  label: "商品名称",
                  prop: "goodsName",
                  rules: [{
                    required: true,
                    message: "请输入商品名称",
                    trigger: "blur"
                  }]
                },
                {
                  label: "商品标签",
                  prop: "goodsLabel",
                  rules: [{
                    required: true,
                    message: "请输入商品标签",
                    trigger: "blur"
                  }]
                },
                {
                  label: "货号",
                  prop: "productNo",
                  rules: [{
                    required: true,
                    message: "请输入货号",
                    trigger: "blur"
                  }]
                },
                {
                  label: "分类",
                  prop: "categoryId",
                  type: "tree",
                  dicUrl: "/api/shopping/category/tree",
                  props: {
                    label: "title",
                    value: "key"
                  },
                },
                {
                  label: "品牌",
                  prop: "brandId",
                  type: "select",
                  dicUrl: "/api/shopping/brands/selectList",
                  props: {
                    label: "brandName",
                    value: "id"
                  },
                },
                {
                  label: "市场价",
                  prop: "marketPrice",
                  dataType: "number",
                  rules: [{
                    required: true,
                    message: "请输入市场价",
                    trigger: "blur"
                  }]
                },
                {
                  label: "销售价",
                  prop: "goodsPrice",
                  dataType: "number",
                  rules: [{
                    required: true,
                    message: "请输入销售价（购买价）",
                    trigger: "blur"
                  }]
                },
                {
                  label: "酒票数",
                  prop: "ticketNum",
                  dataType: "number",
                  rules: [{
                    required: true,
                    message: "请输入酒票数",
                    trigger: "blur"
                  }]
                },
                {
                  label: "成本价",
                  prop: "costPrice",
                  dataType: "number",
                  rules: [{
                    required: true,
                    message: "请输入成本价",
                    trigger: "blur"
                  }]
                },
                {
                  label: "总库存",
                  prop: "goodsStock",
                  dataType: "number",
                  rules: [{
                    required: true,
                    message: "请输入商品总库存",
                    trigger: "blur"
                  }]
                },
                {
                  label: "预警库存",
                  prop: "warnStock",
                  dataType: "number",
                  rules: [{
                    required: true,
                    message: "请输入预警库存",
                    trigger: "blur"
                  }]
                },
                {
                  label: "是否上架",
                  prop: "isSale",
                  type: "select",
                  dicUrl: "/api/blade-system/dict/dictionary?code=yes_no",
                  props: {
                    label: "dictValue",
                    value: "dictKey"
                  },
                  dataType: "number",
                  rules: [{
                    required: true,
                    message: "请选择是否上架",
                    trigger: "blur"
                  }]
                },
                {
                  label: "是否是积分专区",
                  prop: "isTicket",
                  type: "select",
                  dicUrl: "/api/blade-system/dict/dictionary?code=yes_no",
                  props: {
                    label: "dictValue",
                    value: "dictKey"
                  },
                  dataType: "number",
                  hide: true,
                  rules: [{
                    required: true,
                    message: "请选择是否抢购",
                    trigger: "blur"
                  }]
                },

                {
                  label: "销量",
                  prop: "salesVolume",
                  addDisplay: false,
                  editDisplay: false,
                  rules: [{
                    required: true,
                    message: "请输入总销量",
                    trigger: "blur"
                  }]
                },
                {
                  label: "商品状态",
                  prop: "goodsStatus",
                  type: "select",
                  dicUrl: "/api/blade-system/dict-biz/dictionary?code=exam_status",
                  props: {
                    label: "dictValue",
                    value: "dictKey"
                  },
                  dataType: "number",
                  rules: [{
                    required: true,
                    message: "请选择商品状态",
                    trigger: "blur"
                  }]
                },
                {
                  label: "排序",
                  prop: "sort",
                  rules: [{
                    required: true,
                    message: "请输入排序",
                    trigger: "blur"
                  }]
                },
                {
                  label: "上架时间",
                  prop: "saleTime",
                  width: 140,
                  addDisplay: false,
                  editDisplay: false,
                },
              ],
            },
            {
              label: '视频图片',
              prop: 'fileInfo',
              column: [
                {
                  label: '商品图片',
                  prop: 'goodsImg',
                  type: 'upload',
                  listType: 'picture-img',
                  span: 24,
                  fileSize:1,
                  dragFile: true,
                  loadText: '附件上传中，请稍等',
                  tip: '只能上传jpg/png文件，且不超过30kb',
                  accept:'image/png, image/jpeg',
                  propsHttp: {
                    res: 'data',
                    url: 'link',
                  },
                  action: "/api/blade-resource/oss/endpoint/put-file-attach",
                  rules: [{
                    required: true,
                    message: "请上传商品图片",
                    trigger: "blur"
                  }]
                },
                {
                  label: "商品相册",
                  prop: "gallery",
                  listType: 'picture-card',
                  type: 'upload',
                  span: 24,
                  fileSize:1,
                  dragFile: true,
                  limit: 6,
                  loadText: '附件上传中，请稍等',
                  tip: '只能上传jpg/png文件，且不超过30kb',
                  accept:'image/png, image/jpeg',
                  propsHttp: {
                    res: 'data',
                    url: 'link',
                  },
                  action: "/api/blade-resource/oss/endpoint/put-file-attach",
                  rules: [{
                    required: true,
                    message: "请上传商品相册",
                    trigger: "blur"
                  }]
                },
                {
                  label: "图文详情",
                  prop: "textImg",
                  listType: 'picture-card',
                  type: 'upload',
                  span: 24,
                  fileSize:1,
                  dragFile: true,
                  limit: 3,
                  loadText: '附件上传中，请稍等',
                  tip: '只能上传jpg/png文件，且不超过30kb',
                  accept:'image/png, image/jpeg',
                  propsHttp: {
                    res: 'data',
                    url: 'link',
                  },
                  action: "/api/blade-resource/oss/endpoint/put-file-attach",
                  rules: [{
                    required: true,
                    message: "请上传图文详情",
                    trigger: "blur"
                  }]
                },
                {
                  label: "商品视频",
                  prop: "goodsVideo",
                  type: 'upload',
                  fileSize:1,
                  listType: 'picture-img',
                  span: 24,
                  loadText: '附件上传中，请稍等',
                  tip: '只能上传jpg/png文件，且不超过30kb',
                  propsHttp: {
                    res: 'data',
                    url: 'link',
                  },
                  action: "/api/blade-resource/oss/endpoint/put-file-attach",
                },
              ],
            },
            {
              label: '其他信息',
              prop: 'otherInfo',
              column: [
                {
                  label: "商品描述",
                  prop: "goodsDesc",
                  span: 24,
                  type: "textarea",
                },
              ],
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.goods_add, false),
          viewBtn: this.vaildData(this.permission.goods_view, false),
          delBtn: this.vaildData(this.permission.goods_delete, false),
          editBtn: this.vaildData(this.permission.goods_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      rowSave(row, done, loading) {
        add(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          console.log(error);
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      uploadAfter(res, done, loading, column) {
        console.log(column);
        console.log(res);
        this.form.goodsImg = res.link;
        done()
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

<style>
</style>
