<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-item label="站点" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-select placeholder="请选择站点" v-decorator="['siteId',validatorRules.siteId]" showSearch :filterOption="filterOption">
                <a-select-option v-for="option in siteOptions" :key="option.id" :value="option.id">{{option.name}}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="ip" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['ip',validatorRules.ip]" placeholder="请输入ip"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="黑白名单" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-select v-decorator="['blackOrWhite',validatorRules.blackOrWhite]" placeholder="请选择黑白名单">
                <a-select-option :value="0">黑名单</a-select-option>
                <a-select-option :value="1">白名单</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '/@/api/manage/manage'
  import pick from 'lodash.pick'

  import JFormContainer from '/@/components/Form/src/container/JFormContainer.vue';


  export default {
    name: 'AdwebSiteBlackIpForm',
    components: {
      JFormContainer,

    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        form: this.$form.createForm(this),
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          siteId: {
            rules: [
              { required: true, message: '请选择站点'}
            ]
          },
          ip: {
            rules: [
              { required: true, message: 'ip不能为空', trigger: 'blur' }
            ]
          },
          blackOrWhite: {
            rules: [
              { required: true, message: '请选择黑白名单'}
            ],
          }
        },
        url: {
          add: "/adweb/adwebSiteBlackIp/add",
          edit: "/adweb/adwebSiteBlackIp/edit",
          queryById: "/adweb/adwebSiteBlackIp/queryById"
        },
        siteOptions:[],
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
      this.getSiteList();
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'siteId','siteCode','status','ip','blackOrWhite','createTime'))
        })
      },
      //渲染流程表单数据
      showFlowData(){
        if(this.formBpm === true){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          });
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);

            for(let j = 0; j < that.siteOptions.length; j++){
              if(formData.siteId == that.siteOptions[j].id){
                formData.siteCode = that.siteOptions[j].code;
              }
            }
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'siteId','siteCode','status','ip','blackOrWhite','createTime'))
      },

      filterOption(input, option) {
        return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      },

      //获取站点列表
      getSiteList() {
        let that = this
        getAction('/sys/api/getSiteListByUid').then(function (res) {
          if (res.code == 0) {
            that.siteOptions = res.data;
          } else {
            that.$message.error('获取站点失败！')
          }
        }).catch(function (err) {
          console.log(err)
        })
      },
    }
  }
</script>
