<template>
  <el-dialog
    :title="!dataForm.templateId ? this.$i18n.t('crud.addTitle') : this.$i18n.t('temp.modify')"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="100px">
      <el-form-item label="模板系列" prop="templateSeries">
        <el-select v-model="dataForm.templateSeriesId" clearable filterable>
          <el-option
                  v-for="node in seriesList"
                  :key="node.seriesId"
                  :label="node.seriesName"
                  :value="node.seriesId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="语音名称" prop="templateName">
        <el-input v-model="dataForm.templateName"/>
      </el-form-item>
      <el-form-item label="语音地址" prop="templateUrl">
        <audio-upload v-model="dataForm.templateUrl" :limit="1"></audio-upload>
      </el-form-item>
      <el-form-item label="是否禁用" prop="status">
        <el-select v-model="dataForm.status" clearable filterable>
          <el-option key="1" label="启用" :value=1 ></el-option>
          <el-option key="0" label="禁用" :value=-1></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="排序" prop="seq">
        <el-input v-model="dataForm.seq" type="number"/>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button class="default-btn" @click="visible = false">{{$t("crud.filter.cancelBtn")}}</el-button>
      <el-button class="default-btn primary-btn" type="primary" @click="dataFormSubmit()">{{$t("crud.filter.submitBtn")}}</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import AudioUpload from '@/components/audio-upload'
  // import uploadPic from './upload-pic.vue'
export default {
  components: {
    AudioUpload
  },
  data () {
    return {
      visible: false,
      dataForm: {
        templateId: null,
        templateSeriesId: null,
        templateSeries: null,
        templateUrl: null,
        shopId: this.$store.state.user.shopId,
        status: 1,
        createTime: null,
        templateUsage: null,
        seq: 1,
        templateName: null
      },
      resourcesUrl: process.env.VUE_APP_RESOURCES_URL,
      seriesList: [],
      dataRule: {
      }
    }
  },
  created () {
    this.getSeriesList()
  },
  methods: {
    init (templateId) {
      this.dataForm.templateId = templateId || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.templateId) {
          this.$http({
            url: this.$http.adornUrl('/prod/voiceTemplate/info/' + this.dataForm.templateId),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            this.dataForm = data;
            this.usageList = data.templateUsage.split(",");
          })
        }
      })
    },
    // 表单提交
    async dataFormSubmit () {
      try{
        this.handleSeriesName(this.dataForm.templateSeriesId);
        this.$http({
          url: this.$http.adornUrl('/prod/voiceTemplate'),
          method: this.dataForm.templateId ? 'put' : 'post',
          data: this.$http.adornData(this.dataForm)
        }).then(({data}) => {
          this.$message({
            message: this.$i18n.t('publics.operation'),
            type: 'success',
            duration: 100,
            onClose: () => {
              this.visible = false;
              this.$emit('refreshDataList')
            }
          })
        })
      }catch (error) {

      }
    },
    getSeriesList () {
      this.$http({
        url: this.$http.adornUrl('/prod/prodTemplateSeries/page'),
        method: 'get',
        params: this.$http.adornParams(
                Object.assign({
                  size: 100,
                  shopId: this.$store.state.user.shopId
                }
                )
        )
      }).then(({data}) => {
        this.seriesList = data.records
      })
    },
    handleSeriesName (seriesId) {
      this.seriesList.forEach(series =>{
        if (series.seriesId == seriesId) {
          this.dataForm.templateSeries = series.seriesName
        }
      })
    },
  }
}
</script>
