<template>
  <a-card>
    <!-- 高级搜索区域 -->
    <audit-advanced-query
      :expand="expand"
      :search-data="advSearchForm"
      :ref-name="searchform"
      :search-style="{ height: '210px', left: '20px', top: '60px' }"
      :search-fun="advSearch"
      @searchedClick="searchedClick"
      @resetForm="resetForm"
    >
      <template>
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="审计发现标题" prop="findTitle">
              <a-input v-model="advSearchForm.findTitle" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="项目名称" prop="projectName">
              <a-input v-model="advSearchForm.projectName" />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="整改责任主体" prop="auditedUnit">
              <sd-group-picker
                ref="unitSelect"
                v-model="advSearchForm.auditedUnit"
                :single="true"
                :read-only="false"
                @change="changeunit"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item :label="'整改状态'" prop="Status">
              <sd-select
                v-model="advSearchForm.Status"
                :allow-clear="true"
                :options="StatusOptions"
              />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12" class="recitifyrectstatus">
            <a-form-model-item :label="'确认状态'" prop="confirmStatus">
              <sd-select
                v-model="advSearchForm.confrimStatus"
                :allow-clear="true"
                :options="confirmStatusOptions"
              />
            </a-form-model-item>
          </a-col>
        </a-row>
      </template>
    </audit-advanced-query>
    <div :class="[$style.btns]">
      <audit-advanced-export
        ref="orgexport"
        :class-style="[$style.buttonSpacing]"
        :exclebxh="false"
        :expressions="ipmortExpressions"
        :config-id="71"
        :v-if="false"
      />
    </div>
    <sd-data-table-ex
      ref="list"
      :projectlist="true"
      form-id="iamRectifyCancel"
      page-id="audit/work/rectify/iamRectifyCancel"
      :columns="columns"
      :actions="actions"
      show-selection
      data-url="api/xcoa-mobile/v1/iam-rectify-cancel/getList"
      :show-advance-query="true"
      :search-fields="['findingTitle']"
      :filter-expressions="filterExpressions"
      :process-req="processReq"
      @searchbtnClick="searchbtnClick"
    >
      <div slot="islink" slot-scope="text, record">
        <a :title="text" @click="rowClick(record)">{{ text }}</a>
      </div>
      <div slot="opt" slot-scope="text, record" :class="$style.operatecol">
        <!-- 管理员 项目组长 项目副组长 则可以变更 -->
        <span v-for="(bt, index) in buttonsfortable" :key="index">
          <a-button
            v-if="record.flag"
            :key="index"
            type="link"
            size="small"
            @click="optbuttonClick(bt.id, record)"
            >{{ bt.label }}
            <!-- <sd-icon :type="bt.icontype" :theme="bt.theme"/> -->
          </a-button>
        </span>
      </div>
    </sd-data-table-ex>

    <div v-show="false">
      <sd-user-picker
        ref="assignTeamLeader"
        v-model="mainSendUnit"
        :single="true"
        :read-only="false"
        @change="change"
      />
    </div>
  </a-card>
</template>

