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,
      formData: {
        configs: [
          {
            configType: 1,
            name: "按“单日/周/月/年”配置：",
            list: [
              {
                name: "单日",
                checked: false,
                count: {
                  key: "dayCount",
                  checked: false,
                  value: 1,
                  min: 1,
                  max: 9999,
                  step: 1,
                  precision: 0
                },
                amount: {
                  key: "dayAmount",
                  checked: false,
                  value: 100,
                  min: 100,
                  max: 999999,
                  step: 0.01, // 最小间隔
                  precision: 2 // 精度-小数点后位数
                }
              },
              {
                name: "单周",
                checked: false,
                count: {
                  key: "weekCount",
                  checked: false,
                  value: 1,
                  min: 1,
                  max: 9999,
                  step: 1,
                  precision: 0
                },
                amount: {
                  key: "weekAmount",
                  checked: false,
                  value: 500,
                  min: 500,
                  max: 999999,
                  step: 0.01, // 最小间隔
                  precision: 2 // 精度-小数点后位数
                }
              },
              {
                name: "单月",
                checked: false,
                count: {
                  key: "monthCount",
                  checked: false,
                  value: 1,
                  min: 1,
                  max: 9999,
                  step: 1,
                  precision: 0
                },
                amount: {
                  key: "monthAmount",
                  checked: false,
                  value: 1000,
                  min: 1000,
                  max: 999999,
                  step: 0.01, // 最小间隔
                  precision: 2 // 精度-小数点后位数
                }
              },
              {
                name: "最近一年",
                checked: false,
                count: {
                  key: "yearCount",
                  checked: false,
                  value: 5,
                  min: 5,
                  max: 9999,
                  step: 1,
                  precision: 0
                },
                amount: {
                  key: "yearAmount",
                  checked: false,
                  value: 2000,
                  min: 2000,
                  max: 999999,
                  step: 0.01, // 最小间隔
                  precision: 2 // 精度-小数点后位数
                }
              }
            ]
          },

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

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

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

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

          {
            configType: 6,
            name: "按“订单金额”配置：",
            list: [
              {
                name: '单日',
                key: "dayAmount",
                checked: false,
                value: 100,
                min: 100,
                max: 999999,
                step: 0.01,
                precision: 2
              },
              {
                name: '单周',
                key: "weekAmount",
                checked: false,
                value: 500,
                min: 500,
                max: 999999,
                step: 0.01,
                precision: 2
              },
              {
                name: '单月',
                key: "monthAmount",
                checked: false,
                value: 1000,
                min: 1000,
                max: 999999,
                step: 0.01,
                precision: 2
              },
              {
                name: '最近一年',
                key: "yearAmount",
                checked: false,
                value: 2000,
                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 : "";
                configsItem[item1.amount.key] =
                  item1.checked && item1.amount.checked
                    ? item1.amount.value
                    : "";
                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
                  : "";
              configsItem[item.count.key] = item.count.checked
                ? item.count.value
                : "";
              configsItem[item.amount.key] = item.amount.checked
                ? item.amount.value
                : "";
              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
                : "";
              configsItem[item.orderCount.key] = item.checked
                ? item.orderCount.value
                : "";
              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 : '';
                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() {
      doFetchGet(url.getAbnormalSetting).then(res => {
        console.log("res--->",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);
      });
    },
    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];

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

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

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

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

          case 5:
          case 6:
            item.list.forEach((item1)=>{
              if(configsItem[item1.key]){
                item1.checked = true;
                item1.value = configsItem[item1.key];
                canSave = true;
              }
            })
            break;
          default:
            break;
        }
      });
      this.canSave = canSave;
      this.formData = formData;
    },
    submit(){
      doFetch(url.saveAbnormalSetting, this.submitData)
        .then(res => {
          let { errorCode, message } = res.data || {};
          if (errorCode != 0) {
            checkFalse(message);
            return;
          }else{
            this.$router.go(-1);
          }
        })
    },
    save() {
      if(this.isFirstSave){
        this.submit();
      }else{
        this.$confirm(`修改“异常规则”后，次日【异常会员】列表将全量更新，系统会根据最新的异常规则来判断“异常会员”并展示在列表中`, 
        "确定修改异常会员规则配置", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.submit();
        });
      }
    },
    cancel() {
      this.$router.go(-1);
    }
  }
};
