<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-col :span="8">
        <el-form-item label="流程名称" prop="proname">
          <el-input v-model="queryParams.proname" />
        </el-form-item>
      </el-col>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" style="margin-bottom: 8px">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:PROTEMPLATE:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:PROTEMPLATE:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:PROTEMPLATE:remove']"
          >删除</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table :data="PROTEMPLATEList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="流程编码" align="center" prop="procode" />
      <el-table-column label="流程名称" align="center" prop="proname">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="showProname(scope.row.id)"
            v-hasPermi="['business:PROTEMPLATE:remove']"
            >{{ scope.row.proname }}</el-button
          >
        </template>
      </el-table-column>
      <el-table-column label="版本" align="center" prop="version" />
      <el-table-column label="关键字" align="center" prop="keyword" />
      <el-table-column label="排序" align="center" prop="seqno" />
      <el-table-column label="流程说明" align="center" prop="pronotes" />
      <el-table-column label="服务层地址" align="center" prop="serviceUrl" />
      <el-table-column label="前端查看页面地址" align="center" prop="infoUrl" />
      <el-table-column label="是否启用" align="center" prop="status">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.type"
            active-color="#13ce66"
            inactive-color="#ff4949"
            @change="handleStatus(scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:PROTEMPLATE:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:PROTEMPLATE:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改流程模板对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-form-item label="流程编码" prop="procode">
          <el-input
            v-model="form.procode"
            placeholder="请输入流程编码"
            type="text"
          />
        </el-form-item>
        <el-form-item label="流程名称" prop="proname">
          <el-input
            v-model="form.proname"
            placeholder="请输入流程名称"
            type="text"
          />
        </el-form-item>
        <el-form-item label="关键字" prop="keyword">
          <el-input
            v-model="form.keyword"
            placeholder="请输入关键字"
            type="text"
          />
        </el-form-item>
        <el-form-item label="排序" prop="seqno">
          <el-input v-model="form.seqno" placeholder="排序" type="number" />
        </el-form-item>
        <el-form-item label="流程说明" prop="pronotes">
          <el-input
            v-model="form.pronotes"
            placeholder="请输入流程说明"
            type="text"
          />
        </el-form-item>
        <el-form-item label="服务层地址" prop="serviceUrl">
          <el-input
            v-model="form.serviceUrl"
            placeholder="请输入服务层地址"
            type="text"
          />
        </el-form-item>
        <el-form-item label="前端页面地址" prop="infoUrl">
          <el-input
            v-model="form.infoUrl"
            placeholder="请输入前端页面地址"
            type="text"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 列出流程节点对话框 -->
    <el-dialog
      title="流程节点"
      :visible.sync="openProname"
      width="90%"
      append-to-body
    >
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleProAdd"
            v-hasPermi="['business:PRODEFACTION:add']"
            >新增</el-button
          >
        </el-col>
      </el-row>
      <el-table :data="pronameform" style="width: 100%">
        <el-table-column prop="acname" label="节点名称"></el-table-column>
        <el-table-column
          prop="dealhours"
          label="办理期限 小时"
        ></el-table-column>
        <!--        <el-table-column prop="pronotes" label="流程说明"></el-table-column>-->
        <el-table-column prop="exelIds" label="审核人">
          <template slot-scope="scope">
            {{ searchUserName(scope.row.exelIds) }}
          </template>
        </el-table-column>
        <el-table-column prop="reemail" label="邮件提醒">
          <template slot-scope="scope">
            <span v-if="scope.row.reemail == 1">提醒</span>
            <span v-else-if="scope.row.reemail == 2">不提醒</span>
            <span v-else>未知</span>
          </template>
        </el-table-column>
        <el-table-column prop="remessage" label="消息提醒">
          <template slot-scope="scope">
            <span v-if="scope.row.remessage == 1">提醒</span>
            <span v-else-if="scope.row.remessage == 2">不提醒</span>
            <span v-else>未知</span>
          </template>
        </el-table-column>
        <el-table-column prop="seqno" label="活动顺序"></el-table-column>
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleProUpdate(scope.row)"
              v-hasPermi="['business:PRODEFACTION:edit']"
              >修改</el-button
            >
            <el-popconfirm
              title="确定删除吗？"
              @confirm="handleProDelete(scope.row)"
              v-hasPermi="['business:PRODEFACTION:remove']"
            >
              <el-button
                slot="reference"
                size="mini"
                type="text"
                icon="el-icon-delete"
                >删除</el-button
              >
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>

    <!-- 添加或修改流程节点对话框 -->
    <el-dialog
      :title="Protitle"
      :visible.sync="openProAddOrEdit"
      width="60%"
      append-to-body
    >
      <el-form ref="form2" :model="Proform" label-width="140px">
        <el-form-item label="节点名称" prop="acname">
          <el-input
            v-model="Proform.acname"
            placeholder="请输入节点名称"
            type="text"
          />
        </el-form-item>
        <el-form-item label="办理期限 小时" prop="dealhours">
          <el-input
            v-model="Proform.dealhours"
            placeholder="请输入办理期限 小时"
            type="text"
          />
        </el-form-item>
        <!--        <el-form-item label="流程说明" prop="pronotes">-->
        <!--          <el-input-->
        <!--            v-model="Proform.pronotes"-->
        <!--            placeholder="请输入流程说明"-->
        <!--            type="text"-->
        <!--          />-->
        <!--        </el-form-item>-->
        <el-form-item label="审核人" prop="exelIds">
          <el-select v-model="selectValue" placeholder="请选择">
            <el-option
              v-for="item in this.user"
              :key="item.value + 'exelIds'"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="邮件提醒" prop="reemail">
          <el-radio v-model="Proform.reemail" label="1">提醒</el-radio>
          <el-radio v-model="Proform.reemail" label="2">不提醒</el-radio>
        </el-form-item>
        <el-form-item label="消息提醒" prop="remessage">
          <el-radio v-model="Proform.remessage" label="1">提醒</el-radio>
          <el-radio v-model="Proform.remessage" label="2">不提醒</el-radio>
        </el-form-item>
        <el-form-item label="活动顺序" prop="seqno">
          <el-input
            v-model="Proform.seqno"
            placeholder="请输入活动顺序"
            type="text"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm2">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listPROTEMPLATE,
  getPROTEMPLATE,
  delPROTEMPLATE,
  addPROTEMPLATE,
  updatePROTEMPLATE,
  getProcode,
  delPro,
  getProInfo,
  addPro,
  updatePro,
  getUser,
  enableOrDisable
} from "@/api/wf/PROTEMPLATE";

