<template>
  <ele-modal
    :visible="formParserDialogFlag"
    title="审批"
    width="30%"
    :centered="true"
    :close-on-click-modal="false"
    append-to-body
    :before-close="cancel"
  >
    <fm-generate-form
      v-if="Object.keys(form?.formJson || {}).length !== 0"
      :data="jsonData"
      :value="form.valueJson"
      :edit="false"
      ref="generateForm"
    >
      <template v-slot:blank_adopzrdd="scope">
        <div v-for="(item, index) in scope.model.blank_adopzrdd" :key="index">
          <div class="blank_adopzrdd">
            <span>{{ index + 1 }}报销事项：</span>
            <el-input
              v-model="scope.model.blank_adopzrdd[index].remark"
              type="textarea"
              style="width: calc(100% - 80px)"
            ></el-input>
          </div>
          <div class="blank_adopzrdd">
            <span>金额：</span>
            <el-input
              v-model="scope.model.blank_adopzrdd[index].price"
              type="number"
              style="width: calc(100% - 80px)"
            ></el-input>
          </div>
        </div>
      </template>
    </fm-generate-form>
    <div slot="footer" class="dialog-footer">
      <el-form label-width="100px" ref="formRef" :model="form">
        <!-- prop="reason" -->
        <el-form-item
          label="审批建议"
          style="margin-bottom: 20px"
          :rules="{
            required: true,
            message: '',
            trigger: 'blur'
          }"
        >
          <el-input
            type="textarea"
            v-model="form.reason"
            placeholder="请输入审批建议"
          />
        </el-form-item>
      </el-form>
      <el-button
        icon="el-icon-edit-outline"
        type="success"
        size="mini"
        @click="handleAudit(1)"
        >通过
      </el-button>

      <el-button
        icon="el-icon-circle-close"
        type="danger"
        size="mini"
        @click="handleAudit(0)"
        >驳回
      </el-button>

      <el-dropdown
        @command="(command) => handleCommand(command)"
        style="margin-left: 30px"
      >
        <span class="el-dropdown-link">
          更多<i class="el-icon-arrow-down el-icon--right"></i>
        </span>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item command="cancel">作废</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
  </ele-modal>
</template>
<script>
  import Parser from '@/components/FormGenerator/components/parser/Parser.vue';
  import { projectsUpdateAPI } from '@/api/bpm/components/project-manage';
  import {
    approveTaskWithVariables,
    cancelTask,
    rejectTask
  } from '@/api/bpm/task';
  import { getToken } from '@/utils/token-util';

  export default {
    name: 'formParserDialog',
    components: { Parser },
    props: {
      businessId: {
        default: ''
      },
      formParserDialogFlag: {
        type: Boolean,
        default: false
      }
    },
    data() {
      return {
        form: {},
        index: '',
        jsonData: {}
      };
    },
    methods: {
      open(row) {
        this.form = _.cloneDeep(row);
        this.jsonData = JSON.parse(this.form.formJson.makingJson);
        this.jsonData.config.dataSource &&
          this.jsonData.config.dataSource.forEach((item) => {
            item.headers = {
              Authorization: getToken()
            };
            // item.url = item.url && item.url.replace('/api', this.APIUrl);
          });
      },
      cancel() {
        this.$emit('update:formParserDialogFlag', false);
      },
      async handleAudit(status) {
        await this._approveTaskWithVariables(status);
      },
      async _approveTaskWithVariables(status) {
        let variables = {
          pass: !!status
        };
        let API = !!status ? approveTaskWithVariables : rejectTask;

        API({
          id: this.form.id,
          reason: this.form.reason,
          businessId: this.businessId,
          variables
        }).then((res) => {
          if (res.data.code != '-1') {
            let params = {
              status,
              title: status === 0 ? '驳回' : ''
            };
            this.$message.success(`审批${params.title}成功！`);
            this.$emit('reload');
            this.cancel();
          }
        });
      },

      //更多
      handleCommand(command) {
        if (command === 'cancel') {
          this.$confirm('是否确认作废？', {
            type: 'warning',
            cancelButtonText: '取消',
            confirmButtonText: '确定'
          })
            .then(() => {
              cancelTask({
                taskId: this.form.id,
                id: this.form.processInstance.id,
                reason: this.form.reason
              })
                .then(() => {
                  this.$emit('reload');
                  this.cancel();
                })
                .catch(() => {
                  this.$message.error('流程作废失败');
                });
            })
            .catch(() => {});
        }
      }
    }
  };
</script>
<style scoped lang="scss">
  .blank_adopzrdd {
    display: flex;
    align-items: center;
    > span {
      display: inline-block;
      width: 80px;
    }
    margin-bottom: 10px;
  }
</style>
