<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',formRules.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="关键词" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['word',formRules.word]" placeholder="请输入关键词"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="黑白名单" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-select v-decorator="['blackOrWhite',formRules.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 :span="24">-->
<!--            <a-form-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol">-->
<!--              <a-select v-decorator="['isEnable',formRules.isEnable]" placeholder="请选择是否启用该关键词">-->
<!--                <a-select-option :value="1">启用</a-select-option>-->
<!--                <a-select-option :value="0">停用</a-select-option>-->
<!--              </a-select>-->
<!--            </a-form-item>-->
<!--          </a-col>-->
<!--          <a-col :span="24">-->
<!--            <a-form-item label="作用域" :labelCol="labelCol" :wrapperCol="wrapperCol">-->
<!--              <a-select v-decorator="['useStatus',formRules.useStatus]" placeholder="请选择作用域">-->
<!--                <a-select-option :value="0">询盘关键词</a-select-option>-->
<!--                <a-select-option :value="1">OpenAi关键词</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'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'
  import JDate from '@/components/jeecg/JDate'

  export default {
    name: 'AdwebEnquirySiteRuleForm',
    components: {
      JFormContainer,
      JDate,
    },
    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: {
        },
        url: {
          add: "/adweb/adwebEnquirySiteRule/add",
          edit: "/adweb/adwebEnquirySiteRule/edit",
          queryById: "/adweb/adwebEnquirySiteRule/queryById"
        },
        formRules: {
          siteId: {
            rules: [
              { required: true, message: '请选择站点'},
            ],
            trigger: 'change'
          },
          word: {
            rules: [
              { required: true, message: '请输入关键词'},
              { max: 100, message: '关键词长度不能超过100'},
            ],
            trigger: 'blur'
          },
          blackOrWhite: {
            initialValue: 0,
            rules: [
              { required: true, message: '请选择黑白名单'},
            ],
          },
        },
        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','word','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);
            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','word','blackOrWhiteList','isEnable','useStatus','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>