<template>
  <div class="handoverDetail pagePadding">
    <div v-if="powers.some((i) => i == '007')">
      <div class="detail-page-title">
        <span>离职交接记录编号：</span><span v-if="sitem">{{ queryId }}</span>
        <el-button
          @click="statusConfirm('-1', '作废该条信息')"
          type="danger"
          class="fr"
          :size="'mini'"
          plain
          v-if="(status === '0' || status === '1') && powers.some((i) => i == '015')"
          >作废该条信息</el-button
        >
        <el-button
          @click="statusConfirm('0', '取消审核流程')"
          plain
          class="fr"
          :size="'mini'"
          v-if="status === '1' && powers.some((i) => i == '014')"
          >取消审核流程</el-button
        >
      </div>
      <!-- <status-bar
        v-if="newTime !== ''"
        :newTime="newTime"
        :options="statusOptions"
        :status="status"
      /> -->
      <el-tabs v-model="activeTabs">
        <el-tab-pane label="业务详情" name="1">
          <el-collapse v-model="activeNames" style="margin: -18px 0 0 0">
            <el-collapse-item title="离职交接详情" name="0">
              <add-edit-form
                :id="queryId"
                v-if="newTime !== ''"
                :newTime="newTime"
                :sitem="sitem"
                @refresh="initData"
              />
            </el-collapse-item>
            <el-collapse-item
              title="发起审核流程"
              name="1"
              v-if="status === '0' && ppowers.some((i) => i == '0')"
            >
              <div style="height: 50px; padding: 0 0px 20px 0">
                <el-button
                  type="primary"
                  class="fr"
                  :size="'mini'"
                  @click="statusConfirm('1', '发起审核流程')"
                  >发起审核流程
                </el-button>
              </div>
            </el-collapse-item>
            <el-collapse-item
              title="离职交接审核"
              name="2"
              v-if="status === '1' && ppowers.some((i) => i == '1')"
            >
              <div style="height: 50px; padding: 0 0px 20px 0">
                <el-button
                  @click="dialogFormVisible = true"
                  type="danger"
                  class="fr"
                  :size="'mini'"
                  plain
                  style="margin: 0 0 0 12px"
                  >审核不通过</el-button
                >
                <el-button
                  @click="statusConfirm('3', '通过审核')"
                  type="primary"
                  class="fr"
                  :size="'mini'"
                  >通过审核</el-button
                >
                <el-dialog
                  title="审核备注"
                  :visible.sync="dialogFormVisible"
                  :center="true"
                  align="left"
                  top="22vh"
                  width="600px"
                  :close-on-click-modal="false"
                  element-loading-text="拼命加载中"
                  element-loading-spinner="el-icon-loading"
                  element-loading-background="rgba(0, 0, 0, 0.8)"
                  @close="dialogFormVisible = false"
                >
                  <el-card style="margin-top: -20px">
                    <el-form
                      :model="ruleForm"
                      ref="ruleForm"
                      status-icon
                      :rules="rulesThis"
                    >
                      <el-form-item label="审核备注" prop="remark">
                        <el-input
                          type="textarea"
                          :autosize="{ minRows: 4, maxRows: 4 }"
                          placeholder="审核备注"
                          maxlength="250"
                          show-word-limit
                          v-model="ruleForm.remark"
                        />
                      </el-form-item>
                    </el-form>
                    <div class="clear">
                      <el-button
                        type="primary"
                        class="fr"
                        :size="'mini'"
                        style="margin: 0 0 0 12px"
                        @click="submitForm"
                        >确 定</el-button
                      >
                      <el-button
                        @click="dialogFormVisible = false"
                        class="fr"
                        :size="'mini'"
                        >取 消</el-button
                      >
                    </div>
                  </el-card>
                </el-dialog>
              </div>
            </el-collapse-item>
          </el-collapse>
        </el-tab-pane>
        <el-tab-pane label="审批记录" name="2">
          <process-time-line
            v-if="newTime !== ''"
            :newTime="newTime"
            :type="'LZJJ'"
            :orderCode="queryId"
          />
        </el-tab-pane>

        <el-tab-pane label="流程图" name="3">
          <flow-chart process_id="22" type="LZJJ" :orderCode="queryId" />
       </el-tab-pane>
      </el-tabs>
    </div>
    <div v-else>
      <no-auth></no-auth>
    </div>
  </div>
