<template>
  <div :class="$style.searchdiv">
    <a-card :class="$style.searchWrap">
      <a-form-model
        ref="advancedSearchForm"
        :class="$style.antAdvancedSearchForm"
        :model="form"
        layout="horizontal"
        v-bind="formItemLayout"
      >
        <a-row :gutter="24">
          <a-col>
            <a-form-model-item label="案件名称">
              <a-input v-model="form.CASE_NAME"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-model-item label="案件类型">
              <a-checkbox-group v-model="form.CASE_TYPE" :options="caseTypeOptions" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="我方地位">
              <a-checkbox-group v-model="form.OWNER_POSITION" :options="owerOptions" />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="24">
            <a-form-model-item label="案件状态">
              <a-checkbox-group v-model="form.CASE_STATUS" :options="caseStatusOptions" />
            </a-form-model-item>
          </a-col>
        </a-row>

        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-model-item label="案发单位">
              <sd-group-picker v-model="form.REPORT_UNIT_ID" :single="false" :read-only="false" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="案件创建时间">
              <a-range-picker
                v-model="form.CREATION_TIME"
                style="width: 100%"
                :time-range.sync="form.CREATION_TIME"
              />
            </a-form-model-item>
          </a-col>
        </a-row>

        <a-row :gutter="24">
          <a-col :span="24" :class="$style.amountNum">
            <a-form-model-item label="涉案金额（元）">
              <a-input-number v-model="form.CASE_AMOUNT_S"></a-input-number>至
              <a-input-number v-model="form.CASE_AMOUNT_E"></a-input-number>
            </a-form-model-item>
          </a-col>
        </a-row>

        <a-row>
          <a-col :span="16"> </a-col>
          <a-col :class="$style.searchbutton" :span="8">
            <div class="reportbuttonContent" style="margin-right: 15%">
              <a-button @click="handleReset"> 重置 </a-button>
              <a-button type="primary" @click="handleSearch"> 查询 </a-button>
              <a-button type="primary" @click="exportForm">导出</a-button>
              <audit-advanced-export
                ref="export"
                :class-style="[$style.buttonSpacing]"
                :v-if="false"
                :config-id="261"
                :exclebxh="false"
                :expressions="expressions"
              ></audit-advanced-export>
            </div>
          </a-col>
        </a-row>
      </a-form-model>
      <!-- <auditReportSearch :search="search" :expressions="expressions"> </auditReportSearch> -->
    </a-card>
    <a-card class="reporttablecardxm">
      <sd-data-table
        ref="sjzlTable"
        :form-id="'onlhead:d0bb8202233c48f0b5148776754461e1'"
        :filter-expressions="expressions"
        :columns="columns"
        data-url="api/framework/v1/page/businessList"
        :process-req="processReq"
        :show-advance-query="false"
        :defultpagination-pagesize="50"
        @dataLoaded="dataLoaded"
        @searchbtnClick="searchbtnClick"
      >
        <template slot="action" slot-scope="text, record">
          <a-button type="link" @click="openMm(record)"
            ><a class="opt-btn_sd-data-table_common">查看</a>
          </a-button>
        </template>
      </sd-data-table>
    </a-card>
  </div>
</template>

