<template>
  <basic-container>
    <avue-crud :option="goodsinfoOption"
               :table-loading="loading"
               :data="data"
               :page.sync="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               :upload-error="uploadError"
               :upload-after="uploadAfter"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-click="handleRowClick"
               @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-scope="{disabled,size}" slot="promotionRateRangeSearch">
        <span>
          <el-input placeholder="最小毛利率" style="width:89px" v-model="range.from"></el-input>
          -
          <el-input placeholder="最大毛利率"  style="width:89px" v-model="range.to"></el-input>
        </span>
      </template>

      <template slot-scope="{disabled,size}" slot="activityDateSearch">
        <el-select v-model="activityDate" clearable placeholder="请选择">
          <el-option
            v-for="item in activityDateList"
            :key="item.value"
            :label="item.value"
            :value="item.label">
          </el-option>
        </el-select>
      </template>

      <template slot-scope="scope" slot="menu">
        <el-button
          type="text"
          icon="el-icon-view"
          size="small"
          @click="viewDialogShow(scope.row)"
          >查看
        </el-button>
        <el-button
          type="text"
          icon="el-icon-edit"
          size="small"
          @click="$refs.crud.rowEdit(scope.row, scope.index)"
          >编辑</el-button>
        <el-button
          type="text"
          icon="el-icon-setting"
          size="small"
          @click="paramConfig(scope.row)"
        >参数配置
        </el-button>
        <el-button
          type="text"
          icon="el-icon-setting"
          size="small"
          @click="viewSpec(scope.row)"
        >查看规格
        </el-button>
