<template>
  <a-modal
    :title="title"
    width="60%"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    :destroyOnClose="true"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="标题" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input
            placeholder="请输入标题"
            v-decorator="[
              'title',
              {
                validateFirst: true,
                rules: [
                  { required: true, message: '请输入标题' },
                  { max: 100, message: '标题最多100个字' }
                ]
              }
            ]"
          />
        </a-form-item>
        <a-form-item label="排序号" help="数字越小越靠前" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input placeholder="请输入排序号" v-decorator="['sortcode']" />
        </a-form-item>
        <a-form-item label="位置" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select
          @change="posChange"
            placeholder="请选择位置"
            v-decorator="['pos', { rules: [{ required: true, message: '请选择位置' }], initialValue: '' }]"
          >
            <a-select-option
              v-for="(item, index) in GetClientDataList('bannerpostionenum')"
              :key="index"
              :value="item.code"
              >{{ item.val }}</a-select-option
            >
          </a-select>
          <p style="text-align: left;"  alt="位置"> &nbsp&nbsp底部上传视频：选择底部，封面类型选择视频，再上传封面视频。</p>
        </a-form-item>
        <a-form-item  label="封面类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select
           @change="fmtypeChange"
            placeholder="请选择封面类型"
            v-decorator="['posfmtype', { rules: [{ required: true, message: '请选择封面类型' }], initialValue: '' }]"
           >
            <a-select-option
              v-for="(item, index) in test_array2"
              :key="index"
              :value="item.code"
              >{{ item.val }}</a-select-option
            >
          </a-select>
        </a-form-item>
        <a-form-item v-if="(posfmtype == 1 ) && (pos==2 || pos==1 )"  label="封面" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-upload-dragger :customRequest="handleUpload" :show-upload-list="false">
            <img v-if="imageUrl" style="max-height:200px;max-width:400px;" :src="imageUrl" alt="封面图" />
            <div v-else>
              <p class="ant-upload-drag-icon">
                <a-icon :type="loading ? 'loading' : 'inbox'" />
              </p>
              <p class="ant-upload-text">
                上传封面
              </p>
            </div>
          </a-upload-dragger>
        </a-form-item>
        <a-form-item v-if=" ( posfmtype == 2)  && (pos==2 || pos==1) " label="封面视频" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-upload-dragger :customRequest="handleUploadVideo" :show-upload-list="false">
            <p v-if="imageUrl" style="text-align: left;"  alt="封面视频"> &nbsp&nbsp封面视频路径：{{imageUrl}}</p>
            <div v-else>
              <p class="ant-upload-drag-icon">
                <a-icon :type="loading ? 'loading' : 'inbox'" />
              </p>
              <p class="ant-upload-text">
                上传封面视频
              </p>
            </div>
          </a-upload-dragger>
          <p style="text-align: left;"  alt="封面视频"> &nbsp&nbsp注意：视频文件最大20M，MP4格式,视频文件制作建议采用16:9比例,最终显示效果见小程序首页。</p>
          
        </a-form-item>
        <a-form-item label="类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select
            @change="typeChange"
            placeholder="请选择类型"
            v-decorator="['ptype', { rules: [{ required: true, message: '请选择类型' }], initialValue: 1 }]"
          >
            <a-select-option
              v-for="(item, index) in GetClientDataList('conenttypeenum')"
              :key="index"
              :value="item.code"
              >{{ item.val }}</a-select-option
            >
          </a-select>
        </a-form-item>
        <a-form-item label="小程序Appid" v-if="type == 1" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input
            placeholder="请输入小程序appid"
            v-decorator="['appid', { rules: [{ required: true, message: '请输入小程序appid' }] }]"
          />
        </a-form-item>
        <a-form-item label="小程序页面地址" v-if="type == 1" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入小程序页面地址" v-decorator="['content']" />
        </a-form-item>
        <a-form-item label="链接" v-if="type == 2" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea
            :rows="2"
            placeholder="请输入链接地址"
            v-decorator="['content', { rules: [{ required: true, message: '请输入链接地址' }] }]"
          />
        </a-form-item>
        <a-form-item label="页面地址" v-if="type == 3" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input
            placeholder="请输入小程序内的页面地址"
            v-decorator="['content', { rules: [{ required: true, message: '请输入小程序内的页面地址' }] }]"
          />
        </a-form-item>
        <a-form-item label="内容" v-if="type == 4" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea style="display: none;" :rows="4" placeholder="请输入内容" v-decorator="['content']" />
          <WangEditor ref="contentRef" :value="html" :uploadConfig="uploadConfig" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { GetClientDataList } from '@/utils/clientdata'
