<!-- 回款 -->
<template>
  <div class="context">
    <div style="text-align: right;margin-bottom: 20px;">
      <el-button type="primary" @click="newrefund = !newrefund">
        新增回款信息
      </el-button> 
    </div>
    <div>
      <el-table
        :data="tableData"
        border
        size='small'
        :header-cell-style="{'text-align':'center'}"
        :cell-style="{'text-align':'center'}"
        style="width: 100%;">
        <el-table-column
        type="index"
        label="序号"
        width="50">
        </el-table-column>
        <el-table-column
        prop="name"
        label="客户名称"
        width="">
        </el-table-column>
        <el-table-column
        prop="xinxi"
        label="回款信息"
        width="120">
        </el-table-column>
        <el-table-column
        prop="jiner"
        label="回款金额"
        width="130">
        </el-table-column>
        <el-table-column
        prop="time"
        label="回款日期"
        width="100">
        </el-table-column>
        <el-table-column
        prop="caozuo"
        label="操作人"
        width="100">
        </el-table-column>
        <el-table-column
        prop="examine"
        label="审核人"
        width="100">
        </el-table-column>
        <el-table-column
        prop="address"
        label="操作"
        width="180">
        <template slot-scope="scope">
          <el-button type="primary" @click="cahkan_click(scope.row)">查看</el-button>
          <!-- <el-button type="danger">删除</el-button> -->
        </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 弹窗 -->
    <div>
      <!-- 新增 -->
      <el-dialog
        title="新增回款信息"
        :visible.sync="newrefund"
        :modal-append-to-body="false"
        width="60%"
        >
        <div style="height: 50vh;overflow: overlay;">
          <div class="xinxi"  style="">
            <div style="flex: 1;">
              <span>客户名称：</span>
              <el-input v-model="refundobj.name" placeholder="请输入内容" style="width: 60%;"></el-input>
            </div>
            <div style="flex: 1;">
              <span>回款类型：</span>
              <el-select v-model="refundobj.type" placeholder="请选择">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </div>
          </div>
          <div class="xinxi"  style="">
            <div style="flex: 1;">
              <span>回款总金额(元)：</span>
              <el-input v-model="refundobj.totalamount" placeholder="请输入内容" style="width: 60%;"></el-input>
            </div>
            <div style="flex: 1;">
                <span>软件回款信息(元)：</span>
                <el-input v-model="refundobj.software" placeholder="请输入内容" style="width: 60%;"></el-input>
              </div>
          </div>

          <div class="xinxi"  style="">
            <div style="flex: 1;">
                <span>硬件回款信息(元)：</span>
                <el-input v-model="refundobj.hardware" placeholder="请输入内容" style="width: 60%;"></el-input>
            </div>
            <div style="flex: 1;">
              <span>回款日期：</span>
              <el-date-picker
                  v-model="refundobj.date"
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="选择日期">
                </el-date-picker>
            </div>
          </div>
          <div class="xinxi"  style="">
            <div style="flex: 1;">
                <span>回款人员：</span>
                <el-input v-model="refundobj.role" placeholder="请输入内容" style="width: 60%;"></el-input>
            </div>
            <div style="flex: 1;">
              <span> 回款凭证<br/>(上传付款回执图片)：</span>
              <div style="border: 1px dashed #d9d9d9;height: 178px;width: 178px;">
                <!-- <el-upload
                  action="https://jsonplaceholder.typicode.com/posts/"
                  list-type="picture-card"
                  :on-error="handleAvatarError"
                  :on-success="handleAvatarSuccess"
                  :before-upload="beforeAvatarUpload"
                  :on-remove="handleRemove"
                  :limit='1'
                  
                  >
                    <i slot="default" class="el-icon-plus"></i>
                    <div slot="file" slot-scope="{file}">
                      <img
                        class="el-upload-list__item-thumbnail"
                        :src="file.url" alt=""
                      >
                      <span class="el-upload-list__item-actions">
                       
                        <span
                          v-if="!disabled"
                          class="el-upload-list__item-delete"
                          @click="handleRemove(file)"
                        >
                          <i class="el-icon-delete"></i>
                        </span>
                      </span>
                    </div>
                </el-upload> -->

                <el-upload
                  :data="uploadData"
                  class="avatar-uploader"
                  action="https://upload.qiniup.com"
                  :show-file-list="false"
                  :on-success="handleAvatarSuccess"
                  :before-upload="beforeAvatarUpload"
                  style="height: 178px;width: 178px;">
                  
                  <img v-if="refundobj.url" :src="refundobj.url" class="avatar" style="height: 178px;width: 178px;">
                  <i v-else class="el-icon-plus avatar-uploader-icon" style="height: 178px;width: 178px;line-height: 178px;text-align: center;"></i>
                </el-upload>
              </div>
              
            </div>
          </div>
          <div class="xinxi"  style="">
            <div style="flex: 1;">
              <span style="position: relative;bottom: 20px;left: 0;">
                  备注：
              </span>
              <el-input type="textarea" :row="2" v-model="refundobj.remark" placeholder="请输入内容" style="width: 80%;"></el-input>
            </div>
          </div>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button @click="newrefund = false">取 消</el-button>
          <el-button type="primary" @click="refund_click">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 查看 -->
      <el-dialog
        title="编辑回款信息"
        :visible.sync="refund"
        :modal-append-to-body="false"
        width="60%"
        >
        <div style="height: 50vh;overflow: overlay;">
          <div class="xinxi"  style="">
            <div style="flex: 1;">
              <span>客户名称：</span>
              <el-input v-model="refundview.name" placeholder="请输入内容" style="width: 60%;"></el-input>
            </div>
            <div style="flex: 1;">
              <span>回款类型：</span>
              <el-select v-model="refundview.type" placeholder="请选择">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </div>
          </div>
          <div class="xinxi"  style="">
            <div style="flex: 1;">
              <span>回款总金额(元)：</span>
              <el-input v-model="refundview.totalamount" placeholder="请输入内容" style="width: 60%;"></el-input>
            </div>
            <div style="flex: 1;">
                <span>软件回款信息(元)：</span>
                <el-input v-model="refundview.software" placeholder="请输入内容" style="width: 60%;"></el-input>
              </div>
          </div>

          <div class="xinxi"  style="">
            <div style="flex: 1;">
                <span>硬件回款信息(元)：</span>
                <el-input v-model="refundview.hardware" placeholder="请输入内容" style="width: 60%;"></el-input>
            </div>
            <div style="flex: 1;">
              <span>回款日期：</span>
              <el-date-picker
                  v-model="refundview.date"
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="选择日期">
                </el-date-picker>
            </div>
          </div>
          <div class="xinxi"  style="">
            <div style="flex: 1;">
                <span>回款人员：</span>
                <el-input v-model="refundview.role" placeholder="请输入内容" style="width: 60%;"></el-input>
            </div>
            <div style="flex: 1;">
              <span> 回款凭证<br/>(上传付款回执图片)：</span>
              <div style="border: 1px dashed #d9d9d9;height: 178px;width: 178px;">
                <!-- <el-upload
                  action="https://jsonplaceholder.typicode.com/posts/"
                  list-type="picture-card"
                  :on-error="handleAvatarError"
                  :on-success="handleAvatarSuccess"
                  :before-upload="beforeAvatarUpload"
                  :on-remove="handleRemove"
                  :limit='1'
                  
                  >
                    <i slot="default" class="el-icon-plus"></i>
                    <div slot="file" slot-scope="{file}">
                      <img
                        class="el-upload-list__item-thumbnail"
                        :src="file.url" alt=""
                      >
                      <span class="el-upload-list__item-actions">
                       
                        <span
                          v-if="!disabled"
                          class="el-upload-list__item-delete"
                          @click="handleRemove(file)"
                        >
                          <i class="el-icon-delete"></i>
                        </span>
                      </span>
                    </div>
                </el-upload> -->

                <el-upload
                  :data="uploadData"
                  class="avatar-uploader"
                  action="https://upload.qiniup.com"
                  :show-file-list="false"
                  :on-success="handleAvatarSuccess2"
                  :before-upload="beforeAvatarUpload2"
                  style="height: 178px;width: 178px;">
                  
                  <img v-if="refundview.url" :src="refundview.url" class="avatar" style="height: 178px;width: 178px;">
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
              </div>
              
            </div>
          </div>
          <div class="xinxi"  style="">
            <div style="flex: 1;">
              <span style="position: relative;bottom: 20px;left: 0;">
                  备注：
              </span>
              <el-input type="textarea" :row="2" v-model="refundview.remark" placeholder="请输入内容" style="width: 80%;"></el-input>
            </div>
          </div>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button @click="refund = false">取 消</el-button>
          <el-button type="primary" @click="view_click">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import {getFileExtension, uParseTime } from "@/utils/tools";
