<template>
  <a-modal
    :title="'请选择'"
    :visible="modalPapervisible"
    destroy-on-close
    :mask-closable="false"
    :body-style="{
      padding: 0,
      minHeight: '700px',
    }"
    :width="modalWidth"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <div :class="[$style.wrapHeight, $style.operatepaperkjmodel]">
      <div :class="$style.rowHeight">
        <AuditOperateTree
          :key="key"
          :checkable="false"
          :draggable="false"
          :is-search="true"
          :show-line="true"
          :top-node-text="'历史项目'"
          :treeparams="{
            configId: '0',
          }"
          :default-expanded-keys="['0']"
          :is-select-dep="false"
          :dep-options="[]"
          @treeSelect="treeSelect"
          @depChanged="depChanged"
        ></AuditOperateTree>
        <div :class="$style.rightcard">
          <a-card :class="$style.acard">
            <sd-data-table-ex
              ref="auditOperatePaperTreeTable"
              form-id="iamWorkPaper"
              page-id="audit/work/operate/paper/iamWorkPaper"
              :columns="columns"
              :actions="actions"
              show-selection
              :filter-expressions="expressions"
            >
              <div slot="islink" slot-scope="text, record">
                <a :title="text" @click="rowClick(record)">{{ text }}</a>
              </div>
            </sd-data-table-ex>
          </a-card>
        </div>
      </div>
    </div>
  </a-modal>
</template>

<script>
import { Modal } from 'ant-design-vue'
import TableActionTypes from '@/common/services/table-action-types'
import AuditOperateTree from '../../../../components/audit-operate-tree.vue'
import AuditOperatePaperService from './audit-operate-paper-service'
import components from './_import-components/audit-operate-paper-kj-model-import'

