<template>
  <div :class="$style.wrapHeight">
    <a-card>
      <!-- 高级搜索组件 -->
      <audit-advanced-query
        :expand="expand"
        :search-data="formData"
        :ref-name="searchform"
        :search-style="{
          height: '210px',
          width: 'calc(100% - 30px) !important',
          margin: 'auto',
          top: '65px !important',
        }"
        :search-fun="handleSearch"
        @searchedClick="searchedClick"
        @resetForm="resetForm"
      >
        <template>
          <a-col :span="12">
            <a-form-model-item :label="'\u2003整改名称'">
              <a-input v-model="formData.rectName"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item :label="'\u2003控制点名称'">
              <a-input v-model="formData.measureName"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item :label="'\u2003缺陷类型'">
              <a-select
                ref="defectType"
                v-model="formData.defectType"
                :options="defectTypeDict"
              ></a-select
            ></a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="缺陷责任组织">
              <sd-group-picker v-model="formData.liabilityOrgName" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item :label="'整改联系人'">
              <sd-user-picker v-model="formData.rectContactName" single></sd-user-picker>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item :label="'\u2003\u2003整改状态'">
              <a-select
                ref="rectStatus"
                v-model="formData.rectStatus"
                :options="rectStatusDict"
                allow-clear
              ></a-select>
            </a-form-model-item>
          </a-col>
        </template>
      </audit-advanced-query>
      <div :class="[$style.btns]">
        <audit-advanced-export
          ref="Export"
          :class-style="[$style.buttonSpacing]"
          :exclebxh="false"
          :expressions="exportExpressions"
          :config-id="221"
          :v-if="false"
        />
      </div>
      <sd-data-table-ex
        v-show="flag"
        :key="key"
        ref="recityList"
        :class="$style.table"
        :projectlist="true"
        form-id="icRectFeedback"
        page-id="defect/feedback/icRectFeedback"
        data-url="api/xcoa-mobile/v1/icrectfeedback/all-list"
        :columns="columns"
        :actions="actions"
        :show-advance-query="true"
        :filter-expressions="expressions"
        :search-fields="['rectName']"
        @searchbtnClick="searchbtnClick"
      >
        <template slot="islink" slot-scope="text, record">
          <a @click="open(record)">{{ text }}</a>
        </template>
        <template slot="rectStatus" slot-scope="text, record">
          {{ getDictValue(rectStatusDict, text) }}
        </template>
      </sd-data-table-ex>
    </a-card>
  </div>
</template>

