<!-- 添加/编辑 -->
<template>
  <el-dialog :title="title" :visible.sync="isShow" :append-to-body="true" :close-on-click-modal="false" class="drawer-box" width="800px" @close="handleCancel">
    <el-form ref="form" :model="form" label-width="150px" label-position="top">
      <el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="甲方企业名称">
              <el-input placeholder="请输入" disabled size="small" v-model="tenantName">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="采购预算" prop="productPrice" :rules="[{ message: '请输入', required: true, trigger: 'blur'},{ validator: VerifyFunc.validateMoney }]">
              <el-input placeholder="请输入" size="small" :disabled="id ? true :false" v-model="form.productPrice">
                <div slot="suffix">元</div>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="投标金额" prop="moldCost" :rules="[{ message: '请输入', required: true, trigger: 'blur'},{ validator: VerifyFunc.validateMoney }]">
              <el-input placeholder="请输入" size="small" :disabled="id ? true :false" v-model="form.moldCost">
                <div slot="suffix">元</div>
              </el-input>  
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="采购内容" prop="remark">
              <editor v-model="form.remark" v-if="isShow" :readOnly="id ? true :false" :min-height="192" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="附件" required>
              <UploadFile :isAddSp="isAddSp" :propDisabled="id ? true :false" @onUploadFile="handleUpload" v-if="isShow" :files="fileList" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :loading="btnLoading" @click="handleCancel" size="small">取消</el-button>
      <el-button type="primary" :loading="btnLoading" v-if="!id" size="small" @click="handleSave">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { createQuotationSheetBidding, getPageList } from "@/api/bs/quotationSheetBidding";
import VerifyFunc from '@/utils/verity'
import dayjs from 'dayjs';
import ImageUpload from '@/components/ImageUpload';
import Editor from '@/components/Editor';
export default {
  name: '',
  props: {
    id: [String, Number],
    quotationId: [String, Number],
    isOpen: Boolean,
    tenantName: String,
  },
  components: {
    UploadFile: () => import('@/components/UploadFile'),
    CommonName: () => import('@/components/CommonName'),
    ImageUpload,
    Editor
  },
  watch: {
    isOpen: {
      handler(val) {
        this.isShow = val
        this.title = this.id ? '查看' : '投标'
        this.isAddSp = this.id ? true : false
        if (val) {
          this.handleInitForm()
        }
      },
      deep: true,
      immediate: true
    }
  },
  data() {
    return {
      VerifyFunc,
      isAddSp: false,
      btnLoading: false,
      isShow: false,
      form: {
        productPrice: null,
        moldCost: null,
        remark: null
      },
      fileList: [],
      isDisabled: true,
    }
  },
  // 计算属性
  computed: {
    userInfo() {
      return this.$store.getters.userInfo
    }
  },
  methods: {
    handleUpload(list) {
      this.fileList = list
    },
    handleFileList(fileList) {
      this.fileList = fileList
      this.$set(this.form, 'logo', this.form.logo)
    },
    handleDel(index) {
      this.form.extendList.splice(index, 1)
    },
    handleAdd() {
      this.form.extendList.push({ key: '', name: '' })
    },
    handlePictureCardPreview() {

    },
    handlePayCancel() {
      this.payShow = false
    },
    handleBeginScan() {
      if (!(this.fileList && this.fileList.length)) {
        return this.$fm('请上传物料图片！')
      }
    },
    // id 不存在 新增 否则编辑
    handleInitForm(id = this.id) {
      // 初始化参数
      if (!id) {
        this.form = {
          productPrice: null,
          moldCost: null,
          remark: null
        }
        this.fileList = []
        this.$nextTick(() => {
          this.$refs.form && this.$refs.form.resetFields()
          this.$refs.form && this.$refs.form.clearValidate()
        })
      } else {
        getPageList({
          pageNo: 1,
          pageSize: 10,
          creator: this.userInfo.id,
          quotationId: this.id
        }).then(res => {
          this.form = {
            ...(res.data && res.data.list && res.data.list[0] || {}),
            // logo: res.data.files && res.data.files[0].url,
            // extendList: res.data.extendList && res.data.extendList.length ? res.data.extendList : [{ key: '', name: '' }]
          }
          this.fileList = this.form.files
          this.$nextTick(() => {
            this.$refs.form && this.$refs.form.clearValidate()
          })
        })
      }
    },
    handleClose() {
      this.$emit('onClose')
    },
    handleSave() {
      this.$nextTick(() => {
        this.$refs.form.validate((bool) => {
          if (bool) {
            if (!(this.fileList && this.fileList.length)) {
              this.$modal.msgWarning(`请上传附件！`);
              return
            }
            let form = {
              ...this.form,
              quotationId: this.quotationId,
              files: this.fileList,
              moldCost: Number(this.form.moldCost),
              productPrice: Number(this.form.productPrice),
              companyId: 2,
            }
            this.btnLoading = true
            createQuotationSheetBidding(form).then((res) => {
              this.btnLoading = false
              this.$emit('onConfirm')
              this.$modal.msgSuccess(`新增成功啦！`);
            }).catch(() => {
              this.btnLoading = false
            })
          } else {
            this.$nextTick(() => {
              this.$scErr()
            })
          }
        })
      })
    },
    handleCancel() {
      this.$emit('onCancel')
    },
    handleRemove(file, files) {
      this.fileList = []
      this.form.logoUrl = null
    },
    onUploadChange(file, files) {
      console.log('files..', file.raw);
      if (['image/png', 'image/jpeg', 'image/jpeg'].includes(file.raw.type)) {
        if (file.size / 1024 > 5000) {
          setTimeout(() => {
            this.$fm('文件超过5Mb，请压缩后上传!')
          }, 10)
          files.splice(files.length - 1, 1)
          return
        }
        this.fileList = []
        this.fileList.push(file)
      } else {
        this.$fm('不支持此文件类型，请重试！')
      }
    },
  }
}

</script>
<style lang='scss' scoped>
@import '@/assets/styles/edit/index.scss';
// ::v-deep .el-form-item__error {
//   padding-top: 4px !important;
// }
.pro-box {
  display: flex;
  width: 100%;
  justify-content: flex-start;
  flex-wrap: wrap;
  max-height: 150px;
  overflow-y: auto;
  .item-box {
    display: flex;
    max-width: 40%;
    align-items: center;
    ::v-deep .el-input__inner {
      border: none !important;
    }
    .box-lf {
      ::v-deep .el-input__inner {
        border-right: 1px solid #dcdfe6 !important;
        border-radius: 0 !important;
      }
    }
    // justify-content: ;
  }
  .add-box {
    margin-left: 10px;
  }
}
.pro-item-box {
  position: relative;
  .add-box {
    position: absolute;
    top: 0;
    right: 20px;
  }
}
</style>