<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="https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg"
            class="shopping_cell_img"
          />
          <div class="shopping_cell_msg">
            <div class="shopping_cell_name">
              {{ cellItems.name }}
              <span
                v-if="cellItems.mapService"
                class="tags map_service"
              >mapService</span>
              <span v-if="cellItems.state == 1" class="tags shared">共享</span>
              <span v-else-if="cellItems.state ==  2" class="tags restricted">受限</span>
              <span v-else class="tags sensitive">敏感</span>
            </div>
            <div class="shopping_cell_msg_other">
              <div class="shopping_cell_type">{{ cellItems.type }}</div>
              <div class="shopping_cell_creator">{{ cellItems.creator }}</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"
            >规格：{{ specificationBtns[cellItems.specification] }}</div>
            <div
              class="shopping_cell_specification_val_type"
            >申请方式：{{ specificationApplicationBtns[cellItems.specificationApplication] }}</div>
            <div class="shopping_cell_specification_edit">
              <el-popover
                placement="right-start"
                width="280"
                v-model="visible"
                @after-leave="setSpecificationPop"
              >
                <div>
                  <div class="shopping_cell_specification_title">规格：</div>
                  <div>
                    <el-button
                      v-for="(item, index) in specificationBtns"
                      :key="'specification' + index"
                      :class="specificationPop == index ? 'shopping_cell_specification_btn active' : 'shopping_cell_specification_btn'"
                      size="mini"
                      @click="changeSpecification(index)"
                    >{{ index + 1 + ". " + item }}</el-button>
                  </div>
                  <div class="shopping_cell_specification_title">申请方式：</div>
                  <div>
                    <el-button
                      v-for="(item, index) in specificationApplicationBtns"
                      :key="'specification' + index"
                      :class="specificationApplicationPop == index ? 'shopping_cell_specification_btn active' : 'shopping_cell_specification_btn'"
                      size="mini"
                      @click="changeSpecificationApplication(index)"
                    >{{ item }}购买</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">
        <el-input-number
          :disabled="readOnly"
          v-model="cellItems.num"
          @change="changeNum"
          size="mini"
          :min="1"
        ></el-input-number>
      </el-col>
      <el-col :span="4" class="shopping_cell_options">
        <el-checkbox
          v-if="!readOnly"
          v-model="cellItems.checkedSubscription"
          @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
    }
  },
  watch: {
    cellCheck: function(val) {
      this.checkedItem = val;
    },
    cellItem: {
      immediate: true, // 这句重要
      handler(val) {
        this.cellItems = val;
      }
    }
  },
  data: () => ({
    cellItems: {},
    checkedItem: false,
    specificationEdit: require("../../assets/imgs/ic_edit.png"),
    checkedSubscription: false,
    visible: false,
    specificationPop: 1,
    specificationApplicationPop: 0,
    specificationBtns: [
      "访问次数：20/日  访问量：100/日",
      "访问次数：200/日  访问量：1000/日",
      "访问次数：无上限  访问量：无上限"
    ],
    specificationApplicationBtns: ["按月", "按年"]
  }),
  computed: {},
  methods: {
    getState(val) {
      this.$emit("changeState", { state: val, index: this.cellIndex });
    },
    changeSpecification(val) {
      this.specificationPop = val;
    },
    changeSpecificationApplication(val) {
      this.specificationApplicationPop = val;
    },
    submitPop() {
      this.cellItems.specification = this.specificationPop;
      this.cellItems.specificationApplication = this.specificationApplicationPop;
      this.visible = false;
      this.changeCellItem();
    },
    setSpecificationPop() {
      this.specificationPop = this.cellItems.specification;
      this.specificationApplicationPop = this.cellItems.specificationApplication;
    },
    changeSubscription(val) {
      this.changeCellItem();
    },
    changeNum(val) {
      this.changeCellItem();
    },
    changeCellItem() {
      this.$emit("changeCellMsg", {
        index: this.cellIndex,
        data: this.cellItems
      });
    },
    delCellItem() {
      this.$emit("deleteItem", {
        index: this.cellIndex,
        data: this.cellItems
      });
    }
  },
  mounted() {}
};
</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;
}
.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;
}
</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>