<template>
  <div class="page">
    <edit-table
      ref="EditTable"
      v-model="data"
      :table-prop="tableProp"
      :fields="fields"
      :dialog-prop="dialogProp"
      :show-search-form="true"
      row-key="id"
      :search-form-prop="searchFormProp"
      :form-prop="formProp"
      :search-loader="searchLoader"
      @update:selected="(value)=>{selected = value}"
    >
      <template v-slot:divider="scope">
        <div class="divider">
          <span class="text">{{ scope.getPropValue(scope.field.label) }}</span>
        </div>
      </template>
      <template v-slot:table-top="EditTable">
        <div style="margin-bottom: 12px;">
          <el-button
            style="background: #32BBD6;"
            type="primary"
            @click="()=>{
              if (selected.length === 0) {
                $message.warning('请先勾选需要调整的主体')
                return
              }
              if(!(selected[0].regstate === '1')){
                $message.warning('不能调整非开业的主体的属地机关！');
                return;
              }

              // if(!selected[0].suporg){
              //   if(userInfo.orgCode.slice(0, 4) !== selected[0].regorg.slice(0, 4)){
              //     $message.warning('不能调整非本市的主体的属地机关！');
              //     return;
              //   }
              // } else {
              //   if(userInfo.orgCode.slice(0, 4) !== selected[0].suporg.slice(0, 4)){
              //     $message.warning('不能调整非本市的主体的属地机关！');
              //     return;
              //   }
              // }
              $refs.EditTable.editReady(selected[0])
            }"
          >
            调整属地
          </el-button>
        </div>
      </template>
    </edit-table>
  </div>
</template>

