<template>
  <div>
    <Drawer
      :title="formTitle"
      v-model="modalVisible"
      width="720"
      :mask-closable="false"
      :styles="styles"
      @on-close="close"
    >
      <Form
        ref="formValidate"
        :model="formValidate"
        :rules="ruleValidate"
        :label-width="150"
        v-if="formValidate.details"
      >
         <FormItem label="关联">
           <Cascader :data="treeData" v-model="formValidate.ancestors"></Cascader>
        </FormItem>
        <FormItem label="音轨名称" prop="details.name">
           <Input
            v-model="formValidate.details.name"
            placeholder="Enter something"
          ></Input>
        </FormItem>
        <FormItem label="简介">
          <Input
           type="textarea"
            v-model="formValidate.details.summary"
            placeholder="请输入内容"
          ></Input>
        </FormItem>
         <FormItem label="默认icon" prop="details.res_icon">
          <UploadS ref="res_icon" v-model="formValidate.details.res_icon" />
        </FormItem>
         <FormItem label="开始播放间隔" >
            <InputNumber  v-model="formValidate.details.start_space"></InputNumber>
        </FormItem>
         <FormItem label="距结束播放间隔" >
            <InputNumber  v-model="formValidate.details.end_space"></InputNumber>
        </FormItem>
         <FormItem label="播放模式">
            <Select clearable  v-model="formValidate.details.play_mode" style="width:180px">
              <Option
                v-for="item in play_mode"
                :value="item.id"
                :key="item.id"
                >{{ item.name }}</Option
              >
            </Select>
        </FormItem>
          <FormItem label="默认音量" prop="details.volume">
            <InputNumber  v-model="formValidate.details.volume"></InputNumber>
        </FormItem>
        <FormItem label="音轨类型">
            <Select clearable  v-model="formValidate.details.track_type" style="width:180px">
              <Option
                v-for="item in track_type"
                :value="item.id"
                :key="item.id"
                >{{ item.name }}</Option
              >
            </Select>
        </FormItem>
      </Form>
      <div class="demo-drawer-footer">
        <Button style="margin-right: 8px" @click="close">取消</Button>
        <Button type="primary" @click="submit">保存</Button>
      </div>
    </Drawer>
  </div>
</template>
<script>
import UploadS from '@/view/common/upload.vue'
import {
  addTracks,
  editTracks
} from '@/api/tracks'
const _requireBlur = { required: true, message: ' ', trigger: 'blur' }
const validatorPic = (rule, value, callback) => {
  if (!value) {
    callback(new Error(' '))
  } else {
    callback()
  }
}
const _requirePic = { required: true, message: ' ', trigger: 'blur', validator: validatorPic }
export default {
  name: 'editTheme',
  props: {
    search: {
      type: Function,
      default: null
    },
    treeData: {
      type: Array,
      default: () => []
    },
    row: {
      type: Object,
      default: () => {}
    },
    type: {
      type: Number,
      default: 1
    },
    visible: {
      type: Boolean,
      default: false
    }
  },
  components: {
    UploadS
  },
  data () {
    return {
      modalVisible: false,
      editMode: false,
      formTitle: '',
      styles: {
        height: 'calc(100% - 55px)',
        overflow: 'auto',
        paddingBottom: '53px',
        position: 'static'
      },
      play_mode: [{ id: 0, name: '随机播放' }, { id: 1, name: '顺序' }, { id: 2, name: '单次' }, { id: 3, name: '重复' }],
      track_type: [{ id: 0, name: '常规类型' }, { id: 1, name: '语音' }, { id: 2, name: '脑波' }, { id: 3, name: '呼吸引导' }, { id: 4, name: '单音频' }, { id: 5, name: '效果类短音频' }, { id: 6, name: '旋律类短音频' }, { id: 7, name: '混音音轨' }, { id: 8, name: '自定义' }],
      formValidate: {},
      ruleValidate: {
        'details.name': [_requireBlur],
        'details.res_icon': [_requirePic],
        'details.volume': [_requirePic]
      }
    }
  },
  watch: {
    visible: {
      handler: function (val) {
        this.modalVisible = val
        if (val) {
          this.search()
        }
        this.formValidate = this.row
      },
      immediate: true
    },
    row: {
      handler: function (val) {
        if (val.id === 'add') {
          this.editMode = false
          this.formTitle = '新增音轨'
          this.formValidate = {
            details: {
              name: '',
              summary: '',
              res_icon: '',
              start_space: 0,
              end_space: 0,
              play_mode: 0,
              volume: 0,
              track_type: 0
            },
            ancestors: [],
            model_type: this.type,
            model_id: 0

          }
        } else {
          this.editMode = true
          this.formTitle = '编辑音轨'
          this.formValidate = val
        }
      },
      immediate: true
    }
  },
  mounted () {

  },
  methods: {
    submit () {
      let res_icon = this.$refs['res_icon'].getData()
      if (res_icon) {
        this.formValidate.details.res_icon = res_icon
      }
      this.$refs['formValidate'].validate((valid) => {
        if (valid) {
          delete this.formValidate.created_at
          delete this.formValidate.deleted_at
          delete this.formValidate.updated_at
          if (this.formValidate.ancestors.length > 0) {
            this.formValidate.model_id = this.formValidate.ancestors[this.formValidate.ancestors.length - 1]
          }
          let params = [this.formValidate]
          if (this.editMode) {
            editTracks(params).then((res) => {
              if (res.code === 200 && res.data.length > 0) {
                this.$Message.success('修改成功')
                this.close()
                this.search()
              }
            })
          } else {
            addTracks(params).then((res) => {
              if (res.code === 200 && res.data.length > 0) {
                this.$Message.success('新增成功')
                this.close()
                this.search()
              }
            })
          }
        } else {
          this.$Message.error('您还有必填项未填写')
        }
      })
    },
    close () {
      this.$emit('update:visible', false)
    }
  }
}
</script>
<style>
.demo-drawer-footer {
  width: 100%;
  position: absolute;
  bottom: 0;
  left: 0;
  border-top: 1px solid #e8e8e8;
  padding: 10px 16px;
  text-align: right;
  background: #fff;
}
</style>