import { Submit } from '@/api/modular/main/banner'
import { UploadImg } from '@/api/modular/main/upload'
import { UploadImgVideo } from '@/api/modular/main/upload'
import WangEditor from '@/components/Editor/WangEditorV5'
export default {
  props: {
    title: {
      type: String,
      required: true
    }
  },
  components: { WangEditor },
  data() {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      loading: false,
      imageUrl: '',
      headimg: '',
      html: '',
      uploadConfig: {
        method: 'http',
        url: 'common/uploadimg'
      },
      type: 1, // 当前类型,1-小程序,2-链接,3-页面,4-富文本
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 15 }
      },
      currentInfo: {},
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this),
      pos:1,
      posfmtype:1,
      test_array:[],
      test_array2:[],
    }
  },
  methods: {
    GetClientDataList,
    // 初始化方法
    init(record) {
      this.visible = true
      this.currentInfo = record
      this.imageUrl = ''
      this.headimg = ''
      this.html = ''

      this.test_array2 = []
      this.test_array = this.GetClientDataList('bannerpostionimgvideoenum')
      if(record.pos == 1)
      {
        this.test_array2.push(this.test_array[0])
      }else
      {
        this.test_array2 = this.test_array
      }

      if (record && record.id) {
        this.type = record.ptype
        this.headimg = record.headimg
        this.imageUrl = record.imageurl
        console.log(record.pos + '=pos')
        console.log(record.posfmtype+'=posfmtype')

        setTimeout(() => {
          
          // console.log(this.test_array2)
          this.form.setFieldsValue({
            title: record.title,
            ptype: record.ptype,
            content: record.content,
            sortcode: record.sortcode,
            pos: record.pos,
            posfmtype: record.posfmtype == 0?'':record.posfmtype,            
          })
          
          if (record.ptype === 4) 
          {
            this.html = record.content
          } else if (record.ptype === 1) 
          {
            this.form.setFieldsValue({
              appid: record.appid
            })
          }else if(record.posfmtype==1)
          {
            this.posfmtype = record.posfmtype

          }
        }, 100)
        // console.log(pos)
      }
    },
    handleSubmit() {
      const {
        form: { validateFields }
      } = this
      this.confirmLoading = true
      validateFields((errors, values) => {
        if (!errors) {
          values['id'] = this.currentInfo.id ? this.currentInfo.id : null
          values['headimg'] = this.headimg
          values['content'] = values['ptype'] === 4 ? this.$refs.contentRef.gethtml() : values['content']
          Submit(values)
            .then(res => {
              if (res.code === 0) {
                if (this.currentInfo && this.currentInfo.id) {
                  this.$message.success('编辑成功')
                } else {
                  this.$message.success('添加成功')
                }
                this.confirmLoading = false
                this.$emit('ok', this.record)
                this.handleCancel()
              } else {
                this.$myerror(res.message)
              }
            })
            .finally(res => {
              this.confirmLoading = false
            })
        } else {
          this.confirmLoading = false
        }
      })
    },
    typeChange(item) {
      this.type = item
    },
    fmtypeChange(item) 
    {
      console.log(item+'=posfmtype')
      console.log(this.pos+'=pos')
      this.posfmtype = item

    },
    posChange(item) 
    {
      console.log(item)
      this.pos=item
      console.log(this.pos+'=pos')
      this.test_array2 = []
      this.test_array = this.GetClientDataList('bannerpostionimgvideoenum')
      if(item == 1)
      {
        this.test_array2.push(this.test_array[0])
      }else
      {
        this.test_array2 = this.test_array
      }
      // console.log(this.posfmtype)
    },
    // 上传
    handleUpload(data) {
      const formData = new FormData()
      formData.append('file', data.file)
      this.uploading = true
      UploadImg(formData).then(res => {
        if (res.code === 0) {
          this.imageUrl = res.data.url
          this.headimg = res.data.path
        } else {
          this.$myerror(res.message)
        }
        this.uploading = false
      })
    },
    // 上传视频
    handleUploadVideo(data) {
      const formData = new FormData()
      formData.append('file', data.file)
      this.uploading = true
      UploadImgVideo(formData).then(res => {
        if (res.code === 0) {
          this.imageUrl = res.data.url
          this.headimg = res.data.path
        } else {
          this.$myerror(res.message)
        }
        this.uploading = false
      })
    },
    handleCancel() {
      this.form.resetFields()
      this.visible = false
    }
  }
}
</script>
