<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-radio-group v-model:value="model.day">
          <a-col :span="24">
            <a-form-model-item label="Region" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="region">
              <a-input v-model="model.region" placeholder="请输入Region"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="Suffix" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="suffix">
              <a-input v-model="model.suffix" placeholder="请输入Suffix"  ></a-input>
            </a-form-model-item>
          </a-col>
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="Last MON" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lastMon">-->
<!--              <a-input v-model="model.lastMon" placeholder="请输入last_mon"  ></a-input>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="Last_tue" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lastTue">-->
<!--              <a-input v-model="model.lastTue" placeholder="请输入last_tue"  ></a-input>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="Last WED" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lastWed">-->
<!--              <a-input v-model="model.lastWed" placeholder="请输入last_wed"  ></a-input>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="Last THU" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lastThu">-->
<!--              <a-input v-model="model.lastThu" placeholder="请输入last_thu"  ></a-input>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="Last FRI" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lastFri">-->
<!--              <a-input v-model="model.lastFri" placeholder="请输入last_fri"  ></a-input>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="Last SAT" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lastFri">-->
<!--              <a-input v-model="model.lastSat" placeholder="请输入last_fri"  ></a-input>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="Last SUN" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lastFri">-->
<!--              <a-input v-model="model.lastSun" placeholder="请输入last_sun"  ></a-input>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="MON" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="mon">-->
<!--              <a-input v-model="model.mon" placeholder="请输入mon"  ></a-input>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="TUE" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="tue">-->
<!--              <a-input v-model="model.tue" placeholder="请输入tue"  ></a-input>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="WED" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="tue">-->
<!--              <a-input v-model="model.wed" placeholder="请输入wed"  ></a-input>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="THU" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="thu">-->
<!--              <a-input v-model="model.thu" placeholder="请输入thu"  ></a-input>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="FRI" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="fri">-->
<!--              <a-input v-model="model.fri" placeholder="请输入fri"  ></a-input>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="SAT" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="fri">-->
<!--              <a-input v-model="model.sat" placeholder="请输入sat"  ></a-input>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="SUN" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="fri">-->
<!--              <a-input v-model="model.sun" placeholder="请输入sun"  ></a-input>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
<!--            <a-col :span="24">-->
<!--              <a-form-model-item label="Week" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="fri">-->
<!--                <a-input v-model="model.schedule" placeholder="请输入Schedule"  ></a-input>-->
<!--              </a-form-model-item>-->
<!--            </a-col>-->
          <a-col :span="24">
            <a-form-model-item label="Schedule" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="fri">
              <a-radio-group v-model:value="model.schedule" :change="handleChangeDay()">
<!--                <a-radio-button class="radioStyle" value="Last MON">Last MON</a-radio-button>-->
<!--                <a-radio-button class="radioStyle" value="Last TUE">Last TUE</a-radio-button>-->
<!--                <a-radio-button class="radioStyle" value="Last WED">Last WED</a-radio-button>-->
<!--                <a-radio-button class="radioStyle" value="Last THU">Last THU</a-radio-button>-->
<!--                <a-radio-button class="radioStyle" value="Last FRI">Last FRI</a-radio-button>-->
<!--                <a-radio-button class="radioStyle" value="Last SAT">Last SAT</a-radio-button>-->
<!--                <a-radio-button class="radioStyle" value="Last SUN">Last SUN</a-radio-button>-->
<!--                <a-radio-button class="radioStyle" value="MON">MON</a-radio-button>-->
<!--                <a-radio-button class="radioStyle" value="TUE">TUE</a-radio-button>-->
<!--                <a-radio-button class="radioStyle" value="WED">WED</a-radio-button>-->
<!--                <a-radio-button class="radioStyle" value="THU">THU</a-radio-button>-->
<!--                <a-radio-button class="radioStyle" value="FRI">FRI</a-radio-button>-->
<!--                <a-radio-button class="radioStyle" value="SAT">SAT</a-radio-button>-->
<!--                <a-radio-button class="radioStyle" value="SUN">SUN</a-radio-button>-->
                <a-radio class="radioStyle" value="Last MON">Last MON</a-radio>
                <a-radio class="radioStyle" value="Last TUE">Last TUE</a-radio>
                <a-radio class="radioStyle" value="Last WED">Last WED</a-radio>
                <a-radio class="radioStyle" value="Last THU">Last THU</a-radio>
                <a-radio class="radioStyle" value="Last FRI">Last FRI</a-radio>
                <a-radio class="radioStyle" value="Last SAT">Last SAT</a-radio>
                <a-radio class="radioStyle" value="Last SUN">Last SUN</a-radio>
                <a-radio class="radioStyle" value="MON">MON</a-radio>
                <a-radio class="radioStyle" value="TUE">TUE</a-radio>
                <a-radio class="radioStyle" value="WED">WED</a-radio>
                <a-radio class="radioStyle" value="THU">THU</a-radio>
                <a-radio class="radioStyle" value="FRI">FRI</a-radio>
                <a-radio class="radioStyle" value="SAT">SAT</a-radio>
                <a-radio class="radioStyle" value="SUN">SUN</a-radio>
              </a-radio-group>
            </a-form-model-item>
          </a-col>
          </a-radio-group>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'RegionRuleForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/regionRule/add",
          edit: "/regionRule/edit",
          queryById: "/regionRule/queryById"
        },
        dayEnum: {
          'MON': 'mon',
          'TUE': 'tue',
          'WED': 'wed',
          'THU': 'thu',
          'FRI': 'fri',
          'SAT': 'sat',
          'SUN': 'sun',
          'Last MON': 'lastMon',
          'Last TUE': 'lastTue',
          'Last WED': 'lastWed',
          'Last THU': 'lastThu',
          'Last FRI': 'lastFri',
          'Last SAT': 'lastSat',
          'Last SUN': 'lastSun'
        }
      }
    },
    watch: {
      'model.schedule': function (newVal) {
        Object.keys(this.model).forEach(key => {
          if (this.model[key] === "√") {
            this.model[key] = null;
          }
        })
        let day = this.dayEnum[newVal];
        this.model[day] = "√";
        console.log(this.model);
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
      handleChangeDay() {
        let that = this;
        // let dayModel = JSON.parse(JSON.stringify(this.model));
        // Object.keys(dayModel).forEach(key =>{
        //   if(dayModel[key] === "√") {
        //     dayModel[key] = null;
        //   }
        //   if(key == that.dayEnum[dayModel.schedule]) {
        //     dayModel[key] = "√";
        //   }
        // })
        // console.log(dayModel);
        // return dayModel;
        // let day = this.dayEnum[this.model.schedule];
        // let defaultDayEnum = JSON.parse(JSON.stringify(this.dayEnum));
        // let schedule = this.model.schedule;
        // let day = defaultDayEnum[schedule];
        // console.log(day);
        // Object.keys(this.model).forEach(key =>{
        //   if(this.model[key] === "√") {
        //     this.model[key] = null;
        //   }
        //   if(key == day) {
        //     this.model[key] = "√";
        //   }
        // })
        // this.model.fri = "√"
        // console.log(this.model);
        // return this.formModel();
      },
      toCamelCase(str) {
        let words = str.toLowerCase().split('_')
        for (let i = 1; i < words.length; i++) {
          words[i] = words[i][0].toUpperCase() + words[i].substring(1)
        }
        return words.join('')
      }

  }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';

  .radioStyle{
    width: 100px;
    margin: 5px 0;
  }
</style>