</template>
<script>
import mixinPage from "@/mixins/elPaginationHandle";
import resToken from "@/mixins/resToken";
import addEditForm from "./components/addEditForm";
import asyncRequest from "@/apis/service/interest/handover";

export default {
  name: "handoverDetail",
  mixins: [mixinPage, resToken],
  components: {
    addEditForm, //详情组件
  },
  computed: {
    powers() {
      const tran =
        this.$store.getters.btnList.find((i) => i.menu_route == "handoverDetail") || {};
      const { action } = tran ?? {};
      return action ?? [];
    },
    ppowers() {
      const tran =
        this.$store.getters.roleProcess.find((i) => i.process_type === "LZJJ") || {};
      const { action } = tran ?? {};
      return action ?? [];
    },
  },
  watch: {
    dialogFormVisible: function (val) {
      if (val) {
        this.initForm();
      }
    },
  },
  data() {
    return {
      dialogFormVisible: false,
      activeTabs: "1",
      activeNames: ["0", "1", "2", "3"],
      statusOptions: [],
      ruleForm: {
        remark: "",
      },
      rulesThis: this.rules,
      // 验证规则
      rules: {
        remark: [
          {
            required: true,
            message: "审核备注不能为空！",
            trigger: "blur",
          },
        ],
      },
      queryId: "",
      sitem: null,
      newTime: "",
    };
  },

  mounted() {
    this.queryId = this.$route.query.id;
    this.initData();
  },

  methods: {
    getNewTime() {
      this.newTime = new Date().valueOf();
    },
    async initData() {
      const res = await asyncRequest.detail({ id: this.queryId });
      if (res && res.code === 0 && res.data) {
        this.sitem = res.data;
        let { status } = this.sitem;
        this.status = status;
        if (this.status === "2") {
          this.statusOptions = [
            { value: "0", label: "待发起审核流程" },
            { value: "1", label: "待离职交接审核" },
            { value: "2", label: "审核不通过" },
          ];
        } else {
          this.statusOptions = [
            { value: "0", label: "待发起审核流程" },
            { value: "1", label: "待离职交接审核" },
            { value: "3", label: "待系统更新数据" },
            { value: "4", label: "离职交接已成功" },
          ];
        }

        this.getNewTime();
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(res.message);
      }
    },
    async initForm() {
      await this.$nextTick(() => {
        this.rulesThis = this.rules;
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          this.ruleForm = {
            remark: "",
          };
        }
      });
    },
    async statusConfirm(status, message) {
      await this.$confirm(`确定要${message}？`, {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          if (status === "-1") {
            await this.deleteById(message);
          } else {
            await this.setStatus(status, message);
          }
        })
        .catch(() => {
          console.log("取消");
        });
    },

    async deleteById(message) {
      const res = await asyncRequest.delete({ id: this.queryId });
      if (res && res.code === 0) {
        this.$notify.success({
          title: message + "成功！",
          message: "",
        });

        this.routeGoto("handover", {});
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(res.message);
      }
    },

    async setStatus(status, message, remark) {
      let model = {
        id: this.queryId,
        remark: remark || "",
        status: status,
      };
      const res = await asyncRequest.status(model);
      this.dialogFormVisible = false;
      this.loading = false;
      if (res && res.code === 0) {
        this.$notify.success({
          title: message + "成功！",
          message: "",
        });
        this.initData();
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(res.message);
      }
    },
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          this.setStatus("2", "审核提交", this.ruleForm.remark);
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>
