<template>
  <el-dialog
    v-loading="loading"
    :title="title"
    :center="true"
    align="left"
    top="10vh"
    width="900px"
    :close-on-click-modal="false"
    :visible.sync="showModelThis"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(0, 0, 0, 0.8)"
    @close="showModelThis = false"
  >
    <el-card style="margin-top: -20px">
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form
            ref="ruleForm"
            :model="ruleForm"
            status-icon
            :rules="rulesThis"
            label-width="80px"
          >
            <el-row>
              <el-col :span="12">
                <el-form-item label="节点类型" prop="action_type">
                  <el-select
                    v-model="ruleForm.action_type"
                    filterable
                    clearable
                    :disabled="isDetail"
                    placeholder="节点类型"
                    style="width: 100%"
                    @change="action_type_change"
                  >
                    <el-option
                      v-for="item in action_type_list"
                      :key="'action_type' + item.value"
                      :label="item.label"
                      :value="item.value"
                    /> </el-select></el-form-item
              ></el-col>
              <el-col :span="12">
                <el-form-item label="操作类型" prop="operation_type">
                  <el-select
                    v-model="ruleForm.operation_type"
                    filterable
                    clearable
                    :disabled="isDetail"
                    placeholder="操作类型"
                    style="width: 100%"
                  >
                    <el-option
                      v-for="item in operation_type_list"
                      :key="'operation_type' + item.value"
                      :label="item.label"
                      :value="item.value"
                    /> </el-select></el-form-item
              ></el-col>
              <el-col :span="12"
                ><el-form-item label="节点名称" prop="status_name">
                  <el-input
                    v-model="ruleForm.status_name"
                    placeholder="请输入节点名称"
                    maxlength="50"
                    :disabled="isDetail"
                  /> </el-form-item
              ></el-col>
              <el-col :span="12">
                <el-form-item label="节点值" prop="order_process">
                  <el-input
                    v-model="ruleForm.order_process"
                    placeholder="请输入节点值"
                    maxlength="50"
                    :disabled="isDetail"
                  /> </el-form-item
              ></el-col>
              <el-col
                :span="24"
                v-show="ruleForm.action_type === '1' || ruleForm.action_type === '2'"
                ><el-form-item label="下一节点" prop="next_action_ids">
                  <el-checkbox-group
                    v-model="ruleForm.next_action_ids"
                    :disabled="isDetail"
                  >
                    <el-checkbox
                      v-for="(si, sii) in tableData"
                      :key="'table' + sii"
                      :label="si.id"
                      :disabled="si.status !== '1' || ruleForm.id === si.id"
                      >{{ si.status_name }}</el-checkbox
                    >
                  </el-checkbox-group>
                </el-form-item></el-col
              >
              <el-col :span="24">
                <el-form-item  label="主流程" prop="is_master">
                  <el-radio-group v-model="ruleForm.is_master">
                    <el-radio label="1">是</el-radio>
                    <el-radio label="0">否</el-radio>
                  </el-radio-group>
                </el-form-item>
                </el-col>
            </el-row>
          </el-form>
        </el-col>
        <el-col :span="24" style="text-align: right">
          <el-button v-if="!isDetail" type="primary" @click="submitForm"
            >保 存
          </el-button>
          <el-button @click="showModelThis = false">{{
            isDetail ? "关 闭" : "取 消"
          }}</el-button>
        </el-col>
      </el-row>
    </el-card>
  </el-dialog>
