<template>
  <el-dialog
    :title="modalTitle"
    :visible.sync="modalVisible"
    :width="formLabelWidth"
    top="2vh"
    @close="handleCancel">
    <div class="mobile">
          <el-form label-width="90px" :model="form" :rules="rules" ref="from" class="from">
              <el-form-item label="付款银行:" prop="payBank">
                  <el-input type="text" v-model="form.payBank"></el-input>
              </el-form-item>
              <el-form-item label="银行卡号:" prop="payCardNo">
                  <el-input type="number" v-model="form.payCardNo"></el-input>
              </el-form-item>
              <el-form-item label="付款人:" prop="payUserName">
                  <el-input type="text" v-model="form.payUserName"></el-input>
              </el-form-item>
              <el-form-item label="付款时间:" prop="payTime">
                  <el-date-picker
                        v-model="form.payTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择日期">
                      </el-date-picker>
              </el-form-item>
              <el-divider content-position="left">附件</el-divider>
              <div class="uploadImg">
                 <el-upload
                   accept=".jpg, .png"
                   :headers="upload.headers"
                   :action="upload.url"
                   :disabled="upload.isUploading"
                   :on-progress="handleFileUploadProgress"
                   :on-success="handleFileSuccess"
                   :file-list="fileList">
                   <el-button size="small" type="primary">点击上传</el-button>
                   <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
                 </el-upload>
                 <ul class="uploadImgList">
                   <li v-for="(item,index) in form.voucherImages" :key="index">
                     <img :src="imgRequest + item + '?imageView2/1/w/200/h/200'">
                     <span class="del" @click="handelImgRemove(index)" v-if="!limit.isRead"><i class="el-icon-delete-solid"></i></span>
                   </li>
                 </ul>
              </div>
          </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCancel">取 消</el-button>
      <el-button type="primary" @click="submit" v-if="!limit.isRead">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getToken } from "@/utils/auth";
import { pay } from "@/api/order/subscription";
export default {
  name: 'addMobiel',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: 'title'
    },
    action: {
      type: String,
      default: 'add'
    },
    selectRow: {
      type: Object,
      default:null
    }
  },
  watch: {
    visible(newVal, oldVal) {
      this.modalVisible = newVal;
    },
    title(newVal, oldVal) {
      this.modalTitle = newVal;
    },
    action(newVal, oldVal) {
      this.modalAction = newVal;
    },
    selectRow(newVal, oldVal) {
      this.modalSelectRow = newVal;
    }
  },
  data() {
    return {
      modalTitle: this.title,
      modalVisible: this.visible,
      modalAction: this.action,
      modalSelectRow: this.selectRow,
      formLabelWidth: '624px',
      //窗口权限
      limit: {
        isRead: false, //是否只读
        isAdd: false //是否可写
      },
      upload: {
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/system/upload/fileImg",
      },
      fileList:[],
      //购买类型
      buyTypeList:[],
      //验证
      rules: {
        payBank: [{ required: true, message: '付款银行不能为空!', trigger: 'blur' }],
        payCardNo: [{ required: true, message: '银行卡号不能为空!', trigger: 'blur' }],
        payUserName: [{ required: true, message: '付款人不能为空!', trigger: 'blur' }],
        payTime: [{ required: true, message: '付款时间不能为空!', trigger: 'blur' }]
      },
      form: {
        customerId:'',
        orderId:'',
        voucherImages:[]
      }
    };
  },
  created() {
    console.log(this.modalSelectRow)
    this.form.orderId = this.modalSelectRow.guid
    this.form.customerId = this.modalSelectRow.customerId
    console.log(this.form)
    switch (this.modalAction) {
      case 'add':
        this.limit.isAdd = true;
        break;
      default:
        break;
    }
  },
  methods: {
    //表单提交
    submit() {
      this.$refs.from.validate(valid => {
        if (valid) {
          let params = Object.assign({},this.form)
          params.voucherImages = params.voucherImages.join(';')
          pay(params).then(res => {
              if (res.retHead.errCode === 0) {
                this.msgSuccess("操作成功");
                this.modalVisible = false
                this.$emit('closePlaypayModal', true);
              }
          });
        } else {
          return false;
        }
      });
    },
    //窗口关闭
    handleCancel() {
      this.$emit('closePlaypayModal', false);
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
       this.upload.isUploading = true;
    },
    // 上传成功处理
    handleFileSuccess(response, file, fileList){
       this.fileList = []
       this.form.voucherImages.push(response.retBody)
       this.upload.isUploading = false;
    },
    // 图片删除
    handelImgRemove(i){
      this.form.voucherImages.splice(i,1)
    },
  }
};
</script>

<style lang="scss" scoped>
.uploadImg{
      padding:0 30px 30px 30px;
      .uploadImgList{
        list-style:none;
        margin:0;
        padding:15px 0;
        display:flex;
        flex-flow: row;
        li{
          position:relative;
          border:1px solid #ddd;
          border-radius:6px;
          width:120px;
          height:120px;
          margin-right:13px;
          img{
            width:100%;
            height:100%;
            border-radius:6px;
          }
          .del{
            display:none;
            position:absolute;
            top: -9px;
            right: -9px;
            cursor:pointer;
            i{
              font-size:20px;
              color:red;
            }
          }
          &:hover{
            .del{
              display:block;
            }
          }
        }
      }
    }
</style>
