<template>
  <div class="shopping_cell">
    <el-row class="shopping_cell_row">
      <el-col :span="2" class="shopping_cell_check">
        <el-checkbox
          v-if="!readOnly"
          v-model="checkedItem"
          @change="getState"
        ></el-checkbox>
        <div v-else class="shopping_cell_expired">
          <span>已下架</span>
        </div>
      </el-col>
      <el-col :span="8">
        <div class="shopping_cell_item">
          <img
            :src="
              cellIsService
                ? cellItems.service.cover
                : cellItems.application.logo
            "
            class="shopping_cell_img"
          />
          <div class="shopping_cell_msg">
            <div class="shopping_cell_name">
              {{
                cellIsService
                  ? cellItems.service.name
                  : cellItems.application.app_name
              }}
              <span
                v-if="
                  cellIsService &&
                  cellItems.service.data_service_type1_name == '时空服务'
                "
                class="tags map_service"
              >
                mapService
              </span>
              <span
                v-if="cellIsService && cellItems.service.openness == 1"
                class="tags shared"
              >
                共享
              </span>
              <span
                v-else-if="cellIsService && cellItems.service.openness == 2"
                class="tags restricted"
              >
                受限
              </span>
              <span
                v-else-if="cellIsService && cellItems.service.openness == 3"
                class="tags sensitive"
              >
                敏感
              </span>
            </div>
            <div class="shopping_cell_msg_other">
              <div v-if="cellIsService" class="shopping_cell_type">
                {{ cellItems.service.sectors_name }}
              </div>
              <div v-else class="shopping_cell_type">
                {{ cellItems.application.ywly_name }}
              </div>
              <div class="shopping_cell_creator">
                {{
                  cellIsService
                    ? cellItems.service.organization_name
                    : cellItems.application.org_name
                }}
              </div>
            </div>
          </div>
        </div>
      </el-col>
      <el-col :span="5">
        <div
          v-if="
            cellItems.service_id != 0 &&
            cellItems.service.request_spcs_info.spcs_type_1 &&
            cellItems.service.request_spcs_info.spcs_type_1.length != 0
          "
          class="shopping_cell_specification"
        >
          <div class="shopping_cell_specification_val">
            <div class="shopping_cell_specification_val_specification">
              {{
                cellItems.service.spcs_info.spcs_type == 1
                  ? "计次收费"
                  : "时长收费"
              }}：{{ cellItems.service.spcs_info.money }}金币/{{
                cellItems.service.spcs_info.spcs_type == 1
                  ? cellItems.service.spcs_info.spcs_count + "次"
                  : "月"
              }}
            </div>
            <div class="shopping_cell_specification_edit">
              <el-popover
                placement="right-start"
                width="300"
                v-model="visible"
                @show="setSpecificationPop"
              >
                <div>
                  <div v-if="cellIsService">
                    <div class="shopping_cell_specification_title">规格：</div>
                    <div
                      v-if="
                        cellItems.service.request_spcs_info &&
                        cellItems.service.request_spcs_info.spcs_type_1 &&
                        cellItems.service.request_spcs_info.spcs_type_1
                          .length != 0
                      "
                    >
                      <el-button
                        v-for="(item, index) in cellItems.service
                          .request_spcs_info.spcs_type_1"
                        :key="'specification' + index"
                        :class="
                          specificationPop.id == item.id
                            ? 'shopping_cell_specification_btn active'
                            : 'shopping_cell_specification_btn'
                        "
                        size="mini"
                        @click="changeSpecification(item)"
                      >
                        {{ item.money }}金币/{{ item.spcs_count }}次
                      </el-button>
                      <el-button
                        v-if="
                          cellItems.service.request_spcs_info.spcs_type_2 &&
                          cellItems.service.request_spcs_info.spcs_type_2
                            .length != 0
                        "
                        :class="
                          specificationPop.spcs_type == 2
                            ? 'shopping_cell_specification_btn active'
                            : 'shopping_cell_specification_btn'
                        "
                        size="mini"
                        @click="
                          changeSpecification(
                            cellItems.service.request_spcs_info.spcs_type_2[0]
                          )
                        "
                        >{{
                          cellItems.service.request_spcs_info.spcs_type_2[0]
                            .money
                        }}金币/月</el-button
                      >
                    </div>
                    <div class="no_option" v-else>暂无规格</div>
                  </div>
                </div>
                <div class="pop_footer">
                  <el-button
                    class="shopping_cell_specification_cancel"
                    size="mini"
                    type="text"
                    @click="visible = false"
                  >
                    取消
                  </el-button>
                  <el-button
                    class="shopping_cell_specification_submit"
                    size="mini"
                    @click="submitPop"
                  >
                    确定
                  </el-button>
                </div>
                <el-button v-if="!readOnly" circle size="mini" slot="reference">
                  <img :src="specificationEdit" />
                </el-button>
              </el-popover>
            </div>
          </div>
        </div>
        <div v-else-if="cellItems.app_id != 0" class="shopping_cell_num">
          {{ cellItems.application.price }}金币/月
        </div>
        <div v-else class="shopping_cell_num">
          {{
            cellItems.service.request_spcs_info.spcs_type_2 &&
            cellItems.service.request_spcs_info.spcs_type_2.length != 0
              ? cellItems.service.request_spcs_info.spcs_type_2[0].money
              : 0
          }}金币/月
        </div>
      </el-col>
      <el-col :span="3" class="shopping_cell_num">
        <div v-if="cellIsService && cellItems.service.spcs_info.spcs_type == 1">
          不限时长
        </div>
        <div v-else>
          <el-input-number
            :disabled="readOnly"
            v-model="cellItems.duration"
            @change="changeNum"
            size="mini"
            :min="1"
          ></el-input-number>
        </div>
      </el-col>
      <el-col :span="3" class="shopping_cell_num">
        {{
          cellItems.service_id != 0
            ? (cellItems.service.spcs_info.money * cellItems.duration).toFixed(
                2
              )
            : (cellItems.application.price * cellItems.duration).toFixed(2)
        }}金币
      </el-col>
      <el-col :span="3" class="shopping_cell_options">
        <el-checkbox
          v-if="!readOnly"
          v-model="cellItems.is_subscribe"
          @change="changeSubscription"
        >
          订阅该服务
        </el-checkbox>
        <div class="shopping_cell_options_del">
          <el-button @click="delCellItem" size="mini">删除</el-button>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
  components: {},
  props: {
    cellItem: { type: Object, default: {} },
    cellIndex: { type: Number, default: null },
    cellCheck: {
      type: Boolean,
      default: false,
    },
    readOnly: {
      type: Boolean,
      default: false,
    },
    cellIsService: {
      type: Boolean,
    },
  },
  watch: {
    cellCheck: function (val) {
      this.checkedItem = val;
    },
    cellItem: {
      immediate: true, // 这句重要
      handler(val) {
        this.cellItems = val;
        this.cellItems.is_subscribe =
          this.cellItems.is_subscribe == 1 ? true : false;
        if (this.cellIsService) {
          if (this.cellItems.service.buy_method == 1) {
            this.specificationApplicationBtns = [{ id: 1, value: "按月" }];
          } else if (this.cellItems.service.buy_method == 2) {
            this.specificationApplicationBtns = [{ id: 2, value: "按年" }];
          } else {
            this.specificationApplicationBtns = [
              { id: 1, value: "按月" },
              { id: 2, value: "按年" },
            ];
          }
        }
      },
    },
  },
  data: () => ({
    cellItems: {},
    checkedItem: false,
    specificationEdit: require("../../assets/imgs/ic_edit.png"),
    checkedSubscription: false,
    visible: false,
    specificationPop: {},
    specificationApplicationPop: 1,
    appSpecificationBtns: [
      { id: 1, value: "部署" },
      { id: 2, value: "开发" },
    ],
    specificationApplicationBtns: [
      { id: 1, value: "按月" },
      { id: 2, value: "按年" },
    ],
  }),
  computed: {
    getSpecification() {
      console.log();
    },
    getSumMoney() {
      let sum = 0;
      if (this.cellItems.app_id != 0) {
        sum += this.cellItems.application.price * this.cellItems.duration;
      } else if (this.cellItems.service_id != 0) {
      }
      return sum;
    },
  },
  mounted() {},
  methods: {
    getState(val) {
      if (
        this.cellItems.service_id != 0 &&
        !this.cellItems.service.spcs_info.id
      ) {
        this.$message({
          message:
            (this.cellItems.service_id == 0
              ? this.cellItems.application.app_name
              : this.cellItems.service.name) + "暂不支持购买",
          type: "error",
        });
        this.checkedItem = false;
        this.$emit("changeState", { state: false, index: this.cellIndex });
      } else {
        this.$emit("changeState", { state: val, index: this.cellIndex });
      }
    },
    changeSpecification(val) {
      this.specificationPop = val;
    },
    changeSpecificationApplication(val) {
      this.specificationApplicationPop = val;
    },
    submitPop() {
      this.cellItems.service.spcs_info = this.specificationPop;
      this.visible = false;
      this.updateShoppingCart();
    },
    setSpecificationPop() {
      this.specificationPop = this.cellItems.service.spcs_info;
      console.log(this.specificationPop);
      // this.specificationApplicationPop = this.cellItems.duration_method;
    },
    changeSubscription(val) {
      this.changeCellItem();
      this.updateShoppingCart();
    },
    changeNum(val) {
      this.changeCellItem();
    },
    changeCellItem() {
      this.$emit("changeCellMsg", {
        index: this.cellIndex,
        data: this.cellItems,
      });
    },
    delCellItem() {
      this.$api.serviceShop
        .delShoppingCart([this.cellItems.id])
        .then((response) => {
          if (response.data.success == 1) {
            this.$message({
              message: this.cellIsService ? "删除该服务成功" : "删除该应用成功",
              type: "success",
            });
            this.$emit("updateList");
          } else {
            this.$message({
              message: this.cellIsService ? "删除该服务失败" : "删除该应用失败",
              type: "error",
            });
          }
        });
    },
    updateShoppingCart() {
      let query = {
        id: this.cellItems.id,
        duration: this.cellItems.duration,
        spec_id: this.specificationPop.id,
        duration_method: this.cellItems.duration_method,
        is_subscribe: Number(this.cellItems.is_subscribe),
      };
      this.$api.serviceShop.updateShoppingCart(query).then((response) => {
        if (response.data.success == 1) {
        } else {
          console.log(response.data.errMsg);
        }
      });
    },
  },
};
</script>