<!--        <el-button-->
<!--          type="text"-->
<!--          icon="el-icon-setting"-->
<!--          size="small"-->
<!--          @click="openRowEdit(scope.row)"-->
<!--        >行编辑-->
<!--        </el-button>-->
      </template>

      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.goodsinfo_delete"
                   @click="handleDelete">删 除
        </el-button>
      </template>
      <template slot="menuLeft">
        <el-button type="primary"
                   size="small"
                   icon="el-icon-sell"
                   plain
                   v-if="permission.goodsinfo_delete"
                   @click="modifyGoodsStateBatch(1)">上架
        </el-button>
      </template>
      <template slot="menuLeft">
        <el-button type="primary"
                   size="small"
                   icon="el-icon-sold-out"
                   plain
                   v-if="permission.goodsinfo_delete"
                   @click="modifyGoodsStateBatch(0)">下架
        </el-button>
      </template>
      <template slot="menuLeft">
        <el-button type="primary"
                   size="small"
                   icon="el-icon-edit"
                   plain
                   v-if="permission.goodsinfo_delete"
                   @click="modifyCategoryOpen">修改分类
        </el-button>
      </template>
      <template slot="menuLeft">
        <el-button type="primary"
                   size="small"
                   icon="el-icon-edit"
                   plain
                   v-if="permission.goodsinfo_delete"
                   @click="modifyGrossRateOpen">修改毛利
        </el-button>
      </template>
      <template slot="menuLeft">
        <el-button type="primary"
                   size="small"
                   icon="el-icon-edit"
                   plain
                   v-if="permission.goodsinfo_delete"
                   @click="modifyZoneTypeOpen">修改专区类型
        </el-button>
      </template>
      <template slot="menuLeft">
        <el-button type="primary"
                   size="small"
                   icon="el-icon-edit"
                   plain
                   v-if="permission.goodsinfo_delete"
                   @click="setDoubleElevenOpen">设置双十一商品
        </el-button>
      </template>
      <template slot="menuLeft">
        <el-button type="primary"
                   size="small"
                   icon="el-icon-shopping-bag-2"
                   plain
                   v-if="permission.goodsinfo_delete"
                   @click="handleImportOpen">查看选品库商品
        </el-button>
      </template>
      <template slot="menuLeft">
        <el-button type="primary"
                   size="small"
                   icon="el-icon-refresh"
                   plain
                   v-if="permission.sync_data"
                   @click="syncData">同步
        </el-button>
      </template>
    </avue-crud>
    <el-dialog title="参数配置"
               append-to-body
               :visible.sync="box"
               width="1000px">
      <avue-crud :option="goodsParamOption"
                 :table-loading="paramLoading"
                 :data="goodsParamData"
                 :permission="permissionList"
                 :before-open="beforeOpenParam"
                 v-model="goodsParamForm"
                 ref="paramCrud"
                 @row-update="rowUpdateParam"
                 @row-save="rowSaveParam"
                 @row-del="rowDelParam"
                 @on-load="onLoadParam">
      </avue-crud>
    </el-dialog>
    <el-dialog title="商品规格"
               append-to-body
               :visible.sync="viewSpecBox"
               @close="refreshChange"
               width="95%">
      <avue-crud :option="goodsSpecOption"
                 :table-loading="paramLoading"
                 :data="viewSpecData"
                 :permission="permissionList"
                 :before-open="beforeOpenSpec"
                 @row-click="handleRowClickSpec"
                 @selection-change="specSelectionChange"
                 v-model="goodsSpecForm"
                 ref="goodsSpecCrud"
                 @row-update="rowUpdateSpec"
                 @row-save="rowSaveSpec"
                 @row-del="rowDelSpec"
                 @on-load="onLoadViewSpec">
        <template slot="menuLeft">
          <el-button type="primary"
                     size="small"
                     icon="el-icon-sell"
                     plain
                     v-if="permission.goodsinfo_delete"
                     @click="modifyGoodsSpecStateBatch(1)">上架
          </el-button>
        </template>
        <template slot="menuLeft">
          <el-button type="primary"
                     size="small"
                     icon="el-icon-sold-out"
                     plain
                     v-if="permission.goodsinfo_delete"
                     @click="modifyGoodsSpecStateBatch(0)">下架
          </el-button>
        </template>
        <template slot="menuLeft">
          <el-button type="primary"
                     size="small"
                     icon="el-icon-sold-out"
                     plain
                     v-if="permission.goodsinfo_delete"
                     @click="modifySpecPriceBatch">修改价格
          </el-button>
        </template>
        <template slot="menuLeft">
          <el-button type="danger"
                     size="small"
                     icon="el-icon-delete"
                     plain
                     v-if="permission.goodsinfo_delete"
                     @click="deleteGoodSpecBatch">删除
          </el-button>
        </template>
      </avue-crud>
    </el-dialog>
    <el-dialog title="选品库" @close="refreshChange"
               append-to-body
               :visible.sync="scBox"
               width="95%"
                heigh="90%">
      <avue-crud :option="scOption"
                 :table-loading="scLoading"
                 :data="scData"
                 :page.sync="scPage"
                 :permission="permissionList"
                 :before-open="scBeforeOpen"
                 v-model="scForm"
                 ref="scCrud"
                 @search-change="scSearchChange"
                 @search-reset="scSearchReset"
                 @selection-change="scSelectionChange"
                 @current-change="scCurrentChange"
                 @size-change="scSizeChange"
                 @refresh-change="scRefreshChange"
                 @on-load="onLoadSC">
        <template slot="menuLeft">
          <el-button type="primary"
                     size="small"
                     icon="el-icon-add"
                     plain
                     @click="handleImport">导入
          </el-button>
        </template>
      </avue-crud>
    </el-dialog>
    <el-dialog title="批量修改分类" @close="refreshChange"
               append-to-body
               :visible.sync="modifyCategoryBox"
               :before-close="handleClose"
               width="600px"
               heigh="40%">
      <el-form :model="form">
        <el-form-item label="分类">
          <avue-cascader check-strictly lazy :lazy-load="lazyLoad" v-model="categoryIdArr" :props="props" ></avue-cascader>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="modifyCategory">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="修改专区类型" @close="refreshChange"
               append-to-body
               :visible.sync="modifyZoneTypeBox"
               :before-close="handleClose"
               width="600px"
               heigh="40%">
      <el-form :model="form">
        <el-form-item label="专区类型">
          <el-select v-model="zoneType" placeholder="请选择">
            <el-option
              v-for="item in zoneTypeList"
              :key="item.dictKey"
              :label="item.dictValue"
              :value="item.dictKey">
            </el-option>
          </el-select>
        </el-form-item>
