<!--
 * @Description: 详情弹框
 * @Author: Sugar.
 * @Date: 2023-11-24 13:55:00
 * @LastEditors: gcz
 * @LastEditTime: 2025-03-20 15:37:39
 * @FilePath: \great_webui\src\views\order\groupBuyingMr\dialog\details.vue
 * @Copyright: Copyright (c) 2016~2023 by Sugar., All Rights Reserved.
-->
<template>
  <el-dialog :title="title" :visible.sync="open" width="500" append-to-body :close-on-click-modal="false"
    @close="cancel">
    <div class="dialog" v-if="form">
      <div v-loading="loading" element-loading-text="加载详情中...">
        <!-- 对公支付信息 -->
        <el-form :model="form" ref="form" size="mini" :rules="rules" label-width="150px">
          <el-form-item label="对公支付凭证上传：" prop="voucherUrl">
            <div style="display: flex;flex-wrap: wrap;">
              <div v-for="(item, index) in form.voucherUrl" :key="index"
                style="width: 100px; height: 100px;margin-top: 5px; position: relative;border: 1px solid #999;border-radius: 5px;margin-right: 20px;">
                <el-image style="width: 100%; height: 100%" :src="item" :preview-src-list="form.voucherUrl">
                </el-image>
                <span @click="handleRemove(index)"
                  style="position: absolute;top: -15px;right: -15px;color: red;font-size: 24px;z-index: 999;cursor: pointer;">
                  <i class="el-icon-error"></i>
                </span>
              </div>
              <div style="width: 100px; height: 100px;margin-top: 5px;" v-if="!form.voucherUrl || form.voucherUrl.length < 15"
                v-loading="actionUrlLoading" element-loading-text="上传中..." element-loading-spinner="el-icon-loading"
                element-loading-background="rgba(0, 0, 0, 0.8)">
                <el-upload class="avatar-uploader" :action="uploadObj.url" :headers="uploadObj.headers"
                  :show-file-list="false" :before-upload="beforeAvatarUpload" :on-success="handleAvatarSuccess"
                  :on-progress="handleAvatarProgress" :disabled="actionUrlLoading" :on-error="handleAvatarError">
                  <i class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
              </div>
            </div>
          </el-form-item>
          <el-form-item label="备注:" prop="remark">
            <el-input v-model="form.remark" type="textarea" placeholder="请输入备注"></el-input>
          </el-form-item>
        </el-form>
      </div>
    </div>


    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="cancel">关闭</el-button>
      <el-button type="primary" @click="updateCorporateFun()" :loading="loading">
        <span v-if="loading">提交中...</span>
        <span v-else>确认修改</span>
      </el-button>
    </span>

  </el-dialog>
</template>

