<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" slot="detail" :rules="validatorRules" :hideRequiredMark="true">
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-model-item label="租户名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-model="model.name" placeholder="请输入租户名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="租户编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="id">
              <!-- <a-input-number style="width: 100%" :min="1" v-model="model.id" placeholder="请输入租户编号" :disabled="disabledId"></a-input-number> -->
              <a-input style="width: 100%" v-model="model.id" placeholder="请输入租户编号" :disabled="true"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="开始时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择开始时间" v-model="model.beginDate" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss"
                style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="结束时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择结束时间" v-model="model.endDate" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss"
                style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-radio-group name="tenantStatus" v-model="model.status">
                <a-radio :value="1">正常</a-radio>
                <a-radio :value="0">冻结</a-radio>
              </a-radio-group>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="消息推送" prop="isSend">
              <j-dict-select-tag v-model="model.isSend" type="radio" dictCode="yn" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="租户类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type">
              <j-dict-select-tag v-model="model.type" placeholder="请选择租户类型" dictCode="tenant_type" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="跳转链接" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="url">
              <a-input style="width: 100%" :min="1" v-model="model.url" placeholder="请输入跳转链接" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="携带参数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="params">
              <a-input style="width: 100%" :min="1" v-model="model.params" placeholder="请输入携带参数" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="请求APP_ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="clientId">
              <!-- <a-input-number style="width: 100%" :min="1" v-model="model.appId" placeholder="请输入携带参数"></a-input-number> -->
              <a-input style="width: 100%" v-model="model.clientId" placeholder="请输入携带参数" :disabled="true"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="请求秘钥" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="clientSecret">
              <a-input style="width: 100%" v-model="model.clientSecret" placeholder="请输入请求秘钥"
                :disabled="isAddOrEdit == 'add'" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="消息接收地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="msgUrl">
              <a-input style="width: 100%" v-model="model.msgUrl" placeholder="请输入消息接收地址" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="消息通知模式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="msgType">
              <j-dict-select-tag v-model="model.msgType" placeholder="请选择消息通知模式" dictCode="tenant_msg_type" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="子系统备用接口" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="queryUrl">
              <a-input style="width: 100%" v-model="model.queryUrl" placeholder="请输入子系统备用接口" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="租户权限" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="tenantPermission">
              <j-dict-select-tag v-model="model.msgType" placeholder="请选择租户权限" dictCode="tenant_permisson" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="系统名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="systemName">
              <a-input style="width: 100%" v-model="model.systemName" placeholder="请输入系统名称" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-row>
              <a-col :span="12">
                <a-form-model-item label="顺序" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sortOrder">
                  <a-input-number :min="0" :max="99999" v-model="model.sortOrder" placeholder="顺序"> </a-input-number>
                </a-form-model-item>
              </a-col>
              <a-col :span="12">
                <a-form-model-item v-show="show" :labelCol="labelCol" :wrapperCol="wrapperCol" label="菜单图标">
                  <a-input placeholder="点击选择图标" v-model="model.icon" :readOnly="disableSubmit">
                    <a-icon slot="addonAfter" type="setting" @click="selectIcons" />
                  </a-input>
                </a-form-model-item>
              </a-col>
            </a-row>
          </a-col>
          <a-col :span="24" class="aColUn">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea style="width: 100%" v-model="model.remark" placeholder="请输入备注" />
            </a-form-model-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-model>
      <!-- 选择图标 -->
      <icons @choose="handleIconChoose" @close="handleIconCancel" :iconChooseVisible="iconChooseVisible"></icons>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import JFormContainer from '@/components/jeecg/JFormContainer'