export default {
  name: 'AuditOperateKjPaperModel',
  metaInfo: {
    title: '审计框架model',
  },
  components: {
    ...components,
    AuditOperateTree,
  },
  props: {
    // 弹出窗宽度
    modalWidth: {
      type: String,
      default: '1200px',
    },
    modalPapervisible: {
      type: Boolean,
      default: false,
    },
    projectId: {
      type: String,
      default: '',
    },
    classId: {
      type: Number,
      default: -1,
    },
    change: {
      type: Function,
      default: null,
    },
    refresh: {
      type: Function,
      default: null,
    },
  },
  data() {
    return {
      key: 0,
      treeData: [],
      parentId: 0,
      defaultSelectedKeys: [this.orgId],
      infolist: [],
      values: [],
      valuesdemo: [],
      columns: [
        {
          title: '序号',
          customRender: (text, record, index) => `${index + 1}`,
          width: '10%',
        },
        {
          title: '底稿名称',
          dataIndex: 'paperTitle',
          scopedSlots: { customRender: 'islink' },
          width: '25%',
        },
        {
          title: '底稿编号',
          dataIndex: 'paperCode',
          width: '15%',
        },
        {
          title: '编制人员',
          dataIndex: 'creatorName',
          width: '15%',
        },
        { dataIndex: 'instId', title: '流程ID', sdHidden: true },
        { dataIndex: 'flowState', title: '底稿状态' },
      ],
      actions: [
        {
          label: '新建',
          id: 'new',
          type: TableActionTypes.oa.create, // 新建按钮，不需要回调，自动处理
        },
        {
          label: '删除',
          id: 'delete',
          type: TableActionTypes.oa.delete, // 删除按钮，不需要回调，会自动处理
        },
      ],
      radioStyle: {
        height: '30px',
        lineHeight: '30px',
      },
      orgId: null,
      orgName: '',
      rootNode: {},
      expressions: [
        {
          dataType: 'int',
          name: 'projectId',
          op: 'eq',
          intValue: -1,
        },
      ],
    }
  },
  methods: {
    treeSelect(selectedKeys, info) {
      this.parentId = info.node.dataRef.id // 父级id
      this.parentName = info.node.dataRef.text // 父级名称
      this.parentObj = [{ id: info.node.dataRef.id, name: info.node.dataRef.text }]
      if (info.node.dataRef.props.projectId !== undefined) {
        this.expressions.forEach((item) => {
          if (item.name === 'projectId') {
            item.intValue = Number(info.node.dataRef.props.projectId)
          }
        })
        this.$refs.auditOperatePaperTreeTable.refresh(true)
      } else {
        this.expressions.forEach((item) => {
          if (item.name === 'projectId') {
            item.intValue = -1
          }
        })
      }
    },
    // 部门下拉框选择事件
    depChanged(value, info) {
      // console.log(value) // 选中的值
      // console.log(info.data.props) // 选中的下拉json
    },
    getvalues(orgId) {
      AuditOperatePaperService.getEditPermission(orgId).then((res) => {
        const value = res.data
        value.hierarchyManager = JSON.parse(value.hierarchyManager)
        value.moduleEditPermissionList.forEach((item) => {
          item.manager = JSON.parse(item.manager)
        })
        this.values = value
      })
    },
    saveedit() {
      const value = this.values
      if (value.hierarchyManager === null) {
        value.hierarchyManager = []
      } else {
        value.hierarchyManager = JSON.stringify(value.hierarchyManager)
      }
      value.moduleEditPermissionList.forEach((item) => {
        if (item.manager === null) {
          item.manager = []
        } else {
          item.manager = JSON.stringify(item.manager)
        }
      })
      AuditOperatePaperService.saveEditPermission(value).then((res) => {
        if (res.data === true) {
          Modal.warning({
            title: '提示',
            content: '保存成功!',
          })
        } else {
          Modal.warning({
            title: '提示',
            content: '保存失败，请联系管理员!',
          })
        }
        // this.getvalues(this.parentId)
      })
    },
    handleCancel() {
      // this.modalPapervisible = false
      this.change()
    },
    handleOk(e) {
      const selectedRowKeys = this.$refs.auditOperatePaperTreeTable.getSelectedRowKeys()
      if (selectedRowKeys.length === 0) {
        Modal.warning({
          title: '提示',
          content: '请选择需要复制的工作底稿!',
        })
        return false
      } else {
        const params = []
        selectedRowKeys.forEach((i) => {
          params.push({
            id: i.toString(),
            frameClassId: this.classId.toString(),
            projectId: this.projectId.toString(),
            itemId: '',
          })
        })
        AuditOperatePaperService.fnCopyPaper(params).then((res) => {
          if (res.data.flag === 'true') {
            this.refresh()
          } else {
            Modal.warning({
              title: '提示',
              content: '复制工作底稿失败，请联系管理员!',
            })
          }
        })
        this.change()
      }
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
.wrap-height {
  :global(.treewrap_audit-operate-tree_product) {
    width: 20%;
  }
  height: 100%;
  :global(.ant-card-body) {
    height: 100%;
  }
  :global(.collapse_audit-operate-tree_product) {
    :global(.fold_audit-operate-tree_product) {
      left: 0;
    }
  }
  .row-height {
    display: flex;
    flex: auto;
    min-height: 500px;
    .rightcard {
      flex: 1;
      width: calc(100% - 20%);
      height: 100%;
      .acard {
        min-height: 100%;
      }
      :global(.ant-card-body) {
        width: 98%;
        padding: 0;
      }
    }
  }
}
// .wrap-height {
//   height: 100%;
//   .row-height {
//     display: flex;
//     flex: auto;
//     height: 100%;
//     .rightcard {
//       flex: 1;
//       width: calc(100% - 20%);
//       height: 100%;
//     }
//   }
// // }
// .moduletable {
//   border-top: 1px solid grey;
//   width: 100%;
// }
// .buttonstyle {
//   border-top: 1px solid grey;
//   text-align: center;
//   height: 30px;
//   line-height: 30px;
// }
</style>
