<template>
  <div id="containt">
    <el-tabs v-model="activeName" class="tab-box">
      <el-tab-pane :label="$t1('常规')" name="first">
        <itemEditView
          v-if="showItemEdit"
          visible-key="showItemEdit"
          :_dataId.sync="dataId"
          :wfObjConfig.sync="pRow"
          :parent-target="_self"
          @reload="$reloadHandle"
        ></itemEditView>
      </el-tab-pane>
      <el-tab-pane :label="$t1('列表')" name="second">
        <div class="grid-height">
          <vxe-grid ref="table-m2" v-bind="vxeOption1" @resizable-change="$vxeTableUtil.onColumnWitchChange"
                    @custom="$vxeTableUtil.customHandle">
            <template #form>
              <div style="margin-bottom: 8px;" class="clearfix">
                <div class="fl">
                  <vxe-button status="primary" class="button-sty" icon="el-icon-plus" @click="openEditDialog">
                    {{ $t1('新增流程模板') }}
                  </vxe-button>
                </div>
                <div class="fr">
                  <vxe-button icon="el-icon-brush" class="button-sty" @click="resetEvent" type="text" status="primary"
                              plain>{{ $t1('重置') }}
                  </vxe-button>
                  <vxe-button status="warning" icon="el-icon-search" class="button-sty" @click="searchEvent">
                    {{ $t1('搜索') }}
                  </vxe-button>
                </div>
              </div>
              <!--              <vxe-form ref="form" class="screen-box" :data-form="formData" title-width="92px" title-align="right"
                                      @submit="searchEvent" @reset="searchEvent">
                              <vxe-form-item title="单据类型名称:" field="objTypeName">
                                <template v-slot="">
                                  <el-input v-model="formData.objTypeName" size="small" clearable/>
                                </template>
                              </vxe-form-item>
                              <vxe-form-item title="单据类型编号:" field="objTypeCode">
                                <template v-slot="">
                                  <el-input v-model="formData.objTypeCode" size="small" clearable/>
                                </template>
                              </vxe-form-item>
                            </vxe-form>-->
            </template>
            <template #row_edit>
              <a href="#" class="a-link"><i class="el-icon-edit"/></a>
            </template>
          </vxe-grid>
        </div>
      </el-tab-pane>
    </el-tabs>

    <el-dialog
      :title="$t1('流程设计')"
      :append-to-body="true"
      :modal-append-to-body="true"
      :close-on-click-modal="false"
      v-if="showWfDialog"
      :visible.sync="showWfDialog"
      :modal="false"
      custom-class="dialog-style designer-dialog"
      width="100%"
      height="100%"
      top="0px"
      bottom="0px"
      :before-close="closeDesignwinEvent"
      :destroy-on-close="true"
      v-el-drag-dialog
    >
      <iframe :src="wfUrl" :style="'width:100%;height:' + ($root.$el.clientHeight - 30 - 12) + 'px'"></iframe>
    </el-dialog>

    <userDialog v-if="showUserDialog" :visiable.sync="showUserDialog" @confirm="confirmUser"
                :multi.sync="wfAssignMulti"/>
    <positionDialog v-if="showPositionDialog" :visiable.sync="showPositionDialog" @confirm="confirmPosition"/>
    <!--    <formTemplateDialog v-if="showFormTemplateDialog" :visiable.sync="showFormTemplateDialog" @confirm="confirmFormTemplate"/>-->
  </div>
</template>
<script>
import {treeScolly} from '@base/utils/global.js';
import itemEditView from './itemEdit_form.vue';

import userDialog from '../../../../views/user/user/dialog.vue';
import positionDialog from '../../../../views/user/position/dialog.vue';

// import formTemplateDialog from '../../../../views/user/form/form_template/dialog'


