<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="任务ID">
          <a-input placeholder="请输入任务ID" v-decorator="['taskId', validatorRules.taskId ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="父ID">
          <a-input placeholder="请输入父ID" v-decorator="['parentId', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="名称">
          <a-input placeholder="请输入名称" v-decorator="['name', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="顺序">
          <a-input-number v-decorator="[ 'seq', validatorRules.seq ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="查找类型">
          <!--<a-input placeholder="请输入查找类型" v-decorator="['findType', validatorRules.findType ]" />-->
          <a-select v-decorator="['findType', validatorRules.findType ]">
            <a-select-option value="id">id</a-select-option>
            <a-select-option value="xpath">xpath</a-select-option>
            <a-select-option value="class">class</a-select-option>
            <a-select-option value="loop">loop</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="查找关键字">
          <a-input placeholder="请输入查找关键字" v-decorator="['findKey', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="应用包名">
          <a-input placeholder="请输入应用包名" v-decorator="['appPackage', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="app页面(activity)">
          <a-input placeholder="请输入app页面" v-decorator="['appActivity', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="点击类型">
          <!--<a-input placeholder="请输入点击类型" v-decorator="['clickType', {}]" />-->
          <a-select v-decorator="['clickType', {}]">
            <a-select-option value="no">no</a-select-option>
            <a-select-option value="point">point</a-select-option>
            <a-select-option value="element">element</a-select-option>
            <a-select-option value="text">text</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="偏移量X">
          <a-input-number v-decorator="[ 'offsiteX', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="偏移量Y">
          <a-input-number v-decorator="[ 'offsiteY', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="循环间隔">
          <a-input placeholder="请输入循环间隔" v-decorator="['loopRate', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="循环类型">
          <a-input placeholder="请输入循环类型" v-decorator="['loopType', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="是否必经执行">
          <a-input placeholder="请输入是否必经执行" v-decorator="['isMust', validatorRules.isMust ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="文本查找类型">
          <a-input placeholder="请输入文本查找类型" v-decorator="['textEqualType', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="文本查找关键字">
          <a-input placeholder="请输入文本查找关键字" v-decorator="['textEqualKey', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="滑动类型">
          <a-input placeholder="请输入滑动类型" v-decorator="['swapType', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="延迟时长">
          <a-input placeholder="请输入延迟时长" v-decorator="['waitTime', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="是否执行完关闭">
          <a-input placeholder="请输入是否执行完关闭" v-decorator="['isClose', {}]" />
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"

  export default {
    name: "AppiumTaskItemModal",
    data () {
      return {
        title:"操作",
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
        taskId:{rules: [{ required: true, message: '请输入任务ID!' }]},
        seq:{rules: [{ required: true, message: '请输入顺序!' }]},
        findType:{rules: [{ required: true, message: '请输入查找类型!' }]},
        isMust:{rules: [{ required: true, message: '请输入是否必经执行!' }]},
        },
        url: {
          add: "/appium/appiumTaskItem/add",
          edit: "/appium/appiumTaskItem/edit",
        },
      }
    },
    created () {
    },
    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,'taskId','parentId','name','seq','findType','findKey','appPackage','appActivity','clickType','offsiteX','offsiteY','loopRate','loopType','isMust','textEqualType','textEqualKey','swapType','waitTime','isClose'))
		  //时间格式化
        });

      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        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;
              that.close();
            })



          }
        })
      },
      handleCancel () {
        this.close()
      },


    }
  }
</script>

<style lang="less" scoped>

</style>