<template>
  <el-dialog
    :title="openTitle"
    :visible.sync="visible"
    @close="handleClose"
    @open="open"
    v-dialogDrag
    custom-class="common-dialog dig-field"
    :close-on-click-modal="false"
    append-to-body
  >
    <el-form :model="paramForm" :rules="rules" :disabled="openType == 'view'" ref="formRef" label-position="left" label-width="100px" size="mini">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="模板名称" prop="templateName"><el-input v-model="paramForm.templateName" placeholder="请输入" /></el-form-item>
          <el-form-item label="模板分类" prop="templateType"><el-input v-model="paramForm.templateType" placeholder="请输入" /></el-form-item>
          <el-form-item label="模块名称" prop="moduleId">
            <common-select v-model="paramForm.moduleId" @selectChange="moduleChange" dict-type="CRM_Module_Code" ref="moduleRef"></common-select>
          </el-form-item>
          <el-form-item label="数据库表名称" prop="tableName"><el-input v-model="paramForm.tableName" placeholder="请输入" /></el-form-item>
          <el-form-item label="是否必填" prop="isRequired">
            <el-select v-model="paramForm.isRequired" placeholder="请选择" clearable>
              <el-option v-for="item in dataList" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="操作类型" prop="operationType">
            <el-select v-model="paramForm.operationType" multiple placeholder="请选择" clearable>
              <el-option v-for="item in operTypeList" :key="item.key" :label="item.key" :value="item.vaule"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="是否用户操作" prop="isUserOper">
            <el-select v-model="paramForm.isUserOper" placeholder="请选择" clearable>
              <el-option v-for="item in dataList" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="正则校验规则"><el-input v-model="paramForm.regular" placeholder="请输入" /></el-form-item>
          <el-form-item label="是否多选">
            <el-select v-model="paramForm.multiple" placeholder="请选择" clearable>
              <el-option v-for="item in dataList" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <!-- 第二列 -->
        <el-col :span="12">
          <el-form-item label="数据库字段名" prop="fieldId"><el-input v-model="paramForm.fieldId" placeholder="请输入数据库存储字段名称" /></el-form-item>
          <el-form-item label="字段名称" prop="fieldName"><el-input v-model="paramForm.fieldName" placeholder="请输入前端页面显示名称" /></el-form-item>
          <el-form-item label="字段属性" prop="fieldAttr"><el-input v-model="paramForm.fieldAttr" placeholder="请输入" /></el-form-item>
          <el-form-item label="排序" prop="sortBy"><el-input v-model="paramForm.sortBy" v-inputFilter:int placeholder="请输入" /></el-form-item>
          <el-form-item label="占据的列数" prop="span"><el-input v-model="paramForm.span" v-inputFilter:int @blur="spanBlur" placeholder="请输入(1~24列)" /></el-form-item>

        <el-form-item label="是否只读" prop="isReadOnly">
          <el-select v-model="paramForm.isReadOnly" placeholder="请选择" clearable>
            <el-option v-for="item in dataList" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否显示" prop="isShow">
          <el-select v-model="paramForm.isShow" placeholder="请选择" clearable>
            <el-option v-for="item in dataList" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
          <el-form-item label="字段最大长度"><el-input v-model="paramForm.fieldLength" v-inputFilter:int placeholder="请输入" /></el-form-item>
        </el-col>
      </el-row>

      <!-- 横线以下字段-->
      <el-row :gutter="20" style="border-top: solid #ababab 2px;padding-top: 15px;">
        <el-col :span="12">
          <el-form-item label="字段类型" prop="fieldType"><common-select v-model="paramForm.fieldType" dict-type="Crm_Field_Type"></common-select></el-form-item>

          <el-form-item label="字段来源" prop="fieldSource"><common-select v-model="paramForm.fieldSource" dict-type="Crm_Field_Source"></common-select></el-form-item>

          <el-form-item label="依赖查询参数" prop="parentField" :rules="{ required: false, message: '请输入', trigger: 'blur' }">
            <el-input v-model="paramForm.parentField" placeholder="请输入" type="textarea" :autosize="{ minRows: 1, maxRows: 2 }"></el-input>
          </el-form-item>
        </el-col>
        <!-- 第二列 -->
        <el-col :span="12">
          <el-form-item
            label="来源值"
            prop="sourceValue"
            :rules="{ required: paramForm.fieldSource == 'dictDate' || paramForm.fieldSource == 'interface', message: '请输入', trigger: 'blur' }"
          >
            <el-input v-model="paramForm.sourceValue" placeholder="根据字段来源带出或手动输入" />
          </el-form-item>
          <el-form-item
            label="来源标签"
            prop="sourceLabel"
            :rules="{ required: paramForm.fieldSource == 'dictDate' || paramForm.fieldSource == 'interface', message: '请输入', trigger: 'blur' }"
          >
            <el-input v-model="paramForm.sourceLabel" placeholder="根据字段来源带出或手动输入" />
          </el-form-item>

          <el-form-item
            label="来源参数"
            prop="sourceParams"
            :rules="{ required: paramForm.fieldSource == 'dictDate' || paramForm.fieldSource == 'interface', message: '请输入', trigger: 'blur' }"
          >
            <el-input v-model="paramForm.sourceParams" placeholder="请输入数字字典标识或接口url" type="textarea" :autosize="{ minRows: 1, maxRows: 2 }" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <div v-show="openType != 'view'" style="text-align: center">
      <el-button @click="submitForm" type="warning" size="mini">保存</el-button>
      <el-button @click="visible = false" type="info" plain size="mini">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'dig-field',
  mounted() {},
  methods: {
    moduleChange(obj) {
      this.paramForm.moduleName = obj.key;
    },
    spanBlur() {
      let span = Number(this.paramForm.span);
      if (span < 1 || span > 24) {
        this.paramForm.span = 6;
        this.$message('栅格列数介于1~24');
      }
    },
    submitForm() {
      this.$refs['formRef'].validate(valid => {
        if (!valid) {
          return;
        }

        let param = JSON.parse(JSON.stringify(this.paramForm));
        param.operationType = param.operationType.join(',');
        this.$ajax.post({
          domain: 'crmCmUrl',
          url: 'templates.saveOrUpdateTemplates',
          data: param,
          requestType: 'body',
          showLoad: true,
          success: function(res) {
            if (res.errorCode === 200) {
              this.$message.success('保存成功');
              this.$parent.doQuery();
              this.visible = false;
            }
          }.bind(this),
          error: function(res) {
            this.$message.error(res.body.msg || res.msg);
          }.bind(this)
        });
      });
    },
    open() {},
    //关闭弹窗
    handleClose() {
      this.$refs['formRef'].resetFields();
      Object.keys(this.paramForm).forEach(key => (this.paramForm[key] = ''));
      this.paramForm.span = 6;
      this.paramForm.sortBy = 0;
      this.paramForm.isRequired = 'Y';
      this.paramForm.isShow = 'Y';
      this.paramForm.status = 'Y';
      this.paramForm.operationType = [];
      this.visible = false;
    }
  },
  props: ['operTypeList'],
  data() {
    return {
      visible: false,
      openTitle: '',
      openType: '',

      paramForm: {
        moduleId: '',
        moduleName: '',
        templateName: '',
        fieldId: '', //页面显示名称为，数据库字段名, 对应的是数据存的字段
        fieldName: '', //页面模块字段显示的 label 标签名称
        tableName: '',
        parentField: '', //下拉框数据的依赖查询参数(数字字典，接口等)
        sortBy: 1,
        span: 6,
        fieldType: '',
        fieldAttr: '',
        fieldSource: '',
        fieldLength: '',
        sourceParams: '', //下拉框数据的字典标识或接口url
        sourceValue: '', // 存 下拉框 value 值对应的 java 对象 属性字段
        sourceLabel: '', // 存 下拉框 key 值对应的 java 对象 属性字段
        isRequired: 'Y',
        isShow: 'Y',
        status: 'Y',
        templateType: '',
        operationType: [],
        isReadOnly: '',
        isUserOper: '',
        regular: '',
        multiple: ''
      },
      rules: {
        moduleId: [{ required: true, message: '请选择', trigger: 'change' }],
        templateName: [{ required: true, message: '请输入', trigger: 'blur' }],
        fieldId: [{ required: true, message: '请输入', trigger: 'blur' }],
        fieldName: [{ required: true, message: '请输入', trigger: 'blur' }],
        fieldAttr: [{ required: true, message: '请输入', trigger: 'blur' }],
        tableName: [{ required: true, message: '请输入', trigger: 'blur' }],
        parentField: [{ required: true, message: '请输入', trigger: 'blur' }],
        sortBy: [{ required: true, message: '请输入', trigger: 'blur' }],
        span: [{ required: true, message: '请输入', trigger: 'blur' }],
        fieldType: [{ required: true, message: '请选择', trigger: 'change' }],
        fieldSource: [{ required: true, message: '请选择', trigger: 'change' }],
        isRequired: [{ required: true, message: '请选择', trigger: 'change' }],
        isShow: [{ required: true, message: '请选择', trigger: 'change' }],
        templateType: [{ required: true, message: '请选择', trigger: 'change' }],
        operationType: [{ required: true, message: '请选择', trigger: 'change' }],
        isReadOnly: [{ required: true, message: '请选择', trigger: 'change' }],
        isUserOper: [{ required: true, message: '请选择', trigger: 'change' }]
      },

      dataList: [
        {
          label: '是',
          value: 'Y'
        },
        {
          label: '否',
          value: 'N'
        }
      ]
    };
  }
};
</script>

<style type="text/css" lang="scss">
.dig-field {
  margin-top: 1vh !important;
}
</style>
