<template>
  <el-dialog
    v-if="visible"
    :title="form.id?'编辑客诉信息':'新建客诉信息'"
    :visible.sync="visible"
    width="800px"
  >
    <el-form v-loading="isLoading" :model="form" label-width="100px" label-position="top">

      <el-form-item label="标题">
        <el-input v-model="form.title" />
      </el-form-item>
      <el-row :gutter="24">
        <el-col :span="12">
          <el-form-item label="类型">
            <el-input v-model="form.category" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="渠道">
            <el-input v-model="form.channel" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="图片">
        <el-upload
          :file-list="fileList"
          action="#"
          list-type="picture-card"
          name="fileData"
          :http-request="uploaderHandle"
          :on-success="uploadSuccess"
          :on-remove="handleRemove"
          :auto-upload="true"
        >
          <i slot="default" class="el-icon-plus" />
        </el-upload>
      </el-form-item>
      <el-form-item label="内容">
        <el-input v-model="form.content" type="textarea" rows="10" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" @click="handleSubmit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>

import { uploaderHandle } from '@/api/statistics/upload'
import { save, updateById } from '@/api/statistics/complaint'

export default {
  components: {},
  data() {
    return {
      visible: false,
      isLoading: false,
      form: {},
      fileList: []
    }
  },
  mounted() {
  },
  methods: {
    uploaderHandle,
    handleRemove(file, fileList) {
      this.fileList = fileList
    },
    uploadSuccess(res, file, fileList) {
      file.url = res.url
      this.fileList = fileList
    },
    open(data) {
      this.visible = true
      this.isLoading = false
      this.fileList = []
      this.form = {}
      console.log(data)
      if (data.litpics) {
        data.litpics.forEach(o => {
          this.fileList.push({ url: o })
        })
      }
      this.form = Object.assign({}, data)
    },
    handleSubmit() {
      const litpics = []
      this.fileList.forEach(o => {
        litpics.push(o.url)
      })
      this.isLoading = true
      const param = { ...this.form, 'litpics': litpics }
      console.log(param)
      if (this.form.id) {
        updateById(this.form.id, param).then(() => {
          this.visible = false
          this.$emit('ok')
        }).finally(() => {
          this.isLoading = false
        })
      } else {
        save(param).then(res => {
          this.visible = false
          this.$emit('ok')
        }).finally(() => {
          this.isLoading = false
        })
      }
    }
  }
}
</script>