<script>
import { getSelectById, updateCorporateApi } from '@/api/order/groupBuyingMr'
import { getToken } from "@/utils/auth";
export default {
  name: "detailsDia11",
  data() {
    return {
      title: "订单详情",
      model: "EDIT",
      open: false,
      loading: false,
      form: {
        id: undefined
      },
      rules: {
        voucherUrl: [{ required: true, message: "请上传凭证", trigger: ["change","blur"] }],
        remark: [
          { required: true, message: '请输入备注', trigger: ['change','blur' ]}
        ],
      },
      loading_form: false,//  加载表单
      orderType: 'normal', // 订单类型 normal:没有新订单和原订单，new:有新订单，old:有原订单
      uploadObj: {
        url: process.env.VUE_APP_UPLOAD_FILE_API + "/upload/single/minio",
        Headers: { Authorization: "Bearer " + getToken() },
      },
      actionUrlLoading:false,
    };
  },
  methods: {
    /**
     * 打开弹框
     * @date 2023-11-22
     * @param {any} obj
     * @returns {any}
     */
    async openDialog(title, obj, type) {
      this.orderType = type;
      this.title = title
      this.open = true;
      await this.getSelectByIdApi(obj);
      this.$nextTick(() => {
        this.$refs["form"].clearValidate();
      });
    },
    /** 获取详情 */
    async getSelectByIdApi(row) {
      let id = row.id;
      try {
        this.loading = true
        let res = await getSelectById(id)
        this.seatSelectList = res.data.viewersOrderList
        this.resubmit = res.data.resubmit||{};
        this.form = {
          orderId: res.id,
          voucherUrl: [],
          remark: null,
        };
        console.log('this.form',this.form);
        this.form.payOrCredit = null
        if(res.data.corporate){
          this.form.payOrCredit = 1
          this.form.voucherUrl = res.data.corporate.voucherUrl.split(',')||[];
          this.form.remark =  res.data.corporate.remark;
        }
        this.loading = false
      } catch (error) {
        this.loading = false
      }
      
    },
    /** 对公支付凭证修改 */
    async updateCorporateFun() {
      this.$refs["form"].validate(async (valid) => {
        if (valid) {
          try {
            if(this.form.voucherUrl.length==0) {
              this.$message.error("请上传凭证!");
            }
            this.loading = true
            let param = JSON.parse(JSON.stringify(this.form))
            param.voucherUrl = param.voucherUrl
            let res = await updateCorporateApi(param)
            if(res.code == 200) {
              this.$message.success("操作成功!");
              this.loading = false
              this.$emit("getList");
              this.cancel()
            }else {
              this.loading = false
            }
          } catch (error) {
            console.error(error)
            this.loading = false
          }
        }
      })
    },
    /**
     * 关闭弹框
     * @date 2023-11-22
     * @returns {any}
     */
    cancel() {
      this.open = false;
    },
    /**  上传图片 单张  */
    handleAvatarSuccess(response, file, fileList) {
      console.log("res, file",response, file, fileList)
      this.actionUrlLoading = false
      if(response.code == 200) {
        this.form.voucherUrl.push(response.data.url)
      }
      this.$refs.form.validateField('voucherUrl')
    },
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 <= 100;
      let testmsg = file.name.substring(file.name.lastIndexOf('.')+1)
      let typeList = ['png','jepg','jpg','gif']
      const isJPG = typeList.includes(testmsg);
      if (!isJPG) {
        this.$message.error(`上传图片图片只能是 ${typeList} 格式!`);
      }
      if (!isLt2M) {
        this.$message.error('上传图片图片大小不能超过 100MB!');
      }
      return isJPG && isLt2M;
    },
    handleAvatarProgress(){
      this.actionUrlLoading = true
    },
    handleAvatarError() {
      this.actionUrlLoading = false
    },
    handleRemove(index) {
      this.form.voucherUrl.splice(index,1)
      this.$refs.form.validateField('voucherUrl')
    },
  },
};
</script>

<style lang="scss" scoped>
.dialog {
  width: 100%;
}
.dialog {
  padding: 0 30px;
  .upload-btn {
    width: 100px;
    height: 100px;
    background-color: #fbfdff;
    border: dashed 1px #c0ccda;
    border-radius: 5px;
    i {
      font-size: 30px;
      margin-top: 20px;
    }
    &-text {
      margin-top: -10px;
    }
  }
  .avatar {
    cursor: pointer;
  }
  .title-class{
    font-size: 16px;
    font-weight: bold;
    color: black;
    margin-bottom: 20px;
    margin-top: 20px;
  }
  .item-class{
    margin-bottom: 20px;
  }
}

.voucher-list{
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 24rpx;
}
.pay-type-remark{
  margin:14px 0;
}

.dialog-bbb {
  width: 100%;
  display: flex;
  --widdd: 700px;

  >div:first-child {
    width: var(--widdd);
    flex-shrink: 0;
    overflow-y: auto;
    padding: 0 0 10px 0;
    margin-right: 10px;
  }

  .dialog-bbb_2 {
    width: calc(100% - var(--widdd));
    height: 100%;
  }
}

</style>
