<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" v-show="showSearch" :inline="true" label-width="80px">
            <el-form-item label="选择单位" prop="unitId">
                <treeselect
                    v-model="queryParams.unitId"
                    :options="unitList" :normalizer="normalizer"
                    :show-count="true" style="width: 240px" :disable-branch-nodes="true"
                    noChildrenText="没有数据了" noOptionsText="没有数据" noResultsText="没有搜索结果"
                    placeholder="请选择单位" @select="unitChange"
                />
            </el-form-item>

            <el-form-item label="选择工程" prop="projectId">
                <treeselect
                    v-model="queryParams.projectId"
                    :options="proList" :normalizer="normalizer"
                    :show-count="true" style="width: 240px" :disable-branch-nodes="true"
                    noChildrenText="没有数据了" noOptionsText="没有数据" noResultsText="没有搜索结果"
                    placeholder="请选择工程" @select="proChange"
                />
            </el-form-item>

            <el-form-item label="协议号" prop="agreementCode">
                <el-input v-model="queryParams.agreementCode" placeholder="协议号" clearable disabled/>
            </el-form-item>

            <el-form-item label="是否匹配" prop="isMatch">
                <el-select v-model="queryParams.isMatch" placeholder="请选择" clearable>
                    <el-option label="已匹配" value="1"></el-option>
                    <el-option label="未匹配" value="0"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="是否结算" prop="isSettlement">
                <el-select v-model="queryParams.isSettlement" placeholder="请选择" clearable>
                    <el-option label="已结算" value="1"></el-option>
                    <el-option label="未结算" value="0"></el-option>
                </el-select>
            </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-button
                type="warning"
                plain
                icon="el-icon-download"
                size="mini"
                @click="handleExport"
                >导出</el-button
        >
      </el-form-item>
    </el-form>
    <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>

    <el-table
      v-loading="loading"
      :data="protocolList"
      ref="multipleTable"
      row-key="id"
      :max-height="650"
    >
      <el-table-column label="序号" align="center" width="80" type="index">
        <template slot-scope="scope">
          <span>{{
            (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="协议号" align="center" prop="agreementCode" sortable show-overflow-tooltip />
      <el-table-column label="分公司" align="center" prop="impUnitName" width="150px":show-overflow-tooltip="true"/>
      <el-table-column label="单位名称" align="center" prop="unitName" />
      <el-table-column label="工程名称" align="center" prop="projectName" />
      <el-table-column label="工程ID" align="center" prop="projectId" sortable show-overflow-tooltip />
      <el-table-column label="工程编号" align="center" prop="projectCode" sortable show-overflow-tooltip />
      <el-table-column label="是否匹配" align="center" prop="isMatch" sortable show-overflow-tooltip >
        <template slot-scope="scope">
          <span v-if="scope.row.isMatch==1" style="color: #67c23a">已匹配</span>
          <span v-if="scope.row.isMatch==0" style="color: #f56c6c">未匹配</span>
        </template>
      </el-table-column>
      <el-table-column label="是否结算" align="center" prop="isSettlement" sortable show-overflow-tooltip >
        <template slot-scope="scope">
          <span v-if="scope.row.isSettlement==1" style="color: #67c23a">已结算</span>
          <span v-if="scope.row.isSettlement==0" style="color: #f56c6c">未结算</span>
        </template>
      </el-table-column>
      <el-table-column label="结算时间" align="center" prop="settlementTime" sortable show-overflow-tooltip />
    </el-table>

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

  </div>
</template>

  <script>
import {
    getProjectList,
    getUnitList,
    getAgreementInfoById,
} from '@/api/back/index.js'
import {getProtocolList, } from "@/api/costPush/costPush";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
    name: "ProtocolMatch",
  data() {
    return {
      // 遮罩层
      loading: false,
      // 显示搜索条件
      showSearch: true,

      // 总条数
      total: 0,
      // 推送协议表格数据
      protocolList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 单位数据
      unitList: [],
      // 工程数据
      proList: [],

      // 查询参数
      queryParams: {
          pageNum: 1,
          pageSize: 10,
          unitId: null,
          projectId: null,
          agreementId: '',
          agreementCode: '',
          isMatch: null,
          isSettlement: null,
      },

    };
  },
  created() {
    this.GetUnitData()
    this.GetProData()
    this.getList();
  },
  components: { Treeselect },
  methods: {
    /** 转换菜单数据结构 */
    normalizer(node) {
        if (node.children && !node.children.length) {
            delete node.children;
        }
        return {
            id: node.id,
            label: node.name,
            children: node.children,
        };
    },
    // 获取 单位 列表数据
    async GetUnitData() {
            const params = {
                // projectId: this.queryParams.projectId /*  */,
            }
            const res = await getUnitList(params)
            this.unitList = res.data;

            this.getAgreementInfo()
    },
    unitChange(val){
        setTimeout(()=>{
            this.queryParams.projectId=null
            this.queryParams.agreementId = null
            this.queryParams.agreementCode = null
            this.GetProData()
        },500)
    },
    // 获取 工程名称 列表数据
    async GetProData() {
            const params = {
                unitId: this.queryParams.unitId,
            }
            const res = await getProjectList(params)
            this.proList = res.data;

            this.getAgreementInfo()
    },
    proChange(val){
        setTimeout(()=>{
            this.GetUnitData()
        },500)
    },

    // 获取 协议id
    async getAgreementInfo() {
      if (this.queryParams.unitId && this.queryParams.projectId) {
          const params = {
              unitId: this.queryParams.unitId,
              projectId: this.queryParams.projectId,
          }
          const res = await getAgreementInfoById(params)
          console.log(res)
          if (!(res.data && res.data.agreementId)) {
              this.$message.error('当前单位和工程无协议！')
              this.queryParams.unitId = null
              this.queryParams.projectId = null
              this.GetUnitData()
              this.GetProData()
          } else {
              this.queryParams.agreementId = res.data.agreementId
              this.queryParams.agreementCode = res.data.agreementCode
          }
      }
    },


    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      getProtocolList(this.queryParams).then((response) => {
        this.protocolList = response.data.rows;
        this.total = response.data.total;
        this.loading = false;
      });
    },

    /** 重置按钮操作 */
    resetQuery() {
        this.queryParams={
            pageNum: 1,
            pageSize: 10,
            isMatch: null,
            isSettlement: null,
            unitId: null,
            projectId: null,
            agreementId: '',
            agreementCode: '',
        }
        this.resetForm('queryForm')
        this.handleQuery()
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

     /** 导出 */
    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/iws_cost_push/export',
              {
                  ...this.queryParams,
              },
              `推送协议匹配记录_${currentTime}.xlsx`,
          )
    },
  },
};
</script>
  <style lang="scss" scoped>
.uploadImg {
  padding-top: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.deviceCode {
  margin-top: 10px;
  padding-bottom: 20px;
  font-size: 18px;
}
::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;
  }
}
</style>
