<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          v-if="videoLocalUrlItem"
          label="视频保存本地地址"
        >
          <a-input placeholder="请输入视频保存本地地址" v-decorator="['localUrl', {}]" />
        </a-form-item>

        <!-- 视频预览 -->
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          v-if="videoPreviewItem"
          label="视频预览"
        >
          <video
            class="video"
            style="width:200px;"
            :src="videoPreviewUrl"
            controls="controls"
          >您的浏览器不支持 video 标签。</video>
        </a-form-item>
        <!-- 视频预览 E -->

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          v-if="videoDescItem"
          label="视频描述"
        >
          <a-input placeholder="请输入视频描述" v-decorator="['videoDesc', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          v-if="vodeoNameItem"
          label="视频名称"
        >
          <a-input placeholder="请输入视频名称" v-decorator="['videoName', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          v-if="videoTypeItem"
          label="视频类别"
        >
          <a-input placeholder="请输入视频类别" v-decorator="['videoType', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          v-if="videoLoginIdItem"
          label="登录人id"
        >
          <a-input placeholder="请输入登录人id" v-decorator="['loginId', {}]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { httpAction } from '@/api/manage'
import pick from 'lodash.pick'
import moment from 'moment'
import { stopOtherVideo, closeAllVideoFun } from '@/utils/videoControl'

export default {
  name: 'KuaiShouVideoModal',
  data() {
    return {
      title: '操作',
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },

      confirmLoading: false,
      form: this.$form.createForm(this),
      validatorRules: {},
      url: {
        add: '/kuaishou/kuaiShouVideo/add',
        edit: '/kuaishou/kuaiShouVideo/edit'
      },
      videoPreviewUrl: '',
      videoLocalUrlItem: true,
      videoDescItem: true,
      vodeoNameItem: true,
      videoTypeItem: true,
      videoLoginIdItem: true,
      videoPreviewItem: true
    }
  },
  methods: {
    add(sign) {
      /**
       * 在 src\mixins\JeecgListMixin.js 中给 handleAdd 一个参数 sign
       * sign---add---新增
       */
      if (sign == 'add') {
        this.edit({});
        this.videoPreviewItem = false;
        this.vodeoNameItem = true;
        this.videoTypeItem = true;
        this.videoLoginIdItem = true;
      }
      closeAllVideoFun();
    },
    edit(record) {
      this.form.resetFields();
      this.model = Object.assign({}, record);
      this.visible = true;
      this.videoLocalUrlItem = false;
      this.videoDescItem = true;
      this.vodeoNameItem = false;
      this.videoTypeItem = false;
      this.videoLoginIdItem = false;
      this.videoPreviewItem = true;
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model, 'localUrl', 'videoDesc', 'videoName', 'videoType', 'loginId'));
        //时间格式化
        this.videoPreviewUrl = record.localUrl;
      })
      closeAllVideoFun();
    },
    close() {
      this.$emit('close');
      this.visible = false;
      closeAllVideoFun();
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          let formData = Object.assign(this.model, values)
          //时间格式化

          console.log(formData)
          httpAction(httpurl, formData, method)
            .then(res => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        }
      })
      closeAllVideoFun();
    },
    handleCancel() {
      this.close();
      closeAllVideoFun();
    },
  }
}
</script>

<style lang="less" scoped>
</style>