<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"
      >
       <FormItem label="资源名称" prop="title">
          <Input
            v-model="formValidate.title"
            placeholder="Enter something"
          ></Input>
        </FormItem>
        <FormItem label="描述" prop="introduction">
          <Input
           type="textarea"
            v-model="formValidate.introduction"
            placeholder="请输入内容"
          ></Input>
        </FormItem>
         <FormItem label="资源文件" prop="res_uri">
          <FileUpload ref="res_uri" v-model="formValidate.res_uri" />
        </FormItem>
         <FormItem label="资源长度" prop="play_duration">
            <InputNumber  v-model="formValidate.play_duration"></InputNumber>
        </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 FileUpload from '@/view/common/FileUpload.vue'
import {
  addRings,
  editRings
} from '@/api/rings'
import {
  getTreeCategories
} from '@/api/categories'
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 {
  props: {
    search: {
      type: Function,
      default: null
    },
    treeData: {
      type: Array,
      default: () => []
    },
    row: {
      type: Object,
      default: () => {}
    },
    visible: {
      type: Boolean,
      default: false
    }
  },
  components: {
    FileUpload
  },
  data () {
    return {
      modalVisible: false,
      editMode: false,
      formTitle: '',
      styles: {
        height: 'calc(100% - 55px)',
        overflow: 'auto',
        paddingBottom: '53px',
        position: 'static'
      },
      formValidate: {},
      ruleValidate: {
        'title': [_requireBlur],
        'introduction': [_requireBlur],
        'res_uri': [_requirePic],
        'play_duration': [_requirePic]
      }
    }
  },
  watch: {
    visible: {
      handler: function (val) {
        this.modalVisible = val
        if (val) {
          this.search()
        }
        this.formValidate = this.row
      },
      immediate: true
    },
    row: {
      handler: function (val) {
        console.log(val)
        if (val.id === 'add') {
          this.editMode = false
          this.formTitle = '新增铃声'
          this.formValidate = {
            title: '',
            introduction: '',
            res_uri: '',
            play_duration: ''
          }
        } else {
          this.editMode = true
          this.formTitle = '编辑铃声'
          this.formValidate = val
          this.formValidate.res_uri = {
            url: val.res_uri,
            name: val.res_digest
          }
        }
      },
      immediate: true
    }
  },
  methods: {
    submit () {
      let res_uri = this.$refs['res_uri'].getData()
      if (res_uri.url) {
        this.formValidate.res_uri = res_uri.url
        this.formValidate.res_digest = res_uri.name
      }
      console.log(res_uri)
      this.$refs['formValidate'].validate((valid) => {
        if (valid) {
          delete this.formValidate.created_at
          delete this.formValidate.deleted_at
          delete this.formValidate.updated_at
          let params = [this.formValidate]
          if (this.editMode) {
            editRings(params).then((res) => {
              if (res.code === 200 && res.data.length > 0) {
                this.$Message.success('修改成功')
                this.close()
                this.search()
              }
            })
          } else {
            addRings(params).then((res) => {
              if (res.code === 200 && res.data.length > 0) {
                this.$Message.success('新增成功')
                this.close()
                this.search()
              }
            })
          }
        } else {
          this.$Message.error('您还有必填项未填写')
        }
      })
    },
    getTree (e) {
      getTreeCategories(e).then((res) => {
        console.log(res)
      })
    },
    change (val, type) {
      if (type === 1) {
        this.formValidate.is_in_home_lis = val ? 1 : 0
      }
      if (type === 2) {
        this.formValidate.is_in_discover_list = val ? 1 : 0
      }
    },
    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>
