<!--
 * @desc 服务审核-我的任务
 * @author linyuanjie
 * @date 2022/10/25
-->

<template>
  <div class="my-task">
    <div class="header">
      <el-button type="primary" @click="examineBtnPl">批量审批</el-button>
      <div class="filter">
        <el-checkbox
          v-model="checked.status0"
          @change="checked => statusChecked(checked, 0)"
          >审批中</el-checkbox
        >
        <el-checkbox
          v-model="checked.status1"
          @change="checked => statusChecked(checked, 1)"
          >审批通过</el-checkbox
        >
        <el-checkbox
          v-model="checked.status2"
          @change="checked => statusChecked(checked, 2)"
          >审批不通过</el-checkbox
        >
      </div>
    </div>
    <div class="content-box">
      <!-- 左侧菜单 -->
      <taskLeftMent @clickInfo="clickInfo"></taskLeftMent>
      <!-- 右侧表格 -->
      <div class="right-tab">
        <avue-crud
          :data="dataList"
          :option="opt"
          ref="tableRef"
          :page.sync="page"
          @size-change="sizeChange"
          @on-load="getList"
          @selection-change="selectionChange"
          :table-loading="loading"
          @current-change="currentChange"
        >
          <template slot-scope="scope" slot="menu">
            <el-button
              v-if="!scope.row.reportApprovalOpinion"
              type="text"
              size="small"
              icon="el-icon-edit-outline"
              @click="examineBtn(scope.row, scope.index)"
              >审批
            </el-button>
            <el-button
              type="text"
              size="small"
              icon="el-icon-view"
              @click="toView(scope.row)"
              >查看
            </el-button>
          </template>
        </avue-crud>
      </div>
    </div>

    <el-dialog
      title="审批"
      :visible.sync="showDialog"
      top="12vh"
      width="40%"
      center
      :before-close="handleClose"
      class="review-dialog"
    >
      <el-form
        :model="taskForm"
        :rules="rules"
        ref="taskForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <div v-show="oneData">
          <p v-if="atypismData.length" class="teskDetailsty">不一致信息</p>
          <el-table
            v-if="atypismData.length"
            :data="atypismData"
            border
            style="width: 100%; margin-bottom:20px;"
          >
            <el-table-column
              prop="name"
              label="信息项"
              width="180"
              align="center"
            >
            </el-table-column>
            <el-table-column
              prop="oldData"
              label="系统数据"
              width="180"
              align="center"
            >
            </el-table-column>
            <el-table-column prop="newData" label="调研数据" align="center">
            </el-table-column>
          </el-table>
          <p class="missionSty">任务详情</p>
          <div v-if="oneData && showDialog">
            <task-detail
              :id="examineVal"
              @getSurveyTaskInfoById="getSurveyTaskInfoById"
            />
          </div>
        </div>
        <p class="missionSty">审批意见</p>
        <el-form-item label="审批意见：" prop="approvalOpinion">
          <el-radio-group v-model="taskForm.approvalOpinion">
            <el-radio label="1">通过</el-radio>
            <el-radio label="2">拒绝</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="说明：" v-show="oneData">
          <el-input type="textarea" v-model="taskForm.approvalInfo"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="submitForm('taskForm')"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import taskLeftMent from "@/views/components/quota/taskLeftMent";
import { AllTaskOpt } from "@/const/crud/myTask";
import { approvalTask } from "@/api/assignPoints/currency/taskAudit";
import taskDetail from "@/components/taskDetail";
import {
  approvalReport,
  approvalReportByTask,
  getTaskPageByType
} from "@/api/taskManagement/teskReportapproval";
import Config from "@/settings";
import taskApi from "@/api/taskDetail/task";

