<script>
/* eslint-disable */
// sd-skip-auto-import
import cloneDeep from 'lodash.clonedeep'
import qs from 'qs'
import { router } from '@/common/router-ready'
import { Modal, message } from 'ant-design-vue'
import lawCassTree from '../../../../../src_product/iam/law/case/law-case-tree.vue'
import LawService from '../../../../../src_product/iam/law/law-service'
import XmSingleDataService from './xm-single-data-service'
function at(n) {
  n = Math.trunc(n) || 0
  if (n < 0) n += this.length
  if (n < 0 || n >= this.length) return undefined
  return this[n]
}
const TypedArray = Reflect.getPrototypeOf(Int8Array)
for (const C of [Array, String, TypedArray]) {
  Object.defineProperty(C.prototype, 'at', {
    value: at,
    writable: true,
    enumerable: false,
    configurable: true,
  })
}
import components from './_import-components/xm-law-progress-open-task-handler-import'
var _render = function e() {
  var t = this,
    s = t._self._c
  const info = []
  if (this.strtype) {
    if (t.stage.length > 0) {
      t.stage.forEach((ti) => {
        info.push(
          s('a-select-option', { attrs: { value: ti.code }, key: ti.code }, [t._v(ti.name)])
        )
      })
    }
  } else {
    if (t.zxstage.length > 0) {
      t.zxstage.forEach((ti) => {
        info.push(
          s('a-select-option', { attrs: { value: ti.code }, key: ti.code }, [t._v(ti.name)])
        )
      })
    }
  }
  return s(
    'div',
    [
      s(
        'a-modal',
        {
          attrs: {
            'destroy-on-close': '',
            title: t.modaltitle,
            width: 800,
            'ok-text': '确定',
            'cancel-text': '取消',
            'body-style': { minHeight: '380px' },
            'confirm-loading': false,
            'mask-closable': false,
          },
          on: { ok: t.treeok, cancel: () => (t.treevisible = false) },
          model: {
            value: t.treevisible,
            callback: function (e) {
              t.treevisible = e
            },
            expression: 'treevisible',
          },
        },
        [
          s(
            'div',
            [
              s('a-spin', { class: t.$style.spin, attrs: { spinning: t.spinning } }),
              t.empty ? s('a-empty') : t._e(),
              t.ztreeData
                ? s(
                    'a-tree',
                    t._g(
                      t._b(
                        {
                          attrs: {
                            'show-icon': '',
                            selectable: true,
                            'tree-data': t.ztreeData,
                            'default-expand-parent': true,
                            'default-expanded-keys': t.expandedKeys,
                            'selected-keys': t.$attrs['selected-keys'] || t.selectedKeys,
                            'load-data': t.onLoadData,
                            'replace-fields': { title: 'text', key: 'id' },
                          },
                          on: { select: t.onSelect, 'update:selectedKeys': t.setSelectedKeys },
                          scopedSlots: t._u(
                            [
                              {
                                key: 'custom',
                                fn: function (e) {
                                  return [
                                    s('span', [t._v(t._s(e.name))]),
                                    'true' === e.props.hasPoint && t.isAdmin
                                      ? s('a-badge', {
                                          class: t.$style.badge,
                                          attrs: { status: 'error' },
                                        })
                                      : t._e(),
                                  ]
                                },
                              },
                            ],
                            null,
                            false,
                            877135721
                          ),
                        },
                        'a-tree',
                        t.$attrs,
                        false
                      ),
                      t.restListeners
                    )
                  )
                : t._e(),
            ],
            1
          ),
        ],
        1
      ),
      s(
        'a-modal',
        {
          attrs: {
            'destroy-on-close': '',
            title: t.modaltitle,
            width: 800,
            'ok-text': '确定',
            'cancel-text': '取消',
            'confirm-loading': false,
            // 'body-style': { minHeight: '200px' },
            'mask-closable': false,
          },
          on: { ok: t.ok, cancel: () => (t.visible = false) },
          model: {
            value: t.visible,
            callback: function (e) {
              t.visible = e
            },
            expression: 'visible',
          },
        },
        [
          s('a-form-model', { props: { labelCol: { span: 7 }, wrapperCol: { span: 15 } } }, [
            s('a-form-model-item', { attrs: { label: '阶段名称', required: true } }, [
              s(
                'a-select',
                {
                  on: { change: t.handleChange },
                  model: {
                    value: t.Val,
                    callback: function (e) {
                      t.Val = e
                    },
                    expression: 'Val',
                  },
                },
                info,
                1
              ),
            ]),
          ]),
        ],
        1
      ),
    ],
    1
  )
}
var staticRenderFns = []
_render._withStripped = true
export default {
  name: 'SdBatchModifyHandler',
  components: { ...components, lawCassTree: lawCassTree },
  data() {
    return {
      visible: false,
      Val: '',
      strtype: true, // true纠纷 否则案件
      stage: [],
      zxstage: [],
      treevisible: false,
      button: {},
      initValues: [],
      childTable: void 0,
      selectedRows: [],
      event: [],
      spinning: true,
      empty: false,
      treeData: null,
      ztreeData: null,
      expandedKeys: [],
      selectedKeys: [],
      selectedNode: null,
      defaultSelectedKeys: [],
      isNewWindow: '',
      parentid: '', // 父节点
      jdinfo: [],
      modaltitle: '纠纷进展阶段',
    }
  },
  mounted() {
    LawService.getAllCaseTree().then((r) => {
      this.spinning = false
      this.expandedKeys = ['0']
      this.selectedKeys = ['0']
      const treeNode = [
        {
          id: '0',
          text: '案件进展阶段',
          leaf: false,
          props: {},
          children: r.data,
          key: '0',
        },
      ]
      this.treeData = this.transformData(treeNode)
      this.generateList(this.treeData)
      // this.ztreeData = null
      if (this.treeData.length < 1) this.empty = true
    })
    //获取纠纷阶段
    LawService.getAlljfStage().then((res) => {
      const stage = []
      res.data.data.forEach((d) => {
        stage.push({
          name: d.STAGE_NAME,
          code: d.STAGE_CODE,
          businessTypeId: d.text0,
        })
      })
      this.stage = stage
    })
    //获取执行进展阶段
    LawService.getAllcaseproStage().then((res) => {
      const stage = []
      res.data.forEach((d) => {
        stage.push({
          name: d.EXECUTE_NAME,
          code: d.EXECUTE_CODE,
          businessTypeId: d.businessTypeId,
        })
      })
      this.zxstage = stage
    })
  },
  methods: {
    run({ button: e, eventScript: t, context: c }) {
      const strtype = t.strtype === '1' // true纠纷 否则案件
      this.strtype = strtype
      this.event = t
      let zt = ''
      // 模块表单id
      const appModuleId = t.appModuleId
      //通过接口获取最新的数据
      const { fieldInfos } = c
      let flowstatus = c.args[0].STEP_NAME ? c.args[0].STEP_NAME : c.args[0].CURRENT_STATUS
      let beanid = 'onl:9f03a7c8fda944deb87aaaabb305953b' // 纠纷数据BEAN
      const id = fieldInfos.find((f) => f.name === 'id')?.value
      if (!strtype) {
        beanid = 'onl:e4097c3684fc4f6ebd1eae43679f38b0' // 案件数据BEAN
      }

      if (strtype) {
        const strjftype = t.strjftype === '1' // true 进展 否则结案
        let jfid = fieldInfos.find((f) => f.name === 'DISPUTE_ID')?.value
        if (jfid === null || jfid === undefined || jfid === '') {
          jfid = fieldInfos.find((f) => f.name === 'id')?.value
        }
        // 判断打开新建页面 还是打开正在处理中的纠纷进展
        LawService.getProgressData(jfid).then((res1) => {
          let flag = true // true 是弹出纠纷登记 false 是不弹出纠纷登记
          if (appModuleId === '' || appModuleId === undefined) {
            flag = false
          }
          let djurl = ''
          let jzurl = ''
          let parentid = ''
          if (res1.data.length > 1) {
            // 有纠纷登记
            djurl = res1.data[0].props.detailUrl + '/' + res1.data[0].props.detailInstid
            if (res1.data[0]) {
              parentid = res1.data[1].id
            }
            if (res1.data[1].children && res1.data[1].children.length > 0) {
              if (
                res1.data[1].children[res1.data[1].children.length - 1].props.stepStatus === '0'
              ) {
                // 有纠纷进展并且未办结
                jzurl =
                  res1.data[1].children[res1.data[1].children.length - 1].props.detailUrl +
                  '/' +
                  res1.data[1].children[res1.data[1].children.length - 1].props.detailInstid
                flag = false
              } else {
                // 有纠纷登记且办结
              }
            }
          } else {
            // 无纠纷登记 弹出选择阶段名称
          }
          t.id = jfid
          if (appModuleId === '' || appModuleId === undefined) {
            // 打开查看页面 永远打开最后一个页面的
            this.openlastpage(res1.data, jfid)
          } else if (flag && strjftype) {
            // 新建纠纷进展
            t.parentid = parentid
            t.zt = 'progress'
            this.Val = ''
            this.visible = true
          } else {
            let url = ''
            if (!strjftype) {
              zt = 'finish'
              // 判断返回的是否有结案数据，无则打开结案新建页面，否则打开历史
              let jaflag = true // true 新建 false 为打开历史
              if (res1.data.length > 2) {
                if (res1.data[2].props.stepType === 'finish') {
                  jaflag = false
                }
              }
              if (jaflag) {
                // 业务类型 新建纠纷结案页面
                t.zt = 'finish'
                // 判断所有纠纷进展是否都办结
                XmSingleDataService.getBeanByFormId(id, beanid).then((res) => {
                  flowstatus = res.data.pageFormData.pageFieldInfos.find(
                    (o) => o.name === 'STEP_NAME'
                  )?.value
                  if (!flowstatus) {
                    flowstatus = res.data.pageFormData.pageFieldInfos.find(
                      (o) => o.name === 'CURRENT_STATUS'
                    )?.value
                  }

                  if (
                    flowstatus === '已结束' ||
                    flowstatus === '' ||
                    flowstatus === undefined ||
                    flowstatus === null
                  ) {
                    this.opennewja()
                  } else {
                    Modal.warning({
                      title: '提示',
                      content: '存在未发布的进展信息，无法结案',
                    })
                  }
                })
              } else {
                url = res1.data[2].props.detailUrl + '/' + res1.data[2].props.detailInstid
                url = url.replace('#/sd-webflow', '/law-progress-page')
                url = url + '?zt=' + zt + '&id=' + jfid // 打开结案页面
                this.open(url)
              }
            } else {
              if (jzurl !== '') {
                zt = 'progress'
                // 打开正在处理中的文档
                jzurl = jzurl.replace('#/sd-webflow', '/law-progress-page')
                url = jzurl + '?zt=' + zt + '&id=' + jfid // 打开登记页面
              } else {
                zt = 'regist'
                djurl = djurl.replace('#/sd-webflow', '/law-progress-page')
                url = djurl + '?zt=' + zt + '&id=' + jfid // 打开登记页面
              }
              this.open(url)
            }
          }
        })
        // })
      } else {
        // 案件 判断目前是 案件进展 案件结案 案件执行？
        const strcasetype = t.strcasetype // 进展1 否则结案2 执行3 执行结案4
        const { fieldInfos } = c
        let caseid = fieldInfos.find((f) => f.name === 'CASE_ID')?.value
        if (caseid === null || caseid === undefined || caseid === '') {
          caseid = fieldInfos.find((f) => f.name === 'id')?.value
        }
        LawService.getProgressData(caseid).then((res1) => {
          this.jdinfo = res1.data
          let parentid = ''
          // 打开列表页面
          if (appModuleId === '' || appModuleId === undefined) {
            // 打开查看页面 永远打开最后一个页面的
            this.openlastpage(res1.data, caseid)
          } else if (strcasetype === '1') {
            this.modaltitle = '案件进展阶段'
            // 判断进展数据是否有未办结的
            zt = 'jz'
            let flag = true // true 才新建案件进展，弹出选择树，否则展示进展页面
            let casesburl = ''
            let url = ''
            parentid = 'progress'
            if (res1.data.length > 1) {
              // 有案件登记
              const objsz = res1.data.filter((i) => i.props.stepType === 'progress')
              const obj = objsz.at(-1)
              if (obj && obj.id === res1.data.at(-1).id) {
                parentid = obj.id
              }
              if (
                obj &&
                obj.id === res1.data.at(-1).id &&
                obj.children &&
                obj.children.length > 0
              ) {
                const cobj = obj.children[obj.children.length - 1]
                if (cobj.children && cobj.children.length > 0) {
                  if (cobj.children[cobj.children.length - 1].props.stepStatus === '0') {
                    // 有案件登记并且未办结
                    casesburl =
                      cobj.children[cobj.children.length - 1].props.detailUrl +
                      '/' +
                      cobj.children[cobj.children.length - 1].props.detailInstid
                    flag = false
                  }
                }
              }
            }
            if (flag) {
              // 打开案件进展树新建页面
              t.id = caseid
              t.parentid = parentid
              // 获取案件类型
              const ini = setInterval(() => {
                if (this.treeData.length > 0 && this.treeData[0].children) {
                  clearInterval(ini)
                  const casetype = fieldInfos.find((f) => f.name === 'CASE_TYPE')?.value
                  const objszc = this.treeData.length > 0 ? this.treeData[0]?.children : []
                  const obj = objszc.find((i) => i.id === casetype)
                  if (obj !== null && obj !== undefined) {
                    this.ztreeData = [obj]
                    this.treevisible = true
                  }
                }
              }, 100)
            } else {
              // 展示信息
              // 打开正在处理中的文档
              casesburl = casesburl.replace('#/sd-webflow', '/case-progress-page')
              url = casesburl + '?id=' + caseid + '&zt=progress' // 打开登记页面
              this.open(url)
            }
          } else if (strcasetype === '3') {
            // 执行进展 3
            this.modaltitle = '执行进展阶段'
            let jxjzurl = ''
            let flag = true // 判断打开已办 还是新建
            const objsz = res1.data.filter((i) => i.props.stepType === 'execute')
            const obj = objsz.at(-1)
            parentid = 'execute'
            if (obj && obj.id === res1.data.at(-1).id) {
              parentid = obj.id
            }
            if (obj && obj.id === res1.data.at(-1).id && obj.children && obj.children.length > 0) {
              if (obj.children[obj.children.length - 1].props.stepStatus === '0') {
                // 有案件执行进展并且进展未办结
                jxjzurl =
                  obj.children[obj.children.length - 1].props.detailUrl +
                  '/' +
                  obj.children[obj.children.length - 1].props.detailInstid
                flag = false
              } else {
                // 有纠纷登记且办结
              }
              parentid = obj.id
              // zt = obj.id
            }
            if (flag) {
              // 案件执行进展
              // 案件执行阶段获取
              t.id = caseid
              t.parentid = parentid
              t.zt = 'execute'
              this.Val = ''
              this.visible = true
            } else {
              // 打开正在处理中的文档
              jxjzurl = jxjzurl.replace('#/sd-webflow', '/case-progress-page')
              const url = jxjzurl + '?id=' + caseid + '&zt=execute' // 打开登记页面
              this.open(url)
            }
          } else {
            // 结案 案件结案 和执行结案 executeFinish
            if (strcasetype === '2') {
              t.zt = 'finish'
            } else {
              t.zt = 'executeFinish'
              beanid = 'onl:a19ea9fa44194e089af096fe243a9707'
            }
            t.id = caseid
            // 判断所有进展是否都办结
            // 判断所有执行进展是否都办结
            XmSingleDataService.getBeanByFormId(id, beanid).then((res) => {
              flowstatus = res.data.pageFormData.pageFieldInfos.find(
                (o) => o.name === 'STEP_NAME'
              )?.value
              if (!flowstatus) {
                flowstatus = res.data.pageFormData.pageFieldInfos.find(
                  (o) => o.name === 'CURRENT_STATUS'
                )?.value
              }
              if (
                flowstatus === '已结束' ||
                flowstatus === '结束' ||
                flowstatus === '' ||
                flowstatus === undefined ||
                flowstatus === null
              ) {
                this.opennewja()
              } else {
                Modal.warning({
                  title: '提示',
                  content: '存在未发布的进展信息，无法结案',
                })
              }
            })
          }
        })
      }
    },
    ok() {
      const t = this.event
      let businessTypeId = ''
      if (this.Val !== '') {
        this.visible = false
        let url = ''
        if (this.strtype) {
          const obj = this.stage.find((o) => o.code === this.Val)
          businessTypeId = obj.businessTypeId.toString()
          XmSingleDataService.getTypeBusId(businessTypeId).then((res) => {
            businessTypeId = res.data[0].id
            url =
              t.comTaskLink +
              '/pages/draft/0?businessTypeId=' +
              businessTypeId +
              '&zt=' +
              t.zt +
              '&id=' +
              t.id +
              '&stage=' +
              this.Val +
              '&parentid=' +
              t.parentid
            this.open(url)
          })
        } else {
          const obj = this.zxstage.find((o) => o.code === this.Val)
          businessTypeId = obj.businessTypeId.toString()
          url =
            t.comTaskLink +
            '/pages/draft/0?businessTypeId=' +
            businessTypeId +
            '&zt=' +
            t.zt +
            '&id=' +
            t.id +
            '&stage=' +
            this.Val +
            '&parentid=' +
            t.parentid
          this.open(url)
        }
      } else {
        Modal.warning({
          title: '提示',
          content: '请选择阶段名称',
        })
      }
    },
    handleChange(e) {
      if (!this.Val) this.Val = e
    },
    treeok() {
      const t = this.event
      if (this.selectedNode.props.businessTypeId && this.selectedNode.props.businessTypeId !== '') {
        this.treevisible = false
        // 打开新建页面
        // 需要判断是否要增加二级节点
        const objsz = this.jdinfo.filter((i) => i.props.stepType === 'progress')
        const obj = objsz.at(-1)
        let parentid = t.parentid
        let ext = ''
        if (obj) {
          const zobj = obj.children.at(-1) // 最后一个节点是否是和当前的属于同一个类型
          if (obj.id === this.jdinfo.at(-1).id) {
            if (
              zobj &&
              this.selectedNode.props.parentCode === zobj.props.progressCode &&
              this.selectedNode.props.parentId === zobj.props.stagecode
            ) {
              ext = '&flag=1'
              parentid = zobj.id
            } else {
              parentid = obj.id
            }
          }
        }
        const businessTypeId = this.selectedNode.props.businessTypeId
        const url =
          '/case-progress-page/pages/draft/0?businessTypeId=' +
          businessTypeId +
          '&id=' +
          t.id +
          '&cs=' +
          this.selectedNode.props.parentId +
          '&stage=' +
          this.selectedNode.id +
          '&ct=' +
          this.selectedNode.props.parentCode +
          '&parentid=' +
          parentid +
          '&zt=progress' +
          ext
        this.open(url)
      } else {
        Modal.warning({
          title: '提示',
          content: '请选择程序名称',
        })
        return false
      }
    },
    transformData(data) {
      return data.map((d) => {
        const { children, ...rest } = d
        if (children && children.length > 0) {
          children.forEach((c) => {
            c.props.parentName = d.text
            if (c.children && c.children.length > 0) {
              c.children.forEach((cc) => {
                cc.props.parentCode = d.id
              })
            }
            // c.props.parentCode = d.props.parentCode ? d.props.parentCode : d.id
          })
        }
        return {
          ...rest,
          children: children && this.transformData(children),
          scopedSlots: { title: 'title' },
        }
      })
    },
    // 处理搜索用的dataList
    generateList(data) {
      for (let i = 0; i < data.length; i++) {
        const node = data[i]
        if (node.children) {
          if (node.children) {
            node.leaf = true
          }
          this.generateList(node.children)
        }
      }
    },
    onSelect(e, t) {
      this.selectedNode = t.node.dataRef
    },
    setSelectedKeys(e, t) {
      if (0 === e.length) return
      if (!this.$attrs['selected-keys']) this.selectedKeys = e
    },
    onLoadData(e) {
      if (e.dataRef.children) return Promise.resolve()
      let caseType = ''
      let caseStage = ''
      if (e.dataRef.props.parentId === 'root') {
        caseType = e.dataRef.id
      } else {
        caseType = e.dataRef.props.parentId
        caseStage = e.dataRef.id
      }
      return LawService.getCaseTree(caseType, caseStage).then((res) => {
        res.data.forEach((d) => {
          d.props.parentName = e.dataRef.text
          d.props.parentCode = e.dataRef.props.parentId
        })
        e.dataRef.children = res.data
        this.treeData = this.transformData([...this.treeData])
        this.generateList(this.treeData)
      })
    },
    opennewja() {
      const t = this.event
      const strtype = t.strtype === '1' // true纠纷 否则案件
      const strcasetype = t.strcasetype // 进展1 否则结案2 执行3 执行结案4
      let text = ''
      let confirm = ''
      if (strtype) {
        text = '纠纷'
        confirm = '结案后不可再编辑纠纷信息，是否确定结案？'
      } else {
        if (strcasetype === '2') {
          text = '案件'
          confirm = '结案后案件不可再继续添加阶段和程序信息，也不可编辑已发布信息'
        } else {
          text = '执行'
          confirm = '结案后不可再编辑案件执行信息，是否确定结案？'
        }
      }
      Modal.confirm({
        title: text + '结案',
        content: confirm,
        okText: '确定',
        onOk: () => {
          // const businessTypeId = t.businessTypeId
          // 模块表单id
          const appModuleId = t.appModuleId
          // 打开新建页面
          const extobj = { moduleId: appModuleId }
          const ext = qs.stringify('&extParams=' + extobj)

          XmSingleDataService.getTypeBusId(appModuleId).then((res) => {
            const businessTypeId = res.data[0].id
            const url =
              t.comTaskLink +
              '/pages/draft/0?businessTypeId=' +
              businessTypeId +
              ext +
              '&zt=' +
              t.zt +
              '&parentid=' +
              t.zt +
              '&id=' +
              t.id
            this.open(url)
          })
        },
      })
    },
    openlastpage(data, id) {
      const obj = data.at(-1)
      let url = ''
      if (obj.props.stepType === 'progress') {
        // 进展
        if (obj.children && obj.children.length > 0) {
          const zobj = obj.children.at(-1)
          if (zobj.props.detailUrl && zobj.props.detailUrl !== '') {
            url = this.fngetobjurl(zobj, id)
          } else {
            if (zobj.children && zobj.children.length > 0) {
              const zcobj = zobj.children.at(-1)
              if (zcobj.props.detailUrl && zcobj.props.detailUrl !== '') {
                url = this.fngetobjurl(zcobj, id)
              }
            }
          }
        }
      } else if (obj.props.stepType === 'execute') {
        if (obj.children && obj.children.length > 0) {
          const zobj = obj.children.at(-1)
          if (zobj.props.detailUrl && zobj.props.detailUrl !== '') {
            url = this.fngetobjurl(zobj, id)
          }
        }
      } else {
        if (obj.props.detailUrl && obj.props.detailUrl !== '') {
          url = this.fngetobjurl(obj, id)
        }
      }
      if (url === '') {
        url = this.fngetobjurl(data[0], id)
      }
      this.open(url)
    },
    fngetobjurl(obj, id) {
      let url = ''
      url = obj.props.detailUrl + '/' + obj.props.detailInstid
      const t = this.event
      const page = t.comTaskLink
      url = url.replace('#/sd-webflow', page)
      url = url + '?zt=' + obj.props.stepType + '&id=' + id // 打开结案页面
      return url
    },
    open(url) {
      const t = this.event
      const isNewWindow = t.newWindow === '1'
      const view = t.strjftype === 'view' || t.strcasetype === 'view'
      const query = {}
      if (view) {
        url = url + '&view=1'
      }
      const oRoute = router.resolve({
        path: url,
        query: {
          ...query,
        },
      })
      if (isNewWindow) {
        window.open(oRoute.href, '_blank')
      } else {
        router.push(oRoute.route.fullPath)
      }
    },
  },
  render: _render,
  staticRenderFns: staticRenderFns,
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
</style>