</template>
<script>
import asyncRequest from "@/apis/service/serviceParam/setActionProcess";
import resToken from "@/mixins/resToken";
import { action_type_list, operation_type_list, rules } from "./columns";
export default {
  name: "setActionProcess",
  props: ["showModel", "id", "isDetail", "activeId"],
  mixins: [resToken],
  data() {
    return {
      loading: false,
      title: "添加流程节点",
      tableData: [],
      showModelThis: this.showModel,
      ruleForm: {
        id: this.id,
        process_id: "",
        action_type: "",
        operation_type: "",
        status_name: "",
        order_process: "",
        next_action_ids: [],
        remark: "",
      },
      rulesThis: this.rules,
      rules,
      action_type_list,
      operation_type_list,

      activeOptions: [],
    };
  },
  watch: {
    showModel: function (val) {
      this.showModelThis = val;
      if (val) {
        this.initForm();
      }
    },
    showModelThis(val) {
      if (!val) {
        this.$emit("cancel");
      }
    },
  },
  methods: {
    async initForm() {
      this.loading = true;
      if (this.id === "add") {
        this.title = "添加流程节点";
        this.rulesThis = this.rules;
        await this.resetForm();
        await this.searchList();
      } else {
        if (this.isDetail) {
          this.title = "流程节点详情";
          this.rulesThis = {};
        } else {
          this.title = "修改流程节点";
          this.rulesThis = this.rules;

        }
        await this.resetForm();
        await this.searchList();
        await this.initData();
      }
      await this.action_type_change();
      this.loading = false;
    },
    async initData() {
      const { code, data, message } = await asyncRequest.detail({ id: this.id });
      if (code === 0) {
        Object.keys(this.ruleForm).forEach((key) => {
          if (key === "next_action_ids") {
            console.log(key);
            this.ruleForm[key] = [];
            if (this.id !== "add") {
              data[key].forEach((s) => {
                if ((s ?? "") !== "") {
                  this.ruleForm[key].push(s);
                }
              });
            }
          } else {
            this.ruleForm[key] = this.id === "add" ? "" : data[key] + "";
          }
        });
        console.log(this.ruleForm);
      } else if (code >= 100 && code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(message);
      }
    },
    async action_type_change() {
      const { action_type } = this.ruleForm;
      this.rulesThis.next_action_ids[0].required =
        action_type === "1" || action_type === "2";
    },
    async resetForm() {
      // 重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          this.ruleForm = {
            id: this.id,
            process_id: "",
            action_type: "",
            operation_type: "",
            status_name: "",
            order_process: "",
            next_action_ids: [],
            is_master:"0",
            remark: "",
          };
        }
      });
    },
    // 刷新表格
    async searchList() {
      this.loading = true;
      const model = {
        page: 1, // 页码
        size: 1000, // 每页显示条数
        process_id: this.activeId,
        action_type: "",
        operation_type: "",
      };
      const { code, data, message } = await asyncRequest.list(model);
      this.tableData = [];
      if (code === 0) {
        const { list } = data ?? {};
        let arr = list ?? [];
        arr.forEach((si) => {
          let type = si.action_type + "";
          si.id += "";
          // if (type === "2" || type === "4") {
          this.tableData.push(si);
          // }
        });
        console.log(this.tableData);
      } else if (code >= 100 && code <= 104) {
        await this.logout();
      } else {
        this.tableData = [];
        this.$message.error(message);
      }
      this.loading = false;
    },
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          if (this.loading) return;
          this.loading = true;
          let obj = JSON.parse(JSON.stringify(this.ruleForm));
          obj.process_id = this.activeId;
          let res = {};
          if (this.id === "add") {
            delete obj["id"];
            res = await asyncRequest.add(obj);
          } else {
            res = await asyncRequest.update(obj);
          }
          this.loading = false;
          const { code, message } = res;
          if (code === 0) {
            const title = this.id === "add" ? "添加成功" : "修改成功";
            this.$notify.success({
              title,
              message: "",
            });
            this.showModelThis = false;
            // 刷新
            this.$emit("refresh");
          } else if (code >= 100 && code <= 104) {
            await this.logout();
          } else {
            this.$message.warning(message);
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.setActionProcess {
  .setWidth {
    width: 100% !important;
    // display: flex;
    // display: block !important;
    // .el-input__inner {
    //   display: flex;
    //   width: 1000px !important;
    //   display: block !important;
    // }
  }
}
</style>