<style scoped>
.shopping_cell {
  height: 110px;
  border-bottom: 2px #f4f7fc solid;
  margin: 10px 0;
}
.shopping_cell:nth-last-child(1) {
  border-bottom: 0;
  margin: 10px 0;
}
.shopping_cell_row {
}
.shopping_cell_item {
  display: flex;
  justify-content: flex-start;
}
.shopping_cell_check {
  text-align: center;
  padding-top: 35px;
}
.shopping_cell_expired {
  display: inline-block;
  text-align: center;
  padding: 2px 6px;
  width: 50px;
  background-color: #e3e5ef;
  border-radius: 5px;
  font-size: 12px;
  color: #8890a7;
}
.shopping_cell_img {
  width: 100px;
  height: 100px;
  border-radius: 8px;
}
.shopping_cell_msg {
  width: calc(100% - 120px);
  margin-left: 10px;
  position: relative;
}
.shopping_cell_name {
  width: 100%;
  line-height: 24px;
  text-overflow: -o-ellipsis-lastline;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  -webkit-box-orient: vertical;
  font-size: 16px;
}
.shopping_cell_msg_other {
  position: absolute;
  bottom: 2px;
}
.shopping_cell_type {
  color: #8890a7;
  font-size: 14px;
  line-height: 22px;
}
.shopping_cell_creator {
  color: #8890a7;
  font-size: 14px;
  line-height: 22px;
}
.tags {
  display: inline-block;
  margin-left: 10px;
  padding: 1px 8px 2px;
  font-size: 12px;
  font-weight: 500;
  line-height: 12px;
  border-radius: 4px;
  position: relative;
  top: -2px;
}
.map_service {
  background-color: #626de9;
  border: solid 1px #626de9;
  color: #fff;
}
.shared {
  background-color: #e7fdfc;
  border: solid 1px #8bd6d0;
  color: #25bdb1;
}
.restricted {
  background-color: #fffee8;
  border: solid 1px #e4c884;
  color: #ef9433;
}
.sensitive {
  background-color: #ffefef;
  border: solid 1px #d7a4a9;
  color: #e15260;
}
.shopping_cell_specification {
  width: 80%;
  height: 70px;
  margin: 15px 10%;
  background-color: #f9fafc;
  border-radius: 8px;
  padding: 10px;
  position: relative;
}
.shopping_cell_specification_val {
  color: #8890a7;
  font-size: 14px;
}
.shopping_cell_specification_val_type {
  margin-top: 10px;
}
.shopping_cell_num {
  text-align: center;
  padding-top: 32px;
}
.shopping_cell_options {
  text-align: center;
  padding-top: 20px;
}
.shopping_cell_specification_edit {
  position: absolute;
  right: 10px;
  bottom: 10px;
}
.shopping_cell_options_del {
  margin-top: 15px;
}
.shopping_cell_specification_title {
  margin: 10px 0;
}
.shopping_cell_specification_btn {
  margin: 0 10px 10px;
  max-width: calc(100% - 20px);
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.shopping_cell_specification_btn:hover {
  background-color: #b2baf5;
  color: #f4f7fc;
}
.shopping_cell_specification_btn.active {
  background-color: #515fe7;
  color: #f4f7fc;
}
.shopping_cell_specification_submit {
  background-color: #e56600;
  color: #f4f7fc;
  margin-left: 20px;
}
.shopping_cell_specification_cancel {
  color: #58617a;
}
.pop_footer {
  text-align: right;
  margin: 30px 5px 5px;
}
.no_option {
  text-align: center;
  height: 40px;
  line-height: 40px;
}
</style>
<style>
.shopping_cell_specification_edit .el-button {
  border: 0;
  width: 28px;
  height: 28px;
}
.shopping_cell_num .el-input-number--mini {
  width: 100px;
}
.shopping_cell_num .el-input-number__decrease {
  border-radius: 8px 0 0 8px;
  color: #8390ee;
  font-size: 16px;
}
.shopping_cell_num .el-input-number__increase {
  border-radius: 0 8px 8px 0;
  color: #8390ee;
  font-size: 16px;
}
.shopping_cell_options .el-button {
  border: 0;
  font-size: 14px;
  font-weight: 700;
  color: #77192d;
}
.shopping_cell_options .el-checkbox {
  color: #8890a7;
}
.shopping_cell_options .el-checkbox__input.is-checked + .el-checkbox__label {
  color: #586077;
}
.shopping_cell_options .el-checkbox__inner:hover {
  border-color: #d0d6f4;
}
.shopping_cell_options .el-checkbox__input.is-focus .el-checkbox__inner {
  border-color: #d0d6f4;
}
.shopping_cell_options .el-checkbox__input.is-checked .el-checkbox__inner,
.el-checkbox__input.is-indeterminate .el-checkbox__inner {
  background-color: #d0d6f4;
  border-color: #d0d6f4;
}
.shopping_cell_options .el-checkbox__inner::after {
  border: 1px solid #666;
  border-left: 0;
  border-top: 0;
}
.shopping_cell_check .el-checkbox__inner:hover {
  border-color: #626de9;
}
.shopping_cell_check .el-checkbox__input.is-focus .el-checkbox__inner {
  border-color: #626de9;
}
.shopping_cell_check .el-checkbox__input.is-checked .el-checkbox__inner,
.el-checkbox__input.is-indeterminate .el-checkbox__inner {
  background-color: #626de9;
  border-color: #626de9;
}
.el-popover,
.el-radio-button:first-child:last-child .el-radio-button__inner {
  border-radius: 8px;
}
</style>