<script>
import moment from 'moment'
import { router } from '@/common/router-ready'
import auditAdvancedExport from '../../../components/audit-advanced-export'
import auditAdvancedQueryMixins from '../../../components/audit-advanced-query-mixins'
import LawService from '../../law-service'
import components from './_import-components/law-case-query-import'
import crossWindowWatcher from '@/common/services/cross-window-watcher'
import { message } from '@/common/one-ui'
export default {
  name: 'LawCaseQuery',
  metaInfo: {
    title: '案件查询',
  },
  components: {
    ...components,

    auditAdvancedExport,
  },
  mixins: [auditAdvancedQueryMixins],
  data() {
    return {
      expressions: [],
      caseTypeOptions: [],
      owerOptions: [],
      caseStatusOptions: [],
      columns: [
        {
          title: '序号',
          customRender: (text, record, index) => `${index + 1}`,
          width: '80px',
        },
        {
          title: '案件名称',
          dataIndex: 'CASE_NAME',
          sorter: true,
        },
        {
          title: '案件类型',
          dataIndex: 'CASE_TYPE',
          sorter: true,
        },
        {
          title: '上报单位',
          dataIndex: 'REPORT_UNIT_NAME',
          sorter: true,
        },
        {
          title: '我方诉讼地位',
          dataIndex: 'OWNER_POSITION',
          scopedSlots: { customRender: 'OWNER_POSITION' },
          sorter: true,
        },
        {
          title: '案件阶段',
          dataIndex: 'CASE_STATUS',
          sorter: true,
        },
        {
          title: '涉案金额展示',
          dataIndex: 'CASE_AMOUNT',
          sorter: true,
        },
        {
          dataIndex: 'instId',
          sdHidden: true,
        },
        {
          dataIndex: 'CASE_ID',
          sdHidden: true,
          sorter: true,
          defaultSortOrder: 'desc',
        },

        {
          title: '操作',
          dataIndex: '',
          scopedSlots: { customRender: 'action' },
          align: 'center',
          width: '80px',
        },
      ],

      formItemLayout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 14 },
      },
      form: {},
    }
  },
  created() {
    // 获取案件类型
    LawService.getdict('272818597314232320').then((res) => {
      const dict = []
      res.data.forEach((d) => {
        dict.push({
          label: d.label,
          value: d.value,
          disabled: false,
        })
      })
      this.caseTypeOptions = [...dict]
    })
    // 获取我方地位
    LawService.getdict('272888833908473856').then((res) => {
      const dict = []
      res.data.forEach((d) => {
        dict.push({
          label: d.label,
          value: d.value,
          disabled: false,
        })
      })
      this.owerOptions = [...dict]
    })
    // 案件状态
    LawService.getdict('284415197305765888').then((res) => {
      const dict = []
      res.data.forEach((d) => {
        dict.push({
          label: d.label,
          value: d.value,
          disabled: false,
        })
      })
      this.caseStatusOptions = [...dict]
    })
  },
  mounted() {},
  methods: {
    moment,
    openMm(record) {
      LawService.getProgressData(record.CASE_ID).then((res1) => {
        this.openlastpage(res1.data, record.CASE_ID)
      })
    },

    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 page = '/case-progress-page'
      url = url.replace('#/sd-webflow', page)
      url = url + '?zt=' + obj.props.stepType + '&id=' + id // 打开结案页面
      return url
    },
    open(url) {
      const query = {}

      url = url + '&view=1'

      const oRoute = router.resolve({
        path: url,
        query: {
          ...query,
        },
      })

      window.open(oRoute.href, '_blank')
    },
    handleSearch() {
      this.$refs.advancedSearchForm.validate((valid, values) => {
        if (valid) {
          this.search(this.form)
        }
      })
    },
    handleReset() {
      this.$refs.advancedSearchForm.resetFields()
      this.form = {}
    },
    exportForm() {
      if (!this.$refs.sjzlTable.data || this.$refs.sjzlTable.data.length === 0) {
        message.warning('未查询出可导出数据', 1)
        return
      }

      this.$refs.advancedSearchForm.validate((valid, values) => {
        if (valid) {
          this.search(this.form)
          this.$refs.export.exportdata()
        }
      })
    },
    dataLoaded(eventData) {
      if (!eventData.data) {
        return
      }
      eventData.data.forEach((item, index) => {
        item.id = index
      })
      return eventData
    },
    // 刷新列表
    refresh() {
      this.$refs.sjzlTable.refresh()
    },
    // 查询
    search(form) {
      // 默认查询条件
      this.expressions = []
      // 案件名称
      if (form.CASE_NAME) {
        this.expressions.push({
          dataType: 'str',
          name: 'CASE_NAME',
          op: 'like',
          stringValue: '%' + form.CASE_NAME + '%',
        })
      }
      // 案件类型
      if (form.CASE_TYPE) {
        if (form.CASE_TYPE.length > 0) {
          this.expressions.push({
            dataType: 'str',
            name: 'CASE_TYPE',
            op: 'in',
            stringValue: form.CASE_TYPE.map((item) => {
              return item
            }).join(','),
          })
        }
      }
      // 我方地位
      if (form.OWNER_POSITION) {
        if (form.OWNER_POSITION.length > 0) {
          this.expressions.push({
            dataType: 'str',
            name: 'OWNER_POSITION',
            op: 'in',
            stringValue: form.OWNER_POSITION.map((item) => {
              return item
            }).join(','),
          })
        }
      }

      // 案件状态
      if (form.CASE_STATUS) {
        if (form.CASE_STATUS.length > 0) {
          this.expressions.push({
            dataType: 'str',
            name: 'CASE_STATUS',
            op: 'in',
            stringValue: form.CASE_STATUS.map((item) => {
              return item
            }).join(','),
          })
        }
      }
      // 案发单位
      if (form.REPORT_UNIT_ID) {
        if (form.REPORT_UNIT_ID.length > 0) {
          this.expressions.push({
            dataType: 'str',
            name: 'REPORT_UNIT_ID',
            op: 'in',
            stringValue: form.REPORT_UNIT_ID.map((item) => {
              return item.code
            }).join(','),
          })
        }
      }

      // 案件创建时间
      if (form.CREATION_TIME) {
        if (form.CREATION_TIME.length > 0) {
          if (form.CREATION_TIME[0]) {
            var startYear = new Date(form.CREATION_TIME[0].format('YYYY-MM-DD') + ' 00:00:00')
            this.expressions.push({
              dataType: 'str',
              name: 'CREATION_TIME',
              op: 'ge',
              stringValue: startYear.getTime(),
            })
          }
          if (form.CREATION_TIME[1]) {
            var endYear = new Date(form.CREATION_TIME[1].format('YYYY-MM-DD') + ' 23:59:59')
            this.expressions.push({
              dataType: 'str',
              name: 'CREATION_TIME',
              op: 'le',
              stringValue: endYear.getTime(),
            })
          }
        }
      }

      // 涉案金额
      if (form.CASE_AMOUNT_S) {
        this.expressions.push({
          dataType: 'str',
          name: 'CASE_AMOUNT',
          op: 'ge',
          stringValue: form.CASE_AMOUNT_S,
        })
      }
      if (form.CASE_AMOUNT_E) {
        this.expressions.push({
          dataType: 'str',
          name: 'CASE_AMOUNT',
          op: 'le',
          stringValue: form.CASE_AMOUNT_E,
        })
      }
    },
    clickRow(record) {
      const url = '/sd-webflow/done-pages/' + record.instId
      crossWindowWatcher.waitForChanged(url).then((refreshFlag) => {
        if (refreshFlag) {
          // 这里写或者调刷新的方法
          this.refreshDataTable()
        }
      })
    },

    processReq(req) {
      req.data = {
        ...req.data,
        formId: 'onlhead:d0bb8202233c48f0b5148776754461e1',
      }
      return req
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
.toptitle {
  margin-left: calc(50% - 150px);
  font-size: 22px;
  font-weight: bold;
  color: #404040;
}
.search-wrap {
  margin-bottom: $padding-lg;
  :global(.ant-advanced-search-form) {
    padding: 0;
    background: transparent;
    border: none;
  }
  :global .ant-divider-horizontal {
    margin: 10px 0;
  }
}
.searchdiv {
  height: 100%;
  :global(.sd-has-table.ant-card) {
    min-height: 50%;
  }
  :global(.reporttablecardxm) {
    :global(.ant-table-body) {
      height: auto !important;
      min-height: auto !important;
      overflow: auto;
    }
  }
  :global(.ant-table-placeholder) {
    height: auto !important;
  }
  :global(.ant-form-item) {
    :global(.ant-form-item-label):first-child {
      width: 200px;
    }
  }
  .amount-num {
    :global(.ant-input-number) {
      width: 15%;
    }
  }
}
</style>
