<template>
  <!-- 新增工机具 -->
  <div>
        <div class="info" style="margin-top: 10px; display: flex; flex-wrap: wrap;margin-left: 10px;" >
          <div class="item" style="width: 25%;flex-shrink: 0;margin-bottom: 5px;font-size: 14px;">
            <span>领用单位：</span>
            {{ leaseApplyData.leaseUnit }}
          </div>
          <div class="item" style="width: 25%;flex-shrink: 0;margin-bottom: 5px;font-size: 14px;">
            <span>领用工程：</span>
            {{ leaseApplyData.leaseProject }}
          </div>
          <div class="item" style="width: 25%;flex-shrink: 0;margin-bottom: 5px;font-size: 14px;">
            <span>联系人：</span>
            {{ leaseApplyData.leasePerson }}
          </div>
          <div class="item" style="width: 25%;flex-shrink: 0;margin-bottom: 5px;font-size: 14px;">
            <span>联系电话：</span>
            {{ leaseApplyData.phone }}
          </div>
        </div>

        <div class="info" style="margin-top: 10px;margin-bottom:10px; display: flex; flex-wrap: wrap;margin-left: 10px" >
          <div class="item" style="width: 25%;flex-shrink: 0;margin-bottom: 5px;font-size: 14px;">
            <span>供货时间：</span>
            {{ leaseApplyData.supplierTime }}
          </div>
          <div class="item" style="width: 25%;flex-shrink: 0;margin-bottom: 5px;font-size: 14px;">
            <span>供货地点：</span>
            {{ leaseApplyData.supplierPlace }}
          </div>
          <div class="item" style="width: 25%;flex-shrink: 0;margin-bottom: 5px;font-size: 14px;">
            <span>业务单号：</span>
            {{ leaseApplyData.code }}
          </div>
        </div>

    <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-col :span="1.5">
                <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-if="isEdit">导出</el-button>
            </el-col> -->
    </el-row>

    <el-table
      v-loading="loading"
      :data="leaseList"

    >
      <el-table-column align="center" label="序号" type="index" width="55" />
      <el-table-column
        align="center"
        label="类型名称"
        prop="maTypeName"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        align="center"
        label="规格型号"
        prop="typeName"
        show-overflow-tooltip
      />
      <el-table-column align="center" label="计量单位" prop="unitName" />
      <el-table-column align="center" label="需求数量" prop="preNum" />
      <el-table-column align="center" label="当前库存" prop="storageNum" />
      <el-table-column align="center" label="已发布数量" prop="publishNum" :show-overflow-tooltip="true"/>
      <el-table-column align="center" label="已供应数量" prop="outNum" :show-overflow-tooltip="true"/>
      <el-table-column align="center" label="待发布数量" prop="pendingNum" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span :style="{ color: scope.row.pendingNum!== 0? 'red' : 'green' }">
            {{ scope.row.pendingNum }}
          </span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="操作" align="center">
        <template slot-scope="scope" >
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-search"
            @click="handleSend(scope.row)"
            > 查看</el-button
          >
        </template>
      </el-table-column> -->
    </el-table>

      <!-- 发料弹窗-->
    <!-- <el-dialog
      :title="title"
      :visible.sync="openView"
      width="600px"
      append-to-body
    >
      <el-form ref="maForm" :model="maForm" :rules="rules" label-width="120px">
        <el-row :gutter="24">
          <el-col :span="16">
            <el-form-item label="领用单位" prop="unitId">
              <treeselect
                v-model="maForm.unitId"
                :options="uniteList"
                :normalizer="normalizer"
                :show-count="true"
                style="width: 240px"
                :disable-branch-nodes="true"
                noChildrenText="没有数据了"
                noOptionsText="没有数据"
                noResultsText="没有搜索结果"
                placeholder="请选择领用单位"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-button
              type="success"
              @click="handleSign"
              >去签协议</el-button>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="16">
            <el-form-item label="领用数量" prop="num">
              <el-input
                v-model="maForm.num"
                type="number"
                min="1"
                placeholder="请输入领用数量"
                maxlength="10"
                style="width: 240px"
                @change="handleChange"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <span style="display: inline-block; vertical-align: middle; line-height: 32px;">剩余需求量: {{numTemp}}</span>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="20">
            <el-form-item label="领料人" prop="leasePerson">
              <el-input
                v-model="maForm.leasePerson"
                placeholder="请输入领料人"
                maxlength="10"
              >
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="20">
            <el-form-item label="联系电话" prop="phone">
              <el-input
                v-model="maForm.phone"
                placeholder="请输入联系电话"
                maxlength="11"
              >
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer"  style="text-align: right;">

        <el-button @click="cancel" >取 消</el-button>
      </div>
    </el-dialog> -->
  </div>
