<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="供应商编号" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报价单名称" prop="offerName">
        <el-input
          v-model="queryParams.offerName"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="建档日期" prop="jdrq">
        <el-date-picker clearable
          v-model="queryParams.jdrq"
          type="dateTime"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="请选择建档日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['tuya:release:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['tuya:release:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['tuya:release:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['tuya:release:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="releaseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="供应商编号" align="center" prop="code" :show-overflow-tooltip="true"  />
      <el-table-column label="路线编号" align="center" prop="uuid" :show-overflow-tooltip="true"  />
      <el-table-column label="定制编号" align="center" prop="demandUuid" :show-overflow-tooltip="true"  />
      <el-table-column label="建档日期" align="center" prop="jdrq" width="180" :show-overflow-tooltip="true"  />
      <el-table-column label="修改日期" align="center" prop="updateDate" width="180" :show-overflow-tooltip="true" />
      <el-table-column label="价格" align="center" prop="priceAll" :show-overflow-tooltip="true" />
      <el-table-column label="封面图" align="center" prop="titleImg">
        <template slot-scope="scope">
          <image-preview :src="scope.row.titleImg" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="报价单名称" align="center" prop="offerName" :show-overflow-tooltip="true" />
      <el-table-column label="价格明细" align="center" prop="priceDetail" :show-overflow-tooltip="true" />
      <el-table-column label="合同文件" align="center" prop="contractFile" width="90">
        <template slot-scope="scope">
          <el-button
            @click="downloadFile(scope.row)"
            size="small"
          >下载附件</el-button>
        </template>>
      </el-table-column>
      <el-table-column label="价格说明" align="center" prop="priceDetailText" :show-overflow-tooltip="true" />
      <el-table-column label="其他" align="center" prop="other" :show-overflow-tooltip="true" />
      <el-table-column label="单号" align="center" prop="orderNo" :show-overflow-tooltip="true" />
      <el-table-column label="评价分数" align="center" prop="score" :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['tuya:release:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['tuya:release:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改平台报价单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row :gutter="24">
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="供应商编号" prop="code">
                <el-input v-model="form.code" placeholder="请输入" :disabled="true"/>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="路线编号" prop="uuid">
                <el-input v-model="form.uuid" placeholder="请输入" :disabled="true" />
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="定制编号" prop="demandUuid">
                <el-input v-model="form.demandUuid" placeholder="请输入" :disabled="true" />
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="单号" prop="orderNo">
                <el-input v-model="form.orderNo" placeholder="请输入" :disabled="true" />
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="报价单名称" prop="offerName">
                <el-input v-model="form.offerName" placeholder="请输入" />
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="价格" prop="priceAll">
                <el-input-number v-model="form.priceAll" controls-position="right" :min="0"  :precision="2"/>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="价格明细" prop="priceDetail">
                <el-input v-model="form.priceDetail" placeholder="请输入" />
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="价格说明" prop="priceDetailText">
                <el-input v-model="form.priceDetailText" placeholder="请输入" />
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="评价分数" prop="score">
                <el-input-number v-model="form.score" controls-position="right" :min="0"  :precision="2"/>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="合同文件" prop="contractFile">
                <el-upload
                  class="upload-demo"
                  action="#"
                  :auto-upload="false"
                  :on-change="handleupload"
                  :on-preview="handlePreview"
                  :on-remove="handleRemoveFile"
                  :on-exceed="handleExceed"
                  multiple
                  :limit="1"
                  :file-list="fileList2"
                  :show-file-list="true"
                  accept=".doc, .xls, .ppt, .txt, .pdf, .xlsx"
                >
                  <el-button size="small" type="primary">点击上传</el-button>
                  <div slot="tip" class="el-upload__tip">只能上传doc/xls/ppt/txt/pdf文件，且不超过5MB</div>
                </el-upload>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="封面图" prop="titleImg">
                <div class="update-Img">
                  <el-upload
                    action="#"
                    list-type="picture-card"
                    :limit="1"
                    :auto-upload="false"
                    :on-change="uploadImage"
                    :on-remove="handeRemove"
                    :on-exceed="handleExceed"
                    :on-preview="handlePictureCardPreview"
                    :file-list="fileList"
                    accept=".jpg, .jpeg, .gif, .webp, .png"
                  >
                    <i class="el-icon-plus"></i>
                  </el-upload>
                  <el-dialog :visible.sync="dialogVisible">
                    <img width="100%" v-if="imageUrl" :src="imageUrl" alt="">
                  </el-dialog>
                </div>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="24">
            <div class="grid-content bg-purple">
              <el-form-item label="其他" prop="other">
                <el-input v-model="form.other" type="textarea" placeholder="请输入" />
              </el-form-item>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRelease, getRelease, delRelease, addRelease, updateRelease } from "@/api/tuya/supplierRelease";
