<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page.sync="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">
      <template slot="menuLeft">
        <el-button
                   size="small"
                   plain
                   v-if="permission.publicevent_add"
                   @click="addEvent">上报事件
        </el-button>
      </template>
      <template slot-scope="scope" slot="menu">
        <el-button size="small"
                   v-if="permission.publicevent_view"
                   @click.stop="handleDetail(scope.row)">详情
        </el-button>
        <el-button size="small"
                   v-if="permission.publicevent_view"
                   @click.stop="handleImage(scope.row,scope.index)">跟踪
        </el-button>
      </template>
      <template slot-scope="{row}"
                slot="finish">
        <el-tag>{{row.finish==='finish' ? '已完成' : '未完成'}}</el-tag>
      </template>
    </avue-crud>

    <el-dialog title="流程图"
               append-to-body
               :visible.sync="flowBox"
               :fullscreen="true">
      <iframe
        style="border: solid 2px #dddddd;background: white"
        :src=flowUrl
        width="100%"
        height="800"
        title="流程图"
        marginWidth="0"
        marginHeight="0"
        allowTransparency="yes">
      </iframe>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="flowBox = false">关 闭</el-button>
      </span>
    </el-dialog>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove} from "@/api/order/publicevent";
  import {mapGetters} from "vuex";
  import {startList} from "../../api/work/work";

  export default {
    data() {
      return {
        form: {},
        query: {},
        flowBox: false,
        loading: true,
        processDefId: '',
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          editBtn: false,
          delBtn: false,
          addBtn: false,
          height:'auto',
          calcHeight: 30,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: true,
          viewBtn: false,
          selection: true,
          dialogClickModal: false,
          column: [
            {
              label: "姓名",
              prop: "name",
              rules: [{
                required: true,
                message: "请输入姓名",
                trigger: "blur"
              }]
            },
            {
              label: "联系方式",
              prop: "tel",
              rules: [{
                required: true,
                message: "请输入联系方式",
                trigger: "blur"
              }]
            },
            {
              label: "事件详情",
              prop: "eventDetail",
              rules: [{
                required: true,
                message: "请输入事件详情",
                trigger: "blur"
              }]
            },
            {
              label: "流程定义主键",
              prop: "processDefinitionId",
              hide: true,
              rules: [{
                required: true,
                message: "请输入流程定义主键",
                trigger: "blur"
              }]
            },
            {
              label: "流程实例主键",
              prop: "processInstanceId",
              hide: true,
              rules: [{
                required: true,
                message: "请输入流程实例主键",
                trigger: "blur"
              }]
            },
            {
              label: "受理人",
              prop: "auditor",
              rules: [{
                required: true,
                message: "请输入受理人",
                trigger: "blur"
              }]
            },
            {
              label: "事件发生地点",
              prop: "location",
              rules: [{
                required: false,
                message: "请输入受理人",
                trigger: "blur"
              }]
            },
            {
              label: "处理人",
              prop: "handlePerson",
              rules: [{
                required: true,
                message: "请输入处理人",
                trigger: "blur"
              }]
            },
            {
              label: "当前步骤",
              prop: "currentStep",
              rules: [{
                required: true,
                message: "请输入当前步骤",
                trigger: "blur"
              }]
            },
            {
              label: "进度",
              prop: "finish",
              slot: true,
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.publicevent_add, false),
          viewBtn: this.vaildData(this.permission.publicevent_view, false),
          delBtn: this.vaildData(this.permission.publicevent_delete, false),
          editBtn: this.vaildData(this.permission.publicevent_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      handleDetail(row) {
        this.$router.push({ path: `/work/process/publicevent/detail/${row.processInstanceId}/${row.id}`});
      },
      handleImage(row) {
        this.flowUrl = `/api/blade-flow/process/diagram-view?processInstanceId=${row.processInstanceId}`;
        this.flowBox = true;
      },
      rowSave(row, done, loading) {
        add(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          console.log(error);
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      addEvent(){
        let path = `/work/process/publicEvent/form/`+this.processDefId;
        this.$router.push({path});
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      async onLoad(page, params = {}) {
        this.loading = true;
        this.processDefId = (await startList(1,10,{category: 'flow_3'})).data.data.records[0].id;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

<style>
</style>
