<template>
  <div>
    <div>
      <el-page-header :content="!cid ? '存货调价单' : '编辑'" @back="goBack" />
    </div>
    <br><br>
    <div>
      <el-form
        ref="screenForm"
        label-width="100px"
        size="small"
        label-position="left"
      >
        <el-row :gutter="20">
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="单据编号">
              <el-input size="mini"
                v-model="base.updPriceBillId"
                placeholder="系统自动生成"
                disabled
              />
            </el-form-item>
          </el-col>
          <!-- <el-col :xs="24" :sm="12" :lg="6">
                     <el-form-item label="调价业务员">
                       <el-select
                         placeholder="请选择调价业务员"
                         filterable
                         @change="handleUser"
                         v-model="serviceId"
                       >
                         <el-option
                           v-for="(item, index) in userList"
                           :label="item.nickName"
                           :value="item.roleId"
                           :key="index"
                         ></el-option>
                       </el-select>
                     </el-form-item>
                   </el-col>
                   <el-col :xs="24" :sm="12" :lg="6">
                     <el-form-item label="调价部门">
                       <el-input size="mini"
                         placeholder="调价部门"
                         disabled
                         v-model="adminCompanyName"
                       ></el-input>
                     </el-form-item>
                   </el-col> -->
          <!-- <el-col :xs="24" :sm="12" :lg="6">
                     <el-form-item label="销售类型">
                       <el-select
                         placeholder="请选择销售类型"
                         filterable
                         v-model="saleTypeId"
                       >
                         <el-option
                           v-for="item in typeList"
                           :label="item.saleName"
                           :value="item.id"
                         ></el-option>
                       </el-select>
                     </el-form-item>
                   </el-col> -->
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="备注">
              <el-input size="mini" v-model="base.remark" placeholder="请输入" />
            </el-form-item>
          </el-col>

          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="" class="fr" />
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="btn-group clearfixs" v-if="!cid">
      <!-- <el-upload
        class="import-btn"
        :action="baseURL + 'student/import'"
        :http-request="handleImport"
        :file-list="importFileList"
        :show-file-list="false"
      >
        <el-button
          type="primary"
          size="small"
        >导入调价单</el-button>
      </el-upload>
      <el-button
        type="primary"
        size="small"
        @click="hanleDownloadFiles"
      >下载模板</el-button> -->
    </div>
    <div class="mymain-container">
      <!-- 列表 -->
        <el-form v-if="!detail.length" size="small" label-position="left">
          <div class="table">
          <el-table
            v-loading="listLoading"
            :data="items"
            element-loading-text="Loading"
            border
            fit
            highlight-current-row
            stripe
          >
            <el-table-column
              align="center"
              label="操作类型"
              prop="nname"
              fixed="right"
              min-width="160"
              show-overflow-tooltip
            >
              <!-- <el-button type="text" class="textColor">修改</el-button> -->
              <template slot-scope="scope">
                <el-button

                  type="text"
                  class="textColor"
                  @click="hanleDeleInd(scope.$index, scope.row.cid,scope.row.materialId)"
                >删除</el-button>
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              label="销售类型"
              prop="mainId"
              min-width="160"
              show-overflow-tooltip
            >
              <template slot-scope="scope">
                <el-form-item>
                  <template v-if="!cid">
                    <el-select
                    size="mini"
                      v-model="scope.row.mainId"
                      placeholder="请选择销售类型"
                      @change="
                        handleSale($event, scope.$index, scope.row.saleTypes)
                      "
                    >
                      <el-option
                        v-for="item in scope.row.saleTypes"
                        :label="item.saleName"
                        :value="item.id"
                      />
                    </el-select>
                  </template>

                  <template v-else>
                    <el-select
                    size="mini"
                      v-model="scope.row.saleTypeId"
                      placeholder="请选择销售类型"
                      @change="handleSale($event, scope.$index, scope.row.id)"
                    >
                      <el-option
                        v-for="item in typeList"
                        :label="item.saleName"
                        :value="item.id"
                      />
                    </el-select>
                  </template>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              label="物料编码"
              prop="materialNumber"
              min-width="160"
              show-overflow-tooltip
            >
              <template slot-scope="scope">
                <el-form-item>
                  <el-input size="mini"

                    v-model="scope.row.materialNumber"
                    disabled
                  />
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              label="产品名称"
              prop="materialName"
              min-width="160"
              show-overflow-tooltip
            >
              <template slot-scope="scope">
                <el-form-item>
                  <el-input size="mini"
                    v-model="scope.row.materialName"
                    disabled
                  />
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              label="规格型号"
              prop="specification"
              min-width="160"
              show-overflow-tooltip
            >
              <template slot-scope="scope">
                <el-form-item>
                  <el-input size="mini"
                    v-model="scope.row.specification"
                    disabled
                  />
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              label="计量单位"
              prop="unit"
              min-width="160"
              show-overflow-tooltip
            >
              <template slot-scope="scope">
                <el-form-item>
                  <el-input size="mini" v-model="scope.row.unit" disabled />
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              label="批发价"
              prop="batchPrice"
              min-width="160"
              show-overflow-tooltip
            >
              <template slot-scope="scope">
                <el-form-item>
                  <el-input size="mini" v-model="scope.row.batchPrice" placeholder="请输入批发价" />
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              label="格力折扣"
              prop="discAmount"
              min-width="160"
              show-overflow-tooltip
            ><template slot-scope="scope">
              <el-form-item>
                <el-input size="mini" v-model="scope.row.discAmount" placeholder="请输入格力折扣" />
              </el-form-item>
            </template>
            </el-table-column>
            <el-table-column
              align="center"
              label="返利类型"
              prop="rebateUseRate"
              min-width="160"
              show-overflow-tooltip
            ><template slot-scope="scope">
              <el-form-item>
                <el-select
                size="mini"
                  v-model="scope.row.walletRebateId"
                  multiple
                  :disabled="scope.row.fang"
                  placeholder="请选择返利类型"
                  @change="handelRebateList($event, scope.$index, scope.row)"
                  @remove-tag="handleRemove2($event, scope.$index, scope.row)"
                >
                  <el-option
                    v-for="(item, index) in scope.row.rebateList"
                    :key="item.index"
                    :value="item.walletRebateId"
                    :label="item.name"
                  />
                </el-select>
              </el-form-item>
            </template>
            </el-table-column>
            <el-table-column
              align="center"
              label="生效日期"
              prop="startDate"
              min-width="160"
              show-overflow-tooltip
            ><template slot-scope="scope">
              <el-form-item>
                <el-date-picker
                size="mini"
                  v-model="scope.row.startDate"
                  type="date"
                  placeholder="请输入调价日期"
                  value-format="yyyy-MM-dd"
                />
              </el-form-item>
            </template>
            </el-table-column>
            <el-table-column
              align="center"
              label="失效日期"

              prop="endDate"
              min-width="160"
              show-overflow-tooltip
            ><template slot-scope="scope">
              <el-form-item>
                <el-date-picker
                  v-model="scope.row.endDate"
                  type="date"
                        size="mini"
                  placeholder="请输入调价日期"
                  value-format="yyyy-MM-dd"
                />
              </el-form-item>
            </template>
            </el-table-column>
            <!-- <el-table-column
              align="center"
              label="是否促销价"
              prop="isPromote"
              min-width="160"
              show-overflow-tooltip
            ><template slot-scope="scope">
              <el-form-item>
                <el-select
                  v-model="scope.row.isPromote"
                  placeholder="请选择是否促销价"
                >
                  <el-option
                    v-for="item in scope.row.isPromoteArr"
                    :key="item.bool"
                    :value="item.bool"
                    :label="item.value"
                  />
                </el-select>
              </el-form-item>
            </template>
            </el-table-column> -->
            <el-table-column
              align="center"
              label="数量"
              prop="qty"
              min-width="160"
              show-overflow-tooltip
            ><template slot-scope="scope">
              <el-form-item>
                <el-input size="mini" v-model="scope.row.qty" placeholder="请输入数量" />
              </el-form-item>
            </template>
            </el-table-column>
            <el-table-column
              align="center"
              label="现金钱包"
              prop="wallets"
              min-width="160"
              show-overflow-tooltip
            ><template slot-scope="scope">
              <el-form-item>
                <template v-if="!cid">
                  <el-select
                  size="mini"
                    v-model="scope.row.walletsId"
                    multiple
                    clearable
                    placeholder="请选择现金钱包"
                    @change="handelWallets($event, scope.$index, scope.row)"
                    @remove-tag="
                      handleRemove($event, scope.$index, scope.row)
                    "
                  >
                    <el-option
                      v-for="item in scope.row.walleList"
                      :key="item.id"
                      :value="item.id"
                      :label="item.name"
                    /> </el-select></template>
                <template v-else>
                  <el-select
                    v-model="scope.row.walletsId"
                    multiple
                    clearable
                    size="mini"
                    placeholder="请选择现金钱包"
                    @change="handelWallets($event, scope.$index, scope.row)"
                    @remove-tag="
                      handleRemove($event, scope.$index, scope.row)
                    "
                  >
                    <el-option
                      v-for="item in scope.row.walleList"
                      :key="item.walletId || item.id"
                      :value="item.walletId || item.id"
                      :label="item.walletName || item.name"
                    /> </el-select></template>
              </el-form-item>
            </template>
            </el-table-column>
            <el-table-column
              align="center"
              label="发布"
              prop="isPublish"
              min-width="160"
              show-overflow-tooltip
            ><template slot-scope="scope">
              <el-form-item>
                <el-select
                size="mini"
                  v-model="scope.row.isPublish"
                  placeholder="请选择是否发布"
                >
                  <el-option
                    v-for="item in scope.row.isPublishArr"
                    :key="item.bool"
                    :value="item.bool"
                    :label="item.value"
                  />
                </el-select>
              </el-form-item>
            </template>
            </el-table-column>
          </el-table>
          </div>
        </el-form>
        <el-form v-else :model="detail" size="small" label-position="left">
          <el-table
            v-loading="listLoading"
            :data="detail"
            element-loading-text="Loading"
            border
            fit
            highlight-current-row
            stripe
          >
            <el-table-column
              align="center"
              label="操作类型"
              prop="nname"
              min-width="160"
              show-overflow-tooltip
            >
              <!-- <el-button type="text" class="textColor">修改</el-button> -->
              <template slot-scope="scope">
                <el-button
                  type="text"
                  class="textColor"
                  @click="hanleDeleInd(scope.$index)"
                >删除</el-button>
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              label="销售类型"
              prop="mainId"
              min-width="160"
              show-overflow-tooltip
            >
              <template slot-scope="scope">
                <el-form-item>

                  <el-select
                    v-model="scope.row.saleTypeId"
                    placeholder="请选择销售类型"
                    @change="handleSale($event, scope.$index, scope.row.id)"
                  >
                    <el-option
                      v-for="item in typeList"
                      :label="item.saleName"
                      :value="item.id"
                    />
                  </el-select>

                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              label="物料编码"
              prop="materialNumber"
              min-width="160"
              show-overflow-tooltip
            >
              <template slot-scope="scope">
                <el-form-item>
                  <el-input size="mini" v-model="scope.row.materialNumber" />
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              label="产品名称"
              prop="materialName"
              min-width="160"
              show-overflow-tooltip
            >
              <template slot-scope="scope">
                <el-form-item>
                  <el-input size="mini" v-model="scope.row.materialName" />
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              label="规格型号"
              prop="specification"
              min-width="160"
              show-overflow-tooltip
            >
              <template slot-scope="scope">
                <el-form-item>
                  <el-input size="mini" v-model="scope.row.specification" />
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              label="计量单位"
              prop="unit"
              min-width="160"
              show-overflow-tooltip
            >
              <template slot-scope="scope">
                <el-form-item>
                  <el-input size="mini" v-model="scope.row.SalesType" />
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              label="批发价"
              prop="batchPrice"
              min-width="160"
              show-overflow-tooltip
            >
              <template slot-scope="scope">
                <el-form-item>
                  <el-input size="mini" v-model="scope.row.batchPrice" />
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              label="零售价"
              prop="retailPrice"
              min-width="160"
              show-overflow-tooltip
            ><template slot-scope="scope">
              <el-form-item>
                <el-input size="mini" v-model="scope.row.retailPrice" />
              </el-form-item>
            </template>
            </el-table-column>
            <el-table-column
              align="center"
              label="返利使用比例"
              prop="rebateUseRate"
              min-width="160"
              show-overflow-tooltip
            ><template slot-scope="scope">
              <el-form-item>
                <el-input size="mini" v-model="scope.row.rebateUseRate" />
              </el-form-item>
            </template>
            </el-table-column>
            <el-table-column
              align="center"
              label="返利类型"
              prop="rebateUseRate"
              min-width="160"
              show-overflow-tooltip
            ><template slot-scope="scope">
              <el-form-item>
                <el-select
                  v-model="scope.row.walletRebateId"
                  multiple
                  :disabled="scope.row.fang"
                  placeholder="请选择返利类型"
                  @change="handelRebateList($event, scope.$index, scope.row)"
                  @remove-tag="handleRemove2($event, scope.$index, scope.row)"
                >
                  <el-option
                    v-for="(item, index) in scope.row.rebateList"
                    :key="item.index"
                    :value="item.walletRebateId"
                    :label="item.name"
                  />
                </el-select>
              </el-form-item>
            </template>
            </el-table-column>
            <el-table-column
              align="center"
              label="生效日期"
              prop="startDate"
              min-width="160"
              show-overflow-tooltip
            ><template slot-scope="scope">
              <el-form-item>
                <el-date-picker
                  v-model="scope.row.startDate"
                  type="date"
                  placeholder="请输入调价日期"
                  value-format="yyyy-MM-dd"
                />
              </el-form-item>
            </template>
            </el-table-column>
            <el-table-column
              align="center"
              label="失效日期"
              prop="endDate"
              min-width="160"
              show-overflow-tooltip
            ><template slot-scope="scope">
              <el-form-item>
                <el-date-picker
                  v-model="scope.row.endDate"
                  type="date"
                  placeholder="请输入调价日期"
                  value-format="yyyy-MM-dd"
                />
              </el-form-item>
            </template>
            </el-table-column>
            <el-table-column
              align="center"
              label="是否促销价"
              prop="isPromote"
              min-width="160"
              show-overflow-tooltip
            ><template slot-scope="scope">
              <el-form-item>
                <el-select
                  v-model="scope.row.isPromote"
                  placeholder="请选择是否促销价"
                >
                  <el-option
                    v-for="item in scope.row.isPromoteArr"
                    :key="item.bool"
                    :value="item.bool"
                    :label="item.value"
                  />
                </el-select>
              </el-form-item>
            </template>
            </el-table-column>
            <el-table-column
              align="center"
              label="数量"
              prop="qty"
              min-width="160"
              show-overflow-tooltip
            ><template slot-scope="scope">
              <el-form-item>
                <el-input size="mini" v-model="scope.row.qty" />
              </el-form-item>
            </template>
            </el-table-column>
            <el-table-column
              align="center"
              label="支付钱包类型"
              prop="wallets"
              min-width="160"
              show-overflow-tooltip
            ><template slot-scope="scope">
              <el-form-item>
                <el-select
                  v-model="scope.row.isPublish"
                  placeholder="请选择支付钱包类型"
                >
                  <el-option
                    v-for="item in scope.row.rebateList"
                    :key="item.walletRebateId"
                    :value="item.walletRebateId"
                    :label="item.name"
                  />
                </el-select>
              </el-form-item>
            </template>
            </el-table-column>
            <el-table-column
              align="center"
              label="发布"
              prop="isPublish"
              min-width="160"
              show-overflow-tooltip
            ><template slot-scope="scope">
              <el-form-item>
                <el-select
                  v-model="scope.row.isPublish"
                  placeholder="请选择是否发布"
                >
                  <el-option
                    v-for="item in scope.row.isPublishArr"
                    :key="item.bool"
                    :value="item.bool"
                    :label="item.value"
                  />
                </el-select>
              </el-form-item>
            </template>
            </el-table-column>
          </el-table>
        </el-form>
        <el-dialog
          title="提示"
          :visible.sync="centerDialogVisible"
          width="50%"
          center
          class="dia"
        >
          <el-form
            ref="form"
            :model="form"
            label-width="80px"
            :inline="false"
            size="normal"
          >
            <el-form-item label="产品类型">
              <el-select v-model="materialId" placeholder="请选择" width="100%">
                <el-option
                  v-for="item in materialLis"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-form>

          <span slot="footer" class="dialog-footer">
            <el-button @click="centerDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="hanleAdd">确 定</el-button>
          </span>
        </el-dialog>
    </div>
    <div class="btn-group clearfix">
      <template v-if="!cid">
        <el-button size="small" @click="handleReseat">重置</el-button>
        <el-button
          size="small"
          type="primary"
          @click="hanleSbumit"
        >提交</el-button>
      </template>
      <template v-else>
        <el-button
          size="small"
          type="primary"
          @click="hanleSave"
        >保存</el-button>
      </template>
      <el-button
        style="float: right"
        type="primary"
        size="small"
        @click="centerDialogVisible = true"
      >添加</el-button>
    </div>

    <el-dialog title="" :visible.sync="centerDialogVisible" width="80%" center>
      <div>
        <el-form
          ref="screenForm"
          :model="screenForm"
          label-width="70px"
          size="small"
          label-position="left"
        >
          <el-row :gutter="20">
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="名称" prop="keyword">
                <el-input size="mini"
                  v-model="screenForm.keyword"
                  placeholder="请输入名称"
                />
              </el-form-item>
            </el-col>
              <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="物料编码" prop="materialNumber">
                <el-input size="mini"
                  v-model="screenForm.materialNumber"
                  placeholder="请输入物料编码"
                />
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="规格" prop="specification">
                <el-input size="mini"
                  v-model="screenForm.specification"
                  placeholder="请输入规格"
                />
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :lg="12" class="tr">
              <el-form-item label="">
                <el-button
                  size="small"
                  @click="resetScreenForm"
                >清空</el-button>
                <el-button
                  size="small"
                  type="primary"
                  @click="submitScreenForm"
                >搜索</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div class="mymain-container">
        <div class="table">
          <el-table
            ref="multipleTable"
            v-loading="listLoading"
            :data="dataList"
            element-loading-text="Loading"
            border
            fit
            highlight-current-row
            stripe
            @select-all="handleSelectionAllChange"
            @selection-change="handleSelectionChange"
          >
