import editView from "@base/views/bd/setting/form_template/itemEdit.vue";
import wfObjConfigDialog from "@base/views/bd/setting/form_template/editWfObjConfigDialog.vue";
import {getJsxBtn, getJsxBtnList, getJsxStatus} from "@base/views/bd/setting/utils/index";

let modules = {};
modules = {
  name: 'itemList',
  components: {editView, wfObjConfigDialog},
  props: ['formTemplate'],
  data() {
    return {
      activeName: 'second',
      dataId: 0,
      showEdit: false,
      vxeOption: {},
      formData: {},
      wfObjConfig: {},
      wfObjConfigId: null,
      showWfObjConfigDialog: false
    };
  },
  mounted() {
    this.initWfObjConfig();
    this.initTableList();
  },
  methods: {
    searchEvent() {
      this.$refs['table-m1'].commitProxy('reload');
    },
    resetEvent() {
      this.formData = {};
      this.$refs['table-m1'].commitProxy('reload');
    },
    openEditDialog(id) {
      this.dataId = !id || typeof id === 'object' ? 0 : id;
      this.activeName = 'first';
      this.$openEditView('showEdit');
    },
    initTableList() {
      let that = this;
      let startModeMap = {
        1: this.$t1('节点无候选人，提示异常'),
        2: this.$t1('节点无候选人，提示警告'),
        3: this.$t1('节点无候选人，无提示'),
      }
      let tableOption1 = {
        vue: that,
        tableRef: 'table-m1',
        tableName: 'user_wf_obj_config_item_list-m1',
        path: "/" + this.formTemplate.serviceName + '/wf_obj_config_item/list',
        param: () => {
          return {
            objTypeCode: this.formTemplate.objTypeCode
          }
        },
        config: {
          maxHeight: '',
          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
          },
          {
            field: 'enabled',
            title: this.$t1('是否启用'),
            width: 150,
            slots: {
              default: ({row}) => {
                if (row.enabled) {
                  return getJsxStatus(null, this.$t1('启用'))
                } else {
                  return getJsxStatus('s-3', this.$t1('禁用'))
                }
              }
            }
          },
          {
            title: this.$t1('跳过相邻任务'),
            field: 'skipAdjacentTask',
            width: 150,
            slots: {
              default: ({row}) => {
                if (row.skipAdjacentTask) {
                  return getJsxStatus(null, this.$t1('是'))
                } else {
                  return getJsxStatus('s-3', this.$t1('否'))
                }
              }
            }
          },
          {
            title: this.$t1('驳回后提交模式'),
            field: 'toRejectNode',
            width: 180,
            slots: {
              default: ({row}) => {
                if (row.toRejectNode) {
                  return getJsxStatus(null, this.$t1('由提交人确定提交节点'))
                } else {
                  return getJsxStatus('s-3', this.$t1('由驳回人指定提交节点'))
                }
              }
            }
          },
          {
            title: this.$t1('流程启动处理'),
            field: 'startMode',
            width: 180,
            slots: {
              default: ({row}) => {
                return startModeMap[row.startMode] || '';
              }
            }
          },
          {
            title: this.$t1('跨任务跳过'),
            field: 'skipSameUserTask',
            width: 150,
            slots: {
              default: ({row}) => {
                if (row.skipSameUserTask) {
                  return getJsxStatus(null, this.$t1('是'))
                } else {
                  return getJsxStatus('s-3', this.$t1('否'))
                }
              }
            }
          },
          {
            field: 'createDate',
            title: this.$t1('创建时间'),
            width: 200
          },
          {
            width: 150,
            fixed: 'right',
            sortable: false,
            title: '',
            slots: {
              default: ({row}) => {
                return getJsxBtnList([
                  {
                    iconName: "el-icon-edit",
                    content: this.$t1('查看'),
                    onclick: () => {
                      this.openEditDialog(row);
                    }
                  },
                  {
                    iconName: "iconfont icon-liuchengguanli-shejiqi_liucheng",
                    content: this.$t1('流程设计'),
                    onclick: () => {
                      this.wfEdit(row.modelId);
                    }
                  },
                  {
                    iconName: "el-icon-delete",
                    content: this.$t1('删除'),
                    onclick: () => {
                      this.deleteItem(row);
                    }
                  },
                ]);
              }
            }
          }
        ]
      };
      this.$vxeTableUtil.initVxeTable(tableOption1).then(opts => {
        that.vxeOption = opts;
      });
    },
    deleteItem(row) {
      this.$baseConfirm(this.$t1('您确定要删除吗？')).then(() => {
        this.$http({
          aes: true,
          method: 'post',
          url: "/" + this.formTemplate.serviceName + '/wf_obj_config_item/delete',
          data: {id: row.id},
          success: res => {
            this.$message({
              message: res.content,
              type: 'success',
              duration: 500,
            });
            this.searchEvent()
          }
        });
      });
    },
    wfEdit(modelId) {
      this.$emit('openWfDesignDialog', modelId);
    },
    initWfObjConfig() {
      this.$http({
        aes: true,
        method: 'post',
        url: "/" + this.formTemplate.serviceName + '/wf_obj_config/getByObjTypeCode',
        data: {stringOne: this.formTemplate.objTypeCode},
        success: res => {
          let wfObjConfig = res.objx;
          if (wfObjConfig) {
            this.wfObjConfig = wfObjConfig;
          } else {
            this.openWfObjConfigDialog()
          }
        }
      });
    },
    openWfObjConfigDialog(dataId) {
      this.wfObjConfigId = dataId || null
      this.showWfObjConfigDialog = true;
    },
    confirmWfObjConfigDialog(wfObjConfig) {
      this.showWfObjConfigDialog = false;
      this.wfObjConfig = wfObjConfig || {};
    }
  }
};
export default modules
