<template>
  <div class="app-container" >
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="90px"
    >
<!--      <el-form-item  prop="time">
        <el-date-picker
          v-model="queryParams.time"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd">
        </el-date-picker>
      </el-form-item>-->
      <el-form-item prop="time">
        <el-date-picker
          v-model="queryParams.time"
          type="month"
          placeholder="请选择月份"
          value-format="yyyy-MM"
          :clearable="false"
          :editable="false"
        />
      </el-form-item>
      <el-form-item  prop="keyWord">
        <el-input
          v-model="queryParams.keyWord"
          placeholder="请输入关键字"
          clearable
          maxlength="50"
          style="width: 240px"
          @keyup.enter.native="handleQuery"/>
      </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-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          @click="batchPass"
          icon="el-icon-check"
          size="mini"
          :disabled="multiple"
          >通过</el-button
        >
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="danger"
          @click="batchReject"
          icon="el-icon-close"
          size="mini"
          :disabled="multiple"
          >驳回</el-button
        >
      </el-col>
      <el-col :span="21" style="text-align: right;">
        <div style="color: red;">待审核周期：{{ reviewPeriod }}</div>
        <div style="color: red;">审核费用(元)：{{ reviewCost.toFixed(2) }}</div>
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="tableList"
      row-key="id"
      ref="multipleTable"
      :max-height="650"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="55"
        align="center"
        :reserve-selection="true"
      />
      <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="typeName" :show-overflow-tooltip="true"/>
      <el-table-column label="规格型号" align="center" prop="typeModelName" :show-overflow-tooltip="true"/>
      <el-table-column label="计量单位" align="center" prop="unitName" :show-overflow-tooltip="true"/>
      <el-table-column label="报废数量" align="center" prop="scrapNum" :show-overflow-tooltip="true"/>
      <el-table-column label="报废费用(元)" align="center" prop="totalCost" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span>{{ scope.row.totalCost.toFixed(2) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="报废类型" align="center" prop="scrapStyle" :show-overflow-tooltip="true"/>
      <el-table-column label="损坏类型" align="center" prop="scrapType" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span v-if="scope.row.scrapType==0">{{ '自然损坏' }}</span>
          <span v-if="scope.row.scrapType==1">{{ '人为损坏' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="提交人" align="center" prop="createName" :show-overflow-tooltip="true"/>
      <el-table-column label="本次审核周期" align="center" prop="month" :show-overflow-tooltip="true"/>
      <el-table-column label="状态" align="center" prop="ledgerStatus" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span style="color:#70B603">{{ '待审核'}}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true"/>       -->
      <el-table-column label="操作" align="center" width="180" >
        <template slot-scope="scope">
          <el-button size="mini" type="success" @click="pass(scope.row)" v-if="scope.row.ledgerStatus==0">
            通过
          </el-button>
          <el-button size="mini" type="danger" @click="reject(scope.row)" v-if="scope.row.ledgerStatus==0">
            驳回
          </el-button>
        </template>
      </el-table-column>
    </el-table>

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

    <!-- 驳回弹窗 -->
    <el-dialog title="驳回" :visible.sync="confirmShow" width="600px" height="300px" @close="closeCancel">
      <div style="width: 100%; height: 80%; display: flex;margin-bottom: 10px;">
        <div style="width:15%;">驳回原因：</div>
        <el-input
          type="textarea"
          :rows="3" style="width:80%;"
          placeholder="请输入驳回原因"
          v-model="checkResult"
          maxlength="100">
        </el-input>
      </div>
      <div style="width: 100%;height: 20%;display: flex;justify-content: flex-end;align-items: center;">
        <el-button type="primary" @click="confirmCheck">确认</el-button>
        <el-button size="mini" @click="cancel">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

  <script>
import { getTotalList,getTotalPrice,ledgerApprove,ledgerReject } from '@/api/repair/scrapLedgerReview';
export default {
  name: "ScrapLedgerReview",
  dicts: ["part_task_status"],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      total: 0,
      // 显示搜索条件
      showSearch: true,
      showHouse: false,
      ids: [],
      idList: [],
      taskIds: [],
      taskIdList: [],
      checkList: [],
      //表格数据
      tableList: [],
      // 弹出层标题
      title: "",

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        time: null,
        keyWord: '',
      },
      //确认弹窗
      confirmShow: false,
      checkResult: "", // 驳回原因

      reviewPeriod: '', // 新增变量用于存储待审核周期
      reviewCost: null, // 新增变量用于存储审核费用
    };
  },

  created() {
    this.getTopData();
    this.getList();
  },

  methods: {
    // 格式化日期范围
    formatDateRange(dates) {
      if (!dates || dates.length!== 2) {
        return '';
      }
      const startDate = new Date(dates[0]);
      const endDate = new Date(dates[1]);
      const startYear = startDate.getFullYear();
      const startMonth = startDate.getMonth() + 1;
      const endYear = endDate.getFullYear();
      const endMonth = endDate.getMonth() + 1;

      if (startYear === endYear && startMonth === endMonth) {
        return `${startYear}年${startMonth}月`;
      } else {
        return `${startYear}年${startMonth}月 ~ ${endYear}年${endMonth}月`;
      }
    },

    /** 查询列表 */
   async getList() {
      this.loading = true;
      const params = {
        keyWord: this.queryParams.keyWord,
        month: this.queryParams.time,
        // startTime: this.queryParams.time && this.queryParams.time[0],
        // endTime: this.queryParams.time && this.queryParams.time[1],
        pageSize: this.queryParams.pageSize,
        pageNum: this.queryParams.pageNum
      }
      console.log('11111111111',params)
     await getTotalList(params).then((response) => {
      console.log('222222222',params)
        this.tableList = response.data.rows;
        this.total = response.data.total;
        this.loading = false;
      });
    },

    // 获取待审核周期和审核金额
    getTopData() {
      getTotalPrice().then((response) => {
        console.log("xxxxxxxx",response)
        this.reviewPeriod = response.data.month==null ? '': response.data.month;
        this.reviewCost = response.data.totalCost==null ? 0: response.data.totalCost;
      });
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.$refs.multipleTable.clearSelection();
      this.queryParams.keyWord = null;
      this.queryParams.time=[]
      this.handleQuery();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      // this.reviewPeriod = this.formatDateRange(this.queryParams.time); // 更新待审核周期
      this.getTopData();
      this.getList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      console.log('111111111',selection)
      this.ids = selection;
      this.idList = [];
      this.taskIdList = [];
      selection.forEach((item) => {
        if (item.idList) {
          this.idList = this.idList.concat(item.idList);
        }
        if (item.taskIdList) {
          this.taskIdList = this.taskIdList.concat(item.taskIdList);
        }
      });
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    //通过
    pass(row) {
      // if(this.infos==0){
      //   this.$modal.msgError("请选择配件");
      //   return;
      // }
      let idLstTemp = [];
      let taskIdListTemp = [];
      idLstTemp = idLstTemp.concat(row.idList);
      taskIdListTemp = taskIdListTemp.concat(row.taskIdList);
      const infos={'idList':idLstTemp,'taskIdList':taskIdListTemp}
      this.$modal.confirm('是否确认通过？')
            .then(function() {
              return ledgerApprove(infos)
            }).then(() => {
                  this.$modal.msgSuccess('通过成功')
                  this.getTopData();
                  this.getList()
                  // 清除表格的选中状态
                  this.$refs.multipleTable.clearSelection();
            }).catch(() => {});
    },
    //驳回
    reject(row) {
      this.checkResult = "不合格";
      this.confirmShow = true;
      this.idList = [];
      this.taskIdList = [];
      this.idList = this.idList.concat(row.idList);
      this.taskIdList = this.taskIdList.concat(row.taskIdList);
      // this.$modal.confirm('是否确认驳回该配件？')
      //       .then(function() {
      //         return partTypeReject(infos)
      //       }).then(() => {
      //             this.$modal.msgSuccess('驳回成功')
      //             this.getList()
      //       }).catch(() => {});
    },

    //批量通过
    batchPass() {
      if(this.ids.length==0){
        this.$modal.msgError("请选择数据");
        return;
      }
      const infos={'idList':this.idList,'taskIdList':this.taskIdList}
      console.log('111111111infos',infos)
      this.$modal.confirm('是否确认通过？')
        .then(() => {
          return ledgerApprove(infos)
        })
        .then(() => {
          this.$modal.msgSuccess('通过成功')
          this.getTopData();
          this.getList()
          // 清除表格的选中状态
          this.$refs.multipleTable.clearSelection();
        })
        .catch(() => {});
    },

    //批量驳回
    batchReject() {
     if(this.ids.length==0){
        this.$modal.msgError("请选择数据");
        return;
      }
      this.checkResult = "不合格";
      this.confirmShow = true;
    },

    //驳回确认请求
    confirmCheck() {
      const infos={'idList':this.idList,'taskIdList':this.taskIdList,'rejectReason':this.checkResult}
      ledgerReject(infos).then((response) => {
        if (response.code == 200) {
          this.$modal.msgSuccess("驳回成功");
          this.confirmShow = false;
          this.getTopData();
          this.getList();
          this.$refs.multipleTable.clearSelection();
        }
      });
    },

    //取消
    cancel() {
      this.confirmShow = false;
      this.checkResult = "";
    },

    //关闭
    closeCancel() {
      this.cancel()
    },
  },
};
</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>