</template>

<script>
import {
  getApplyInfo,
} from "@/api/leasePublish/index";
import { getListUnite  } from '@/api/lease/apply'
import { getToken } from "@/utils/auth";
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
export default {
  name: "QueryTools",
  components: { Treeselect },

  props: {
    isView: {
      type: Boolean,
      default: () => {
        return false;
      },
    },
    queryTaskId: {
      type: [String, Number],
      default: () => {
        return "";
      },
    },
    queryId: {
      type: [String, Number],
      default: () => {
        return "";
      },
    },
  },
  data() {
    return {
      taskId: "",
      // isEdit: false,
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,

      uniteList: [],
      // 角色表格数据
      leaseList: [],
      // 弹出层标题
      title: "",
      rowData: {},
      // 查询参数
      queryParams: {
        equipmentId: undefined,
        productionTime: "",
      },
      maForm: {
        unitId: undefined,
        projectId: undefined,
        arrivalTime: "",
        purchaser: "",
        remark: "",
        purchaseNumber: "",
        bmFileInfos: [],
      },
      openView: false,
      // 表单参数
      form: {},
      //剩余需求量
      numTemp: 0,
    leaseApplyData: {},  // 申请详情数据
      // 表单校验
      rules: {
        unitId: [
          {
            required: false,
            message: '请选择领用单位',
            trigger: 'blur'
          }
        ],
        num: [
          {
            required: false,
            message: '请输入领料数量',
            trigger: 'blur'
          }
        ],
        leasonPerson: [
          {
            required: true,
            message: '请输入领料人',
            trigger: 'blur'
          }
        ],
        phone: [
          { required: false, message: '联系电话不能为空', trigger: 'blur' },
          {
            pattern: /^(?:(?:\+|00)86)?1[3-9]\d{9}$/,
            message: '请输入正确的手机号码',
            trigger: 'blur'
          }
        ],

      },

    };
  },
  computed: {
  },
  mounted() {
    // this.getUnit();
    console.log("isView", this.isView);
    this.taskId = this.queryTaskId;
    this.id = this.queryId;
    this.getTaskInfo();

  },
  methods: {
    /** 转换菜单数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children
      }
    },

    // /** 选择单位 */
    // getUnit(){
    //     getListUnite({ projectId: null }).then(response => {
    //       this.uniteList = response.data
    //     })
    // },

    async getTaskInfo() {
      this.loading = true;
      try {
        const response = await getApplyInfo({ id: this.id });
        this.leaseList = response.data.leaseApplyDetailsList;
        this.leaseApplyData = response.data.leaseApplyInfo;

      } catch (error) {
        console.error("获取任务信息失败:", error);
      } finally {
        this.loading = false;
        // this.$forceUpdate(); // 强制刷新组件
      }
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.roleId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },



    /** 导出按钮操作 */
    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/leaseTask/exportPublishDetails",
        { id: this.id },
        `领用申请发布详情_${currentTime}.xlsx`
      );
    },



  },

};
</script>
<style lang="scss" scoped>

.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
}

.el-icon-arrow-down.is-reverse {
  transform: rotateZ(180deg);
}

</style>