<!--            :selectable="selectable"-->
            <el-table-column
              align="center"
              type="selection"
              width="55"
            />
            <el-table-column
              align="center"
              label="使用组织"
              prop="useOrgName"
              min-width="100"
            />
            <el-table-column
              align="center"
              label="物料编码"
              prop="number"
              min-width="100"
              label-class-name="bianma"
              class-name="fontstyle"
            />

            <el-table-column
              align="center"
              label="名称"
              prop="name"
              min-width="100"
            />
            <el-table-column
              align="center"
              label="规格型号"
              prop="specification"
              min-width="100"
            />
            <el-table-column
              align="center"
              label="数据状态"
              prop="forbidStatus"
              min-width="100"
            >
              <template slot-scope="scope">
                <el-tag
                  v-if="scope.row.forbidStatus == 'A'"
                  type="success"
                >正常</el-tag>
                <el-tag v-else type="danger">禁用</el-tag>
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              label="是否维护商城资料"
              prop="maintainData"
              min-width="100"
            />
            <el-table-column
              align="center"
              label="是否维护内外机档案"
              prop="maintainFiles"
              min-width="100"
            />
            <el-table-column
              align="center"
              label="基本单位"
              prop="baseUnitId"
              min-width="100"
            />
            <el-table-column
              align="center"
              label="已使用"
              prop="used"
              min-width="100"
            />
          </el-table>
        </div>
        <!-- 分页 -->
        <div class="fr">
          <el-pagination
            :current-page="currentPage"
            :page-sizes="[10]"
            :page-size="10"
            layout="total, sizes, prev, pager, next, jumper"
            :total="listTotal"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleDetermine">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getMaterialList,
  getProductRriceAdd,
  getProductRriceDetail,
  getRebate,
  getTypeList,
  getUserList,
  getWalletList,
  handleEdit
} from '@/api/basic_data/material'

