<script>
export default {
  props:{
    type:{
      type:String,
      default:"normal"
      //"normal" "form"
    },
    width:{
      type:Number,
      default:55
    },
    isShow:{
      type:Boolean,
      default:false
    },
    title:{
      type:String,
      default: ''
    },
    form:{
      type:Object,
      default:()=>{
        return {}
      }
    },
    rules:{
      type:Object,
      default:()=>{
        return {}
      }
    },
    okText:{
      type:String
    }
  },
  data() {
    return {

    }
  },
  methods: {
    footerRender(){
      return (
        <div>
          <Button ghost type="primary" on-click={this.reset}>重置</Button>
          <Button type="primary" on-click={this.submit}>确定</Button>
        </div>
      )
    },
    showChange(e){
      this.$emit('update:isShow',e)
    },
    cancelClick(){
      this.$emit('update:isShow',false)
    },
    reset(){
      if(this.type === 'normal'){
        return
      }
      this.$refs['elForm'].resetFields()
    },
    submit(){
      if(this.type === 'normal'){
        return
      }
      this.$refs['elForm'].validate().then(res=>{
        if(res)this.$emit('submit')
      }).catch(err=>{
        this.$Message.warning({
          content:'请填写完整信息',
          duration:1
        })
      })
    },
    okClick(){
      this.$emit('on-ok')
    }
  },
  render(h) {
    /*
    type='form' slot:
      extraFormTop
      extraFormBottom
      {表单名}
    type='normal' slot:
      normalContent
    footer slot:
      footerContent
     */
    const {cancelClick,okText,okClick,footerRender,width,type,$scopedSlots,rules,form,showChange,isShow,title} = this
    return (
      <Modal
        ok-text={okText}
        class-name={'vertical-center-modal'}
        width={width}
        title={title}
        value={isShow}
        on={{['on-visible-change']:showChange,['on-ok']:okClick}}
        scopedSlots={{
          default(){
            if(type === "form"){
              let formItems = []
              Object.keys(form).map((key)=>{
                formItems.push(
                  <el-form-item
                    prop={key}>
                    {eval(`{$scopedSlots.${key} ? $scopedSlots.${key}() : ''}`)}
                  </el-form-item>
                )
              })
              return (
                <el-form
                  style={title.length === 0 ? {'margin-top':'32px'} : {}}
                  ref="elForm"
                  props={{
                    model:form,
                    rules
                  }}>

                  {$scopedSlots.extraFormTop ? $scopedSlots.extraFormTop() : ''}

                  <div
                    style={
                      {
                        'display':'flex',
                        'align-items':'center',
                        'flex-wrap':'wrap',
                        'min-width':'380px'
                      }
                    }>
                    {
                      formItems.map(item=>{
                        return item
                      })
                    }
                  </div>

                  {$scopedSlots.extraFormBottom ? $scopedSlots.extraFormBottom() : ''}
                </el-form>
              )
            }else{
              return (
                <div
                  style={title.length === 0 ? {'margin-top':'32px'} : {}}>
                  {$scopedSlots.normalContent ? $scopedSlots.normalContent() : ''}
                </div>
              )
            }
          },
          footer(){
            {
              if(type === 'form') return ($scopedSlots.footerContent ? $scopedSlots.footerContent() : footerRender())
              if(type === 'normal') return (
                <div>
                  <Button ghost type="primary" on-click={cancelClick}>取消</Button>
                  <Button type="primary" on-click={okClick}>确定</Button>
                </div>
              )
            }
          }
        }}>
      </Modal>
    )
  }
}
</script>

<style lang="scss">
.vertical-center-modal{
  display: flex;
  align-items: center;
  justify-content: center;

  .ivu-modal{
    top: 0;
  }
}
.ivu-modal-body{
  max-height: 65vh !important;
  min-height: 300px;
  overflow-y: scroll;

}
.xy-table-item{
  display: flex;
  align-items: center;
  margin-right: 80px;
  &-label{

    padding: 0 20px;
  }
  &-content{

  }
}
.el-form-item__error{
  word-break: keep-all !important;
  top: 100% !important;
  left: calc(100% - 80px) !important;
  transform: translateX(-100%);
}
</style>

