<script lang="ts">
import log from './functionDialog/log.vue';
import request from './functionDialog/request.vue';
import setPageData from './functionDialog/setPageData.vue';
import getPageData from './functionDialog/getPageData.vue';

export default {
  name: 'function-type',
  components: {
    log,
    request,
    setPageData,
    getPageData
  }
};
</script>
<template>
  <el-form-item>
    <template #label>
      <div style="line-height: 32px">
        <el-text>{{ itemName }}</el-text>
        <el-tooltip placement="top">
          <template #content>
            预定义参数：<br>
            formData:表单数据<br>
            request:网络请求<br>
            localData:组件数据<br>
            formDataOperation:页面数据<br>
            包含get,set两个方法<br>
          </template>
          <el-icon style="margin-left: 4px">
            <Warning />
          </el-icon>
        </el-tooltip>
      </div>
    </template>
    <el-button @click="dialogVisible = true">编辑方法</el-button>
    <el-button @click="addDialogVisible = true">添加</el-button>
  </el-form-item>
  <el-dialog v-model="dialogVisible" title="编辑方法" width="500">
    <div class="code">(formData, request, formDataOperation: {get, set}, localData)=>{</div>
    <el-input v-model="formData[keyValue]" type="textarea" :rows="16" />
    <div class="code">}</div>
    <template #footer>
      <div>
        <el-button @click="dialogVisible = false">关闭</el-button>
      </div>
    </template>
  </el-dialog>

  <el-dialog
    v-model="addDialogVisible"
    title="选择方法"
    width="500"
  >
    <el-button style="margin-bottom: 12px;margin-left: 12px" type="primary" @click="addFunction('log')">打印变量
    </el-button>
    <el-button style="margin-bottom: 12px;" type="primary" @click="addFunction('request')">网络请求</el-button>
    <el-button style="margin-bottom: 12px;" type="primary" @click="addFunction('setPageData')">设置页面变量</el-button>
    <el-button style="margin-bottom: 12px;" type="primary" @click="addFunction('getPageData')">读取页面变量</el-button>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="addDialogVisible = false">关闭</el-button>
      </div>
    </template>
  </el-dialog>

  <el-dialog
    v-model="addFunctionDialogVisible"
    title="添加方法"
    width="500"
  >
    <component :is="addFunctionType" ref="addFunctionRef" v-if="addFunctionDialogVisible" />
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="addFunctionSubmit" type="primary">确定</el-button>
        <el-button @click="addFunctionDialogVisible = false">关闭</el-button>
      </div>
    </template>
  </el-dialog>

</template>
<script lang="ts" setup>

const props = defineProps({
  formData: String,
  keyValue: String,
  itemName: String
});

const { formData, keyValue, itemName } = toRefs(props);
const dialogVisible = ref(false);
const addDialogVisible = ref(false);
const addFunctionDialogVisible = ref(false);
const addFunctionType = ref('');
const addFunctionRef = ref();


const addFunction = (e) => {
  addFunctionType.value = e;
  addFunctionDialogVisible.value = true;
};

const addFunctionSubmit = () => {
  const value = addFunctionRef.value.onSubmit() || '';
  formData.value[keyValue.value] += value;
  addDialogVisible.value = false;
  addFunctionDialogVisible.value = false;
};

</script>
<style scoped lang="less">
.code {
  width: 100%;
  height: 24px;
  line-height: 24px;
  font-size: 14px;
  padding-left: 8px;
  background-color: #0001;
  user-select: text;
}
</style>



