<template>
  <el-dialog
    :modal="modal"
    title="设置指定价"
    :visible="isShow"
    width="60%"
    @close="cancel"
  >
    <div>
      <div class="clearfix view-head">
        <div class="float_left">
          <span class="label">指定价</span>
          <span class="desc">（指定某个客户订货价）</span>
        </div>
        <div class="float_right">
          <el-button
            :disabled="!switch_value"
            type="primary"
            @click="customer_show = true"
          >
            选择客户
          </el-button>
        </div>
      </div>
      <el-table
        ref="goodstabel"
        :data="customer_data"
        @selection-change="selectionChange"
      >
        <el-table-column
          fixed="left"
          type="selection"
          align="center"
          width="55"
        ></el-table-column>
        <el-table-column type="index" label="序号" width="60"></el-table-column>
        <el-table-column prop="name" label="客户名称" min-width="140">
          <template slot-scope="scope">
            <div>{{ scope.row.name }}</div>
            <div style="color: #999999; margin-top: 5px">
              {{ scope.row.code }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="销售价" min-width="200">
          <template slot-scope="scope">
            <el-select
              v-model="scope.row.adjPricetag"
              style="width: 60px; display: inline-block"
              size="small"
              :disabled="!!scope.row.priceId"
              placeholder="请选择"
            >
              <el-option
                v-for="(tag, tagi) in adjPricetag_list"
                :key="tagi"
                :label="tag.label"
                :value="tag.value"
              ></el-option>
            </el-select>
            <el-input-number
              v-model="scope.row.price"
              style="width: calc(100% - 80px); margin-left: 5px"
              :controls="false"
              :disabled="!!scope.row.priceId"
              :min="0"
              size="small"
            ></el-input-number>
          </template>
        </el-table-column>
        <el-table-column
          v-if="enabledLadder === 1"
          label="阶梯价"
          min-width="300"
        >
          <template slot-scope="scope">
            <ul>
              <li
                v-for="(price, pi) in scope.row.ladderPrice"
                :key="pi"
                class="ladder-li"
              >
                <span class="price-span">{{ price.from }}</span>
                <span style="font-size: 12px; margin: 0 4px">至</span>
                <span class="price-span">
                  {{ pi === scope.row.ladderPrice.length - 1 ? "∞" : price.to }}
                </span>
                =
                <span style="margin-right: 5px" class="price-span">
                  <span v-if="parseInt(price.adjPricetag) === 1">
                    {{ $NP.minus(price.price, price.adjPrice) }}
                  </span>
                  <span v-else-if="parseInt(price.adjPricetag) === 2">
                    {{ $NP.plus(price.price, price.adjPrice) }}
                  </span>
                  <span v-else-if="parseInt(price.adjPricetag) === 3">
                    {{ $NP.times(price.price, price.adjPrice) }}
                  </span>
                  <span v-else>
                    {{ price.price }}
                  </span>
                </span>
                <el-select
                  v-model="price.adjPricetag"
                  style="width: 60px; display: inline-block"
                  size="small"
                  :disabled="!!scope.row.priceId"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="(tag, tagi) in adjPricetag_list"
                    :key="tagi"
                    :label="tag.label"
                    :value="tag.value"
                  ></el-option>
                </el-select>
                <!-- 如果做减法，最大值只能为当前的价格-->
                <el-input-number
                  v-model="price.adjPrice"
                  style="width: 80px"
                  :controls="false"
                  :disabled="!!scope.row.priceId"
                  :min="0"
                  :max="
                    parseInt(price.adjPricetag) === 1
                      ? price.price
                      : 99999999999
                  "
                  size="small"
                ></el-input-number>
              </li>
            </ul>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="140">
          <template slot-scope="scope">
            <el-button
              v-if="!scope.row.priceId"
              type="text"
              @click="delData(scope.$index)"
            >
              删除
            </el-button>
            <el-button type="text" @click="batchSet(scope.row)">同步</el-button>
            <el-button
              v-if="scope.row.priceId"
              type="text"
              @click="delCustomerPrice(scope.row.priceId, scope.$index)"
            >
              作废
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div slot="footer">
      <div slot="btn-div" class="float_left" style="margin-left: 20px">
        <span class="batch-checkbox">
          <el-checkbox
            v-model="checkedAll"
            @change="checkAllChange"
          ></el-checkbox>
        </span>
        <el-button size="mini" @click="batchDelCustomerTypePrice">
          批量作废
        </el-button>
      </div>
      <el-button size="small" type="primary" @click="confirm">确定</el-button>
      <el-button size="small" @click="cancel">取消</el-button>
    </div>
    <!--  客户选择-->
    <ClientListModal
      v-if="customer_show"
      :is-show="customer_show"
      :is-check="true"
      :modal="false"
      :is-show-add="false"
      @cancel="customer_show = false"
      @confirm="customerSel"
    />
  </el-dialog>
</template>

<script>
  import { delCustomerPrice } from "@/api/Price";
  import ClientListModal from "@/component/common/ClientListModal";
  export default {
    name: "CustomerPrice",
    components: {
      ClientListModal,
    },
    props: {
      isShow: {
        type: Boolean,
        default: false,
      },
      // 是否需要遮罩层
      modal: {
        type: Boolean,
        default: true,
      },
      customerPrice: {
        type: Array,
        default: () => {
          return [];
        },
      },
      customerPriceOpen: {
        type: Boolean,
        default: false,
      },
      enabledLadder: {
        type: [String, Number],
        default: 0,
      },
      ladderPrice: {
        type: Array,
        default: () => {
          return [];
        },
      },
      skuId: {
        type: [String, Number],
        default: 0,
      },
    },
    data() {
      return {
        batch_price: "",
        adjPricetag: "2",
        customer_show: false,
        customer_data: [],
        switch_value: true,
        adjPricetag_list: [
          {
            label: "+",
            value: "2",
          },
          {
            label: "-",
            value: "1",
          },
          {
            label: "*",
            value: "3",
          },
        ],
        checkedAll: false,
        choose_data: [],
      };
    },
    mounted() {
      // this.switch_value = this.customerPriceOpen;
      if (this.customerPrice.length) {
        this.customer_data = this.customerPrice;
      }
    },
    methods: {
      delData(index) {
        this.customer_data.splice(index, 1);
      },
      confirm() {
        if (!this.customer_data) {
          this.$message.warning("至少设置一条指定价数据");
          return;
        }
        const isPrice = this.customer_data.some(
          (item) => !item.price || item.price <= 0
        );
        if (isPrice) {
          this.$message.warning("请输入正确的销售价");
          return;
        }
        this.$emit("confirm", this.switch_value, this.customer_data);
        this.$emit("cancel", this.switch_value, this.customer_data);
      },
      // 关闭弹窗
      cancel() {
        this.$emit("cancel", this.switch_value, this.customer_data);
      },
      switchChange() {},
      // 批量填充
      batchSet(row) {
        this.customer_data = this.customer_data.map((item) => {
          return {
            ...item,
            price: row.price,
            adjPricetag: row.adjPricetag,
            ladderPrice: item.ladderPrice.map((ladder, lai) => {
              return {
                ...ladder,
                adjPricetag: row.ladderPrice[lai].adjPricetag,
                adjPrice: row.ladderPrice[lai].adjPrice,
              };
            }),
          };
        });
      },
      customerSel(arr) {
        const arrData = arr.map((item) => {
          return {
            id: item.id,
            name: item.name,
            code: item.code,
            price: "",
            adjPricetag: "2",
            ladderPrice: this.$_common
              .deepClone(this.ladderPrice)
              .map((price) => {
                return {
                  ...price,
                  adjPricetag: "2",
                  adjPrice: 0,
                };
              }),
          };
        });
        this.customer_data = this.$_common.unique(
          this.customer_data.concat(arrData),
          ["id"]
        );
      },
      delCustomerPrice(id, index) {
        this.$confirm("确认要作废该调价单吗?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(async () => {
          const data = await delCustomerPrice({
            skuId: this.skuId,
            id: [id],
          });
          this.$message({
            type: "success",
            message: "作废成功!",
          });
          this.customer_data.splice(index, 1);
        });
      },
      checkAllChange() {
        this.$refs.goodstabel.toggleAllSelection();
      },
      selectionChange(val) {
        this.checkedAll = val.length === this.customer_data.length;
        this.choose_data = val;
      },
      // 批量删除
      batchDelCustomerTypePrice() {
        this.$confirm("确认要批量作废调价单吗?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(async () => {
          if (!this.choose_data.length) {
            this.$message.warning("请选择要操作的商品");
            return;
          }
          const idData = this.choose_data.map((item) => {
            return item.priceId;
          });
          const data = await delCustomerPrice({
            skuId: this.skuId,
            id: idData,
          });
          this.$message({
            type: "success",
            message: "作废成功!",
          });
          let arr = this.customer_data;
          idData.forEach((item, index) => {
            this.customer_data.forEach((item1, index) => {
              if (item1.priceId === item) {
                arr.splice(index, 1);
              }
            });
          });
          this.customer_data = arr;
        });
      },
    },
  };
</script>
<style scoped lang="scss">
  .view-head {
    padding: 10px 0;
    .label {
      margin-left: 10px;
    }
    .desc {
      color: #999999;
    }
  }
  .ladder-li {
    margin-top: 5px;
    .price-span {
      width: 40px;
      text-align: center;
      line-height: 30px;
      display: inline-block;
      border: 1px solid #dcdfe6;
      border-radius: 4px;
      background-color: #ffffff;
    }
  }
</style>
