<template>
  <el-form
    ref="ruleForm"
    :model="ruleForm"
    label-width="80px"
    class="demo-ruleForm"
  >
    <el-form-item
      label="备注信息"
      prop="remarks"
      :rules="[{ required: true, message: '请输入备注信息', trigger: 'blur' }]"
    >
      <el-input
        v-model="ruleForm.remarks"
        type="textarea"
        size="small"
        rows="3"
      />
    </el-form-item>
    <el-form-item>
      <Upload :fileUrl="fileUrl" />
    </el-form-item>
    <el-form-item style="text-align: right">
      <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
      <el-button @click="$listeners.close()">关闭</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import {
  processStructure,
  handleWorkOrder,
  activeOrder,
  asyncPlayLog,
  queryUserInfo,
  queryAllOrgan,
  orderComment
} from "@/api/process/work-order";
import store from "@/store";
import load from "@/utils/loading";
import Upload from "./upload";
export default {
  name: "transferInversion",
  props: {
    submitItem: {
      type: Object,
      default: {}
    },
    type: {
      type: String,
      default: "commit"
    }
  },
  components: { Upload },
  data() {
    return {
      ruleForm: {
        remarks: ""
      },
      fileUrl: []
    };
  },
  mounted() {},
  methods: {
    submitForm() {
      this.$refs["ruleForm"].validate(async _ => {
        console.log(_);
        if (_) {
          // 评论
          if (this.type === "commit") {
            await this.submitCommit();
          } else {
            await this.submitOperation();
          }
        }
      });
    },
    async submitCommit() {
      load.startLoading();
      const res = await orderComment({
        workOrderId: this.submitItem.workOrderId,
        remarks: this.ruleForm.remarks,
        fileUrl: JSON.stringify(this.fileUrl || [])
      });
      load.endLoading();
      if (res.code == 200) {
        this.$message.success("评论成功");
        this.ruleForm.remarks = "";
        this.fileUrl = [];
        // 充值数据
        this.$listeners.close();
        this.$listeners.getList();
      } else {
        this.$message.error(res.message);
      }
    },
    async submitOperation() {
      const { fileList, work_order_id, tips, ...res } = this.submitItem;
      this.$confirm(tips, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(async () => {
        load.startLoading();
        handleWorkOrder({
          work_order_id,
          fileUrl: JSON.stringify(this.fileUrl || []),
          remarks: this.ruleForm.remarks,
          ...res
        }).then(async response => {
          load.endLoading();
          if (response.code === 200) {
            await asyncPlayLog({
              workOrderId: parseInt(work_order_id),
              fileUrl: fileList.join(",")
            });
            this.$listeners.close();
            this.$listeners.getList();
            await store.dispatch("user/getInitData");
          }
        });
      });
    }
  }
};
</script>