import { downloadFiles, handleImport } from '@/utils/util'

import Mixin from '@/mixin/index'

export default {
  mixins: [Mixin],
  props: ['cid'],
  data() {
    return {
      baseURL: '',
      importFileList: [],
      listLoading: false,
      importLoading:false,
      screenForm: {
        keyword: '',
        specification: '',
        materialNumber:''
      },
      base: {
        adminCompanyId: '',
        adminWebsitId: '',
        billId: '',
        examineNote: '',
        examineResult: true,
        examineStatus: '',
        id: null,
        serviceId: '',
        serviceName: '',
        remark: '',
        saleTypeId: ''
      },
      items: [],
      materialLis: [],
      materialId: '',
      rebateList: [],
      typeList: [],
      saleTypeId: '',
      serviceId: '',
      adminCompanyId: '',
      serviceName: '',
      adminCompanyName: '',
      adminWebsitId: '',

      detail: [],
      form: {},
      startDate: '',
      userList: [],
      centerDialogVisible: false,
      selectData: [],
      walleList: [],
      fang: true,
      wall1: [],
      wall2: []
    }
  },
  mounted() {

  },
  methods: {
    handleSale(e, index, saleTypes) {
      // console.log(this.rebateList);
       this.$set(this.items[index], 'rebateList', [])
      const linshi = []
      let saleArr = []
      const rebateList = []
      this.rebateList.forEach((k) => {
        k.saleTypes.forEach((j) => {
          if (!linshi.includes(j.saleTypeId) && j.saleTypeId == e) {
            rebateList.push(k)
          }
        })
      })

      saleArr = this.typeList.filter((i) => {
        return e == i.id
      })
      console.log(rebateList, '4444', this.rebateList, e)
      // this.items[index].mainName = saleArr[0].mainName;
      this.items[index].saleTypeId = e
      this.items[index].fang = false
      this.items[index].saleTypeName = saleArr[0].saleName
      this.$set(this.items[index], 'rebateList', rebateList)
      // this.fang = false;
    },
    resArr(arr1, arr2) {
      return arr1.filter((v) => arr2.every((val) => val.id != v.id))
    },
    // xua
    handelWallets(e, index, row) {
      // const id = row.cid || row.updPriceBillId
      // // if (e && e.length) {
      //   for (let i = 0; i < e.length; i++) {
      //     // if (!this.wall1.includes(e[e.length - 1])) {
      //       this.$set(row, 'wallets', [
      //         ...row.wallets,
      //         {
      //           type: 'COMMONLY',REBATE
      //           id: '',
      //           updPriceBillId: id,
      //           updPriceBillItemId: id,
      //           walletId: e[e.length - 1]
      //         }
      //       ])
      //       // this.wall1.push(e[e.length - 1])
      //     }
      //   // }
      // // }
    },
    handelRebateList(e, index, row) {
      // const id = row.cid || row.updPriceBillId
      // if (e && e.length) {
      //   for (let i = 0; i < e.length; i++) {
      //     if (!this.wall2.includes(e[e.length - 1])) {
      //       console.log(44544)
      //       this.$set(row, 'wallets', [
      //         ...row.wallets,
      //         {
      //           type: 'REBATE',
      //           id: '',
      //           updPriceBillId: id,
      //           updPriceBillItemId: id,
      //           walletId: e[e.length - 1]
      //         }
      //       ])
      //       this.wall2.push(e[e.length - 1])
      //     }
      //   }
      // }
    },
    handleRemove(e, index, row) {
      // const copxyWall = JSON.parse(JSON.stringify(row.wallets))
      // if (row.wallets[0].walletId === e) {
      //   copxyWall.splice(0, 1)
      // }
      // for (let i = 0; i < row.wallets.length; i++) {
      //   if (this.wall1.includes(e) && row.wallets[i].walletId === e) {
      //     this.wall1.splice(i, 1)
      //     copxyWall.splice(i, 1)
      //   }
      // }
      // this.$set(row, 'wallets', copxyWall)
    },
    handleRemove2(e, index, row) {
      // const copxyWall = JSON.parse(JSON.stringify(row.wallets))
      // for (let i = 0; i < row.wallets.length; i++) {
      //   if (this.wall2.includes(e) && row.wallets[i].walletId == e) {
      //     copxyWall.splice(i, 1)
      //     this.wall2.splice(i, 1)
      //   }
      // }
      // this.$set(row, 'wallets', copxyWall)
    },
    handleUser(e) {
      const adminCompany = this.userList.filter((k) => {
        return e == k.roleId
      })[0]
      this.adminWebsitId = adminCompany.adminUserId
      this.adminCompanyName = adminCompany.adminCompanyName
      this.serviceName = adminCompany.nickName || 'd'
      this.serviceId = adminCompany.roleId
    },
    goBack() {
      this.$parent.cid = ''
      this.$parent.show = 1
      // 更新父组件数据
      this.$parent.getList()
    },
    indexMethod(index) {
      console.log(index)
    },
    hanleDeleInd(index, id,materialId) {

      if (id || materialId) {
        const dataArr = JSON.parse(JSON.stringify(this.dataList))
        for (let j = 0; j < dataArr.length; j++) {
          if ((id || materialId) === dataArr[j].id) {
            // dataArr[j].disabled = false
            console.log(747)
            this.$set(dataArr[j], 'disabled', false)
          }
        }
        this.dataList = dataArr
      }

      this.items.splice(index, 1)
    },
    handleDetermine() {
      if (this.selectData.length) {
        for (let i = 0; i < this.selectData.length; i++) {
          for (let j = 0; j < this.dataList.length; j++) {
            if (this.selectData[i].id == this.dataList[j].id) {
              this.dataList[j].disabled = true
            }
          }
          this.items.push({
            baseUnitId: '',
            batchPrice: '',
            unit: this.selectData[i].baseUnitId,
            billId: '',
            endDate: '',
            isPromote: '',
            isPublish: '',
            materialId: this.selectData[i].id,
            materialName: this.selectData[i].name,
            materialNumber: this.selectData[i].number,
            discAmount: '',
            saleTypes: this.selectData[i].saleTypes,
            materialOldNumber: this.selectData[i].oldNumber,
            parentId: '',
            walletRebateName: '',
            walletRebateId: [],
            rebateRate: '',
            qty: '',
            rebateUseRate: '',
            retailPrice: '' || 0,
            saleTypeId: '',
            specification: this.selectData[i].specification,
            startDate: this.selectData[i].startDate,
            wallets: [],
            rebateWallets: [],
            rebateList: [],
            walletsId: [this.selectData[i].wallets[0].id],
            walleList: this.selectData[i].wallets,
            isPublishArr: [
              {
                value: '是',
                bool: true
              },
              {
                value: '否',
                bool: false
              }
            ],
            isPromoteArr: [
              {
                value: '是',
                bool: true
              },
              {
                value: '否',
                bool: false
              }
            ],
            cid: this.selectData[i].id,
            fang: true
          })
        }

        this.selectData = []
        this.$refs.multipleTable.clearSelection()
        this.centerDialogVisible = false
      } else {
        this.$errorMsg('请选择内容')
      }
    },
    getMaterialList() {
      const params = {
        pageNum: 1,
        pageSize: 10,
        keyword: '',
        bindMain: true
      }
      getMaterialList(params).then((res) => {
        this.materialLis = res.data.records
      })
    },
    handleSelectionAllChange(e) {
      this.selectData = e
    },
    handleSelectionChange(e) {
      this.selectData = e
    },
    hanleAdd() {
      if (this.materialId) {
        this.centerDialogVisible = false
      } else {
        this.$errorMsg('请选择产品')
      }
    },
    async getList() {
      const params1 = {
        pageNum: 1,
        pageSize: -1,
        saleCode: '',
        saleName: '',
        status: ''
      }
      getTypeList(params1).then((res) => {
        this.typeList = res.data.records
      })
      getUserList({
        pageNum: 1,
        pageSize: -1,
        adminWebsitId: '',
        isCustomer: 0,
        roleId: '',
        status: '',
        userName: ''
      }).then((res) => {
        this.userList = res.data.records
      })
      this.listLoading = true
      const params = {
        pageNum: this.currentPage,
        pageSize: 10 || this.pageSize,
        keyword: this.screenForm.keyword,
        specification: this.screenForm.specification,
        number:this.screenForm.materialNumber,
        bindMain: true,
      }
      getMaterialList(params).then((res) => {
        this.dataList = res.data.records
        this.listTotal = res.data.total
        this.listLoading = false
      })

      const rebateParams = {
        pageNum: 1,
        pageSize: -1,
        mainName: '',
        saleTypeCode: '',
        saleTypeName: '',
        status: ''
      }
      const ress = await getRebate(rebateParams)
      this.rebateList = ress.data.records
      const wall = await getWalletList({ mainId: '', walletName: '' })
      for (let u = 0; u < wall.data.length; u++) {
        wall.data[u].walletId = wall.data[u].id
        wall.data[u].walletName = wall.data[u].name
      }
      this.walleList = wall.data
      if (this.$parent.show === 5) {
        getProductRriceDetail({ id: this.cid }).then((res) => {
          for (let i = 0; i < res.data.items.length; i++) {
            const linshi = []
            const rebateList = []
            const walletIds = []
            const walletRebateIds = []
            res.data.items[i].isPublishArr = [
              {
                value: '是',
                bool: true
              },
              {
                value: '否',
                bool: false
              }
            ]
            res.data.items[i].isPromoteArr = [
              {
                value: '是',
                bool: true
              },
              {
                value: '否',
                bool: false
              }
            ]
            res.data.items[i].wallets = []
            res.data.items[i].walletsId = []
            res.data.items[i].walletRebateId = []
            res.data.items[i].rebateWallets = []

            for (let p = 0; p < res.data.items[i].wallets2.length; p++) {
              if (res.data.items[i].wallets2[p].type === 'COMMONLY') {
                walletIds.push(res.data.items[i].wallets2[p].walletId)
                this.$set(res.data.items[i], 'wallets', [
                  ...res.data.items[i].wallets,
                  {
                    type: 'COMMONLY',
                    id: '',
                    updPriceBillId: res.data.items[i].updPriceBillId,
                    updPriceBillItemId: res.data.items[i].updPriceBillId,
                    walletId: res.data.items[i].wallets2[p].walletId
                  }
                ])
              } else if (res.data.items[i].wallets2[p].type === 'REBATE') {
                walletRebateIds.push(res.data.items[i].wallets2[p].walletId)
                this.$set(res.data.items[i], 'wallets', [
                  ...res.data.items[i].wallets,
                  {
                    type: 'REBATE',
                    id: '',
                    updPriceBillId: res.data.items[i].updPriceBillId,
                    updPriceBillItemId: res.data.items[i].updPriceBillId,
                    walletId: res.data.items[i].wallets2[p].walletId
                  }
                ])
              }
            }
            console.log(res.data.items[i].wallets)
            this.rebateList.forEach((k) => {
              k.saleTypes.forEach((j) => {
                if (
                  !linshi.includes(j.saleTypeId) &&
                  j.saleTypeId === res.data.items[i].saleTypeId
                ) {
                  for (let w = 0; w < walletRebateIds.length; w++) {
                    if (walletRebateIds[w] !== k.walletRebateId) {
                      // walletRebateIds.splice(w, 1)
                    }
                  }
                  rebateList.push(k)
                }
              })
            })
            if (this.dataList.length) {
              for (let j = 0; j < res.data.items.length; j++) {
                for (let k = 0; k < this.dataList.length; k++) {
                  if (res.data.items[j].materialId === this.dataList[k].id) {
                    this.dataList[k].disabled = true
                  }
                }
              }
              // console.log(this.dataList)
            }
            this.$set(res.data.items[i], 'walletsId', walletIds)
            this.$set(res.data.items[i], 'walletRebateId', walletRebateIds)
            res.data.items[i].walleList = this.walleList
            res.data.items[i].rebateList = rebateList
            res.data.items[i].fang = false
          }

          this.items = res.data.items
          this.base = res.data
          this.listLoading = false
        })
      } else {

      }
    },
    /**
     * 根据条件禁用行复选框
     * 函数返回值为false则禁用选择(反之亦然)
     * @param {Object} row - 行数据
     * @param {String} index - 索引值
     * @return Boolean
     */
    // selectable: function(row, index) {
    //   // row.disabled == undefined 才能被选中
    //   if (row.disabled == undefined || row.disabled == false) {
    //     return true
    //   } else {
    //     return false
    //   }
    //   // 函数必须有返回值且是布尔值
    //   // 页面刷新后该函数会执行 N 次进行判断(N 为表格行数)
    //   // 如果没有返回值则默认返回false(全部无法选中)
    // },
    hanleSbumit() {
      this.filterWallets(this.items)

      // return
      const params = {
        ...this.base,
        items: this.items,
        saleTypeId: this.saleTypeId,
        adminWebsitId: this.adminWebsitId,
        serviceId: this.serviceId,
        adminCompanyName: this.adminCompanyName,
        serviceName: this.nickName
      }
      //  console.log(this.filterWallets(this.items),params);
      getProductRriceAdd(params).then((res) => {
        console.log(res.code,'klkk');
        this.$successMsg('成功')
        this.$parent.show = 1
        this.$parent.getList()
      }).catch((err)=>{
        console.log(err);
       for (let j = 0; j < this.items.length; j++) {
          this.$set(this.items[j],'wallets',[])
        }
      })
    },
    hanleSave() {
      this.filterWallets(this.items)
      handleEdit({
        ...this.base,
        items: this.items,
        saleTypeId: this.saleTypeId,
        adminWebsitId: this.adminWebsitId,
        serviceId: this.serviceId,
        adminCompanyName: this.adminCompanyName,
        serviceName: this.nickName
      }).then((res) => {
        this.$successMsg('成功')
        this.$parent.cid = ''
        this.$parent.show = 1
        this.$parent.getList()

      }).catch((err)=>{
       for (let j = 0; j < this.items.length; j++) {

         this.$set(this.items[j],'wallets',[])

        }


      })
    },
    handleReseat() {
      this.base = {
        adminCompanyId: '',
        adminWebsitId: '',
        billId: '',
        examineNote: '',
        examineResult: true,
        examineStatus: '',
        id: null,
        serviceId: '',
        serviceName: '',
        remark: '',
        saleTypeId: ''
      }
      this.saleTypeId = ''
      this.items = []
    },
    // 导入
    async handleImport(param) {
      this.importLoading = true
      const file = param.file
      const formData = new FormData()
      formData.append('file', file)

      const result = await handleImport('/product-upd-price/material/import', formData)
      this.importLoading = false
      this.importFileList = []
      if (result.code === 200) {
        this.$alert(result.message, '导入成功', {
          confirmButtonText: '确定'
        })

        for (let i = 0; i < result.data.length; i++) {
          const linshi = []
          const rebateList = []
          const walletIds = []
          const walletRebateIds = []
          // result.data[i].id = result.data[i].materialId
          result.data[i].isPublishArr = [
            {
              value: '是',
              bool: true
            },
            {
              value: '否',
              bool: false
            }
          ]
          result.data[i].isPromoteArr = [
            {
              value: '是',
              bool: true
            },
            {
              value: '否',
              bool: false
            }
          ]
          result.data[i].wallets = []
          result.data[i].walletsId = []
          result.data[i].walletRebateId = []
          result.data[i].rebateWallets = []
          result.data[i].saleTypes = this.typeList
          result.data[i].mainId = result.data[i].saleTypeId
          for (let p = 0; p < result.data[i].wallets2.length; p++) {
            if (result.data[i].wallets2[p].type === 'COMMONLY') {
              walletIds.push(result.data[i].wallets2[p].walletId)
              this.$set(result.data[i], 'wallets', [
                ...result.data[i].wallets,
                {
                  type: 'COMMONLY',
                  id: '',
                  updPriceBillId: result.data[i].updPriceBillId,
                  updPriceBillItemId: result.data[i].updPriceBillId,
                  walletId: result.data[i].wallets2[p].walletId
                }
              ])
            } else if (result.data[i].wallets2[p].type === 'REBATE') {
              walletRebateIds.push(result.data[i].wallets2[p].walletId)
              this.$set(result.data[i], 'wallets', [
                ...result.data[i].wallets,
                {
                  type: 'REBATE',
                  id: '',
                  updPriceBillId: result.data[i].updPriceBillId,
                  updPriceBillItemId: result.data[i].updPriceBillId,
                  walletId: result.data[i].wallets2[p].walletId
                }
              ])
            }
          }

          this.rebateList.forEach((k) => {
            k.saleTypes.forEach((j) => {
              if (
                !linshi.includes(j.saleTypeId) &&
                j.saleTypeId === result.data[i].saleTypeId
              ) {
                for (let w = 0; w < walletRebateIds.length; w++) {
                  if (walletRebateIds[w] !== k.walletRebateId) {
                    // walletRebateIds.splice(w, 1)
                  }
                }

                rebateList.push(k)
              }
            })
          })

          this.$set(result.data[i], 'walletsId', walletIds)
          this.$set(result.data[i], 'walletRebateId', walletRebateIds)
          result.data[i].walleList = this.walleList
          result.data[i].rebateList = rebateList
          result.data[i].fang = false
        }
        // if (this.dataList.length) {
        //   for (let j = 0; j < result.data.length; j++) {
        //     for (let k = 0; k < this.dataList.length; k++) {
        //       if (result.data[j].materialId === this.dataList[k].id) {
        //         this.dataList[k].disabled = true
        //       }
        //     }
        //   }
        // }


        // this.items = [...this.items,...result.data]
        // let newArr = [];
        // let obj = {};
        // for (var i = 0; i < this.items.length; i++) {
        //   if (!obj[this.items[i].materialId]) {
        //     newArr.push(this.items[i])
        //     obj[this.items[i].materialId] = true
        //   }
        // }
        // console.log(newArr,454 ,obj)
        this.items = [...this.items,...result.data]
        // console.log(resArrs(result.data,this.dataList),'55555')

      } else {
        this.$alert(result.message, '导入失败', {
          confirmButtonText: '确定'
        })
      }
    },
    // 下载经销商模板
    hanleDownloadFiles() {
      downloadFiles('/product-upd-price/download')
    },
    filterWallets(array){

      for (let i = 0; i < array.length; i++) {
         const wallets = []
        if (array[i].walletsId.length){
        for (let j = 0; j < array[i].walleList.length; j++) {
          for (let k = 0; k < array[i].walletsId.length; k++) {
            if (array[i].walleList[j].id === array[i].walletsId[k]) {
              wallets.push({
                type: 'COMMONLY',
                id: '',
                updPriceBillId: array[i].cid || array[i].updPriceBillId,
                updPriceBillItemId: array[i].cid || array[i].updPriceBillId,
                walletId: array[i].walletsId[k]
              })
            }
          }
        }
        array[i].wallets = wallets
       }
       if(array[i].walletRebateId.length){
          for (let k = 0; k < array[i].rebateList.length; k++) {
          if (!array[i].walletRebateId.length) return
          for (let j = 0; j < array[i].walletRebateId.length; j++) {
            if (array[i].rebateList[k].walletRebateId === array[i].walletRebateId[j]) {
              wallets.push({
                type: 'REBATE',
                id: '',
                updPriceBillId: array[i].cid || array[i].updPriceBillId,
                updPriceBillItemId: array[i].cid || array[i].updPriceBillId,
                walletId: array[i].walletRebateId[j]
              })
            }
          }
              array[i].wallets = wallets
        }

       }

      }
      return array
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-date-editor--date {
  width: 100%;
}
.dia .el-select--normal {
  width: 100%;
}
.clearfixs{
  display: flex;
}
.import-btn{
  margin-right: 10px;
}
</style>
