<template>
  <div class="project-cost-ledger-container">
    <div class="project-cost-ledger-inner" v-if="!isupload">
      <div class="title_wrap">
        <div class="enterprise_title">
          项目成本台账
        </div>
      </div>
      <div class="content-project">
        <el-form :model="formdata" label-width="82px" label-position="right"  :inline="true" >
          <el-row>
            <el-form-item label="工程名称">
              <el-input v-model="formdata.customerName" placeholder="请输入工程名称" clearable></el-input>
            </el-form-item>
            <el-form-item label="IPM项目编码" label-width="100px">
              <el-input v-model="formdata.customerName" placeholder="请输入IPM项目编码" clearable></el-input>
            </el-form-item>
            <el-form-item label="成本阶段">
              <el-select v-model="formdata.customerClass" placeholder="请选择" :collapse-tags="true" clearable>
                  <el-option v-for="(item,index) in cbStagelist" :label="item.dictLabel" :value="item.dictValue" :key="index"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="项目状态">
              <el-select v-model="formdata.customerClass" placeholder="请选择" :collapse-tags="true" clearable>
                  <el-option v-for="(item,index) in ztStagelist" :label="item.dictLabel" :value="item.dictValue" :key="index"></el-option>
              </el-select>
            </el-form-item>
          </el-row>
          <el-divider></el-divider>
          <el-form-item label="  ">
            <el-button type="primary" size="small" @click="getSearch">查询</el-button>
            <el-button size="small" @click="clears">重置</el-button>
          </el-form-item>
        </el-form>
      </div>

      <div class="table-project">
        <div class="table-item-jf1" v-if="total == 0 && !isSkeleton">
          <img class="item-jf-img" src="@/assets/images/kong.png" alt="">
          <div class="item-jf-titel">暂无数据，点击按钮导入项目成本数据</div>
          <el-button type="primary" class="jf-btn" size="small" @click="addPro">新增项目</el-button>
        </div>
        <skeleton v-if="isSkeleton"></skeleton>
        <div style='padding: 16px' v-if="!isSkeleton&&total>0">
          <div class="total-title">
            <div class="totals">共{{total}}条</div>
            <div class="btns">
              <div class="btn btn_primary h32" @click="addPro">导入项目</div>
              <div class="btn btn_default h32" @click="getcgx">草稿箱 ({{cgxtotal}})</div>
            </div>
          </div>
          <div class="table-item-jf table-item">
            <el-table :data="tableData" :header-cell-style="{ background:'#f0f3fa',color: 'rgba(35,35,35,0.8)'}"
                      v-sticky-header.always="{offsetTop : '-16px',offsetBottom : '10px'}" class="table-item1 fixed-table" border highlight-current-row
                      :header-row-class-name="setHeaderRow" :cell-class-name="setCellClass" :row-class-name="setRowClass" :header-cell-class-name="setCellClass"
                      ref="theOwnerListTable" :row-key="'customerKey'">

              <el-table-column type="index" label="序号" width="60" :resizable="false">
                <template slot-scope="scope">
                  <span>{{(formdata.pageNum - 1) *formdata.pageSize + scope.$index + 1}}</span>
                </template>
              </el-table-column>

              <el-table-column label="工程名称" width="416" :resizable="false">
                <template slot-scope="scope">
                  <div style="display:flex;align-items:center">
                    <!--<el-tooltip placement="top" v-if="scope.row.customerText.length>20">-->
                      <!--<div slot="content">{{scope.row.customerText}}</div>-->
                      <!--<div class="renling">-->
                        <!--<router-link v-if="scope.row.companyId" :to="`/enterprise/${encodeStr(scope.row.companyId)}?path=cooperationRecord&companyName=${scope.row.customerName}`" tag="a"-->
                                     <!--class="wordprimary" v-html="scope.row.customerName"></router-link>-->
                        <!--<span v-else v-html="scope.row.customerName"></span>-->
                      <!--</div>-->
                    <!--</el-tooltip>-->
                    <!--<div class="renling" v-else>-->
                      <!--<router-link v-if="scope.row.companyId" :to="`/enterprise/${encodeStr(scope.row.companyId)}?path=cooperationRecord&companyName=${scope.row.customerName}`" tag="a"-->
                                   <!--class="wordprimary" v-html="scope.row.customerName"></router-link>-->
                      <!--<span v-else v-html="scope.row.customerName"></span>-->
                    <!--</div>-->
                  </div>

                </template>

              </el-table-column>
              <el-table-column label="项目状态" prop="customerCode"  width="90" :resizable="false">
              </el-table-column>

              <el-table-column label="IPM项目编码" min-width="136">
                <template slot-scope="scope">
                  {{scope.row.recommendOrg||"--"}}
                </template>
              </el-table-column>
              <el-table-column label="文件名称" min-width="286" :resizable="false">
                <template slot-scope="scope">
                  {{scope.row.registerRegion||"--"}}
                </template>
              </el-table-column>
              <el-table-column label="成本阶段" prop="customerCode"  width="90" :resizable="false">
              </el-table-column>
              <el-table-column label="上传人" prop="customerCode"  width="90" :resizable="false">
              </el-table-column>

              <el-table-column label="上传时间" min-width="180" :resizable="false">
                <template slot-scope="scope">
                  {{scope.row.approveDate2||"--"}}
                </template>
              </el-table-column>
              <el-table-column label="操作" min-width="256" :resizable="false" fixed="right">
                <template slot-scope="scope">
                  <span class="wordprimary tabs" @click="editpro(scope.row)">修改项目信息</span>
                  <span class="wordprimary tabs" @click="detailpro(scope.row)">查看导入进度</span>
                  <span class="worddel tabs" @click="deleetpro(scope.row)">删除</span>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </div>
        <div class="pagination clearfix" v-show="total>formdata.pageSize">
          <el-pagination background :page-size="formdata.pageSize" :current-page.sync="formdata.pageNum" @current-change="handleCurrentChange" layout="prev, pager, next"
                         :total="total">
          </el-pagination>
        </div>
      </div>
      <el-dialog title="删除项目" :visible.sync="deletevisible" width="480px" custom-class='dialog-supplier'>
        <el-divider></el-divider>
        <div class="protypes">
          <div class="ck" v-if="typenum>1">项目台账共有3个阶段</div>
          <div class="ck" v-else>是否删除此项目？删除后无法返回。</div>
          <div class="ck"><el-checkbox :indeterminate="indeterminate" v-model="checkAll" @change="handleCheckAllChange">删除全部</el-checkbox></div>
          <el-checkbox-group v-model="checkds" @change="handleCheck">
            <div class="ck" v-for="(item,index) in allchecks">
              <el-checkbox :key="index" :label="item.label">{{item.text}}</el-checkbox>
            </div>
          </el-checkbox-group>
        </div>
        <el-divider></el-divider>
        <div style="padding: 16px 20px 24px;text-align: right">
        <el-button size="small" @click="deletevisible = false">取消</el-button>
        <el-button type="primary" size="small" @click="getDelete">确定删除</el-button>
        </div>
      </el-dialog>
      <el-dialog
        class="pro-news"
        :visible.sync="dialogVisible"
        width="480px">
        <div class="poptitle">
          <span>{{protitle}}</span>
        </div>
        <el-form class="popform" label-width="97px" :model="queryParam" :rules="rules" ref="ruleForm">
          <el-form-item label="项目名称" class="row">
            <el-input type="text" v-model="queryParam.projectName" placeholder="请输入完整的项目名称"></el-input>
          </el-form-item>
          <el-form-item label="IPM项目编码" class="row">
            <el-input type="text" placeholder="请输入IPM项目编码" v-model="queryParam.ipmProjectNo">
              <template slot="append"><div class="pro-getbtn" @click="getipmProjectNo">获取数据</div></template>
            </el-input>
          </el-form-item>
          <el-form-item label="文件名称" class="row">
            <el-input type="text" placeholder="请输入文件名称" v-model="queryParam.projectFileName"></el-input>
          </el-form-item>
          <el-form-item label="成本阶段" class="row" prop="cbStage">
            <el-checkbox v-model="ischeck" v-if="isedit" class="checkcb"></el-checkbox>
            <el-select placeholder="请选择成本阶段" v-model="queryParam.cbStage" :disabled="!ischeck">
              <el-option v-for="(item,index) in cbStagelist" :label="item.dictLabel" :value="item.dictValue" :key="index"></el-option>
            </el-select>
          </el-form-item>
        </el-form >
        <div class="popbot">
          <div class="btn btn_cancel h32" @click="dialogVisible = false">取消</div>
          <div class="btn btn_primary h32" v-if="isedit && !ischeck" @click="savepro">保存</div>
          <div class="btn btn_primary h32" v-if="isedit && ischeck" @click="changepro">下一步，导入数据</div>
          <div class="btn btn_primary h32" v-if="!isedit && !ischeck" @click="insertPro" :class="{'btn_disabled':!queryParam.cbStage}" :disabled="!queryParam.cbStage">下一步，导入数据</div>
        </div>
      </el-dialog>

    </div>
    <proupload v-if="isupload" :uploadData="uploadData" :cbStagelist="cbStagelist" :prodetail="prodetail" @closeupload="colsepro"></proupload>

    <el-dialog title="草稿箱" :visible.sync="cgxVisible" width="960px" custom-class='dialog-supplier'>
      <el-divider></el-divider>
      <div style="padding: 24px 20px;">
        <skeleton v-if="cgxSkeleton"></skeleton>
        <div class="table-item-jf table-item" v-if="!cgxSkeleton">
          <el-table :data="cgxlist" :header-cell-style="{ background:'#f0f3fa',color: 'rgba(35,35,35,0.8)'}"
                    v-sticky-header.always="{offsetTop : '-16px',offsetBottom : '10px'}" class="table-item1 fixed-table"
                    border highlight-current-row>

            <!--<el-table-column type="index" label="序号" width="60" :resizable="false">-->
              <!--<template slot-scope="scope">-->
                <!--<span>{{scope.$index + 1}}</span>-->
              <!--</template>-->
            <!--</el-table-column>-->
            <el-table-column label="项目名称" width="451px" >
              <template slot-scope="scope">
                <span>{{scope.row.projectName||'--'}}</span>
              </template>
            </el-table-column>
            <el-table-column label="成本阶段" width="105px">
              <template slot-scope="scope">
                <div v-for="(item,index) in cbStagelist" :key="index+11">
                  <span v-if="scope.row.cbStage == item.dictValue">{{item.dictLabel}}</span>
                </div>
              </template>
            </el-table-column>
            <el-table-column label="更新时间" width="205px" :resizable="false">
              <template slot-scope="scope">
                <span>{{scope.row.updateTime||'--'}}</span>
              </template>
            </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <span class="wordprimary" @click="jxbj(scope.row)">继续编辑</span>
                <span class="worddel" @click="deletecg(scope.row.id)">删除</span>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="pagination clearfix" v-show="total>cgPagesize">
          <el-pagination background :page-size="cgPagesize" :current-page.sync="cgPagenum"
                         @current-change="getCGXlist" layout="prev, pager, next"
                         :total="cgxtotal">
          </el-pagination>
        </div>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import '@/assets/styles/public.scss'
  import {
    addNewStageProject,
    addProject,
    checkProjectCodeExist,
    deleteDraft,
    editProjectInfo,
    getDraftDialogList,getProjectCbStageNotDraft
  } from '@/api/projectCostLedger/index'
  import { getDicts } from '@/api/system/dict/data'
  import proupload from '@/views/projectCostLedger/upload/index'
  import skeleton from '@/views/project/projectList/component/skeleton'

  export default {
  name: "projectCostLedger",
    components:{proupload,skeleton},
  data() {
    return {
      typenum:2,
      formdata:{},
      isSkeleton:false,
      total:10,
      protitle:'',
      dialogVisible:false,
      cbStagelist:[],
      ztStagelist:[],
      //新建项目参数
      queryParam:{
        projectName:'',//项目名称
        ipmProjectNo:'',//	IPM项目编码
        isGetProjectDetail:0,//	是否获取项目详情 0:否、1:是)
        projectFileName:'',//	文件名称
        cbStage:'',//成本阶段
      },
      rules:{
        cbStage:[{ required: true, message: '请选择成本阶段！', trigger: 'blur' },],
      },
      isupload:false,
      tableData:[{}],
      cgxVisible:false,
      cgxSkeleton:false,
      cgxtotal:0,
      cgxlist:[],
      uploadData:{
        cbStage:0,
      },//新增项目返回的数据
      cgPagesize:10,
      cgPagenum:1,
      prodetail:false,
      isedit:false,//是否修改项目
      ischeck:false,//是否可选
      deletevisible:false,
      indeterminate:false,//半选状态
      checkAll:false,
      checkds:[],
      allchecks:[],//可以用的项目阶段
    };
  },
  //可访问data属性
  created() {
    // this.isupload = true
    //成本阶段
    getDicts('pro_cbstage').then(res=>{
      this.cbStagelist = res.data
    })
    //项目文件状态
    getDicts('pro_ztstage').then(res=>{
      this.ztStagelist = res.data
    })
    //获取草稿箱
    this.getCGXlist()
    //获取列表
    this.getlist()
  },
  //计算集
  computed: {

  },
  //方法集
  methods: {
    getlist(){

    },
    handleCheckAllChange(val){
      this.checkAll = val
      this.checkds = []
      if(val == true){
        this.allchecks.forEach(item=>{
          this.checkds.push(item.label)
        })
      }
      this.indeterminate = false
    },
    handleCheck(){
      if(this.checkds.length == this.typenum){
        this.checkAll = true
      }else{
        this.checkAll = false
      }
      this.indeterminate = this.checkds.length > 0 && this.checkds.length < this.typenum
    },
    //修改项目
    editpro(row){
      this.isedit = true
      this.ischeck = false
      this.prodetail = false
      this.dialogVisible = true
      this.queryParam = {}
      this.queryParam = [...this.queryParam,...row]
      this.protitle = '修改项目信息'
    },
    //查看进度
    detailpro(row){
      this.uploadData = row
      this.uploadData.id = this.uploadData.projectId
      this.prodetail = true
    },
    //删除项目
    deleetpro(row){
      this.deletevisible = true
      this.prodetail = false
      getProjectCbStageNotDraft(row.relatedId).then(res=>{
        let list = res.data
        let cb = this.cbStagelist
        let datas = []
        list.forEach(item =>{
          cb.for(i=>{
            if(item.dictValue == i){
              let arr = {'label':i,'text':item.dictLabel}
              datas.push(arr)
            }
          })
        })
        this.allchecks = datas
        this.typenum = list.length
        this.checkds = []
      })
    },
    getDelete(){
       // this.checkds   勾选了要删除的数据


      this.getlist()
    },
    //返回列表
    colsepro(){
      this.isupload = false
      this.getCGXlist()
      this.getlist()
    },
    addPro(){
      this.ischeck = true
      this.isedit = false
      this.dialogVisible = true
      this.queryParam = {
        projectName: '',//项目名称
        ipmProjectNo: '',//	IPM项目编码
        isGetProjectDetail: 0,//	是否获取项目详情 0:否、1:是)
        projectFileName: '',//	文件名称
        cbStage: ''//成本阶段
      }
      this.protitle = '新增项目'
    },
    getSearch(){
      this.formdata.pageNum = 1

      this.getlist()
    },
    getipmProjectNo(){
      //点击且校验了当前编码有数据才获取
      checkProjectCodeExist(this.queryParam.ipmProjectNo).then(res=>{
        if(res.code == 200 && res.data == true){
          this.queryParam.isGetProjectDetail = 1
        }else{
          this.queryParam.isGetProjectDetail = 0
        }
      })
    },
    clears(){
      this.formdata={
        customerName: '',
        customerClass:[],
        areas:[],
        province:[],
        city:[],
        customerState:[],
        creditLevel:[],
        credential:[],
        approveDateBegion:null,
        approveDateEnd:null,
        times:[],
        pageSize:50,
        pageNum:1,
      }
      // this.customerAll()
    },
    //添加项目
    insertPro(){
      addProject(JSON.stringify(this.queryParam)).then(res=>{
        if(res.code == 200){
          this.uploadData = res.data
          this.isupload = true
        }
      })
    },
    //修改项目
    savepro(){
      editProjectInfo(JSON.stringify(this.queryParam)).then(res=>{
        if(res.code == 200){
          this.dialogVisible = false
          this.getlist()
        }
      })
    },
    //修改阶段
    changepro(){
      addNewStageProject(JSON.parse(JSON.stringify(this.queryParam))).then(res=>{
        this.uploadData = res.data
        this.uploadData.id =  this.uploadData.projectId
        this.isupload = true
      })
    },
    //继续编辑
    jxbj(data){
      this.uploadData = data
      this.isupload = true
      this.cgxVisible = false
    },
    handleCurrentChange(val){
      this.formdata.pageNum = val
      this.customerAll()
    },
    setHeaderRow() {
      return "owner-table-list-header";
    },
    setCellClass({ row, column, rowIndex, columnIndex }) {
      if (columnIndex == 1) {
        return "enterprise-name-column";
      }
    },
    setRowClass({ row, rowIndex }) {
      return "enterprise-name-row";
    },
    getcgx(){
      if(this.cgxtotal>0){
        this.getCGXlist(1)
        this.cgxVisible = true
      }
    },
    getCGXlist(page){
      this.cgxSkeleton = true
      this.cgPagenum = page
      let param = {
        pageSize: this.cgPagesize,
        pageNum: this.cgPagenum
      }
      getDraftDialogList(param).then(res=>{
        this.cgxtotal = res.total
        this.cgxlist = res.rows
        this.cgPagenum = res.currentPage
        this.cgxSkeleton = false
      })
    },
    deletecg(id){
      this.$confirm('是否确定删除此项目，删除后无法找回。', '提示', {
        confirmButtonText: '确定删除',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteDraft(id).then(res=>{
          if(res.code == 200){
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.getCGXlist(1)
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
  },
}
</script>
<style lang="scss" scoped>
  .project-cost-ledger-container {
    width: 100%;
    height: 100%;
    box-sizing: border-box;
    padding: 16px 24px;
    overflow: auto;
    .project-cost-ledger-inner {
      width: 100%;
      /*height: 100%;*/
    }

    .content-project {
      background: #fff;
      padding-top: 14px;

      .el-form-item {
        margin-bottom: 16px;
      }

      .el-divider {
        margin-bottom: 16px;
      }
    }

    .table-project {
      background: #fff;
      margin-top: 16px;
      height: calc(100vh - 296px);
      padding-top: 1px;
      min-height: 350px;

      .table-item-jf1 {
        margin-top: calc(27vh - 90px);

        .item-jf-img {
          margin-bottom: 16px;
        }
      }

      .item-jf-titel {
        font-size: 14px;
        color: #232323;
        line-height: 22px;
        margin-bottom: 16px;
      }

      .jf-btn {
        display: block;
        background: #0081ff;
        margin: auto;
      }
      .btn{
        width: auto;
        padding: 0 12px;
      }
      .btn + .btn{
       margin-left: 16px;
      }
    }

    ::v-deep .pro-news {
      .el-dialog__body {
        padding: 24px 0 0;
      }

      .el-dialog__headerbtn {
        top: 20px;
        right: 20px;
        z-index: 3;
      }

      .poptitle {
        line-height: 56px;
        border-bottom: 1px solid #eee;
        height: 56px;
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;

        > span {
          padding-left: 20px;
          font-weight: bold;
          font-size: 16px;
          color: #232323;
          font-family: Microsoft YaHei-Bold, Microsoft YaHei;
          width: 385px;
          display: block;
          overflow: hidden;
          white-space: nowrap;
          text-overflow: ellipsis;
        }
      }

      .popform {
        padding: 24px 20px 0;

        .row {
          margin-bottom: 16px;
          position: relative;
          .checkcb{
            position: absolute;
            left: -100px;
          }
          .el-form-item__label {
            font-weight: 400;
            opacity: 0.8;
            padding: 0;
            font-size: 14px;
            color: #232323;
            padding-right: 16px;
          }

          .el-input,
          .el-textarea,
          .el-select {
            display: inline-block;
            width: 343px;

            .el-input__inner,
            .el-textarea__inner {
              width: 100%;
              height: 32px;
              border-radius: 0px;

              &:hover {
                border-color: #0081ff;
              }
            }

            .el-textarea__inner {
              height: 90px;
              font-family: inherit;
              color: #000;
            }

            .el-input__suffix {
              height: 32px;
            }

            .el-input__icon {
              line-height: 32px;
            }
          }

          .el-form-item__error {
            padding-top: 2px;
          }
        }
        .el-input-group--append{
          position: relative;
          .el-input-group__append{
            position: absolute;
            right: 0;
            width: 76px;
            top: 0;
            border: 0;
            background: none;
            &:hover{
              background: none !important;
            }
            .pro-getbtn{
              border-left: 1px solid #DCDFE6;
              height: 32px;
              color: #0081FF;
              line-height: 32px;
              text-align: center;
            }
          }
        }
      }

      .popbot {
        border-top: 1px solid #EEEEEE;
        text-align: right;
        margin-top: 24px;
        padding: 16px 20px;

        .btn {
          border-radius: 2px;
          margin-left: 12px;
          padding: 0 24px;
          &.btn_disabled{
            background: #8BD1FF;
          }
        }
      }
    }

    .total-title{
      height: 32px;
      line-height: 32px;
      .totals{
        padding-left: 6px;
        position: relative;
        font-size: 12px;
        color: #3D3D3D;
        display: inline-block;
        &::before{
          content: ' ';
          position: absolute;
          width: 2px;
          height: 2px;
          background: rgba(35,35,35,0.4);
          border-radius: 50%;
          left: 0;
          top: 12px;
        }
      }
      margin-bottom: 16px;
      .btns{
        float: right;
        display: inline-block;
        width: 30%;
        text-align: right;
      }
    }
  }
  ::v-deep .dialog-supplier{
    .el-dialog__body{
      padding: 0;
    }
  }
  .wordprimary{
    margin-left: 12px;
  }
.worddel{
  color: #FA5351;
  padding-left: 12px;
  cursor: pointer;
}
  .pagination{
    padding-top: 16px;
    text-align: right;
  }
  .protypes{
    padding: 24px 20px;
    .ck{
      margin-bottom: 12px;
      height: 22px;
      font-size: 14px;
      color: rgba(35,35,35,0.8);
      line-height: 22px;
      &:last-child{
        margin-bottom: 0;
      }
    }
  }
</style>
