<template>
  <div class="goodsList">
    <div class="head-title">商品列表</div>
    <div class="content">
      <div>
        <el-form style="display: flex; flex-wrap: wrap">
          <el-form-item label="同步状态" style="display: flex; width: 20%">
            <el-select
              v-model="msg.Isynchro"
              size="small"
              placeholder="请选择"
              @change="getList"
            >
              <el-option key="0" label="全部" :value="0"> </el-option>
              <el-option key="1" label="是" :value="1"> </el-option>
              <el-option key="2" label="否" :value="2"> </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="商品名称" style="display: flex; width: 20%">
            <el-input
              size="small"
              v-model="msg.Name"
              placeholder="请输入内容"
              @input="getList"
            ></el-input>
          </el-form-item>
          <el-form-item label="所属供应商" style="display: flex; width: 20%">
            <el-select
              v-model="msg.SupplierId"
              size="small"
              filterable
              placeholder="请选择"
              @change="getList"
            >
              <el-option key="0" label="不限" :value="0"> </el-option>
              <el-option
                v-for="item in options"
                :key="item.ID"
                :label="item.Name"
                :value="item.ID"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="" style="display: flex; width: 25%">
            <el-button type="primary" size="small" @click="batchSync"
              >批量同步</el-button
            >
          </el-form-item>
        </el-form>
      </div>
      <el-table
        :data="tableData"
        v-loading="loading"
        border
        sortable="custom"
        @sort-change="getSort"
        @selection-change="tableSelect"
        style="width: 100%; margin: 20px 0"
      >
        <el-table-column type="selection" width="55"> </el-table-column>

        <el-table-column prop="address" label="商品名称">
          <template slot-scope="scope">
            <div flex="box:first">
              <el-image
                title="点击查看大图"
                style="width: 50px; height: 50px; margin-right: 1px"
                :src="scope.row.CoverImage"
                :preview-src-list="scope.row.CarouselImageList"
              >
              </el-image>
              <template v-if="!scope.row.editname">
                <div flex="cross:top" style="display: inline-flex">
                  <div flex="dir:left">
                    <el-tooltip
                      class="item"
                      effect="dark"
                      :content="scope.row.Name"
                      placement="top"
                    >
                      <div
                        class="vue-line-clamp"
                        style="word-break: break-all; -webkit-line-clamp: 2"
                      >
                        <span>{{ scope.row.Name }}</span>
                      </div>
                    </el-tooltip>
                    <!-- <img
                      @click="
                        (scope.row.editname = true),
                          (scope.row.NameNew = scope.row.Name)
                      "
                      style="width: 15px; height: 15px; margin-left: 7px"
                      src="../../assets/img/userman/edit1.png"
                      alt=""
                    /> -->
                  </div>
                </div>
              </template>
              <template v-else>
                <el-input
                  v-model="scope.row.NameNew"
                  size="small"
                  style="display: inline-block; width: 150px"
                ></el-input>
                <span
                  @click="scope.row.editname = false"
                  class="el-icon-error"
                  style="
                    margin-left: 5px;
                    color: rgb(245, 108, 108);
                    position: relative;
                    top: 9px;
                  "
                ></span>
                <span
                  @click="Edit(scope.row, 2)"
                  class="el-icon-success"
                  style="color: #67c23a; position: relative; top: 9px"
                ></span>
              </template>
            </div>
            <div
              flex="dir:left"
              style="margin-top: 5px; color: #f56c6c; font-size: 12px"
              v-if="scope.row.Remark"
            >
              <div style="width: 38px">备注：</div>
              <el-tooltip
                class="item"
                effect="dark"
                :content="scope.row.Remark"
                placement="top"
              >
                <div
                  class="vue-line-clamp"
                  style="
                    word-break: break-all;
                    -webkit-line-clamp: 2;
                    width: 90%;
                    margin-left: 0;
                  "
                >
                  <span>{{ scope.row.Remark }}</span>
                </div>
              </el-tooltip>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="SellingPrice"
          sortable
          width="200"
          label="分销价格"
          v-if="currentUserInfo.IsKorea == 0"
        >
        </el-table-column>
        <el-table-column prop="SupplierName" label="供应商"> </el-table-column>
        <el-table-column prop="Isynchro" width="160" label="是否已同步">
          <template slot-scope="scope">
            <div>
              <span v-if="scope.row.Isynchro > 0">是</span>
              <span v-else>否</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="address" width="200" label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              v-if="scope.row.Isynchro === 0"
              @click="singleSync(scope.row.Id)"
              >同步商品</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        style="text-align: right"
        background
        @current-change="handleCurrentChange"
        :page-size="msg.pageSize"
        layout="prev, pager, next"
        :current-page.sync="msg.pageIndex"
        :total="total"
      >
      </el-pagination>
    </div>
    <!-- 选择分类 -->
    <el-dialog
      custom-class="app-add-cat"
      title="选择分类"
      :visible.sync="flDig"
      width="1100px"
    >
      <el-row>
        <el-col :span="8" style="padding: 0 10px; box-sizing: border-box">
          <h3 style="padding: 15px 0">一级分类</h3>
          <div class="app-goods-cat-list active">
            <el-checkbox-group v-model="checkList">
              <div
                v-for="(item, index) in fenleiData"
                :key="index"
                flex="dir:left box:first"
                class="cat-item"
              >
                <el-checkbox :label="item">
                  <span style="display: none">{{ item.Name }}</span>
                </el-checkbox>
                <div flex="box:last cross:center">
                  <span>{{ item.Name }}</span>
                  <i
                    v-if="item.ChildList.length > 0"
                    @click="getChild2(item.ChildList)"
                    class="el-icon-arrow-right"
                  ></i>
                </div>
              </div>
            </el-checkbox-group>
          </div>
        </el-col>
        <el-col
          v-show="childList2.length > 0"
          :span="8"
          style="padding: 0 10px; box-sizing: border-box"
        >
          <h3 style="padding: 15px 0">二级分类</h3>
          <div class="app-goods-cat-list active">
            <el-checkbox-group v-model="checkList">
              <div
                v-for="(item, index) in childList2"
                :key="index"
                flex="dir:left box:first"
                class="cat-item"
              >
                <el-checkbox :label="item">
                  <span style="display: none">{{ item.Name }}</span>
                </el-checkbox>
                <div flex="box:last cross:center">
                  <span>{{ item.Name }}</span>
                  <i
                    v-if="item.ChildList.length > 0"
                    @click="getChild3(item.ChildList)"
                    class="el-icon-arrow-right"
                  ></i>
                </div>
              </div>
            </el-checkbox-group>
          </div>
        </el-col>
        <el-col
          v-show="childList3.length > 0"
          :span="8"
          style="padding: 0 10px; box-sizing: border-box"
        >
          <h3 style="padding: 15px 0">三级分类</h3>
          <div class="app-goods-cat-list active">
            <el-checkbox-group v-model="checkList">
              <div
                v-for="(item, index) in childList3"
                :key="index"
                flex="dir:left box:first"
                class="cat-item"
              >
                <el-checkbox :label="item">
                  <span style="display: none">{{ item.Name }}</span>
                </el-checkbox>
                <div flex="box:last cross:center">
                  <span>{{ item.Name }}</span>
                  <i class="el-icon-arrow-right"></i>
                </div>
              </div>
            </el-checkbox-group>
          </div>
        </el-col>
      </el-row>
      <div class="tag-box">
        <el-tag
          @close="exitCheck(item)"
          v-for="(item, index) in checkList"
          :key="index"
          type="warning"
          closable
          style="margin-right: 5px"
          >{{ item.Name }}</el-tag
        >
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="flDig = false">取 消</el-button>
        <el-button @click="chooseFl" size="small" type="primary"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "quickBuy",
  data() {
    return {
      plDig: false,
      flDig: false,
      val: "",
      dateList: [],
      activeName: "first",
      editState: false,
      msg: {
        pageIndex: 1,
        pageSize: 15,
        Name: "",
        GoodsStatus: 0,
        IsSelectSellOut: 0,
        CategoryIds: "",
        Id: "",
        StartTime: "",
        EndTime: "",
        OrderBy: 0,
        Isynchro: 0,
        SourceGoodsId: 0,
      },
      total: 0,
      tableData: [],
      loading: false,
      options: [],
      addMsg: {
        Id: 0,
        Sort: "",
        Name: "",
        IsDefault: 1,
        Remark: "",
        UpdateType: 1,
      },
      rules: {
        CategoryIdNew: [
          {
            required: true,
            message: "分类不能为空",
            trigger: "change",
          },
        ],
        Sort: [
          {
            required: true,
            message: "请输入排序",
            trigger: "blur",
          },
        ],
      },
      fenleiData: [],
      checkList: [],
      childList2: [],
      childList3: [],
      GoodsIds: [],
      plszMsg: {
        GoodsIds: "",
        Type: 7,
        FreightId: 0,
        LimitBuyGoodsNum: -1,
        LimitBuyOrderNum: -1,
        IntegralPresent: 0,
        IntegralPresentType: 2,
        PointsDeduction: 0,
        PointsDeductionType: 2,
        IsMultipleDeduction: 2,
        IsQuickBuy: 2,
        IsGoodsNegotiable: 2,
        SupplierId: "",
      },
      ruleList: [],
      ruleSelect: {},
      keepGoodsIds: "",
      BuyGoodsNumState: true,
      BuyOrderNumState: true,
      hpState: 0,
      FxState: 0,
      currentUserInfo: {}, //当前用户
    };
  },
  created() {
    this.currentUserInfo = JSON.parse(localStorage.mall_userInfo);
    this.getList();
    this.getTree();
    this.getRule();
    this.getHpState();
    this.getFxState();
    this.getSupplierList(); //获取供应商
  },
  methods: {
    //获取是否开启和平返佣
    getHpState() {
      this.apipost("/api/user/GetHpDistributorIsEnabled", {}, (res) => {
        if (res.data.resultCode == 1) {
          this.hpState = res.data.data;
        }
      });
    },
    //是否开启粉象返佣
    getFxState() {
      this.apipost("/api/user/GetIsEnableFXDistributorGrade", {}, (res) => {
        if (res.data.resultCode == 1) {
          this.FxState = res.data.data;
        }
      });
    },
    goodChange(val, num) {
      if (!val) {
        if (num == 1) {
          this.plszMsg.LimitBuyGoodsNum = 0;
        } else if (num == 2) {
          this.plszMsg.LimitBuyOrderNum = 0;
        }
      }
    },
    Export() {
      let msg = this.msg;
      if (msg.Id == "") {
        msg.Id = 0;
      }
      msg = JSON.parse(JSON.stringify(msg));
      msg.pageSize = 999;
      this.GetLocalFile(
        "/api/product/GetProductGoodsListToExcel",
        msg,
        "商品列表.xls"
      );
    },
    plsetOk() {
      if (this.plszMsg.Type == 7) {
        // if(this.plszMsg.FreightId==0){
        //     this.Error("请选择运费规则");
        //     return;
        // }
      }
      if (this.plszMsg.Type == 8) {
        if (this.plszMsg.BuyGoodsNumState) {
          this.plszMsg.LimitBuyGoodsNum = -1;
        }
        if (this.plszMsg.BuyOrderNumState) {
          this.plszMsg.LimitBuyOrderNum = -1;
        }
      }
      let mall_userInfo = JSON.parse(localStorage.mall_userInfo);
      if (mall_userInfo.TenantId == 1 && mall_userInfo.MallBaseId == 1) {
        if (this.plszMsg.SupplierId == "") {
          this.Error("请选择供应商!");
          return;
        } else {
          this.commonSet(this.plszMsg);
        }
      } else {
        if (this.plszMsg.SupplierId == "") {
          this.plszMsg.SupplierId = 0;
          this.commonSet(this.plszMsg);
        } else {
          this.commonSet(this.plszMsg);
        }
      }
    },
    // plsetBtn() {
    //   if (this.GoodsIds.length == 0) {
    //     this.Error("请先勾选要设置的商品!");
    //     return;
    //   }
    //   let idList = [];
    //   this.GoodsIds.forEach((item) => {
    //     idList.push(item.Id);
    //   });
    //   this.keepGoodsIds = idList.join(",");
    //   this.plszMsg.GoodsIds = this.keepGoodsIds;
    //   this.plszMsg.SupplierId = "";
    //   this.plDig = true;
    // },
    ClearSelect(type) {
      this.plszMsg = {
        GoodsIds: this.keepGoodsIds,
        Type: type,
        FreightId: 0,
        LimitBuyGoodsNum: -1,
        LimitBuyOrderNum: -1,
        IntegralPresent: 0,
        IntegralPresentType: 2,
        PointsDeduction: 0,
        PointsDeductionType: 2,
        IsMultipleDeduction: 2,
        IsQuickBuy: 2,
        IsGoodsNegotiable: 2,
        SupplierId: "",
      };
    },
    YfSelect(val) {
      this.ruleSelect = val;
      this.plszMsg.FreightId = val.ID;
    },
    tableSelect(val) {
      this.GoodsIds = val;
    },
    // 上架 下架 删除
    tableSet(num) {
      if (this.GoodsIds.length == 0) {
        this.Error("请先勾选要设置的商品!");
        return;
      }
      let idList = [];
      this.GoodsIds.forEach((item) => {
        idList.push(item.Id);
      });
      let GoodsIds = idList.join(",");
      let msg = {
        GoodsIds: GoodsIds,
        Type: num,
      };
      let str = "";
      if (num == 4) {
        str = "批量上架,是否继续";
      }
      if (num == 5) {
        str = "批量下架,是否继续";
      }
      if (num == 6) {
        str = "是否确认删除选中的商品?";
      }
      let that = this;
      that
        .$confirm(str, "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
        .then(() => {
          this.commonSet(msg);
        })
        .catch(() => {});
    },
    Edit(row, num) {
      let addMsg = {
        GoodsIds: row.Id,
        Type: num,
        Sort: row.SortNew,
        GoodsName: row.NameNew,
        IsQuickBuy: row.IsQuickBuy,
      };
      this.commonSet(addMsg);
    },
    commonSet(addMsg) {
      this.apipost(
        "/api/product/SetProductGoodsQuickUpdate",
        addMsg,
        (res) => {
          if (res.data.resultCode === 1) {
            this.getList();
            this.Success(res.data.message);
            this.plDig = false;
          } else {
            this.Error(res.data.message);
          }
        },
        null
      );
    },
    chooseFl() {
      let CategoryIds = [];
      let CategoryArr = [];
      this.checkList.forEach((item) => {
        CategoryIds.push(item.Id);
        CategoryArr.push({
          Id: item.Id,
          CategoryId: item.Id,
          CategoryName: item.Name,
          Name: item.Name,
        });
      });
      this.distributionGoods(CategoryArr, this.keepGoodsIds);
      // this.msg.pageIndex = 1;
      // this.msg.CategoryIds = CategoryIds.join(",");
      // this.getList();
      // this.flDig = false;
    },
    getChild3(ChildList) {
      this.childList3 = ChildList;
    },
    getChild2(ChildList) {
      this.childList2 = ChildList;
    },
    exitCheck(item, index) {
      this.checkList.forEach((list, k) => {
        if (list.Id == item.Id) {
          this.checkList.splice(k, 1);
        }
      });
    },

    handleCurrentChange(val) {
      this.msg.pageIndex = val;
      this.getList();
    },
    getList() {
      if (this.dateList && this.dateList.length > 0) {
        this.msg.StartTime = this.dateList[0];
        this.msg.EndTime = this.dateList[1];
      } else {
        this.msg.StartTime = "";
        this.msg.EndTime = "";
      }
      let msg = JSON.parse(JSON.stringify(this.msg));
      if (msg.Id == "") {
        msg.Id = 0;
      }
      this.loading = true;
      this.apipost(
        "/api/product/GetProductGoodsSynchroPageList",
        msg,
        (res) => {
          this.loading = false;
          if (res.data.resultCode == 1) {
            this.total = res.data.data.count;
            let pageData = res.data.data.pageData;
            pageData.forEach((item) => {
              item.SortNew = item.Sort;
              item.NameNew = item.Name;
              item.editsort = false;
              item.editname = false;
            });
            this.tableData = pageData;
          }
        }
      );
    },
    getTree() {
      let msg1 = {
        Id: 0,
        Name: "",
        Tier: 0,
        ParentId: 0,
        Enabled: 1,
        IsShow: 1,
      };
      this.apipost("/api/product/GetProductCategoryTreeList", msg1, (res) => {
        if (res.data.resultCode == 1) {
          let pageData = res.data.data;
          this.fenleiData = pageData;
        }
      });
    },
    getRule() {
      let msg1 = {
        pageIndex: 1,
        pageSize: 999,
        RulesName: "",
      };
      this.apipost("/api/MallBase/GetLogisticsRulesPage", msg1, (res) => {
        if (res.data.resultCode == 1) {
          let pageData = res.data.data.pageData;
          this.ruleList = pageData;
        }
      });
    },
    changeSupplier(val) {},
    getSupplierList() {
      this.apipost("/api/Supplier/GetAllSupplierList", {}, (res) => {
        this.loading = false;
        if (res.data.resultCode == 1) {
          this.options = res.data.data;
        }
      });
    },
    getSort(val) {
      //列表的升序降序
      if (val.prop == "Id") {
        if (val.order == "ascending") {
          this.msg.OrderBy = 1;
        } else if (val.order == "descending") {
          this.msg.OrderBy = 2;
        } else {
          this.msg.OrderBy = 0;
        }
      } else if (val.prop == "address") {
        if (val.order == "ascending") {
          this.msg.OrderBy = 3;
        } else if (val.order == "descending") {
          this.msg.OrderBy = 4;
        } else {
          this.msg.OrderBy = 0;
        }
      } else if (val.prop == "SellingPrice") {
        if (val.order == "ascending") {
          this.msg.OrderBy = 5;
        } else if (val.order == "descending") {
          this.msg.OrderBy = 6;
        } else {
          this.msg.OrderBy = 0;
        }
      } else if (val.prop == "InventoryNum") {
        if (val.order == "ascending") {
          this.msg.OrderBy = 7;
        } else if (val.order == "descending") {
          this.msg.OrderBy = 8;
        } else {
          this.msg.OrderBy = 0;
        }
      }
      this.getList();
    },
    //新窗口跳转
    goGoodsEdit(Id) {
      let routeData = this.$router.resolve({
        name: "goodsListEdit",
        query: {
          GoodsId: Id,
          hpState: this.hpState,
          FxState: this.FxState,
        },
      });
      window.open(routeData.href, "_blank");
    },
    //分销商品
    distributionGoods(CategoryList, ids) {
      let msg = {
        CategoryList: CategoryList,
        GoodsIds: ids,
      };
      this.apipost("/api/product/SynchroGoods", msg, (res) => {
        if (res.data.resultCode == 1) {
          this.Success("操作成功");
          this.msg.pageIndex = 1;
          this.getList();
          this.flDig = false;
        } else {
          this.Error(res.data.message);
        }
      });
    },
    //单个同步
    singleSync(id) {
      this.keepGoodsIds = id;
      this.plszMsg.GoodsIds = this.keepGoodsIds;
      this.plszMsg.SupplierId = "";
      this.flDig = true;
    },
    //批量设置
    batchSync() {
      if (this.GoodsIds.length == 0) {
        this.Error("请先勾选要设置的商品!");
        return;
      }
      let idList = [];
      this.GoodsIds.forEach((item) => {
        idList.push(item.Id);
      });
      this.keepGoodsIds = idList.join(",");
      this.plszMsg.GoodsIds = this.keepGoodsIds;
      this.plszMsg.SupplierId = "";
      this.flDig = true;
    },
  },
};
</script>
<style>
.plxg .spmy .el-form-item__label {
  padding-right: 26px;
}

