<template>
  <div>
    <el-form
      :model="maForm"
      ref="maForm"
      size="small"
      :rules="rules"
      :inline="true"
      label-width="120px"
    >
      <el-form-item label="租赁单位" prop="unitId">
        <el-select
          v-model="maForm.unitId"
          placeholder="租赁单位"
          clearable
          filterable
          style="width: 240px"
          disabled
        >
          <el-option
            v-for="item in uniteList"
            :key="item.unitId"
            :label="item.unitName"
            :value="item.unitId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="租赁工程" prop="projectId">
        <el-select
          v-model="maForm.projectId"
          placeholder="租赁工程"
          clearable
          filterable
          style="width: 240px"
          disabled
        >
          <el-option
            v-for="item in projectList"
            :key="item.proId"
            :label="item.proName"
            :value="item.proId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="领料人" prop="leasePerson">
        <el-input
          v-model="maForm.leasePerson"
          placeholder="请输入领料人"
          clearable
          maxlength="50"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
          disabled
        />
      </el-form-item>

      <el-form-item label="联系电话" prop="phone">
        <el-input
          v-model="maForm.phone"
          placeholder="请输入联系电话"
          clearable
          maxlength="50"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
          disabled
        />
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          >导出</el-button
        >
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="equipmentList">
      <el-table-column label="序号" align="center" type="index" />
      <el-table-column
        label="类型名称"
        align="center"
        prop="maTypeName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="规格型号"
        align="center"
        prop="typeName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="计量单位"
        align="center"
        prop="unitName"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        label="当前库存"
        align="center"
        prop="purchaseNum"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        label="预领数量"
        align="center"
        prop="purchaseTaxPrice"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="出库数量"
        align="center"
        prop="purchasePrice"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="备注"
        align="center"
        prop="remark"
        :show-overflow-tooltip="true"
      />
    </el-table>
  </div>
</template>

