import * as API from "api/virtual/flow_pools";

export default {
  props: {
    show: {
      type: Boolean,
      default: false
    },
    data: {
      type: Object,
      default() {
        return null;
      }
    }
  },
  data() {
    return {
      listStyle: {
        width: "230px",
        height: "300px"
      },
      companies: [],
      packages: [],
      packageFilters: [],
      package_ids: [],
      reals: [],
      realFilters: [],
      real_pool_ids: [],
      my_show: false,
      isUpdate: false,
      loading: false,
      params: {
        name: "",
        carrier_operator: "",
        company_id: "",
        real_pool_ids: [],
        package_ids: [],
        status: 0,
        remark: "",
        start_at: this.moment()
          .subtract("1", "months")
          .format("YYYY-MM")
      }
    };
  },
  watch: {
    show(bool) {
      this.my_show = bool;
      if (bool) {
        if (this.data) {
          for (let k in this.data) {
            if (k in this.params) {
              this.params[k] = this.data[k];
            }
          }

          this.package_ids = this.data.package_ids;
          this.real_pool_ids = this.data.real_pool_ids;
        }

        this.initCompleteCompanies().then(companies => {
          this.companies = companies.filter(item => {
            return item.status === 0;
          });
        });

        if (!this.reals.length) {
          API.real().then(res => {
            if (res.code == 0) {
              this.reals = res.data.map(item => {
                return {
                  key: item.id,
                  label: item.sn + " - " + item.name,
                  disabled: false,
                  virtual_pool_id: item.virtual_pool_id,
                  carrier_operator: item.carrier_operator
                };
              });

              this.filterReals();
            }
          });
        } else {
          this.filterReals();
        }

        if (!this.packages.length) {
          API.packages().then(res => {
            if (res.code == 0) {
              let packages = res.data.map(item => {
                return {
                  key: item.id,
                  label: item.name + (item.type === 0 ? "(基础)" : "(续费包)"),
                  disabled: false,
                  company_ids: item.company_ids,
                  virtual_pool_id: item.virtual_pool_id,
                  carrier_operator: item.carrier_operator
                };
              });

              if (this.data) {
                for (
                  let index = 0;
                  index < this.data.packages.length;
                  index++
                ) {
                  const item = this.data.packages[index];

                  if (packages.findIndex(el => { return el.key === item.package_id; }) === -1) {
                    packages.push({
                      key: item.package_id,
                      label:
                        item.package_name +
                        (item.type === 0 ? "(基础)" : "(续费包)"),
                      disabled: false,
                      company_ids: [this.data.company_id],
                      virtual_pool_id: this.data.id,
                      carrier_operator: item.carrier_operator
                    });
                  }
                }
              }

              this.packages = packages;

              this.filterPackages();
            }
          });
        } else {
          this.filterPackages();
        }
      }
    }
  },
  methods: {
    filterReals() {
      this.realFilters = this.reals.filter(item => {
        if (
          this.params.carrier_operator !== "" &&
          this.params.carrier_operator !== undefined &&
          this.params.carrier_operator !== item.carrier_operator
        ) {
          return false;
        }

        if (item.virtual_pool_id === 0) {
          return true;
        }

        if (this.data && item.virtual_pool_id === this.data.id) {
          return true;
        }

        return false;
      });
    },
    filterPackages() {
      let packages = this.packages.filter(item => {
        if (
          this.params.carrier_operator !== "" &&
          this.params.carrier_operator !== undefined &&
          this.params.carrier_operator !== item.carrier_operator
        ) {
          return false;
        }

        if (item.company_ids.length === 0) {
          return true;
        }

        if (
          this.data &&
          item.company_ids.indexOf(this.data.company_id) !== -1
        ) {
          return true;
        }

        if (
          this.params.company_id !== "" &&
          this.params.company_id !== undefined &&
          item.company_ids.indexOf(this.params.company_id) !== -1
        ) {
          let index = this.params.package_ids.indexOf(item.id);

          if (index !== -1) {
            this.params.package_ids.splice(index, 1);
          }

          return false;
        }

        return true;
      });

      this.packageFilters = packages;
    },
    selectCO() {
      this.filterReals();
      this.filterPackages();
    },
    ok() {
      if (this.params.company_id === "") {
        this.$Message.info("请选择企业");
      }

      if (this.params.carrier_operator === "") {
        this.$Message.info("请选择运营商");
      }

      this.params.start_at = this.moment(this.params.start_at).format(
        "YYYY-MM"
      );

      if (this.data) {
        // 编辑
        API.update(this.params, this.data.id)
          .then(res => {
            this.loading = false;
            if (res.code == 0) {
              this.$emit("update-success");
              this.$Message.success("更新成功");
              this.clear();
            }
          })
          .catch(err => {
            this.loading = false;
          });
      } else {
        // 添加
        API.create(this.params)
          .then(res => {
            this.loading = false;
            if (res.code == 0) {
              this.$emit("add-success");
              this.$Message.success("添加成功");
              this.clear();
            }
          })
          .catch(err => {
            this.loading = false;
          });
      }
    },

    visibleChange(bool) {
      if (!bool) {
        this.$emit("update:show", false);
      }
    },
    clear() {
      for (let k in this.params) {
        if (k == "status") {
          this.params[k] = 0;
        } else if (k == "real_pool_ids" || k == "package_ids") {
          this.params[k] = [];
        } else if (k == "start_at") {
          this.params[k] = this.moment()
            .subtract("1", "months")
            .format("YYYY-MM");
        } else {
          this.params[k] = "";
        }
      }

      this.my_show = false;
      this.package_ids = [];
      this.real_pool_ids = [];
      this.packages = [];
    },
    transferPackages(ids) {
      if (ids.length) {
        this.packages.map(item => {
          if (item.key === ids[0]) {
            this.params.carrier_operator = item.carrier_operator;
          }
        });
      }

      if (this.package_ids.length > ids.length) {
        this.$Modal.confirm({
          title: "请谨慎操作！",
          content: "移除已选套餐，可能会引起已有数据的变化。",
          onOk: () => {
            this.package_ids = ids;
            this.params.package_ids = ids;
          }
        });
      } else {
        this.package_ids = ids;
        this.params.package_ids = ids;
      }

      this.filterReals();
      this.filterPackages();
    },
    transferRealFlowPools(ids) {
      if (ids.length) {
        this.reals.filter(item => {
          if (item.key === ids[0]) {
            this.params.carrier_operator = item.carrier_operator;
          }
        });
      }

      this.real_pool_ids = ids;
      this.params.real_pool_ids = ids;

      this.filterReals();
      this.filterPackages();
    }
  }
};