import JDate from '@/components/jeecg/JDate'
import JDictSelectTag from '@/components/dict/JDictSelectTag'
import { createDateStr, randomStr } from '@/config/common'
import Icons from '@/views/system/modules/icon/Icons'
export default {
  name: 'TenantForm',
  components: {
    JFormContainer,
    JDate,
    JDictSelectTag,
    Icons
  },
  props: {
    formData: {
      type: Object,
      default: () => { },
      required: false,
    },
    normal: {
      type: Boolean,
      default: false,
      required: false,
    },
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data () {
    return {
      model: { status: 1 },
      id: '',
      iconChooseVisible: false,
      disableSubmit: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        id: [{ required: true, message: '请输入租户编号!' }],
        clientId: [{ required: true, message: '请输入APP_ID!' }],
        clientSecret: [{ required: true, validator: this.clientSecretValidator }],
        isSend: [{ required: true, message: '请选择是否消息推送' }],
        sortOrder: [
          {
            required: true,
            message: '请输入顺序',
          },
        ],
      },
      url: {
        add: '/sys/tenant/add',
        edit: '/sys/tenant/edit',
        queryById: '/sys/tenant/queryById',
      },
      // 判断是新增还是编辑,add表示新增，edit表示编辑
      isAddOrEdit: 'add',
    }
  },
  computed: {
    formDisabled () {
      if (this.normal === false) {
        if (this.formData.disabled === false) {
          return false
        } else {
          return true
        }
      }
      return this.disabled
    },
    disabledId () {
      return this.id ? true : false
    },
    showFlowSubmitButton () {
      if (this.normal === false) {
        if (this.formData.disabled === false) {
          return true
        } else {
          return false
        }
      } else {
        return false
      }
    },
  },
  created () {
    this.showFlowData()
  },
  methods: {
    show (record) {
      this.model = record ? Object.assign({}, record) : this.model
      this.id = record ? record.id : ''
      this.visible = true
    },
    add (record) {
      this.model = Object.assign({}, record)
      this.visible = true
      // this.model.id = new Date().getTime();
      this.model.id = createDateStr()
      this.model.clientId = randomStr(18) + createDateStr()
      this.model.clientSecret = randomStr(36) + randomStr(14) + createDateStr()
      this.model.isSend = '0'
      this.isAddOrEdit = 'add'
    },
    edit (record) {
      this.model = record ? Object.assign({}, record) : this.model
      this.id = record ? record.id : ''
      this.visible = true
      this.isAddOrEdit = 'edit'
    },
    showFlowData () {
      if (this.normal === false) {
        let params = { id: this.formData.dataId }
        getAction(this.url.queryById, params).then((res) => {
          if (res.success) {
            this.edit(res.result)
          }
        })
      }
    },
    submitForm () {
      const that = this
      // 触发表单验证
      that.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                if ('该编号已存在!' == res.message) {
                  this.model.id = ''
                }
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        } else {
          return false
        }
      })
    },
    popupCallback (row) {
      this.model = Object.assign(this.model, row)
    },
    clientSecretValidator (rule, value, callback) {
      if (!value) {
        callback(new Error('请输入秘钥'))
      } else if (value.trim().length != 64) {
        callback(new Error('请输入64位秘钥'))
      } else {
        this.model.clientSecret = value.trim()
        callback()
      }
    },
    selectIcons () {
      this.iconChooseVisible = true
    },
    handleIconCancel () {
      this.iconChooseVisible = false
    },
    handleIconChoose (value) {
      console.log(value)
      this.model.icon = value
      this.iconChooseVisible = false
    },
  },
}
</script>
<style lang="less" scoped>
::v-deep .ant-select-selection__rendered {
  line-height: 38px !important;
}

::v-deep .ant-select-selection--single {
  height: 38px !important;
}

::v-deep .ant-input-number:focus {
  
  
}

::v-deep .ant-input-number:hover {
  
}

::v-deep .ant-input-number-focused {
  
  

}

.ant-input-number {
  height: 38px;
  line-height: 38px;
  border-radius: 2px;
}

::v-deep .ant-input-number-handler:hover .ant-input-number-handler-up-inner {
  
}

::v-deep .ant-input-number-handler:hover .ant-input-number-handler-down-inner {
  

}
</style>
<style scoped>
.aColUn>div>>>.ant-col:first-child {
  width: 10.4%;
}

.aColUn>div>>>.ant-col:nth-child(2) {
  width: 89.6%;
}

.aColUn>div>>>.ant-col:nth-child(2)>.ant-form-item-control {
  width: 93%;
}
</style>