<script>
import { getPurchaseCheckInfo } from "@/api/purchase/goodsArrived";
import {
  uploadPurchaseFile,
  getPurchaseFileList,
} from "@/api/purchase/goodsAccept";
import { getToken } from "@/utils/auth";
export default {
  name: "QueryTools",
  dicts: ["purchase_task_status"],
  components: {
    // UploadImg,
  },
  props: {
    isView: {
      type: Boolean,
      default: () => {
        return false;
      },
    },
    queryTaskId: {
      type: [String, Number],
      default: () => {
        return "";
      },
    },
    queryId: {
      type: [String, Number],
      default: () => {
        return "";
      },
    },
  },
  data() {
    return {
      fixCodeList: ["否", "是"],
      //任务ID
      taskId: "",
      // 遮罩层
      loading: true,
      //任务数据
      taskInfo: {},
      // 表格数据
      equipmentList: [],
      // 是否显示弹出层
      open: false,
      rowData: {},
      maForm: {},
      fileDataList: [
        { dictLabel: "合格证", fileType: "0", name: "", url: "" },
        { dictLabel: "型式试验报告", fileType: "1", name: "", url: "" },
        { dictLabel: "出厂检测报告", fileType: "2", name: "", url: "" },
        { dictLabel: "第三方监测报告", fileType: "3", name: "", url: "" },
        { dictLabel: "其他报告", fileType: "4", name: "", url: "" },
      ],
      //上传
      upload: {
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/file/upload",
      },
      //图片查看弹窗
      dialogImageUrl: "",
      dialogVisible: false,
    };
  },
  computed: {},
  mounted() {
    this.taskId = this.queryTaskId;
    this.id = this.queryId;
    this.getTaskInfo();
  },
  methods: {
    //获取任务详情-列表数据
    getTaskInfo() {
      this.loading = true;
      getPurchaseCheckInfo({ taskId: this.taskId, id: this.id }).then(
        (response) => {
          this.taskInfo = response.data;
          this.equipmentList = response.data.purchaseCheckDetailsList;
          this.equipmentList.forEach((item) => {
            item.fixCode = this.fixCodeList[Number(item.fixCode)];
          });
          this.loading = false;
        }
      );
    },
    //文件管理
    openFileDialog(row) {
      this.rowData = row;
      this.fileDataList = [
        { dictLabel: "合格证", fileType: "0", name: "", url: "" },
        { dictLabel: "型式试验报告", fileType: "1", name: "", url: "" },
        { dictLabel: "出厂检测报告", fileType: "2", name: "", url: "" },
        { dictLabel: "第三方监测报告", fileType: "3", name: "", url: "" },
        { dictLabel: "其他报告", fileType: "4", name: "", url: "" },
      ];
      this.getFileData();
      this.open = true;
    },
    getFileData() {
      let param = {
        modelId: this.rowData.typeId,
        taskType: 0,
        taskId: this.rowData.taskId,
      };
      getPurchaseFileList(param)
        .then((response) => {
          if (response.rows.length > 0) {
            response.rows.forEach((item) => {
              let index = this.fileDataList.findIndex(
                (v) => v.fileType == item.fileType
              );
              this.fileDataList[index].name = item.name;
              this.fileDataList[index].url = item.url;
            });
          }
        })
        .catch(() => {});
    },
    beforeFileUpload(row) {
      this.rowData.fileType = row.fileType;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      if (response.code == 200) {
        if (this.taskId == "") {
          //新增逻辑
          // console.log(response)
          // console.log(this.rowData)
          // console.log(this.rowData.bmFileInfos)
          let obj = {
            taskId: this.taskId,
            taskType: "0",
            name: response.data.name,
            url: response.data.url,
            modelId: this.rowData.typeId,
            fileType: this.rowData.fileType,
            //   "dictLabel": this.rowData.dictLabel,
          };
          //根据文件上传返回更新文件管理弹窗内容
          let index = this.fileDataList.findIndex(
            (v) => v.fileType == this.rowData.fileType
          );
          this.fileDataList[index].name = response.data.name;
          this.fileDataList[index].url = response.data.url;
          //判断当前上传的文件是否已上传过-再往机具类型数据中插入
          if (this.rowData.bmFileInfos.length > 0) {
            let index2 = this.rowData.bmFileInfos.findIndex(
              (v) => v.fileType == this.rowData.fileType
            );
            if (index2 > -1) {
              //相同类型文件重复上传-替换
              this.rowData.bmFileInfos.splice(index2, 0, obj);
            } else {
              //不存在相同类型文件-添加
              this.rowData.bmFileInfos.push(obj);
            }
          } else {
            this.rowData.bmFileInfos.push(obj);
          }
        } else {
          //编辑逻辑
          let param = {
            taskId: this.taskId,
            taskType: "0",
            name: response.data.name,
            url: response.data.url,
            modelId: this.rowData.typeId,
            fileType: this.rowData.fileType,
            //   "dictLabel": this.rowData.dictLabel,
          };
          uploadPurchaseFile(param)
            .then((response) => {
              this.$modal.msgSuccess("上传成功");
              this.getFileData();
            })
            .catch(() => {
              this.$modal.msgError("上传失败");
            });
        }
      }
    },
    //图片查看
    picturePreview(file) {
      this.dialogImageUrl = file.url;
      const parts = file.name.split(".");
      const extension = parts.pop();
      if (extension === "doc" || extension === "docx" || extension === "pdf") {
        const windowName = file.name;
        window.open(file.url, windowName);
      } else {
        this.dialogVisible = true;
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "/material/purchase_check_info/exportDetails",
        { taskId: this.taskId },
        `新购到货详情_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style lang="scss" scoped>
::v-deep.el-table .fixed-width .el-button--mini {
  width: 60px !important;
  margin-bottom: 10px;
}
//隐藏图片上传框的css
::v-deep.disabled {
  .el-upload--picture-card {
    display: none;
  }
}
.custom-textarea {
  width: 300px;
  height: 100px;
}
.accept-img {
  color: #409eff;

  .a-two {
    margin-left: 20px;
  }
}

.left-tip {
  font-size: 16px;
  letter-spacing: 1px;
}
</style>
