<template>
  <el-row>
    <p v-if="activeData.__vModel__ && activeData.__config__.viewDataType !== 'STRING'" style="color: #ff0000; font-size: 12px; margin-bottom: 18px">
      {{
        '提示：当前绑定控件字段 「 ' +
        activeData.__config__.label +
        ' 」 为 ' +
        activeData.__config__.viewDataType +
        ' 类型，使用单行文本可能会造成录入信息错误，如需使用单行文本请添加数据类型校验！！！'
      }}
    </p>
    <el-form-item label="控件标题">
      <el-input v-model="activeData.__config__.label" placeholder="请输入控件标题" />
    </el-form-item>
    <el-form-item label="占位提示">
      <el-input v-model="activeData.placeholder" placeholder="请输入占位提示" />
    </el-form-item>
    <template v-if="!activeData.__config__.isSubTable">
      <el-form-item label="控件栅格">
        <el-slider v-model="activeData.__config__.span" :max="24" :min="6" show-stops :step="2" show-tooltip />
      </el-form-item>
      <el-form-item label="标题宽度">
        <el-input-number v-model="activeData.__config__.labelWidth" placeholder="标题宽度" :min="0" :precision="0" controls-position="right" />
      </el-form-item>
    </template>
    <el-form-item label="控件宽度" v-if="activeData.__config__.isSubTable">
      <el-input-number v-model="activeData.__config__.columnWidth" placeholder="控件宽度" :min="0" :precision="0" controls-position="right" />
    </el-form-item>
    <el-form-item>
      <template slot="label">
        <span>自动填写</span>
        <el-tooltip style="cursor: pointer" effect="dark" :content="'设置指定规则，将结果自动填写到字段输入框中'" placement="top">
          <i class="el-icon-question" />
        </el-tooltip>
      </template>
      <el-select v-model="activeData.__config__.inputType" @change="activeData.defaultVarType = []" placeholder="请选择">
        <el-option label="指定数值" :value="1">
          <span>指定数值</span>
          <el-tooltip style="cursor: pointer" effect="dark" :content="'创建数据时将指定数值自动输入'" placement="top">
            <i class="el-icon-question" />
          </el-tooltip>
        </el-option>
        <el-option label="自定义表达式" :value="2">
          <span>自定义表达式</span>
          <el-tooltip style="cursor: pointer" effect="dark" :content="'通过函数和本表其他字段的运算结果自动填入，被引用的字段值改变时不会改变当前字段值'" placement="top">
            <i class="el-icon-question" />
          </el-tooltip>
        </el-option>
        <el-option label="页面变量" :value="4">
          <span>页面变量</span>
          <el-tooltip style="cursor: pointer" effect="dark" :content="'通过页面内定义变量值结果自动填入'" placement="top">
            <i class="el-icon-question" />
          </el-tooltip>
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="指定数值" v-if="activeData.__config__.inputType === 1">
      <el-input :value="setDefaultValue(activeData.__config__.defaultValue)" placeholder="请输入默认值" @input="onDefaultValueInput" />
    </el-form-item>
    <el-form-item label="自定义表达" v-if="activeData.__config__.inputType === 2">
      <customExpression :defaultValueExp="activeData.defaultValueExp" @onChangeExp="setDefaultValExp" :dbLinkId="dbLinkId" :drawList="drawList" :activeData="activeData" />
    </el-form-item>
    <el-form-item label="选择变量" v-if="activeData.__config__.inputType === 4">
      <el-cascader ref="varCascader" @change="varchange" v-model="activeData.defaultVarType" :options="varTypeList" @visible-change="allVarListChange"></el-cascader>
    </el-form-item>
    <el-form-item v-if="(activeData.__config__.inputType === 4 && cascaderTypeshow) || activeData.cascaderType">
      <template slot="label">
        <span>选择层级</span>
        <el-tooltip style="cursor: pointer" effect="dark" :content="'适用于级联组件全局变量，可配置选择层级'" placement="top">
          <i class="el-icon-question" />
        </el-tooltip>
      </template>
      <el-select v-model="activeData.cascaderType" placeholder="请选择">
        <el-option label="子节点值" value="son"></el-option>
        <el-option label="父节点值" value="topLeve"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="前缀">
      <el-input v-model="activeData.__slot__.prepend" placeholder="请输入前缀" />
    </el-form-item>
    <el-form-item label="后缀">
      <el-input v-model="activeData.__slot__.append" placeholder="请输入后缀" />
    </el-form-item>
    <el-form-item label="前图标">
      <el-input v-model="activeData['prefix-icon']" placeholder="请输入前图标名称">
        <el-button slot="append" @click="openIconsDialog('prefix-icon')"> 选择 </el-button>
      </el-input>
    </el-form-item>
    <el-form-item label="后图标">
      <el-input v-model="activeData['suffix-icon']" placeholder="请输入后图标名称">
        <el-button slot="append" @click="openIconsDialog('suffix-icon')"> 选择 </el-button>
      </el-input>
    </el-form-item>
    <el-form-item label="最多输入">
      <el-input v-model="activeData.maxlength" placeholder="请输入字符长度" type="number">
        <template slot="append">个字符</template>
      </el-input>
    </el-form-item>
    <!-- <el-form-item label="显示标签">
      <el-switch v-model="activeData.__config__.showLabel" />
    </el-form-item> -->
    <!-- <el-form-item label="输入统计">
      <el-switch v-model="activeData['show-word-limit']" />
    </el-form-item> -->
    <!-- <el-form-item label="能否清空">
      <el-switch v-model="activeData.clearable" />
    </el-form-item> -->
    <el-form-item>
      <template slot="label">
        <span>应用变量</span>
        <el-tooltip style="cursor: pointer" effect="dark" :content="'将当前组件应用为表单内全局变量'" placement="top">
          <i class="el-icon-question" />
        </el-tooltip>
      </template>
      <el-switch v-model="activeData.allVar" />
    </el-form-item>
    <el-form-item label="是否密码">
      <el-switch v-model="activeData['show-password']" />
    </el-form-item>
    <el-form-item label="是否只读">
      <el-switch v-model="activeData.readonly" />
    </el-form-item>
    <el-form-item label="是否禁用">
      <el-switch v-model="activeData.disabled" />
    </el-form-item>
    <el-form-item label="是否隐藏">
      <el-switch v-model="activeData.displayNone" />
    </el-form-item>
    <el-form-item label="是否URL">
      <el-switch v-model="activeData.isUrl" />
    </el-form-item>
    <el-form-item label="只做展示">
      <el-switch v-model="activeData.isText" @change="activeData.__vModel__ = 'isOnlyRead_' + +new Date();activeData.__vModelName__ = 'isOnlyRead_' + +new Date()" />
    </el-form-item>
    <el-divider>校验</el-divider>
    <el-form-item label="是否必填">
      <el-switch v-model="activeData.__config__.required" />
    </el-form-item>
    <div v-for="(item, index) in activeData.__config__.regList" :key="index" class="reg-item">
      <span class="close-btn" @click="activeData.__config__.regList.splice(index, 1)">
        <i class="el-icon-close" />
      </span>
      <el-form-item label="表达式">
        <el-input v-model="item.pattern" placeholder="请输入正则" />
      </el-form-item>
      <el-form-item label="错误提示" style="margin-bottom: 0">
        <el-input v-model="item.message" placeholder="请输入错误提示" />
      </el-form-item>
    </div>
    <div class="mt-10">
      <el-dropdown>
        <el-button type="primary">添加常用校验<i class="el-icon-arrow-down el-icon--right"></i> </el-button>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item @click.native="addHandle(item)" v-for="(item, i) in ruleList" :key="i"> {{ item.label }}</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
      <el-button type="primary" @click="addReg" style="margin-left: 10px"> 自定义规则 </el-button>
    </div>
    <iconBox :visible.sync="iconsVisible" :current="activeData[currentIconModel]" @choiceIcon="setIcon" />
  </el-row>