export default {
  components: {
    taskLeftMent,
    taskDetail
  },
  data() {
    return {
      // 审核状态
      checked: {
        status0: true,
        status1: false,
        status2: false
      },
      opt: AllTaskOpt,
      // 任务名称
      taskTypeName: "",
      // 提交审核状态
      taskStatus: [0],
      // 页数配置
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      // 列表数据
      dataList: [],
      examineVal: "",
      jfbID: "",
      showDialog: false,
      singleFlag: false,
      taskDetail: "",
      taskForm: {
        taskStatus: "",
        taskStatusInfo: "",
        approvalOpinion: "",
        approvalInfo: ""
      },
      atypismData: [],
      rules: {
        approvalOpinion: [
          { required: true, message: "请选择审批意见", trigger: "change" }
        ]
      },
      isHasAudit: false,
      packageIds: null,
      loading: false,
      oneData: false // 是否单条审批
    };
  },
  methods: {
    // 初始化页数 数据
    initInfo() {
      this.$set(this.page, "total", 0);
      this.$set(this.page, "pageSize", 10);
      this.$set(this.page, "currentPage", 1);
      this.dataList = [];
    },
    //选择
    clickInfo(info) {
      console.log(info);
      this.initInfo();
      this.taskTypeName = info;
      this.getList();
    },
    // 获取数据
    async getList() {
      this.loading = true;
      const obj = {
        current: this.page.currentPage,
        size: this.page.pageSize,
        taskTypeName: this.taskTypeName,
        entTaskStatus: this.taskStatus.join(",")
      };
      const res = await getTaskPageByType(obj);
      if (res.data.code === 0) {
        this.dataList = res.data.data.records;
        this.page.total = res.data.data.total;
        this.$nextTick(() => {
          this.$refs.tableRef.doLayout();
        });
      }
      this.loading = false;
    },
    // 审核状态改变
    statusChecked(checcked, type) {
      let { status0, status1, status2 } = this.checked;
      this.$nextTick(() => {
        if (!checcked) {
          let index = this.taskStatus.findIndex(item => item === type);
          this.taskStatus.splice(index, 1);
          if (!status0 && !status1 && !status2) {
            this.$message.info("至少选择一种状态！");
            let str = "status" + type;
            this.checked[str] = true;
            this.taskStatus = [type];
            return;
          }
        } else {
          if (!this.taskStatus.includes(type)) {
            this.taskStatus.push(type);
          }
        }
        this.initInfo();
        this.getList();
      });
    },
    // 分页查询
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    currentChange(current) {
      this.page.currentPage = current;
    },
    // 审核
    examineBtn(row) {
      this.oneData = true;
      this.examineVal = row.id;
      this.jfbID = row.scorePackageId;
      this.showDialog = true;
      this.singleFlag = true;
      this.taskDetail =
        Config.outsideUrl + `/h5/#/pages/task/task-detail/index?id=${row.id}`;
    },

    // 多选
    selectionChange(selection) {
      this.isHasAudit = false;
      this.packageIds = "";
      if (undefined != selection) {
        for (var i = 0; i < selection.length; i++) {
          if (selection[i].taskStatus > 0) {
            this.isHasAudit = true;
          }
          this.packageIds += selection[i].id;
          if (i < selection.length - 1) {
            this.packageIds += ",";
          }
        }
      }
    },
    //批量审核按钮
    examineBtnPl() {
      this.oneData = false;
      this.examineVal = this.packageIds;
      this.singleFlag = false;
      if (this.examineVal == null) {
        this.$message.error("请选择需要审批的列");
        this.examineVal = null;
        return false;
      } else if (this.examineVal == "") {
        this.$message.error("请选择需要审批的列");
        this.examineVal = null;
        return false;
      } else {
        this.showDialog = true;
      }
    },
    // 查看
    toView(row) {
      window.open(
        Config.outsideUrl + "/h5/#/pages/task/task-detail/index?id=" + row.id,
        "newwindow",
        "height=750, width=400, top=200, left=300, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no"
      );
    },
    handleClose() {
      this.showDialog = false;
      this.$refs.taskForm.resetFields();
      this.taskForm = {
        taskStatus: "",
        taskStatusInfo: "",
        approvalOpinion: "",
        approvalInfo: ""
      };
      this.atypismData = [];
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          let args = {
            taskIds: this.examineVal,
            approvalOpinion: this.taskForm.approvalOpinion,
            approvalInfo: this.taskForm.approvalInfo
          };
          approvalReportByTask(args)
            .then(response => {
              this.getList(this.page);
              this.handleClose();
              this.tableLoading = false;
              this.$message.success("审批成功");
              this.taskForm.resource = "";
            })
            .catch(() => {
              this.tableLoading = false;
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 获取不一致信息
    getSurveyTaskInfoById(id) {
      taskApi.getSurveyTaskInfoById(id).then(res => {
        this.atypismData = res.data.data.compareList;
      });
    }
  }
};
</script>

<style scoped lang="scss">
.review-dialog {
  ::v-deep .el-dialog__body {
    padding: 0 25px 0;
  }
  .formitem-box {
    margin-bottom: 6px;
  }
}

.my-task {
  .header {
    padding: 0 50px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 50px;
    border: 1px solid #ccc;
    border-radius: 15px;
  }
  .content-box {
    margin-top: 10px;
  }
}

.content-box {
  display: flex;
  .right-tab {
    flex: 1;
    margin: 20px;
    height: 100%;
    width: 100%;
    ::v-deep .avue-crud__menu {
      display: none;
    }
  }
}
</style>

<style>
.teskDetailsty {
  width: 100%;
  height: 30px;
  line-height: 30px;
  font-size: 16px;
  font-weight: 600;
  color: #333333;
  border-bottom: 1px solid #d7d6d5;
  margin: 0 0 10px 0;
}
.missionContsty {
  height: 300px;
  border: 1px solid #f2f2f2;
  margin-bottom: 15px;
}
</style>