export default {
  name: "PROTEMPLATE",
  data() {
    return {
      selectValue: "",
      //用户列表
      user: [],
      pronameform: [],
      // 根路径
      baseURL: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 流程模板表格数据
      PROTEMPLATEList: [],
      // 弹出层标题
      title: "",
      Protitle: "",
      // 是否显示弹出层
      open: false,
      openProname: false,
      openProAddOrEdit: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        delFlag: null,
        procode: null,
        proname: null,
        version: null,
        keyword: null,
        seqno: null,
        pronotes: null,
        linkurl: null,
        serviceUrl: null,
        infoUrl: null,
      },
      // 表单参数
      form: {},
      Proform: {},
      // 表单校验
      rules: {
        id: [{ required: true, message: "$comment不能为空", trigger: "blur" }],
        delFlag: [
          { required: true, message: "删除标识不能为空", trigger: "blur" },
        ],
        procode: [
          { required: true, message: "流程编码不能为空", trigger: "blur" },
        ],
        proname: [
          { required: true, message: "流程名称不能为空", trigger: "blur" },
        ],
        version: [{ required: true, message: "版本不能为空", trigger: "blur" }],
        keyword: [
          { required: true, message: "关键字不能为空", trigger: "blur" },
        ],
        seqno: [{ required: true, message: "排序不能为空", trigger: "blur" }],
        pronotes: [
          { required: true, message: "流程说明不能为空", trigger: "blur" },
        ],
        serviceUrl: [
          { required: true, message: "服务层地址不能为空", trigger: "blur" },
        ],
        infoUrl: [
          {
            required: true,
            message: "前端查看页面地址不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleStatus(row){
      enableOrDisable(row)
      this.getList();
    },
    searchUserName(id) {
      let fined = this.user.find((item) => item.value == id);
      return fined.label;
    },
    handleProAdd() {
      //节点的新增
      this.reset();
      this.openProAddOrEdit = true;
      this.Protitle = "新增节点";
      this.Proform.proId = this.PROTEMPLATEID;
    },
    handleProUpdate(r) {
      //节点的修改
      this.reset();
      this.openProAddOrEdit = true;
      getProInfo(r.id).then((res) => {
        this.Protitle = "修改节点";
        this.Proform = res.data;
        this.Proform.proId = this.PROTEMPLATEID;
        this.selectValue = Number(this.Proform.exelIds);
      });
    },
    handleProDelete(r) {
      delPro(r.id).then((res) => {
        this.showProname(r.proId);
      });
    },
    /** 查询流程模板列表 */
    getList() {
      this.loading = true;
      listPROTEMPLATE(this.queryParams).then((response) => {
        this.PROTEMPLATEList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    //展示节点列表
    showProname(id) {
      this.openProname = true;
      this.PROTEMPLATEID = id;

      //获取流程节点列表
      getProcode(id).then((res) => {
        this.pronameform = res.rows;
      });

      this.user = []; //获取并清空用户列表
      getUser().then((res) => {
        for (const i in res.data) {
          this.user.push({
            value: res.data[i].userId,
            label: res.data[i].username,
          });
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openProAddOrEdit = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        delFlag: null,
        procode: null,
        proname: null,
        version: null,
        keyword: null,
        seqno: null,
        pronotes: null,
        linkurl: null,
        serviceUrl: null,
        infoUrl: null,
      };

      this.Proform = {
        acname: null,
        approvaltype: null,
        keyword: null,
        seqno: null,
        pronotes: null,
        exelIds: null,
        parentnr: null,
        proId: null,
        reemail: null,
        remessage: null,
        seqno: null,
      };
      this.resetForm("form");
      this.resetForm("form2");
      this.selectValue = "";
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加流程模板";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getPROTEMPLATE(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改流程模板";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updatePROTEMPLATE(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPROTEMPLATE(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 提交按钮 流程节点*/
    submitForm2() {
      this.$refs["form2"].validate((valid) => {
        this.Proform.exelIds = this.selectValue; //审核人
        if (valid) {
          if (this.Proform.id != null) {
            updatePro(this.Proform).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.openProAddOrEdit = false;
              this.showProname(this.PROTEMPLATEID);
            });
          } else {
            addPro(this.Proform).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.openProAddOrEdit = false;
              this.open = false;
              this.showProname(this.PROTEMPLATEID);
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm("是否确认删除已选择的流程模板?")
        .then(function () {
          return delPROTEMPLATE(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