import {uploadAvatar} from "@/api/tuya/stiesinfo";

export default {
  name: "Release",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 平台报价单表格数据
      releaseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        uuid: null,
        demandUuid: null,
        jdrq: null,
        updateDate: null,
        priceAll: null,
        titleImg: null,
        offerName: null,
        priceDetail: null,
        contractFile: null,
        priceDetailText: null,
        other: null,
        orderNo: null,
        score: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      fileList: [],
      fileList2: [],
      //图片地址
      imageUrl: "",
      dialogVisible: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 上传图片
    uploadImage(file) {
      const isJPG = file.raw.type === "image/jpg";
      const isJPEG = file.raw.type === "image/jpeg";
      const isPNG = file.raw.type === "image/png";
      const isGIF = file.raw.type === "image/gif";
      const isBMP = file.raw.type === "image/bmp";
      const isLt2M = file.size / 1024 / 1024 < 5;
      if (!isJPG && !isJPEG && !isPNG && !isGIF && !isBMP) {
        this.$message.error("只能上传图片格式为jpg,jpeg,png,gif,bmp！");
      } else if (!isLt2M) {
        this.$message.error('图片大小不能超过 5MB!');
      } else {
        let fd = new FormData()
        fd.append('file', file.raw)
        uploadAvatar(fd).then(response => {
          this.form.titleImg = response.data;
        });
      }
    },
    //图片预览
    handlePictureCardPreview(file) {
      this.imageUrl = file.url;
      this.dialogVisible = true;
    },
    // 文件个数超出
    handleExceed() {
      this.$modal.msgError(`上传图片数量不能超过1个!`);
    },
    handeRemove(file) {
      this.form.titleImg = null;
    },
    /** 查询平台报价单列表 */
    getList() {
      this.loading = true;
      listRelease(this.queryParams).then(response => {
        this.releaseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        code: null,
        uuid: null,
        demandUuid: null,
        jdrq: null,
        updateDate: null,
        priceAll: null,
        titleImg: null,
        offerName: null,
        priceDetail: null,
        contractFile: null,
        priceDetailText: null,
        other: null,
        orderNo: null,
        score: null
      };
      this.fileList = [];
      this.fileList2 = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加平台报价单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRelease(id).then(response => {
        this.form = response.data;
        if (response.data.titleImg != '' && response.data.titleImg != null) {
          this.fileList.push({url: response.data.titleImg})
        }
        if (response.data.contractFile != '' && response.data.contractFile != null) {
          let name = response.data.contractFile.substring(response.data.contractFile.lastIndexOf("/")+1);
          this.fileList2.push({name: name,url: response.data.contractFile})
        }
        this.open = true;
        this.title = "修改平台报价单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRelease(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRelease(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除平台报价单编号为"' + ids + '"的数据项？').then(function() {
        return delRelease(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('tuya/supplierRelease/export', {
        ...this.queryParams
      }, `release_${new Date().getTime()}.xlsx`)
    },
    downloadFile(row){
      console.log(row)
      window.open(row.contractFile, '_self');
    },
    handleupload(file){
      const isDOC = file.raw.type === "application/msword";
      const isXLS = file.raw.type === "application/vnd.ms-excel";
      const isPPT = file.raw.type === "application/vnd.ms-powerpoint";
      const isTXT = file.raw.type === "text/plain";
      const isPDF = file.raw.type === "application/pdf";
      const isLt2M = file.size / 1024 / 1024 < 5;
      if (!isDOC && !isXLS && !isPPT && !isTXT && !isPDF) {
        this.$message.error("只能上传图片格式为doc,xls,ppt,txt,pdf！");
      } else if (!isLt2M) {
        this.$message.error('图片大小不能超过 5MB!');
      } else {
        let fd = new FormData()
        fd.append('file', file.raw)
        uploadAvatar(fd).then(response => {
          this.form.contractFile = response.data;
        });
      }
    },
    handlePreview(){

    },
    handleRemoveFile(file){
      this.form.contractFile = '';
    }
  }
};
</script>
