<template>
    <div>
      <van-nav-bar
        title="投诉建议"
        left-arrow
        @click-left="$router.back(-1)"
      />
      <van-cell-group style="width: 96%;margin:2%;border-radius: 6px;overflow: hidden;padding-top: 10px;padding-bottom: 10px;box-shadow: 0px 3px 6px 0px rgba(0,0,0,0.16);">
        <van-cell>
          <template #title>
            <van-row style="">
              <van-col span="4" :offset="1">
                <h3 style="">类型：</h3>
              </van-col>
              <van-col span="10" >
                <van-radio-group v-model="form.type" direction="horizontal">
                  <van-radio name="1">投诉</van-radio>
                  <van-radio name="2">建议</van-radio>
                </van-radio-group>
              </van-col>
            </van-row>
          </template>
        </van-cell>
        <van-cell>
          <template #title>
            <van-row>
              <van-col span="24">
                <van-field
                  v-model="form.adviceContent"
                  rows="7"
                  autosize
                  type="textarea"
                  maxlength="1000"
                  placeholder="请输入您的反馈意见（字数1000以内）"
                  show-word-limit
                />
              </van-col>
            </van-row>
          </template>
        </van-cell>
      </van-cell-group>
      <van-cell-group style="width: 96%;margin:2%;border-radius: 6px;overflow: hidden;padding-top: 10px;padding-bottom: 10px;box-shadow: 0px 3px 6px 0px rgba(0,0,0,0.16);">
        <van-cell>
          <template #title>
            <van-row style="">
              <van-col span="23" :offset="1">
                <van-field name="uploader" label="">
                  <template #input>
                    <van-uploader :after-read="afterRead" :before-delete="deleteFile" v-model="fileList" multiple :max-count="5" />
                  </template>
                </van-field>
              </van-col>
            </van-row>
          </template>
        </van-cell>
      </van-cell-group>
      <van-row style="text-align: center;margin-top: 40px">
        <van-button color="#1D6FE9" style="border-radius: 6px;width: 90%;margin: 0 auto" @click="adviceAdd">提交</van-button>
      </van-row>
    </div>
</template>

<script>
    import onlineHomeIndex from "../onlineHomeIndex";
    import {adviceAdd} from "../../api/onlineHome/my";
    import * as Toast from "vant";
    import {uploadFileBase} from "../../api/authenticRight";
    import {Dialog} from "vant";
    import {base64Attach, deleteUserImg} from "../../api/user";

    export default {
      components: {
        onlineHomeIndex
      },
        name: "advice",
      data() {
        return {
          form:{
            id:'',
            type: '1',
            adviceContent:'',
            url:[],
            files:[],
          },
          file: {
            files:[],
            fileType:'',
            bizPath:'advice',
            tableName:'t_sys_advice',
            tableId:''
          },
          fileList:[],
          upLoadList:[],
        };
      },
      created() {
        this.reset();
      },
      methods: {
        reset(){
          this.form={
            id:'',
            type: '1',
            adviceContent:'',
            url:[],
            files:[],
          }
        },
        afterRead(file) {
          // 此时可以自行将文件上传至服务器
          for ( let i = 0 ; i < this.fileList.length ; i++){
            if(this.fileList[i].url == undefined&&this.fileList!='') {
              this.fileList[i] = {url:this.fileList[i].content, id: '', isImage: true};
            }
          }
        },
        deleteFile(elIndex){
          if(elIndex.id != ''){
            deleteUserImg(elIndex.id).then(response => {});
          }
          return (file, name) => {
            let fileIndex = name.index
            this.fileList[elIndex].splice(fileIndex, 1)
            this.upLoadList[elIndex].splice(fileIndex, 1)
          }
        },
        adviceAdd(){
          adviceAdd(this.form).then(res => {
            this.file.tableId = res.data;
            for(let i = 0 ; i < this.fileList.length ; i++){
              this.file.files.push(this.fileList[i].url);
            }
            base64Attach(this.file).then(response => {
              if (response.code == 200){
                Dialog.confirm({
                  title: '系统提示',
                  message: '发布成功',
                  confirmButtonText: '确定',
                  showCancelButton:false
                }).then(() => {
                  self.location=document.referrer;
                })
              }
            });

          });
        }
      },
    }
</script>

<style scoped>

</style>