<script>
import { getOrgunits, updateBaseinfoSuperviseDept, pageBaseinfo } from '@/api'
import { getInfo } from '@/api/user'
export default {
  data() {
    return {
      isExSearch: false,
      data: [],
      selected: [],
      userInfo: [],
      orgMap: [],
      tableProp: {
        emitLoadOnCreate: true,
        tableConfig: {
          showIndex: false,
          selection: true,
          indexWidth: '100px',
          handlerWidth: '120px',
          showPagination: true,
          tableSelectionProps: {
            selectable: (row, index) => {
              return this.selected.length === 0 || this.selected[0] === row
            }
          },
          tableProps: {
            headerCellStyle: { background: '#F5F5F5!important', color: '#333333!important', padding: '0px' },
            headerRowStyle: { height: '48px' },
            rowStyle: { height: '48px' },
            cellStyle: { padding: '16px 0', color: '#333' }
          },
          indexMethod: (index, scope, defaultIndex) => {
            const s = String(defaultIndex)
            return `${new Array(3 - s.length).fill('0').join('')}${defaultIndex}`
          }
        }
      },
      searchFormProp: {
        formConfig: {
          labelWidth: '100px',
          labelPosition: 'right',
          gutter: 32
        }
      },
      formProp: {
        formConfig: {
          labelWidth: '210px',
          labelPosition: 'right',
          gutter: 32,
          buttonPosition: 'right'
        },
        buttons: [
          {
            label: '返回',
            type: 'primary',
            buttonProps: {
              plain: true
            },
            handler: (scope, { row }) => {
              this.$refs.EditTable.cancelEdit()
            }
          },
          {
            label: '保存',
            type: 'primary',
            handler: (scope, { row }) => {
              return scope.formInstance.submitForm().then((formData) => {
                return this.$confirm('是否继续提交', '提示', {
                  type: 'info',
                  customClass: 'type-1',
                  confirmHandlerPromise: () => {
                    return updateBaseinfoSuperviseDept({
                      pripid: formData.pripid,
                      uniscid: formData.uniscid,
                      entname: formData.entname,
                      regno: formData.regno,
                      afterDeptid: formData.afterDeptid
                    }).then(() => {
                      this.$refs.EditTable.editConfirm(formData)
                      this.$message.success('调整属地成功')
                      return this.$refs.EditTable.search({ reset: true })
                    })
                  }
                })
              })
            }
          }
        ]
      },
      dialogProp: {
        title: '调整属地',
        width: '1200px',
        customClass: 'dialog-abnormal-list'
      },
      fields: [
        {
          type: 'input',
          label: '统一社会信用代码/注册号',
          prop: 'searchUscc',
          span: 9,
          tableProps: {
            width: '210px',
            showOverflowTooltip: true,
            formatter: (row, column, cellValue, index) => {
              return `${row.uniscid || row.regno || ''}`
            }
          },
          showInForm: ({ meta }) => {
            return meta.isSearchForm === true
          }
        },
        {
          type: 'divider',
          label: '调整属地',
          showInTable: false,
          showInForm: ({ meta }) => {
            return meta.isForm === true
          }
        },
        {
          type: 'input',
          label: '主体名称',
          prop: 'entname',
          span: ({ meta }) => {
            return meta.isForm ? 24 : 9
          },
          tableProps: {
            width: '260px',
            showOverflowTooltip: true
          },
          isView: ({ meta }) => {
            return meta.isForm
          }
        },
        {
          type: 'input',
          label: '主体状态',
          prop: 'regstateCn',
          isView: true,
          showInForm: false
        },
        {
          type: 'input',
          label: '法定代表人',
          prop: 'name',
          isView: true,
          showInForm: false
        },
        {
          type: 'buttons',
          showInTable: false,
          showInForm: ({ meta }) => {
            return meta.isSearchForm === true
          },
          hiddenLabel: true,
          label: '搜索操作',
          prop: '搜索操作',
          span: 6,
          formItemProp: {
            labelWidth: '0px'
          },
          buttons: [
            {
              label: '查询',
              type: 'primary',
              handler: (scope) => {
                const { meta } = scope
                const { _editTable } = meta
                const { editTableInstance } = _editTable
                editTableInstance.search()
              }
            },
            {
              label: '重置',
              type: 'primary',
              buttonProps: {
                plain: true
              },
              handler: (scope) => {
                const { meta } = scope
                const { _editTable } = meta
                const { editTableInstance } = _editTable
                editTableInstance.search({
                  reset: true,
                  resetData: {
                    查询类型: editTableInstance.searchForm.查询类型
                  }
                })
              }
            }
          ]
        },
        {
          type: 'input',
          label: '地址',
          prop: 'dom',
          showInTable: true,
          showInForm: false
        },
        {
          type: 'input',
          label: ({ meta }) => {
            return meta.isForm ? '当前所属机关' : '所属机关'
          },
          prop: 'suporgCn',
          span: ({ meta }) => {
            return meta.isForm ? 24 : 6
          },
          isView: ({ meta }) => {
            return meta.isForm
          },
          showInForm: ({ meta }) => {
            return meta.isForm || (this.isExSearch && meta.isSearchForm === true)
          }
        },
        {
          type: 'cascader',
          label: '调整后所属机关',
          prop: 'afterDeptid',
          span: 24,
          required: true,
          optionConfig: {
            labelKeyName: 'label',
            valueKeyName: 'value'
          },
          formProps: {
            props: { checkStrictly: true }
          },
          options: () => {
            return this.orgMap
          },
          onCreated: ({ setOptionLoading }) => {
            if (this.orgMap.length === 0) {
              setOptionLoading('authItem', getOrgunits('15', 0).then((data) => {
                this.orgMap = data.data
              }))
            }
          },
          showInForm: ({ meta }) => {
            return meta.isForm === true
          },
          showInTable: false
        }
      ]
    }
  },
  beforeRouteEnter(to, from, next) {
    next((vm) => {
      vm.$refs.EditTable.search({ reset: true })
    })
  },
  created() {
    getInfo().then((user) => {
      this.userInfo = user
    })
  },
  methods: {
    searchLoader(pageParam, parseSearchFormData, instance) {
      return pageBaseinfo({
        size: pageParam.size,
        current: pageParam.current,
        entity: {
          ...parseSearchFormData
        }
      }).then((data) => {
        return {
          tableData: data.data.records,
          total: data.data.total
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
@import "~@/styles/abnormal-list.scss";
</style>