<script>
import axios from '@/common/services/axios-instance'
import { Modal } from 'ant-design-vue'
import crossWindowWatcher from '@/common/services/cross-window-watcher'
import TableActionTypes from '@/common/services/table-action-types'
import TableColumnTypes from '@/common/services/table-column-types'
import auditAdvancedExport from '../../../components/audit-advanced-export.vue'
import auditAdvancedQuery from '../../../components/audit-advanced-query.vue'
import auditAdvancedQueryMixins from '../../../components/audit-advanced-query-mixins'
import AuditRectplanprojectService from '../../rectplanproject/audit-rectplanproject-service'
import components from './_import-components/iam-audit-rectify-list-import'
const columns = [
  {
    title: '序号',
    dataIndex: 'sortNumber',
    width: '80px',
    customRender: (text, record, index) => `${index + 1}`,
  },
  {
    title: '整改计划子表id',
    dataIndex: 'rectPlanFindId',
    sdHidden: true,
  },
  {
    title: '审计发现id',
    dataIndex: 'findingId',
    sdHidden: true,
  },
  {
    title: '审计发现标题',
    dataIndex: 'findingTitle',
    // sdClickable: true, // 列内容是否可点击
    // scopedSlots: { customRender: 'islink' },
    width: '300px',
  },
  {
    title: '审计发现编号',
    dataIndex: 'findingCode',
    defaultSortOrder: 'descend',
    sorter: true,
    sortDirections: ['ascend', 'descend'],
    width: '200px',
  },
  {
    title: '项目名称',
    dataIndex: 'projectName',
    sorter: true,
    sortDirections: ['ascend', 'descend'],
    width: '450px',
  },
  {
    title: '整改责任主体',
    dataIndex: 'auditedUnitName',
    width: '150px',
  },
  {
    title: '整改状态',
    dataIndex: 'rectStatus',
    width: '100px',
  },
  {
    title: '整改完成日期',
    dataIndex: 'rectFinishDate',
    sdRender: TableColumnTypes.date,
    width: '170px',
  },
  {
    title: '确认状态',
    dataIndex: 'confirmStatus',
    width: '100px',
  },
  {
    title: '确认人姓名',
    dataIndex: 'confirmName',
    width: '150px',
  },
  {
    title: '确认日期',
    dataIndex: 'confirmTime',
    sdRender: TableColumnTypes.date,
    width: '100px',
  },
  {
    title: '操作',
    dataIndex: 'id',
    scopedSlots: { customRender: 'opt' },
    width: '150px',
  },
  {
    dataIndex: 'id',
    sdHidden: true,
  },
]
export default {
  name: 'IamAuditRectifyList',
  metaInfo: {
    title: '整改销号',
  },
  components: { ...components, auditAdvancedQuery, auditAdvancedExport },
  mixins: [auditAdvancedQueryMixins, AuditRectplanprojectService],
  data() {
    return {
      columns,
      filterExpressions: [],
      ipmortExpressions: [],
      mainSendUnit: [],
      actions: [
        {
          label: '确认',
          id: 'new',
          // type: TableActionTypes.ex.create,
          permission: 'iamRectifyCancel-create',
          type: TableActionTypes.primary,
          callback: this.confirmrectify,
        },
        {
          label: '导出',
          id: 'export',
          // type: TableActionTypes.ex.create,
          permission: 'iamRectifyCancel-create',
          callback: this.export,
        },
      ],
      searchform: 'searchform',
      expand: false,
      advSearchForm: {
        projectName: '',
        findTtile: '',
        auditedUnit: '',
        Status: [],
        confrimStatus: [],
      },
      unitName: '',
      StatusOptions: [],
      confirmStatusOptions: [],
      buttonsfortable: [
        {
          label: '指定整改确认人',
          id: 'changeconfirmuser',
          showCondition: '',
        },
      ],
    }
  },
  mounted() {
    // 初始化数据字典信息
    this.initDictionaryInfo()
  },
  methods: {
    initDictionaryInfo() {
      axios({
        url: 'api/xcoa-mobile/v1/iam-law/dictionary?key=RECT_STATUS',
        method: 'get',
      }).then((res) => {
        if (res.status === 200) {
          this.StatusOptions = res.data
        }
      })
      axios({
        url: 'api/xcoa-mobile/v1/iam-law/dictionary?key=CONFIRMS_STATUS',
        method: 'get',
      }).then((res) => {
        if (res.status === 200) {
          this.confirmStatusOptions = res.data
        }
      })
    },
    changeunit(values) {
      this.unitName = ''
      values.forEach((element) => {
        this.unitName = element.name
      })
    },
    rowClick(record) {
      const url = '/iam-audit-rectify-form?record=' + record.id + '&edit=false'
      crossWindowWatcher.waitForChanged(url).then((refreshFlag) => {
        if (refreshFlag) {
          this.$refs.list.refresh()
        }
      })
    },
    confirmrectify() {
      debugger
      const selectedRowKeys = this.$refs.list.getSelectedRowKeys()
      if (selectedRowKeys.length === 0) {
        Modal.info({
          content: '请选择需要确认的数据！',
        })
        return false
      } else if (selectedRowKeys.length > 1) {
        axios({
          url:
            'api/xcoa-mobile/v1/iam-rectify-cancel/updateRowKeys?ids=' + selectedRowKeys.join(','),
          method: 'get',
        }).then(() => {
          this.$refs.list.refresh()
        })
        return true
      }
      axios({
        url:
          'api/xcoa-mobile/v1/iam-rectify-cancel/getConfirmStatus?id=' + selectedRowKeys.join(','),
        method: 'get',
      }).then((res) => {
        if (res.data) {
          const selectRows = this.$refs.list.getSelectedRows()
          const record = selectRows[0]
          const url = '/iam-audit-rectify-form?record=' + record.id + '&edit=true'
          crossWindowWatcher.waitForChanged(url).then((refreshFlag) => {
            if (refreshFlag) {
              this.$refs.list.refresh()
            }
          })
        } else {
          Modal.info({
            content: '已确认无法再次确认！',
          })
        }
      })
    },
    advSearch() {
      console.log('--1')
      this.filterExpressions = []
      // 项目名称
      if (this.advSearchForm.projectName) {
        this.filterExpressions.push({
          dataType: 'str',
          name: 'projectName',
          op: 'like',
          stringValue: `%${this.advSearchForm.projectName}%`,
        })
      }
      // 发现标题
      if (this.advSearchForm.findTitle) {
        this.filterExpressions.push({
          dataType: 'str',
          name: 'findingTitle',
          op: 'like',
          stringValue: `%${this.advSearchForm.findTitle}%`,
        })
      }
      // 项目组长
      if (this.advSearchForm.auditedUnit) {
        this.filterExpressions.push({
          dataType: 'str',
          name: 'auditedUnitName',
          op: 'like',
          stringValue: `%${this.unitName}%`,
        })
      }
      if (this.advSearchForm.Status.length > 0) {
        this.filterExpressions.push({
          dataType: 'str',
          name: 'rectStatus',
          op: 'eq',
          stringValue: `${this.advSearchForm.Status[0].id}`,
        })
      }
      if (this.advSearchForm.confrimStatus.length > 0) {
        this.filterExpressions.push({
          dataType: 'str',
          name: 'confirmStatus',
          op: 'eq',
          stringValue: `${this.advSearchForm.confrimStatus[0].id}`,
        })
      }
      // this.expand = false
    },
    optbuttonClick(val, record) {
      // 变更编辑人
      if (val === 'changeconfirmuser') {
        const jsonArry = []
        this.mainSendUnit = jsonArry
        this.projectId = record.id
        setTimeout(() => {
          this.$refs.assignTeamLeader.openPicker()
        }, 0)
      }
    },
    export() {
      this.$refs.orgexport.exportdata()
    },
    // 重置年份
    resetForm() {
      this.advSearchForm.confirmStatus = []
      this.advSearchForm.Status = []
      this.advSearchForm.auditedUnit = ''
      this.advSearchForm.confrimStatus = []
      this.unitName = ''
    },
    // // 项目组长配置保存
    change(val) {
      const params = {}
      let codes = ''
      if (val.length > 0) {
        val.forEach((e) => {
          codes += e.code + ','
        })
        if (codes.indexOf(',') > -1) {
          codes = codes.substr(0, codes.length - 1)
        }
        params[this.projectId] = codes
      }
      return new Promise((resolve) => {
        AuditRectplanprojectService.changeconfirmEditor(params).then((res) => {
          if (res.data) {
            this.$refs.list.clearSelection()
            this.$refs.list.refresh()
          }
        })
      })
    },
    processReq(req) {
      const url = req.url
      if (url === 'api/xcoa-mobile/v1/iam-rectify-cancel/getList') {
        this.ipmortExpressions = req.data.expressions
      }

      return req
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
.btns {
  position: absolute;
  top: 24px;
  right: 99.5px;
  z-index: 100;
}
.buttonSpacing {
  margin-left: 5px;
}
:global(.recitifyrectstatus) {
  :global(.ant-form-item-label) {
    margin-left: 2em;
  }
}
</style>
