import { parseTime } from '@/utils'
import { sueInspectListFile } from '@/api/market'

export default {
  data() {
    return {
      dailySupervisionFields: [
        {
          type: 'view',
          prop: 'uscc',
          label: '统一社会信用代码',
          showInForm: true, // 是否显示在表单中
          showInTable: true,
          span: 12
        },
        {
          type: 'view',
          prop: 'regNo',
          label: '注册号',
          showInForm: true, // 是否显示在表单中
          showInTable: true,
          span: 12 // 同el-row
        },
        {
          type: 'view',
          prop: 'entName',
          label: '主体名称',
          showInForm: true,
          showInTable: true,
          span: 12
        },
        {
          type: 'view',
          prop: 'dom',
          label: '经营地址',
          showInForm: false,
          showInTable: true,
          span: 12
        },
        {
          type: 'view',
          prop: 'legalName',
          label: '法定代表人',
          showInForm: true,
          showInTable: true,
          span: 12
        },
        {
          type: 'view',
          prop: 'caozuoleixing',
          label: '操作类型',
          showInForm: false,
          showInTable: true,
          span: 12,
          tableProps: {
            formatter: (row) => {
              let status_cn = ''
              if (row.status === '0') {
                status_cn = '待检查'
              } else if (row.status === '2') {
                status_cn = '列异回退'
              } else {
                status_cn = '已检查'
              }
              return status_cn
            }
          }
        },
        {
          type: 'view',
          prop: 'time',
          label: '责令更正期限',
          showInForm: false,
          showInTable: () => {
            return this.type === '1'
          },
          span: 12
        },
        {
          type: 'view',
          prop: 'inspectOrgName',
          label: '检查实施机关',
          showInForm: true,
          showInTable: false,
          span: 12
        },
        {
          type: 'date-picker',
          prop: 'inspectDate',
          label: '检查时间',
          showInForm: true,
          showInTable: false,
          span: 12,
          required: true,
          isView: ({ formConfig }) => {
            return formConfig.readOnly
          },
          defaultValue: () => {
            return parseTime(new Date(), '{yyyy}-{mm}-{dd} {hh}:{ii}:{ss}')
          },
          formProps: {
            type: 'date',
            valueFormat: 'yyyy-MM-dd HH:mm:ss',
            format: 'yyyy-MM-dd'
          },
          tableProps: {
            formatter: (row, column, cellValue, index) => {
              return parseTime(cellValue, '{yyyy}-{mm}-{dd}')
            }
          }
        },
        {
          type: 'checkbox',
          prop: 'inspectType',
          label: '检查方式',
          showInForm: true,
          showInTable: false,
          span: 12,
          defaultValue: [],
          required: true,
          valueFormat: (value) => {
            return value.join(',')
          },
          options: [
            { label: '书面检查', value: '1' },
            { label: '实地核查', value: '2' },
            { label: '网络监测', value: '3' },
            { label: '委托专业机构开展检查', value: '4' }
          ],
          praseValue: (value) => {
            return typeof value === 'string' ? value.split(',') : value
          }
        },
        {
          type: 'input',
          prop: 'helper',
          label: '协查人',
          showInForm: true,
          span: 12
        },
        {
          type: 'radio',
          prop: 'inspectResult',
          label: '检查结果',
          showInForm: true,
          showInTable: false,
          span: 24,
          required: true,
          options: [
            { label: '未发现问题', value: '1' },
            { label: '未按规定公示应当公示的信息', value: '2' },
            { label: '公示信息隐瞒真实情况弄虚作假', value: '3' },
            { label: '通过登记住所（经营场所）无法联系', value: '4' },
            { label: '发现问题已责令改正', value: '5' },
            { label: '不配合检查情节严重', value: '6' },
            { label: '未发现开展本次检查涉及的经营活动', value: '7' },
            { label: '发现问题待后续处理', value: '8' }
          ]
        },
        {
          type: 'input',
          prop: 'correctWriteNo',
          label: '《责令期限履行公示义务通知书》文号',
          showInForm: (scope) => {
            // return scope.formData.inspectResult.some((item) => {
            //   return ['2', '3', '4'].includes(item)
            // })
            return ['2', '3', '4'].includes(scope.formData.inspectResult)
          },
          showInTable: false,
          required: false,
          span: 24
        },
        {
          type: 'date-picker',
          prop: 'correctDate',
          label: '责令更正期限',
          showInForm: (scope) => {
            // return scope.formData.inspectResult.some((item) => {
            //   return ['2', '3', '4'].includes(item)
            // })
            return ['2', '3', '4'].includes(scope.formData.inspectResult)
          },
          showInTable: false,
          span: 24,
          required: false,
          formProps: {
            type: 'date',
            valueFormat: 'yyyy-MM-dd',
            format: 'yyyy-MM-dd'
          },
          tableProps: {
            formatter: (row, column, cellValue, index) => {
              return parseTime(cellValue, '{yyyy}-{mm}-{dd}')
            }
          },
          rule: (scope) => {
            return [
              {
                validator: (rule, value, callback) => {
                  callback(value && (new Date(value)).valueOf() - (new Date(scope.formData.inspectDate)).valueOf() < 1000 * 60 * 60 * 24 * 10 ? '责令改正期限必须为检查时间的10天后' : undefined)
                }
              }
            ]
          }
        },
        {
          type: 'date-picker',
          prop: 'rectDate',
          label: '整改时间',
          required: false,
          showInForm: (scope) => {
            // return scope.formData.inspectResult.some((item) => {
            //   return ['5', '8'].includes(item)
            // })
            return ['5', '8'].includes(scope.formData.inspectResult)
          },
          showInTable: false,
          span: 12,
          isView: ({ formConfig }) => {
            return formConfig.readOnly
          },
          formProps: {
            type: 'date',
            valueFormat: 'yyyy-MM-dd HH:mm:ss',
            format: 'yyyy-MM-dd'
          },
          tableProps: {
            formatter: (row, column, cellValue, index) => {
              return parseTime(cellValue, '{yyyy}-{mm}-{dd}')
            }
          }
        },
        {
          prop: 'backOpinion',
          label: '回退意见',
          type: 'view',
          span: 24,
          showInForm: ({ formData, formConfig, componentName }) => {
            return formData.status === '2'
          }
        },
        {
          type: 'upload',
          label: '责令整改通知书',
          required: true,
          prop: 'files0',
          span: 24,
          showInForm: (scope) => {
            // return scope.formData.inspectResult.some((item) => {
            //   return ['5'].includes(item)
            // })
            return scope.formData.inspectResult === '5'
          },
          defaultValue: () => {
            return []
          },
          uploadData: () => {
            return {
              id: this._currentId,
              attachType: '0'
            }
          },
          formProps: {
            action: `${process.env.VUE_APP_BASE_API}/market/sueInspect/upload`,
            autoUpload: true,
            onPreview: (file) => {
              if (file.attachmentid) {
                window.open(`${process.env.VUE_APP_BASE_API}/market/tSUsualInspect/downloadFile?fileId=${file.attachmentid}`)
              } else {
                window.open(URL.createObjectURL(file.raw))
              }
            }
          }
        },
        {
          type: 'upload',
          label: '上传线索来源',
          prop: 'files',
          showInForm: true,
          span: 24,
          required: true,
          uploadData: () => {
            return {
              id: this._currentId,
              attachType: '1'
            }
          },
          defaultValue: () => {
            return []
          },
          formProps: {
            action: `${process.env.VUE_APP_BASE_API}/market/sueInspect/upload`,
            autoUpload: true,
            onPreview: (file) => {
              if (file.attachmentid) {
                window.open(`${process.env.VUE_APP_BASE_API}/market/tSUsualInspect/downloadFile?fileId=${file.attachmentid}`)
              } else {
                window.open(URL.createObjectURL(file.raw))
              }
            }
          }
        },
        {
          type: 'upload',
          label: '上传附件',
          prop: 'files2',
          showInForm: true,
          span: 24,
          defaultValue: () => {
            return []
          },
          uploadData: () => {
            return {
              id: this._currentId,
              attachType: '2'
            }
          },
          formProps: {
            action: `${process.env.VUE_APP_BASE_API}/market/sueInspect/upload`,
            autoUpload: true,
            onPreview: (file) => {
              if (file.attachmentid) {
                window.open(`${process.env.VUE_APP_BASE_API}/market/tSUsualInspect/downloadFile?fileId=${file.attachmentid}`)
              } else {
                window.open(URL.createObjectURL(file.raw))
              }
            }
          },
          rule: (scope) => {
            return [
              {
                validator: (rule, value, callback) => {
                  // if (scope.formData.inspectResult.some((item) => {
                  //   return ['2', '3', '4'].includes(item)
                  // }) && scope.formData.files2.length === 0) {
                  //   callback(`检查结果为“公示信息隐瞒真实情况弄虚作假”或“通过登记住所（经营场所）无法联系”，必须上传相关附件`)
                  // } else {
                  //   callback()
                  // }

                  if (['2', '3', '4'].includes(scope.formData.inspectResult) && scope.formData.files2.length === 0) {
                    callback(`检查结果为“公示信息隐瞒真实情况弄虚作假”或“通过登记住所（经营场所）无法联系”，必须上传相关附件`)
                  } else {
                    callback()
                  }
                }
              }
            ]
          }
        }
      ],
      dailySupervisionInputDialog: {
        visible: false,
        formData: {},
        loading: false,
        config: {
          labelPosition: 'right',
          labelWidth: '130px',
          // gutter: 20,
          readOnly: false,
          submitPosition: 'center', // 页脚按钮位置
          showValidMessage: true
        }
      }
    }
  },
  methods: {
    dailySupervisionEvent(scope, button, easyTableInstance) {
      this.dailySupervisionInputDialog.visible = true
      this.dailySupervisionInputDialog.loading = true
      sueInspectListFile({ id: scope.row.id }).then((res) => {
        const { code, data } = res
        if (code !== 0) {
          throw res
        }
        this.currentId = scope.row.id
        this.dailySupervisionInputDialog.formData = scope.row
        this.$set(this.dailySupervisionInputDialog.formData, 'files0', data.filter((file) => {
          return file.attachtype === '0'
        }).map((item) => {
          return {
            ...item,
            name: item.filename
          }
        }))
        this.$set(this.dailySupervisionInputDialog.formData, 'files', data.filter((file) => {
          return file.attachtype === '1'
        }).map((item) => {
          return {
            ...item,
            name: item.filename
          }
        }))
        this.$set(this.dailySupervisionInputDialog.formData, 'files2', data.filter((file) => {
          return file.attachtype === '2'
        }).map((item) => {
          return {
            ...item,
            name: item.filename
          }
        }))
        this.$nextTick(() => {
          if (this.$refs.dailySupervisionInputDialog) {
            this.$refs.dailySupervisionInputDialog.clearValidate()
          }
        })
      }).finally(() => {
        this.dailySupervisionInputDialog.loading = false
      })
    }
  }
}