.yfradio {
  display: block;
  padding: 10px;
}

.plxg .batch-div-active {
  background-color: #e2e2e2;
}

.plxg .batch-box-left div {
  padding: 5px 0;
  margin: 5px 0;
  cursor: pointer;
  -webkit-border-radius: 5px;
  -moz-border-radius: 5px;
  border-radius: 5px;
}

.plxg .batch-box-left {
  width: 120px;
  border-right: 1px solid #e2e2e2;
  padding: 0 20px;
}

.plxg .el-dialog__body {
  padding-top: 10px !important;
}

.plxg .absoSpan {
  position: absolute;
  top: 22px;
  left: 100px;
}

.app-add-cat .el-checkbox-group {
  font-size: 14px !important;
}

.app-add-cat .el-checkbox {
  margin-right: 0;
}

.app-add-cat .el-dialog__body {
  padding: 10px 20px !important;
}

.app-add-cat .tag-box .tag-item {
  margin-right: 5px;
}

.app-add-cat .tag-box {
  margin: 20px 0;
}

.app-add-cat .app-goods-cat-list .active {
  background: #fafafa;
}

.app-add-cat .app-goods-cat-list .cat-item {
  cursor: pointer;
  padding: 5px 10px;
}

.app-add-cat .app-goods-cat-list {
  border: 1px solid #e8eaee;
  border-radius: 5px;
  margin-top: -5px;
  padding: 10px 0;
  overflow: scroll;
  height: 400px;
}

.goodsList .vue-line-clamp {
  display: block;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
}

.goodsList .content {
  background: #fff;
  margin-top: 10px;
  padding: 20px;
  box-sizing: border-box;
}
.goodsList .el-form-item__label {
  white-space: nowrap;
}
</style>
