import { doFetch, doFetchGet } from "@/components/axios/api";
import url from "@/components/axios/url";
import { checkFalse } from "../../../../../static/js/checkStatus";

import AbnormalNumberInput from "./components/number-input.vue";

export default {
  name: "AbnormalSetting",
  components: {
    AbnormalNumberInput
  },
  mixins: [],
  filters: {},
  data() {
    return {
      canSave: false,
      load: false,
      isSave: false,
      formData: {
        configs: [
          {
            configType: 1,
            name: "按“单日/周/月/年”配置：",
            list: [
              {
                name: "单日",
                checked: false,
                count: {
                  key: "dayCount",
                  checked: false,
                  value: undefined,
                  min: 1,
                  max: 9999,
                  step: 1,
                  precision: 0
                },
                amount: {
                  key: "dayAmount",
                  checked: false,
                  value: undefined,
                  min: 100,
                  max: 999999,
                  step: 0.01, // 最小间隔
                  precision: 2 // 精度-小数点后位数
                }
              },
              {
                name: "单周",
                checked: false,
                count: {
                  key: "weekCount",
                  checked: false,
                  value: undefined,
                  min: 1,
                  max: 9999,
                  step: 1,
                  precision: 0
                },
                amount: {
                  key: "weekAmount",
                  checked: false,
                  value: undefined,
                  min: 500,
                  max: 999999,
                  step: 0.01, // 最小间隔
                  precision: 2 // 精度-小数点后位数
                }
              },
              {
                name: "单月",
                checked: false,
                count: {
                  key: "monthCount",
                  checked: false,
                  value: undefined,
                  min: 1,
                  max: 9999,
                  step: 1,
                  precision: 0
                },
                amount: {
                  key: "monthAmount",
                  checked: false,
                  value: undefined,
                  min: 1000,
                  max: 999999,
                  step: 0.01, // 最小间隔
                  precision: 2 // 精度-小数点后位数
                }
              },
              {
                name: "最近一年",
                checked: false,
                count: {
                  key: "yearCount",
                  checked: false,
                  value: undefined,
                  min: 5,
                  max: 9999,
                  step: 1,
                  precision: 0
                },
                amount: {
                  key: "yearAmount",
                  checked: false,
                  value: undefined,
                  min: 2000,
                  max: 999999,
                  step: 0.01, // 最小间隔
                  precision: 2 // 精度-小数点后位数
                }
              }
            ]
          },

          {
            configType: 2,
            name: "按“周连续”消费配置：",
            setting: {
              key: "continueTime",
              value: undefined,
              min: 2,
              max: 52,
              step: 1,
              precision: 0
            },
            count: {
              key: "weekCount",
              checked: false,
              value: undefined,
              min: 1,
              max: 9999,
              step: 1,
              precision: 0
            },
            amount: {
              key: "weekAmount",
              checked: false,
              value: undefined,
              min: 500,
              max: 999999,
              step: 0.01, // 最小间隔
              precision: 2 // 精度-小数点后位数
            }
          },

          {
            configType: 3,
            name: "按“月连续”消费配置：",
            setting: {
              key: "continueTime",
              value: undefined,
              min: 2,
              max: 12,
              step: 1,
              precision: 0
            },
            count: {
              key: "monthCount",
              checked: false,
              value: undefined,
              min: 1,
              max: 9999,
              step: 1,
              precision: 0
            },
            amount: {
              key: "monthAmount",
              checked: false,
              value: undefined,
              min: 1000,
              max: 999999,
              step: 0.01, // 最小间隔
              precision: 2 // 精度-小数点后位数
            }
          },

          {
            configType: 4,
            name: "按“商品购买情况”配置：",
            checked: false,
            goodsCount: {
              key: "skuNumber",
              value: undefined,
              min: 2,
              max: 9999,
              step: 1,
              precision: 0
            },
            orderCount: {
              key: "orderCount",
              value: undefined,
              min: 1,
              max: 9999,
              step: 1,
              precision: 0
            }
          },

          {
            configType: 5,
            name: "按“有效消费次数”配置：",
            list: [
              {
                name: "单日",
                key: "dayCount",
                checked: false,
                value: undefined,
                min: 1,
                max: 9999,
                step: 1,
                precision: 0
              },
              {
                name: "单周",
                key: "weekCount",
                checked: false,
                value: undefined,
                min: 1,
                max: 9999,
                step: 1,
                precision: 0
              },
              {
                name: "单月",
                key: "monthCount",
                checked: false,
                value: undefined,
                min: 1,
                max: 9999,
                step: 1,
                precision: 0
              },
              {
                name: "最近一年",
                key: "yearCount",
                checked: false,
                value: undefined,
                min: 5,
                max: 9999,
                step: 1,
                precision: 0
              }
            ]
          },

          {
            configType: 6,
            name: "按“订单金额”配置：",
            list: [
              {
                name: "单日",
                key: "dayAmount",
                checked: false,
                value: undefined,
                min: 100,
                max: 999999,
                step: 0.01,
                precision: 2
              },
              {
                name: "单周",
                key: "weekAmount",
                checked: false,
                value: undefined,
                min: 500,
                max: 999999,
                step: 0.01,
                precision: 2
              },
              {
                name: "单月",
                key: "monthAmount",
                checked: false,
                value: undefined,
                min: 1000,
                max: 999999,
                step: 0.01,
                precision: 2
              },
              {
                name: "最近一年",
                key: "yearAmount",
                checked: false,
                value: undefined,
                min: 2000,
                max: 999999,
                step: 0.01,
                precision: 2
              }
            ]
          }
        ]
      },
      isFirstSave: true,
      submitData: {
        frozen: 2,
        configs: []
      }
    };
  },
  created() {
    this.init();
  },
  beforeMount() {},
  watch: {
    formData: {
      immediate: true,
      deep: true,
      handler(formData) {
        let { configs } = formData;
        let submitConfigs = [];
        let canSave = false;
        configs.forEach(item => {
          let configsItem = {
            configType: item.configType
          };
          switch (+item.configType) {
            case 1:
              item.list.forEach(item1 => {
                configsItem[item1.count.key] =
                  item1.checked && item1.count.checked ? item1.count.value : undefined;
                configsItem[item1.amount.key] =
                  item1.checked && item1.amount.checked
                    ? item1.amount.value
                    : undefined;
                if (
                  configsItem[item1.count.key] ||
                  configsItem[item1.amount.key]
                ) {
                  canSave = true;
                }
              });
              break;

            case 2:
            case 3:
              configsItem[item.setting.key] =
                item.count.checked || item.amount.checked
                  ? item.setting.value
                  : undefined;
              configsItem[item.count.key] = item.count.checked
                ? item.count.value
                : undefined;
              configsItem[item.amount.key] = item.amount.checked
                ? item.amount.value
                : undefined;
              if (
                configsItem[item.setting.key] &&
                (configsItem[item.count.key] || configsItem[item.amount.key])
              ) {
                canSave = true;
              }
              break;

            case 4:
              configsItem[item.goodsCount.key] = item.checked
                ? item.goodsCount.value
                : undefined;
              configsItem[item.orderCount.key] = item.checked
                ? item.orderCount.value
                : undefined;
              if (
                configsItem[item.goodsCount.key] ||
                configsItem[item.orderCount.key]
              ) {
                canSave = true;
              }
              break;

            case 5:
            case 6:
              item.list.forEach(item1 => {
                configsItem[item1.key] = item1.checked ? item1.value : undefined;
                if (configsItem[item1.key]) {
                  canSave = true;
                }
              });
              break;

            default:
              break;
          }
          submitConfigs.push(configsItem);
        });
        this.submitData.configs = submitConfigs;
        this.canSave = canSave;
      }
    }
  },
  methods: {
    change(value) {
      setTimeout(() => {
        this.$refs["submitForm"].validate(valid => {
          if (valid) {
          } else {
            return false;
          }
        });
      }, 200);
    },
    init() {
      this.load = true;
      doFetchGet(url.getAbnormalSetting).then(res => {
        let { errorCode, result, message } = res.data || {};
        if (errorCode != 0) {
          checkFalse(message);
          return;
        }
        if (result) {
          this.isFirstSave = false;
        } else {
          this.isFirstSave = true;
        }
        this.updateData(result);
      }).finally(()=> this.load = false);
    },
    updateData(result) {
      let { frozen, configs } = result;
      this.submitData.frozen = frozen || 2;
      let formData = this.formData;
      let canSave = false;
      formData.configs.forEach(item => {
        let configsItem = (configs || []).find(item1 => {
          return item.configType == item1.configType;
        });
        if (!configsItem) {
          return;
        }
        switch (+item.configType) {
          case 1:
            item.list.forEach(item1 => {
              if (configsItem[item1.count.key]) {
                item1.checked = true;
                item1.count.checked = true;
                item1.count.value = configsItem[item1.count.key] || undefined;

                canSave = true;
              }
              if (configsItem[item1.amount.key]) {
                item1.checked = true;
                item1.amount.checked = true;
                item1.amount.value = configsItem[item1.amount.key] || undefined;
                canSave = true;
              }
            });
            break;

          case 2:
          case 3:
            item.setting.value = configsItem[item.setting.key] || undefined;
            if (configsItem[item.count.key]) {
              item.count.checked = true;
              item.count.value = configsItem[item.count.key] || undefined;
              canSave = true;
            }

            if (configsItem[item.amount.key]) {
              item.amount.checked = true;
              item.amount.value = configsItem[item.amount.key] || undefined;
              canSave = true;
            }
            break;

          case 4:
            if (
              configsItem[item.goodsCount.key] &&
              configsItem[item.orderCount.key]
            ) {
              item.checked = true;
              item.goodsCount.value = configsItem[item.goodsCount.key] || undefined;
              item.orderCount.value = configsItem[item.orderCount.key] || undefined;
              canSave = true;
            }
            break;

          case 5:
          case 6:
            item.list.forEach(item1 => {
              if (configsItem[item1.key]) {
                item1.checked = true;
                item1.value = configsItem[item1.key] || undefined;
                canSave = true;
              }
            });
            break;
          default:
            break;
        }
      });
      this.canSave = canSave;
      this.formData = formData;
    },
    submit() {
      this.isSave = true;
      doFetch(url.saveAbnormalSetting, this.submitData).then(res => {
        let { errorCode, message } = res.data || {};
        if (errorCode != 0) {
          checkFalse(message);
          return;
        } else {
          this.$router.go(-1);
        }
      }).finally(()=> this.isSave = false);;
    },
    save() {
      this.$refs.submitForm.validate(valid => {
        if (valid) {
          if (this.isFirstSave) {
            this.submit();
          } else {
            this.$confirm(
              `确定修改规则配置后，次日异常会员列表会全量更新（只调整“是否自动冻结”，不算调整异常判定规则，不会全量更新）`,
              "确定修改异常会员规则配置",
              {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
              }
            ).then(() => {
              this.submit();
            });
          }
        } else {
          return false;
        }
      });
    },
    cancel() {
      this.$router.go(-1);
    }
  }
};