<script>
import { getUserInfo } from '@/common/store-mixin'
import systemManage from '@/system-manage/system-manage'
import TableColumnTypes from '@/common/services/table-column-types'
import crossWindowWatcher from '@/common/services/cross-window-watcher'
import auditAdvancedExport from '@product/iam/components/audit-advanced-export.vue'
import auditAdvancedQuery from '@product/iam/components/audit-advanced-query.vue'
import auditAdvancedQueryMixins from '@product/iam/components/audit-advanced-query-mixins'
import iamIcRectifyFeedbackServices from './iam-ic-rectify-feedback-service'
import components from './_import-components/iam-ic-rectify-feedback-list-import'
export default {
  name: 'IamIcRectifyFeedbackList',
  metaInfo: {
    title: '整改反馈列表',
  },
  components: {
    ...components,
    auditAdvancedQuery,
    auditAdvancedExport,
  },
  mixins: [auditAdvancedQueryMixins],
  data() {
    return {
      defectTypeDict: [], // 缺陷类型
      rectStatusDict: [], // 整改状态
      waterMark: systemManage.getFormWaterMark(),
      flag: true,
      key: 0,
      searchform: 'searchform',
      formData: {
        rectName: null,
        measureName: null,
        defectType: null,
        liabilityOrgName: [],
        rectContactName: [],
        rectStatus: null,
      },
      columns: [
        {
          title: '序号',
          customRender: (text, record, index) => `${index + 1}`,
          width: '80px',
        },
        { dataIndex: 'id', sdHidden: true },
        { dataIndex: 'instId', sdHidden: true },
        {
          title: '整改名称',
          dataIndex: 'rectName',
          scopedSlots: { customRender: 'islink' },
          width: '300px',
        },
        {
          title: '控制点名称',
          dataIndex: 'measureName',
        },
        {
          title: '控制点编号',
          dataIndex: 'measureCode',
        },
        {
          title: '缺陷类型',
          dataIndex: 'defectType',
        },
        {
          title: '缺陷责任组织',
          dataIndex: 'liabilityOrgName',
        },
        {
          title: '整改联系人',
          dataIndex: 'rectContactName',
        },
        {
          title: '整改跟踪人',
          dataIndex: 'rectTrackerName',
        },
        {
          title: '拟整改完成日期',
          dataIndex: 'finishDate',
          sdRender: TableColumnTypes.date,
        },
        {
          title: '整改状态',
          dataIndex: 'rectStatus',
          scopedSlots: { customRender: 'rectStatus' },
        },
        {
          title: '整改完成日期',
          dataIndex: 'rectFinishDate',
          sdRender: TableColumnTypes.date,
        },
        {
          title: '创建日期',
          dataIndex: 'creationTime',
          sdRender: TableColumnTypes.date,
          sorter: true,
          defaultSortOrder: 'descend',
        },
        {
          title: '当前状态',
          dataIndex: 'flowState',
        },
        { title: '当前处理人', dataIndex: 'nowUser' },
      ],
      expressions: [
        {
          dataType: 'exps',
          op: 'or',
          expressionsValue: [
            {
              dataType: 'str',
              name: 'rectContactAccount',
              op: 'eq',
              stringValue: getUserInfo().account,
            },
            {
              dataType: 'str',
              name: 'rectTrackerAccount',
              op: 'eq',
              stringValue: getUserInfo().account,
            },
          ],
        },
      ],
      exportExpressions: [
        {
          dataType: 'exps',
          op: 'or',
          expressionsValue: [
            {
              dataType: 'str',
              name: 'rectContactAccount',
              op: 'eq',
              stringValue: getUserInfo().account,
            },
            {
              dataType: 'str',
              name: 'rectTrackerAccount',
              op: 'eq',
              stringValue: getUserInfo().account,
            },
          ],
        },
      ],
      actions: [
        {
          label: '导出',
          id: 'export',
          permission: null,
          callback: this.exportExcel,
          index: 1,
        },
      ],
    }
  },
  mounted() {
    // 获取字典 缺陷类型和整改状态
    iamIcRectifyFeedbackServices
      .getDictValue('DEFECT_TYPE')
      .then((res) => {
        this.defectTypeDict = res.data.map((item) => {
          return {
            label: item.name,
            value: item.id,
            title: item.name,
          }
        })
        return iamIcRectifyFeedbackServices.getDictValue('IC_RECT_STATUS')
      })
      .then((res) => {
        this.rectStatusDict = res.data.map((item) => {
          return {
            label: item.name,
            value: item.id,
            title: item.name,
          }
        })
      })
  },
  methods: {
    // 导出
    exportExcel() {
      this.exportExpressions = [...this.expressions]
      if (document.querySelector('.ant-input-search input').value !== '') {
        this.exportExpressions.push({
          dataType: 'str',
          name: 'rectName',
          op: 'like',
          stringValue: `%${document.querySelector('.ant-input-search input').value}%`,
        })
      }
      setTimeout(() => {
        this.$refs.Export.exportdata()
      }, 0)
    },
    refreshDataTable() {
      if (this.$refs.recityList) {
        this.$refs.recityList.refresh()
      }
    },
    open(record) {
      let openurl = ''
      const loginUser = getUserInfo()
      if (
        (record.flowState === '起草') |
        (record.flowState === '开始') |
        (record.flowState === null)
      ) {
        if (loginUser.name === record.nowUser) {
          openurl = '/sd-webflow/pages/draft/' + record.instId
        } else {
          openurl = '/sd-webflow/done-pages/' + record.instId
        }
      } else {
        openurl = '/sd-webflow/done-pages/' + record.instId
      }
      crossWindowWatcher.waitForChanged(openurl).then((refreshFlag) => {
        if (refreshFlag) {
          // 这里写或者调刷新的方法
          this.refreshDataTable()
        }
      })
    },
    handleSearch() {
      this.expressions = [
        {
          dataType: 'exps',
          op: 'or',
          expressionsValue: [
            {
              dataType: 'str',
              name: 'rectContactAccount',
              op: 'eq',
              stringValue: getUserInfo().account,
            },
            {
              dataType: 'str',
              name: 'rectTrackerAccount',
              op: 'eq',
              stringValue: getUserInfo().account,
            },
          ],
        },
      ]
      this.exportExpressions = [
        {
          dataType: 'exps',
          op: 'or',
          expressionsValue: [
            {
              dataType: 'str',
              name: 'rectContactAccount',
              op: 'eq',
              stringValue: getUserInfo().account,
            },
            {
              dataType: 'str',
              name: 'rectTrackerAccount',
              op: 'eq',
              stringValue: getUserInfo().account,
            },
          ],
        },
      ]
      // 整改名称
      if (this.formData.rectName) {
        this.expressions.push({
          dataType: 'str',
          name: 'rectName',
          op: 'like',
          stringValue: '%' + this.formData.rectName + '%',
        })
      }
      // 控制点名称
      if (this.formData.measureName) {
        this.expressions.push({
          dataType: 'str',
          name: 'measureName',
          op: 'like',
          stringValue: '%' + this.formData.measureName + '%',
        })
      }
      // 缺陷类型
      if (this.formData.defectType) {
        const defectTypeText = this.defectTypeDict.find((item) => {
          return item.value === this.formData.defectType
        }).label
        this.expressions.push({
          dataType: 'str',
          name: 'defectType',
          op: 'eq',
          stringValue: defectTypeText,
        })
      }
      // 责任缺陷组织
      if (this.formData.liabilityOrgName.length > 0) {
        const orgExp = {
          dataType: 'exps',
          op: 'or',
          expressionsValue: [],
        }
        this.formData.liabilityOrgName.forEach((item) => {
          orgExp.expressionsValue.push({
            dataType: 'str',
            name: 'liabilityOrgName',
            op: 'like',
            stringValue: '%' + item.name + '%',
          })
        })
        this.expressions.push(orgExp)
      }
      // 整改联系人
      if (this.formData.rectContactName.length > 0) {
        this.expressions.push({
          dataType: 'str',
          name: 'rectContactName',
          op: 'eq',
          stringValue: this.formData.rectContactName[0].name,
        })
      }
      // 整改状态
      if (this.formData.rectStatus) {
        const rectStatusText = this.rectStatusDict.find((item) => {
          return item.value === this.formData.rectStatus
        }).label
        this.expressions.push({
          dataType: 'str',
          name: 'rectStatus',
          op: 'eq',
          stringValue: this.formData.rectStatus,
        })
      }

      this.expressions = [...this.expressions]
      this.exportExpressions = [...this.expressions]
    },

    // 重置
    resetForm() {
      this.formData = {
        rectName: null,
        measureName: null,
        defectType: null,
        liabilityOrgName: null,
        rectContactName: [],
        rectStatus: null,
      }
    },
    // 获取字典值
    getDictValue(list, val) {
      return list.find((item) => {
        return item.value.trim() === (val + '').trim()
      })?.label
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
.wrap-height {
  height: 100%;
  .table {
    table {
      width: 2200px;
    }
  }
  .row-height {
    display: flex;
    flex: auto;
    height: 100%;
    .rightcard {
      flex: 1;
      width: calc(100% - 20%);
      height: 100%;
    }
  }
}
</style>