<!--        <el-form-item label="活动日期" v-if="zoneType == 7">-->
<!--          <el-date-picker type="date" placeholder="选择日期" v-model="activityDate" value-format="yyyy-MM-dd"></el-date-picker>-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="modifyZoneType">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="设置双十一商品" @close="refreshChange"
               append-to-body
               :visible.sync="setDoubleElevenBox"
               :before-close="handleClose"
               width="600px"
               heigh="40%">
      <el-form :model="form">
        <el-form-item label="活动日期">
          <el-select v-model="activityDate" placeholder="请选择">
            <el-option
              v-for="item in activityDateList"
              :key="item.value"
              :label="item.value"
              :value="item.label">
            </el-option>
          </el-select>
        </el-form-item>
        <!--        <el-form-item label="活动日期" v-if="zoneType == 7">-->
        <!--          <el-date-picker type="date" placeholder="选择日期" v-model="activityDate" value-format="yyyy-MM-dd"></el-date-picker>-->
        <!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="setDoubleEleven">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="商品导入" @close="refreshChange"
               append-to-body
               :visible.sync="importCategoryBox"
               :before-close="handleClose"
               width="600px"
               heigh="100%">
      <el-form :model="form" label-width="80px">
        <el-form-item
          label="毛利:"
        >
          <el-input type="grossRate" v-model.number="grossRate" autocomplete="off" placeholder="请输入毛利"></el-input>
        </el-form-item>
        <el-form-item label="分类:">
          <avue-cascader placeholder="请选择分类" check-strictly lazy :lazy-load="lazyLoad" v-model="categoryIdArr" :props="props" ></avue-cascader>
        </el-form-item>
        <el-form-item label="专区类型:">
          <el-select v-model="zoneType" placeholder="请选择专区类型">
            <el-option
              v-for="item in zoneTypeList"
              :key="item.dictKey"
              :label="item.dictValue"
              :value="item.dictKey">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="importSubmit">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="修改毛利" @close="refreshChange"
               append-to-body
               :visible.sync="modifyGrossRateBox"
               :before-close="handleClose"
               width="600px"
               heigh="40%">
      <el-form :model="form">
        <el-form-item
          label="毛利(百分比):"
          placeholder="请输入毛利"
        >
          <el-input type="grossRate" v-model.number="grossRate" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="modifyGrossRateHandle">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="查看" append-to-body :visible.sync="viewDialog" heigh="40%">
      <demo-model ref="kdemoModel" :src="demoModelSrc" ></demo-model>
    </el-dialog>
  </basic-container>
</template>