</template>
<script>
import iconBox from '@/components/JNPF-iconBox'
import customExpression from '@/components/Generator/index/RightComponents/customExpression'
import comMixin from './mixin'
import { loopActiveTabelList, loopRelationList, loopRelationTabelList } from '@/utils'
export default {
  props: ['activeData', 'dbLinkId', 'drawList'],
  mixins: [comMixin],
  components: { iconBox, customExpression },
  data() {
    return {
      iconsVisible: false,
      currentIconModel: null,
      ruleList: [
        {
          pattern: '/^\\d+$/',
          message: '请输入正确的数字',
          label: '数字'
        },
        {
          pattern: '/^[1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0$/',
          message: '请输入正确的金额',
          label: '金额'
        },
        {
          pattern: '/^0\\d{2,3}-?\\d{7,8}$/',
          message: '请输入正确的电话号码',
          label: '电话'
        },
        {
          pattern: '/^1[3456789]\\d{9}$/',
          message: '请输入正确的手机号码',
          label: '手机'
        },
        {
          pattern: '/^1[3456789]\\d{9}$|^0\\d{2,3}-?\\d{7,8}$/',
          message: '请输入正确的联系方式',
          label: '电话/手机'
        },
        {
          pattern: '/^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/',
          message: '请输入正确的邮箱',
          label: '邮箱'
        },
        {
          pattern: '/^[1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$/',
          message: '请输入正确的身份证号码',
          label: '身份证'
        },
        {
          pattern: '/^\\d{4}-\\d{1,2}-\\d{1,2}$/',
          message: '请输入格式为YYYY-MM-DD的日期',
          label: '年日日'
        },
        {
          pattern: '/^\\d{4}-\\d{1,2}-\\d{1,2} \\d{2}:\\d{2}:\\d{2}$/',
          message: '请输入格式为YYYY-MM-DD HH:mm:ss的日期',
          label: '年日日 时分秒'
        },
        {
          pattern: '/^\\d{4}-\\d{1,2}-\\d{1,2} \\d{2}:\\d{2}$/',
          message: '请输入格式为HH:mm:ss的时间',
          label: '时分秒'
        },
        {
          pattern: '/^\\d{2}:\\d{2}:\\d{2}$/',
          message: '请输入格式为HH:mm的时间',
          label: '时分'
        },
        {
          pattern: '/^\\d{2}:\\d{2}$/',
          message: '请输入格式为HH的时间',
          label: '时'
        }
      ],
      allVarList: [],
      apiVarList: [],
      varTypeList: [],
      cascaderTypeshow: false
    }
  },
  created() {
    this.allVarListChange(true)
  },
  methods: {
    varchange() {
      this.cascaderTypeshow = false
      this.activeData.cascaderType = ''
      let vardata = this.$refs['varCascader'].getCheckedNodes()[0].data
      if (vardata && vardata.cascaderType) {
        this.activeData.cascaderType = ''
        this.cascaderTypeshow = true
      }
    },
    getAllVarList() {
      //获取表单内变量
      let mapList = this.activeData.__config__.relationTable ? loopActiveTabelList(this.drawList, this.activeData.__config__.relationTable) : loopRelationList(this.drawList)
      this.allVarList = mapList
        .map((o) => {
          if (o.allVar) {
            return {
              label: o.__config__.label,
              value: o.__vModel__
            }
          }
        })
        .filter((l) => l !== undefined)
    },
    getAllapiVarList() {
      //获取API返回变量
      this.apiVarList = []
      let mapList = this.activeData.__config__.relationTable ? loopActiveTabelList(this.drawList, this.activeData.__config__.relationTable) : loopRelationList(this.drawList)
      mapList.forEach((item, key) => {
        let config = item.__config__
        if (config.ApiReturn && config.ApiReturn.length) {
          let list = []
          if (config.jnpfKey === 'cascader') {
            list = config.ApiReturn.map((o) => {
              return {
                label: o.varName,
                value: o.varName,
                cascaderType: true
              }
            }).filter((l) => l !== undefined)
          } else {
            list = config.ApiReturn.map((o) => {
              return {
                label: o.varName,
                value: o.varName
              }
            }).filter((l) => l !== undefined)
          }
          this.apiVarList = [...this.apiVarList, ...list]
        }
      })
    },
    //相同组件切换时 表单内全局变量数据不会及时更新  利用下拉框出现时刷新一下
    allVarListChange(val) {
      if (val) {
        this.getAllVarList()
        this.getAllapiVarList()
        this.varTypeList = [
          { value: 3, label: '表单全局变量', children: this.allVarList || [] },
          { value: 4, label: 'api全局变量', children: this.apiVarList || [] }
        ]
      }
    },
    openIconsDialog(model) {
      this.iconsVisible = true
      this.currentIconModel = model
    },
    setIcon(val) {
      this.activeData[this.currentIconModel] = val
    },
    addHandle(row) {
      this.activeData.__config__.regList.push({
        pattern: row.pattern,
        message: row.message
      })
    }
  }
}
</script>