export default {
  name: 'wf_obj_config:list',
  components: {
    itemEditView,
    userDialog,
    positionDialog,
    // formTemplateDialog
  },
  created() {
  },
  mounted() {
    treeScolly({target: this, type: 'default'});
    // this.initTableList();
    this.initTableM2();

    this.ph = this.$root.$el.clientHeight;
    this.initWfWinParam();
  },
  data() {
    return {
      dataId: 0,
      showEdit: false,
      showItemEdit: false,
      vxeOption: {},
      vxeOption1: {},
      formData: {},
      editId: '',
      itemEditId: '',
      pRow: '',
      activeName: 'second',
      currentRow: {},
      serviceId: null,
      itemServiceId: null,

      showWfDialog: false,
      wfUrl: '',
      ph: '',
      dialogActiveName: 'first',
      showFunctionScriptList: false,
      showUserDialog: false,
      wfAssignData: [],
      wfAssignCallback: null,
      wfAssignMulti: true,

      showPositionDialog: false,
      wfAssignPositionData: [],
      wfAssignPositionCallback: null,

      showFormTemplateDialog: false,
      wfAssignFormTemplateCallback: null,
    };
  },
  watch: {
    dialogActiveName(val) {
      if (val == 'second') {
        this.showFunctionScriptList = true;
      }
    }
  },
  methods: {
    searchEvent() {
      this.$refs['table-m2'].commitProxy('reload');
    },
    resetEvent() {
      this.formData = {};
      this.$refs['table-m2'].commitProxy('reload');
    },
    openEditDialog(id) {
      this.dataId = !id || typeof id == 'object' ? 0 : id;
      this.activeName = 'first';
      this.$openEditView('showItemEdit');
    },
    initTableM2() {
      var that = this;
      let tableOption1 = {
        vue: that,
        tableRef: 'table-m2',
        tableName: 'user_wf_obj_config_item_list_form-m2',
        path: USER_PREFIX + '/wf_obj_config_item/list',
        param: () => {
          return {
            ...this.formData,
            objConfigId: 0
          };
        },
        config: {
          proxyConfig: {
            props: {
              result: 'objx', // 配置响应结果列表字段
              total: 'objx.length' // 配置响应结果总页数字段
            },
          }
        },
        columns: [
          {type: 'checkbox', width: 48, resizable: false, fixed: 'left'},
          {
            title: this.$t1('流程模版名称'),
            field: 'modelName',
            width: 150,
            fixed: 'left'
          },
          {
            title: this.$t1('流程模版KEY'),
            field: 'modelKey',
            width: 150
          },
          {
            title: this.$t1('单据类型编码'),
            field: 'objTypeCode',
            width: 150
          },
          {
            title: 'modelId',
            field: 'modelId',
            width: 150
          },
          {
            field: 'createDate',
            title: this.$t1('创建时间'),
            width: 200
          },
          {
            width: 150,
            fixed: 'right',
            sortable: false,
            title: '',
            slots: {
              default: ({row}) => {
                return [
                  <a
                    href="javascript:void(0);"
                    class="a-link"
                    onclick={() => {
                      this.openEditDialog(row.id);
                    }}
                  >
                    <el-tooltip enterable={false} effect="dark" content={this.$t1('查看')} placement="top"
                                popper-class="tooltip-skin">
                      <i class="el-icon-edit"/>
                    </el-tooltip>
                  </a>,
                  <a
                    href="javascript:void(0);"
                    onClick={() => {
                      this.wfEdit(row.modelId);
                    }}
                    class="a-link"
                  >
                    <el-tooltip enterable={false} effect="dark" content={this.$t1('流程设计')} placement="top"
                                popper-class="tooltip-skin">
                      <i class="iconfont icon-liuchengguanli-shejiqi_liucheng"/>
                    </el-tooltip>
                  </a>,
                  <a
                    href="javascript:void(0);"
                    class="a-link"
                    onClick={() => {
                      this.deleteItem(row);
                    }}
                  >
                    <el-tooltip enterable={false} effect="dark" content={this.$t1('删除')} placement="top"
                                popper-class="tooltip-skin">
                      <i class="el-icon-delete"/>
                    </el-tooltip>
                  </a>
                ];
              }
            }
          }
        ]
      };
      this.$vxeTableUtil.initVxeTable(tableOption1).then(opts => {
        that.vxeOption1 = opts;
      });
    },
    chooseCellM1({row}) {
      if (row && row.id) {
        let $table1 = this.$refs['table-m1'];
        this.$nextTick(() => {
          $table1.clearCheckboxRow();
          $table1.setCheckboxRow(row, true);
        });
        this.currentRow = row;
        this.$http({
          method: 'post',
          url: USER_PREFIX + '/wf_obj_config_item/list',
          data: {objConfigId: row.id || ''},
          success: res => {
            let rows = res.objx || [];
            /* rows.forEach(function(item) {
							item.name = row.name;
						}); */
            this.$refs['table-m2'].loadData(rows);
          }
        });
      } else {
        this.$refs['table-m2'].loadData([]);
      }
    },

    wfEdit(actModelId) {
      let serviceId = 'sf-cloud-user';
      //wfType 0:单据流程，1：场景流程
      this.wfUrl = WEB_PREFIX + '/activiti-explorer/modeler.html?wfType=1&serviceId=' + serviceId + '&modelId=' + actModelId;
      this.dialogActiveName = 'first';
      this.showWfDialog = true;
    },
    closeDesignwinEvent(done) {
      this.$baseConfirm(this.$t1('请确认"已保存"流程模板，是否继续关闭？')).then(() => {
        this.showWfDialog = false;
      });
    },
    initWfWinParam() {
      this.initCloseWfDesignWin();
      this.initWinUserDialog();
      this.initWinPositionDialog();
      this.initSysParamForWf();
      this.initWinFormTemplateDialog();
    },
    initCloseWfDesignWin() {
      window.closeWfDesignWin = () => {
        this.showWfDialog = false;
      };
    },
    initWinUserDialog() {
      window.openAssignUserDialog = opts => {
        this.wfAssignMulti = opts.multi;
        this.wfAssignData = opts.data || [];
        this.wfAssignCallback = opts.callback;
        this.showUserDialog = true;
      };
    },
    confirmUser(rows) {
      this.wfAssignCallback(rows);
    },
    initWinPositionDialog() {
      window.openAssignPositionDialog = opts => {
        this.wfAssignPositionData = opts.data || [];
        this.wfAssignPositionCallback = opts.callback;
        this.showPositionDialog = true;
      };
    },
    initSysParamForWf() {
      let webPrefix = WEB_PREFIX;
      let baseApi = process.env.VUE_APP_BASE_API;
      window.sysParamForWf = {
        webPrefix,
        baseApi
      }
    },
    confirmPosition(rows) {
      this.wfAssignPositionCallback(rows);
    },
    deleteItem(row) {
      this.$baseConfirm(this.$t1('您确定要删除吗？')).then(() => {
        this.$http({
          method: 'post',
          url: USER_PREFIX + '/wf_obj_config_item/delete',
          data: {id: row.id},
          success: res => {
            this.$message({
              message: res.content,
              type: 'success',
              duration: 500,
            });
            this.$refs['table-m2'].remove(row);
          }
        });
      });
    },
    initWinFormTemplateDialog() {
      window.openAssignFormTemplateDialog = opts => {
        this.wfAssignFormTemplateCallback = opts.callback;
        this.showFormTemplateDialog = true;
      };
    },
    confirmFormTemplate(rows) {
      this.wfAssignFormTemplateCallback(rows);
    },
  }
};
</script>