import {getpaymentcollectioncreate,getpaymentcollectionlist,
  getpaymentcollectionget,getpaymentcollectionedit} from '../../../api/index'
import { getToken } from '../../../api/employee'
  export default{
    props:{
      Message:Object
    },
    data(){
      return{
        tableData: [],
        newrefund:false,
        refund:false,
        value1:'',
        value:'',
        input:'',
        textarea:'',
        options:[{value:1,label:'首年回款'},{value:2,label:'续费回款'}],

        refundobj:{
          name:'',
          type:'',
          totalamount:'',
          software:'',
          hardware:'',
          date:this.getTime(new Date()),
          role:'',
          url:'',
          remark:'',
        },
        refundview:{
          name:'',
          type:'',
          totalamount:'',
          software:'',
          hardware:'',
          date:this.getTime(new Date()),
          role:'',
          url:'',
          remark:'',
        },
        disabled:false,
        qiniuDomain: 'https://images.shengws.com/',
        uploadData: { token: '', key: '' },
        imageUrl:'',
        editor_id:0,
      }
    },
    created(){
      this.refundobj.name = this.Message.name 
      this.getlist()
    },
    methods:{
      // 查看弹窗
      cahkan_click(row){
        this.refund =true
        
        this.editor_id = row.id
        getpaymentcollectionget(row.id).then(res =>{
         
          const response = res.data.pc
          console.log('row',response);
          this.refundview={
            name:response.custom_name,
            type:response.payment_collection_type,
            totalamount:response.payment_collection_sumamt,
            software:response.software_sumamt,
            hardware:response.hardware_sumamt,
            date:response.payment_collection_date,
            role:response.payment_collection_role,
            url:response.url,
            remark:response.remark,
          }
        })
        
      },
      // 查看确认
      view_click(){
        const data={
          id:this.editor_id,
          custom_name:this.refundview.name,
          payment_collection_type:this.refundview.type,
          payment_collection_sumamt:this.refundview.totalamount,
          software_sumamt:this.refundview.software,
          hardware_sumamt:this.refundview.hardware,
          payment_collection_date:this.refundview.date,
          payment_collection_role:this.refundview.role,
          url:this.refundview.url,
          remark:this.refundview.remark,
        }
        getpaymentcollectionedit(data).then(res =>{
          console.log('getpaymentcollectionedit',res);
        })
        this.refund =false
      },
      // 新增确认
      refund_click(){
        const data={
          custom_id:this.Message.id,
          custom_name:this.refundobj.name,
          payment_collection_type:this.refundobj.type,
          payment_collection_sumamt:this.refundobj.totalamount,
          software_sumamt:this.refundobj.software,
          hardware_sumamt:this.refundobj.hardware,
          payment_collection_date:this.refundobj.date,
          payment_collection_role:this.refundobj.role,
          url:this.refundobj.url,
          remark:this.refundobj.remark,
        }
        getpaymentcollectioncreate(data).then(res =>{
          console.log('res',res);
          this.getlist()
        })
        this.newrefund = false
      },
      // 数据源
      getlist(){
        getpaymentcollectionlist().then(res =>{
          const list = res.data.list
          const table = []
          list.forEach((item,index) => {
            if(item.is_check==1){
              const arr= {
                id:item.id,
                index:index+1,
                name:item.custom_name,
                xinxi:this.getoptions(item.payment_collection_type),
                jiner:item.payment_collection_sumamt,
                time:item.payment_collection_date,
                caozuo:item.payment_collection_role,
                remark:item.remark,
                examine:item.check_person,
                
              }
              table.unshift(arr)
            }
          });
          this.tableData = table
          console.log('4444444444list回款',res);
        })
      },
      // 转换时间
      getTime(val) {
        if(val < 0){
          return ""
        }
        if(val == ""){
          return ""
        }else {
          return uParseTime(val, '{y}-{m}-{d}')
        }
      },
      // 回款类型
      getoptions(val){
        const options = this.options
        for(let i in options){
          if(val == options[i].value){
            return options[i].label
          }
        }
      },
      // 上传文件之前
      beforeAvatarUpload(file) {
        console.log('2222222',file);
        // const isJPG = file.type === "image/jpeg";
        var fileType = file.type
        const isJPG = fileType.indexOf('image') > -1
        const isLt2M = file.size / 1024 / 1024 < 5

        if (!isJPG) {
          this.$message.error('只能上传图片')
          return false
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 5MB!')
          return false
        }

        var date = new Date()
        var ext = getFileExtension(file.name)
        var key =
          date.getFullYear() +
          '/' +
          (date.getMonth() + 1) +
          '/' +
          date.getDate() +
          '/' +
          date.getHours() +
          '/' +
          date.getMinutes() +
          '/' +
          date.getSeconds() +
          '/' +
          '_s_' +
          file.uid +
          '.' +
          ext
        // this.loading = this.$loading({
        //   lock: true,
        //   text: '上传中...',
        //   spinner: 'el-icon-loading',
        //   background: 'rgba(0, 0, 0, 0.7)'
        // })

        const _self = this
        const token = "-l_Pcc1YJs0gh3w0YwN2uoaZO_5fY5J9SIYnSjg0:Z_J9cGOKTCqsWQlaftl4o8DCRNM=:eyJzY29wZSI6InN5aGNsdWItc3RvcmFnZSIsImRlYWRsaW5lIjoxNzE1MzMwNjgxLCJyZXR1cm5Cb2R5Ijoie1widXJsXCI6XCIkKGtleSlcIixcImhhc2hcIjpcIiQoZXRhZylcIixcInN0YXRlXCI6XCJTVUNDRVNTXCJ9In0="
              _self._data.uploadData.token = token
              _self._data.uploadData.key = key
//         return new Promise((resolve, reject) => {
//           getToken()
//             .then(response => {
//               console.log('response',response);
//               // const token = response.data.data.uptoken
//               const token = "-l_Pcc1YJs0gh3w0YwN2uoaZO_5fY5J9SIYnSjg0:Z_J9cGOKTCqsWQlaftl4o8DCRNM=:eyJzY29wZSI6InN5aGNsdWItc3RvcmFnZSIsImRlYWRsaW5lIjoxNzE1MzMwNjgxLCJyZXR1cm5Cb2R5Ijoie1widXJsXCI6XCIkKGtleSlcIixcImhhc2hcIjpcIiQoZXRhZylcIixcInN0YXRlXCI6XCJTVUNDRVNTXCJ9In0="
//               _self._data.uploadData.token = token
//               _self._data.uploadData.key = key
//               resolve(true)
//             })
//             .catch(err => {
//               console.log('66666',err)
//               reject(false)
//               // this.loading.close()
//             })
//         })
      },
      beforeAvatarUpload2(file){
        var fileType = file.type
        const isJPG = fileType.indexOf('image') > -1
        const isLt2M = file.size / 1024 / 1024 < 5

        if (!isJPG) {
          this.$message.error('只能上传图片')
          return false
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 5MB!')
          return false
        }
        var date = new Date()
        var ext = getFileExtension(file.name)
        var key =
          date.getFullYear() +
          '/' +
          (date.getMonth() + 1) +
          '/' +
          date.getDate() +
          '/' +
          date.getHours() +
          '/' +
          date.getMinutes() +
          '/' +
          date.getSeconds() +
          '/' +
          '_s_' +
          file.uid +
          '.' +
          ext
        // this.loading = this.$loading({
        //   lock: true,
        //   text: '上传中...',
        //   spinner: 'el-icon-loading',
        //   background: 'rgba(0, 0, 0, 0.7)'
        // })

        const _self = this
        const token = "-l_Pcc1YJs0gh3w0YwN2uoaZO_5fY5J9SIYnSjg0:5EWyZLzC-l8bN8dTcI1n8bE1sgM=:eyJzY29wZSI6InN5aGNsdWItc3RvcmFnZSIsImRlYWRsaW5lIjoxNzE1MzEwODgxLCJyZXR1cm5Cb2R5Ijoie1widXJsXCI6XCIkKGtleSlcIixcImhhc2hcIjpcIiQoZXRhZylcIixcInN0YXRlXCI6XCJTVUNDRVNTXCJ9In0="
              _self._data.uploadData.token = token
              _self._data.uploadData.key = key
        // return new Promise((resolve, reject) => {
        //   getToken()
        //     .then(response => {
        //       console.log('response',response);
        //       // const token = response.data.data.uptoken
        //       // "uptoken": "-l_Pcc1YJs0gh3w0YwN2uoaZO_5fY5J9SIYnSjg0:LItIfUSj1c25tr-6q323D5KPgqc=:eyJzY29wZSI6InN5aGNsdWItc3RvcmFnZSIsImRlYWRsaW5lIjoxNzE1MjU4MDgxLCJyZXR1cm5Cb2R5Ijoie1widXJsXCI6XCIkKGtleSlcIixcImhhc2hcIjpcIiQoZXRhZylcIixcInN0YXRlXCI6XCJTVUNDRVNTXCJ9In0="
        //       const token = "-l_Pcc1YJs0gh3w0YwN2uoaZO_5fY5J9SIYnSjg0:5EWyZLzC-l8bN8dTcI1n8bE1sgM=:eyJzY29wZSI6InN5aGNsdWItc3RvcmFnZSIsImRlYWRsaW5lIjoxNzE1MzEwODgxLCJyZXR1cm5Cb2R5Ijoie1widXJsXCI6XCIkKGtleSlcIixcImhhc2hcIjpcIiQoZXRhZylcIixcInN0YXRlXCI6XCJTVUNDRVNTXCJ9In0="              
        //       _self._data.uploadData.token = token
        //       _self._data.uploadData.key = key
        //       resolve(true)
        //     })
        //     .catch(err => {
        //       console.log('66666',err)
        //       reject(false)
        //       // this.loading.close()
        //     })
        // })
      },
      // 上传成功
      handleAvatarSuccess(res, file) {
        // this.refundobj.url =URL.createObjectURL(file.raw);
        this.refundobj.url = this.qiniuDomain + res.url + '?imageView2/2/w/2000/h/2000/q/90',
        console.log('111111',file,res);
        // this.refundobj.url.push({
        //   img_url: this.qiniuDomain + res.url + '?imageView2/2/w/2000/h/2000/q/90',
        //   desc: '',
        //   id: 0
        // })
        // this.loading.close()
      },
      handleAvatarSuccess2(res, file) {
        // this.refundview.url =URL.createObjectURL(file.raw);
        this.refundview.url = this.qiniuDomain + res.url + '?imageView2/2/w/2000/h/2000/q/90',
        console.log('111111',file,this.refundview.url);
        // this.refundobj.url.push({
        //   img_url: this.qiniuDomain + res.url + '?imageView2/2/w/2000/h/2000/q/90',
        //   desc: '',
        //   id: 0
        // })
        // this.loading.close()
      },
      // 上传失败
      handleAvatarError(err, file, fileList) {
        console.log('333333',file, fileList,err);
        // this.$message.error(err)
        // this.loading.close()
        // return false
      },
      handleRemove(file) {
        // console.log(file);
      },
      
    }
  }
</script>
<style lang="scss" scoped>
  .context{
    padding: 20px 0;
    .xinxi{
      display: flex;
      margin-bottom: 15px;
    }
  }

</style>