<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="9">
        <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 class="shopping_cell_specification">
          <div class="shopping_cell_specification_val">
            <div class="shopping_cell_specification_val_specification">规格：{{ getSpecification }}</div>
            <div
              class="shopping_cell_specification_val_type"
              v-if="cellIsService"
            >申请方式：{{ cellItems.duration_method == 1 ? "按月":"按年" }}</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.length != 0"
                    >
                      <el-button
                        v-for="(item, index) in cellItems.service.request_spcs_info"
                        :key="'specification' + index"
                        :class="specificationPop == item.id ? 'shopping_cell_specification_btn active' : 'shopping_cell_specification_btn'"
                        size="mini"
                        @click="changeSpecification(item.id)"
                      >
                        {{ index + 1 + ". " + "访问次数：" +
                        item.pv +
                        "/日，" +
                        "访问量：" +
                        item.count +
                        "/日" }}
                      </el-button>
                    </div>
                    <div class="no_option" v-else>暂无规格</div>
                  </div>
                  <div v-else>
                    <div class="shopping_cell_specification_title">规格：</div>
                    <div>
                      <el-button
                        v-for="(item, index) in appSpecificationBtns"
                        :key="'specification' + index"
                        :class="specificationPop == item.id ? 'shopping_cell_specification_btn active' : 'shopping_cell_specification_btn'"
                        size="mini"
                        @click="changeSpecification(item.id)"
                      >{{ index + 1 + ". " + item.value }}</el-button>
                    </div>
                  </div>
                  <div v-if="cellIsService" class="shopping_cell_specification_title">申请方式：</div>
                  <div v-if="cellIsService">
                    <el-button
                      v-for="(item, index) in specificationApplicationBtns"
                      :key="'specification' + index"
                      :class="specificationApplicationPop == item.id ? 'shopping_cell_specification_btn active' : 'shopping_cell_specification_btn'"
                      size="mini"
                      @click="changeSpecificationApplication(item.id)"
                    >{{ item.value }}购买</el-button>
                  </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>
      </el-col>
      <el-col :span="4" class="shopping_cell_num">
        <div v-if="cellIsService">
          <el-input-number
            :disabled="readOnly"
            v-model="cellItems.duration"
            @change="changeNum"
            size="mini"
            :min="1"
          ></el-input-number>
        </div>
      </el-col>
      <el-col :span="4" 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: 1,
    specificationApplicationPop: 1,
    appSpecificationBtns: [
      { id: 1, value: "部署" },
      { id: 2, value: "开发" },
    ],
    specificationApplicationBtns: [
      { id: 1, value: "按月" },
      { id: 2, value: "按年" },
    ],
  }),
  computed: {
    getSpecification() {
      if (this.cellIsService) {
        let arr = [];
        if (
          this.cellItems.service.request_spcs_info &&
          this.cellItems.service.request_spcs_info.length != 0
        ) {
          let requestArr = this.cellItems.service.request_spcs_info;
          requestArr.forEach((item) => {
            arr.push(item.id);
          });
        }
        let num = arr.indexOf(this.cellItems.spec_id);
        let specification = "";
        if (num != -1) {
          specification =
            "访问次数：" +
            this.cellItems.service.request_spcs_info[num].pv +
            "/日，" +
            "访问量：" +
            this.cellItems.service.request_spcs_info[num].count +
            "/日";
        }
        return specification;
      } else {
        if (this.cellItems.spec_id == 1) {
          return "部署";
        } else {
          return "开发";
        }
      }
    },
  },
  mounted() {},
  methods: {
    getState(val) {
      this.$emit("changeState", { state: val, index: this.cellIndex });
    },
    changeSpecification(val) {
      this.specificationPop = val;
    },
    changeSpecificationApplication(val) {
      this.specificationApplicationPop = val;
    },
    submitPop() {
      this.cellItems.spec_id = this.specificationPop;
      this.cellItems.duration_method = this.specificationApplicationPop;
      this.visible = false;
      this.changeCellItem();
      this.updateShoppingCart();
    },
    setSpecificationPop() {
      this.specificationPop = this.cellItems.spec_id;
      this.specificationApplicationPop = this.cellItems.duration_method;
    },
    changeSubscription(val) {
      this.changeCellItem();
      this.updateShoppingCart();
    },
    changeNum(val) {
      this.changeCellItem();
      this.updateShoppingCart();
    },
    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.cellItems.spec_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: 100px;
  margin: 0 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>