<script>
  import {getList, getDetail, add, update, remove, modifyState, tree, modifyCategory, modifyGrossRate, zoneTypeOption, modifyZoneType, syncFull, setActivityGoods} from "@/api/redenvelopemall/goodsinfo";
  import {getGoodsStorageList} from "@/api/redenvelopemall/supplyChain";
  import {getParamList, getDetailParam, addParam, updateParam, removeParam} from "../../api/redenvelopemall/goodsparams";
  import {goodsinfoOption, goodsParamOption, scOption, goodsSpecOption} from "@/option/redenvelopemall/goodsinfo";
  import {mapGetters} from "vuex";
  import {importGoods} from "../../api/redenvelopemall/supplyChain";
  import {getByParentId} from "../../api/redenvelopemall/categoryinfo";
  import {getList as getSpecList, getDetail as getSpecDetail, update as updateSpec, remove as removeSpec, add as addSpec, batchOnSale, batchShelves, batchModifyPrice} from "../../api/redenvelopemall/goodsspec";
  import demoModel from "@/components/demo-model/main"

  export default {
    components: {demoModel},
    data() {
      return {
        range: {
          from: '',
          to: ''
        },
        zoneType: '',
        activityDate: '',
        zoneTypeList: [],
        grossRate: '',
        categoryData: [],
        props: {
          label: "title",
          value: "key"
        },
        categoryIdArr: [],
        modifyCategoryBox: false,
        importCategoryBox: false,
        modifyGrossRateBox: false,
        modifyZoneTypeBox: false,
        setDoubleElevenBox: false,
        showFlag: '',
        goodsinfoOption: goodsinfoOption,
        goodsParamOption: goodsParamOption,
        goodsSpecOption: goodsSpecOption,
        scOption: scOption,
        form: {},
        goodsParamForm: {},
        goodsSpecForm: {},
        scForm: {},
        query: {},
        goodsParamQuery: {},
        viewSpecQuery: {},
        scQuery: {},
        loading: true,
        paramLoading: true,
        scLoading: true,
        box: false,
        scBox: false,
        viewSpecBox: false,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        scPage: {
          page: 10,
          currentPage: 1,
          total: 0,
          pageSizes: [10, 20, 30, 40, 50]
        },
        selectionList: [],
        scSelectionList: [],
        specSelectionList: [],
        data: [],
        goodsParamData: [],
        viewSpecData: [],
        scData: [],

        viewDialog: false,
        // demoModelSrcYuan: 'http://localhost:8080/#/pages/index/index',
        demoModelSrcYuan: '/dome/index.html#/pages/index/index',
        demoModelSrc: '',
        activityDateList: [
          {
            value: '11月5日',
            label: '2022-11-05'
          }, {
            value: '11月6日',
            label: '2022-11-06'
          }, {
            value: '11月7日',
            label: '2022-11-07'
          }, {
            value: '11月8日',
            label: '2022-11-08'
          }, {
            value: '11月9日',
            label: '2022-11-09'
          }, {
            value: '11月10日',
            label: '2022-11-10'
          }, {
            value: '11月11日',
            label: '2022-11-11'
          }
        ]
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.goodsinfo_add, false),
          viewBtn: this.vaildData(this.permission.goodsinfo_view, false),
          delBtn: this.vaildData(this.permission.goodsinfo_delete, false),
          editBtn: this.vaildData(this.permission.goodsinfo_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      },
      specIds() {
        let ids = [];
        this.specSelectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      viewDialogShow(row) {
        this.viewDialog = true;
        this.demoModelSrc = `${this.demoModelSrcYuan}?aa=${JSON.stringify(row)}`

        console.log('========>', this.demoModelSrc);
        // setTimeout(() => {
        //   this.$refs.kdemoModel.postMsg(row);
        // }, 1000)
      },
      rowSave(row, done, loading) {
        row.goodsCategoryId = row.categoryIdArr.join("-");
        if (row.promotionRateRange == '') {
          delete row.promotionRateRange;
        }
        add(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowUpdate(row, index, done, loading) {
        row.goodsCategoryId = row.categoryIdArr.join("-") + "-";
        if (row.promotionRateRange == '') {
          delete row.promotionRateRange;
        }
        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;
            this.form.uploadCover = res.data.data.goodsCover;
            this.form.goodsVideo = res.data.data.videoUrl;
            this.form.slideshowList = res.data.data.slideshowList;
            this.form.detailImgUrlList = res.data.data.detailImgUrlList;
            this.form.categoryIdArr = this.form.goodsCategoryId.split("-");
          });
          console.log(this.form.goodsCategoryId.split("-"));
        }
        done();
      },
      beforeOpenParam(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetailParam(this.goodsParamForm.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      beforeOpenSpec(done, type) {
        if (["edit", "view"].includes(type)) {
          getSpecDetail(this.goodsSpecForm.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.range = {
          from: '',
            to: ''
        };
        this.activityDate = '';
        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;
        if (this.query.categoryIdArr) {
          this.query.goodsCategoryId = this.query.categoryIdArr.join("-");
          this.query.categoryIdArr = null;
        }
        this.query.promotionRateRange = this.range;
        this.query.activityDate = this.activityDate;
        console.log(this.query);
        // this.goodsCategoryId = this.query.categoryIdArr.join(",");
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const needAmount = this.findObject(this.goodsinfoOption.column, "needAmount");
          const exchangeNeedIntegral = this.findObject(this.goodsinfoOption.column, "needIntegral");
          if (this.query.zoneType == 2) {
            needAmount.hide = false;
            exchangeNeedIntegral.hide = false;
          } else {
            needAmount.hide = true;
            exchangeNeedIntegral.hide = true;
          }
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.data.forEach(e => {
            e.$cellEdit = true;
            if (e.isSale == "1") {
              e.saleSwitch = true;
            } else {
              e.saleSwitch = false;
            }
          })
          this.loading = false;
          this.selectionClear();
        });
      },
      uploadError(error, column) {
        this.$message.success('上传失败:' + error)
        console.log(error, column)
      },
      uploadAfter(res, done, loading,column) {
        console.log(res,column)
        if(column.prop == "uploadCover") {
          this.form.goodsCover = res.link;
        }
        if (column.prop == "goodsVideo") {
          this.form.videoUrl = res.link;
        }
        done()
      },
      paramConfig(row) {
        this.box = true;
        this.goodsParamQuery.goodsId = row.id;
        this.onLoadParam();
      },
      viewSpec(row) {
        this.viewSpecBox = true;
        this.viewSpecQuery.goodsId = row.id;
        this.viewSpecQuery.goodsNo = row.goodsNo;
        this.onLoadViewSpec();
      },
      onLoadParam() {
        this.paramLoading = true;
        getParamList(this.goodsParamQuery.goodsId).then(res => {
          this.goodsParamData = res.data.data;
          this.paramLoading = false;
          this.selectionClear();
        });
      },
      onLoadViewSpec() {
        this.paramLoading = true;
        getSpecList(1, 100, this.viewSpecQuery).then(res => {

          this.viewSpecData = res.data.data.records;
          this.viewSpecData.forEach(e => {
            e.coverShow = e.cover;
            e.$cellEdit = true;
            if (e.isSale == "1") {
              e.saleSwitch = true;
            } else {
              e.saleSwitch = false;
            }
          })

          const needAmount = this.findObject(this.goodsSpecOption.column, "needAmount");
          const exchangeNeedIntegral = this.findObject(this.goodsSpecOption.column, "exchangeNeedIntegral");
          if (this.viewSpecData[0] && this.viewSpecData[0].zoneType == 2) {
            needAmount.hide = false;
            exchangeNeedIntegral.hide = false;
          } else {
            needAmount.hide = true;
            exchangeNeedIntegral.hide = true;
          }

          this.paramLoading = false;
          this.selectionClear();
        });
      },
      rowSaveParam(row, done, loading) {
        console.log("rowSaveParam", row)
        row.goodsId = this.goodsParamQuery.goodsId;
        addParam(row).then(() => {
          this.onLoadParam();
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowSaveSpec(row, done, loading) {
        row.goodsId = this.viewSpecQuery.goodsId;
        addSpec(row).then(() => {
          this.onLoadViewSpec();
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowUpdateParam(row, index, done, loading) {
        updateParam(row).then(() => {
          this.onLoadParam();
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          console.log(error);
        });
      },
      rowUpdateSpec(row, index, done, loading) {
        updateSpec(row).then(() => {
          this.onLoadViewSpec();
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          console.log(error);
        });
      },
      rowDelParam(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return removeParam(row.id);
          })
          .then(() => {
            this.onLoadParam();
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      rowDelSpec(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return removeSpec(row.id);
          })
          .then(() => {
            this.onLoadViewSpec();
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleRowClick(row, event, column, loading) {
        if (event.label == '是否上架') {
          const param = {id:row.id, isSale:row.saleSwitch ? 1 : 0}
          update(param).then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: row.saleSwitch ? "上架成功" : "下架成功"
            });
          }, error => {
            loading();
            console.log(error);
          });
        }
      },
      handleRowDbClick(row, column) {    //行的双击事件
        console.log("dbclick -> ")
        if (["salePrice"].includes(column.property)) {
          console.log()
          // this.$refs.crud.rowCell(row, row.$index);
        }
      },
      handleClose() {
        this.modifyCategoryBox = false;
        this.importCategoryBox = false;
        this.modifyGrossRateBox = false;
        this.modifyZoneTypeBox = false;
        this.setDoubleElevenBox = false;
        this.zoneType = '';
        this.categoryIdArr = [];
        this.grossRate = '';
      },
      handleImportOpen() {
        this.scBox = true;
      },
      onLoadSC(page, params = {}) {
        this.scLoading = true;
        if (params.is_import) {
          params.isImport = params.is_import;
        }

        getGoodsStorageList(page.currentPage, page.pageSize, Object.assign(params, this.scQuery)).then(res => {
          const data = res.data.data;
          this.scPage.total = data.total;
          this.scData = data.records;
          this.scLoading = false;
          this.scSelectionClear();
        });
      },
      scBeforeOpen() {

      },
      scSearchChange(params, done) {
        this.scQuery = params;
        this.scPage.currentPage = 1;
        this.onLoadSC(this.scPage, params);
        done();
      },
      scSearchReset() {
        this.scQuery = {};
        this.onLoadSC(this.scPage);
      },
      scSelectionChange(list) {
        this.scSelectionList = list;
      },
      specSelectionChange(list) {
        console.log("2-> ", list)
        this.specSelectionList = list;
      },
      scSelectionClear() {
        this.scSelectionList = [];
        this.$refs.scCrud.toggleSelection();
      },
      specSelectionClear() {
        this.specSelectionList = [];
        this.$refs.goodsSpecCrud.toggleSelection();
      },
      scCurrentChange(currentPage){
        this.scPage.currentPage = currentPage;
      },
      scSizeChange(pageSize){
        this.scPage.pageSize = pageSize;
      },
      scRefreshChange() {
        this.onLoadSC(this.scPage, this.scQuery);
      },
      modifyGoodsStateBatch(state) {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定修改数据?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            const loading = this.$loading({
              lock: true,
              text: '修改中，请稍等...',
              spinner: 'el-icon-loading',
              background: 'rgba(0, 0, 0, 0.7)'
            });
            modifyState(this.ids, state).then(() => {
              loading.close();
              this.onLoad(this.page, this.query);
              this.$message({
                type: "success",
                message: "操作成功!"
              });
              this.selectionClear()
            });
          })
      },
      modifyGoodsSpecStateBatch(state) {
        if (this.specSelectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm(state == 1 ? "确定上架？" : "确定下架？", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            const loading = this.$loading({
              lock: true,
              text: '修改中，请稍等...',
              spinner: 'el-icon-loading',
              background: 'rgba(0, 0, 0, 0.7)'
            });
            if (state == 1) {
              batchOnSale(this.specIds).then(() => {
                loading.close();
                this.onLoadViewSpec();
                this.$message({
                  type: "success",
                  message: "操作成功!"
                });
                this.specSelectionClear()
              });
            } else {
              batchShelves(this.specIds).then(() => {
                loading.close();
                this.onLoadViewSpec();
                this.$message({
                  type: "success",
                  message: "操作成功!"
                });
                this.specSelectionClear()
              });
            }
          })
      },
      modifySpecPriceBatch() {
        if (this.specSelectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$prompt('请输入价格', '批量更新价格', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputPattern: /^([1-9]\d{0,9}|0)([.]?|(\.\d{1,2})?)$/,
          inputErrorMessage: '请输入合法的价格',
        }).then(({ value }) => {
          const loading = this.$loading({
            lock: true,
            text: '修改中，请稍等...',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
          });
          batchModifyPrice(this.specIds, value).then(() => {
            loading.close();
            this.onLoadViewSpec();
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.specSelectionClear()
          }).catch(() => {
            loading.close();
          });
        });
      },
      deleteGoodSpecBatch() {
        if (this.specSelectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("删除？", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            const loading = this.$loading({
              lock: true,
              text: '删除中，请稍等...',
              spinner: 'el-icon-loading',
              background: 'rgba(0, 0, 0, 0.7)'
            });
            removeSpec(this.specIds).then(() => {
              loading.close();
              this.onLoadViewSpec();
              this.$message({
                type: "success",
                message: "操作成功!"
              });
              this.specSelectionClear()
            });

          })
      },
      modifyCategoryOpen() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.modifyCategoryBox = true;
        // tree().then(res => {
        //   this.categoryData = res.data.data
        // });
      },
      modifyGrossRateOpen() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.modifyGrossRateBox = true;
      },
      modifyZoneTypeOpen() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.modifyZoneTypeBox = true;
        zoneTypeOption().then(res => {
           this.zoneTypeList = res.data.data;
        })
      },
      setDoubleElevenOpen() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.setDoubleElevenBox = true;
      },
      modifyCategory() {
        if (this.categoryIdArr.length === 0) {
          this.$message.warning("请选择分类");
          return;
        }
        const loading = this.$loading({
          lock: true,
          text: '修改中，请稍等...',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
        modifyCategory(this.ids, this.categoryIdArr.join("-")).then(() => {
          loading.close();
          this.onLoad(this.page, this.query);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          this.selectionClear()
          this.handleClose();
        });
      },
      modifyGrossRateHandle() {
        if (this.grossRate == '') {
          this.$message.warning("请输入毛利");
          return;
        }
        const loading = this.$loading({
          lock: true,
          text: '修改中，请稍等...',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
        modifyGrossRate(this.ids, this.grossRate).then(() => {
          loading.close();
          this.onLoad(this.page, this.query);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          this.selectionClear()
          this.handleClose();
        });
      },
      handleImport() {
        if (this.scSelectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.importCategoryBox = true;
        tree().then(res => {
          this.categoryData = res.data.data
        });
        zoneTypeOption().then(res => {
          this.zoneTypeList = res.data.data;
        })
      },
      importSubmit() {
        if (this.grossRate == '') {
          this.$message.warning("请输入毛利");
          return;
        }
        if (this.categoryIdArr.length == 0) {
          this.$message.warning("请选择分类");
          return;
        }
        if (this.zoneType == '') {
          this.$message.warning("请选择专区类型");
          return;
        }
        const loading = this.$loading({
          lock: true,
          text: '导入中，请稍等...',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
        const ids = this.scSelectionList.map(item => item.id).join(",");
        console.log(this.categoryIdArr.join("-"))
        importGoods(ids, this.grossRate, this.categoryIdArr.join("-") + "-", this.zoneType).then(() => {
          loading.close();
          this.onLoadSC(this.scPage);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          this.scSelectionClear()
          this.handleClose()
        }).catch(() => {
          loading.close();
        });
      },
      modifyZoneType() {
        if (this.zoneType == '') {
          this.$message.warning("请选择专区类型");
          return;
        }
        if (this.zoneType == 7 && this.activityDate == '') {
          this.$message.warning("请选择活动日期");
          return;
        }
        const loading = this.$loading({
          lock: true,
          text: '导入中，请稍等...',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
        modifyZoneType(this.ids, this.zoneType, '2022-11-04').then(() => {
          loading.close();
          this.onLoadSC(this.scPage);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          this.selectionClear()
          this.handleClose();
        }).catch(() => {
          loading.close();
        });
      },
      setDoubleEleven() {
        if (this.activityDate == '') {
          this.$message.warning("请选择活动日期");
          return;
        }
        const loading = this.$loading({
          lock: true,
          text: '导入中，请稍等...',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
        setActivityGoods(this.ids, this.activityDate).then(() => {
          loading.close();
          this.onLoadSC(this.scPage);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          this.selectionClear()
          this.handleClose();
        }).catch(() => {
          loading.close();
        });
      },
      lazyLoad(node, resolve) {
        let parentId;
        let stop_level = 2;
        let level = node.level;
        let list = [];

        if (node.level === 0) {
          parentId = 0;
          getByParentId(0).then((res) => {
            list = res.data.data;
            list = (list || []).map(ele => {
              return Object.assign(ele, {
                leaf: level >= stop_level
              })
            })
            resolve(list)
          })
        } else {
          parentId = node.data.id;
          getByParentId(node.data.id).then((res) => {
            list = res.data.data;
            list = (list || []).map(ele => {
              return Object.assign(ele, {
                leaf: level >= stop_level
              })
            })
            resolve(list)
          })
        }
      },
      handleRowClickSpec(row, event, column, loading) {
        if (event.label == '是否上架') {
          row.isSale = row.saleSwitch ? 1 : 0;
          updateSpec(row).then(() => {
            this.onLoadViewSpec(this.page);
            this.$message({
              type: "success",
              message: row.saleSwitch ? "上架成功" : "下架成功"
            });
          }, error => {
            loading();
            console.log(error);
          });
        }
      },
      openRowEdit(row) {
        this.$refs.crud.rowCell(row, row.$index);
      },
      specClear() {
        this.goodsSpecForm = {};
        this.viewSpecData = [];
      },
      syncData() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        const loading = this.$loading({
          lock: true,
          text: '同步中，请稍等...',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
        syncFull(this.ids, this.zoneType).then(() => {
          loading.close();
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          this.onLoad(this.page, this.query);
          this.selectionClear()
          this.handleClose();
        }).catch(() => {
          loading.close();
        });
      },
    }
  };
</script>

<style>
</style>
