<template>
  <div>
    <el-form
      :model="maForm"
      ref="maForm"
      size="small"
      :inline="true"
      label-width="120px"
    >
      <el-form-item label="租赁单位" prop="unitId">
        <el-cascader
          v-model="unitId"
          :show-all-levels="false"
          :options="uniteList"
          :props="selectTreeProps"
          filterable
          clearable
          collapse-tags
          disabled
          placeholder="请选择租赁单位"
        ></el-cascader>
      </el-form-item>
      <el-form-item label="租赁工程" prop="projectId">
        <el-cascader
          v-model="projectId"
          :show-all-levels="false"
          :options="projectList"
          :props="selectTreeProps"
          filterable
          clearable
          collapse-tags
          disabled
          placeholder="请选择租赁工程"
        ></el-cascader>
      </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="storageNum"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        label="预领数量"
        align="center"
        prop="preNum"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="出库数量"
        align="center"
        prop="alNum"
        :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 { getApplyInfo, getListProject, getListUnite } from "@/api/lease/apply";
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 {
      //任务ID
      taskId: "",
      // 遮罩层
      loading: true,
      //租赁单位
      uniteList: [],
      //租赁工程
      projectList: [],
      // 表格数据
      equipmentList: [],
      unitId: null,
      projectId: null,
      // 是否显示弹出层
      open: false,
      rowData: {},
      maForm: {
        unitId: undefined,
        projectId: undefined,
      },
      selectTreeProps: {
        children: "children",
        label: "name",
        // multiple: false,
        value: "id",
        // multiple: true,
      },
    };
  },
  computed: {},
  mounted() {
    this.taskId = this.queryTaskId;
    this.id = this.queryId;
    this.projectInfoList().then(() => {
      this.getTaskInfo();
    });
  },
  methods: {
    /** 租赁单位和工程-下拉选 */
    async projectInfoList() {
      const uniteResponse = await getListUnite({ id: null });
      this.uniteList = uniteResponse.data;
      const projectResponse = await getListProject({ unitId: null });
      this.projectList = projectResponse.data;
    },
    //获取任务详情-列表数据
    async getTaskInfo() {
      this.loading = true;
      await getApplyInfo(this.id).then((response) => {
        this.maForm = response.data.leaseApplyInfo;
        this.maForm.unitId = response.data.leaseApplyInfo.leaseUnitId;
        this.maForm.projectId = response.data.leaseApplyInfo.leaseProjectId;
        this.unitId = this.treeParentsById(this.uniteList, this.maForm.unitId);
        this.projectId = this.treeParentsById(
          this.projectList,
          this.maForm.projectId
        );
        this.equipmentList = response.data.leaseApplyDetailsList;

        this.loading = false;
      });
    },
    //单位，工程树结构数据获取父
    treeParentsById(list, id) {
      for (let i in list) {
        if (list[i].id == id) {
          //查询到就返回该数组对象的value
          return [list[i].id];
        }
        if (list[i].children) {
          let node = this.treeParentsById(list[i].children, id);
          if (node !== undefined) {
            //查询到把父节把父节点加到数组前面
            node.unshift(list[i].id);
            return node;
          }
        }
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      const formatTime = (date) => {
        const year = date.getFullYear();
        const month = String(date.getMonth() + 1).padStart(2, '0');
        const day = String(date.getDate()).padStart(2, '0');
        const hours = String(date.getHours()).padStart(2, '0');
        const minutes = String(date.getMinutes()).padStart(2, '0');
        const seconds = String(date.getSeconds()).padStart(2, '0');
        return `${year}${month}${day}_${hours}${minutes}${seconds}`;
      };

      const currentTime = formatTime(new Date());
      this.download(
        "/material/lease_apply_info/exportDetails",
        { id: this.id },
        `领料申请详情_${currentTime}.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>
