import Pagination from '@/components/pagination'
import {
  NButton,
  NDataTable,
  NFormItem,
  NModal,
  NSelect,
  NSpace,
  NTag,
  useDialog,
  useMessage
} from 'naive-ui'
import { defineComponent, onMounted, reactive, ref } from 'vue'
import { appVersionInfoPage } from '../api'
import VersionOperation from './version-operation'
import { filterPlatform, filterStatus, platformArr, statusArr } from './operation'
import SaveForm from '@/components/save-form'

export default defineComponent({
  name: 'app-version',
  setup() {
    const state = reactive({
      loading: false,
      pagination: {
        page: 1,
        rows: 10,
        pageTotal: 0
      },
      searchForm: {
        keyword: null,
        status: null,
        platform: null
      },
      dataList: [] as any,
      visiableApp: false,
      appOperation: 'add',
      appData: {} as any
    })
    const columns = () => {
      return [
        {
          title: '编号',
          key: 'id'
        },
        {
          title: '客户端',
          key: 'platform',
          render(row: any) {
            return filterPlatform(row.platform)
          }
        },
        {
          title: '版本号',
          key: 'version'
        },
        {
          title: '强制更新',
          key: 'isForceUpdate',
          render(row: any) {
            return (
              <NTag type={row.isForceUpdate ? 'primary' : 'default'}>
                {row.isForceUpdate ? '是' : '否'}
              </NTag>
            )
          }
        },
        {
          title: '状态',
          key: 'status',
          render(row: any) {
            return filterStatus(row.status)
          }
        },
        {
          title: '描述',
          key: 'description'
        },
        {
          title: '下载连接',
          key: 'downloadUrl',
          width: 250
        },
        {
          title: '操作',
          key: 'operation',
          render(row: any) {
            return (
              <NSpace>
                <NButton
                  type="primary"
                  size="small"
                  text
                  //v-auth="appVersionInfo/update1598525730761052161"
                  onClick={() => {
                    state.visiableApp = true
                    state.appOperation = 'edit'
                    state.appData = row
                  }}
                >
                  修改
                </NButton>
              </NSpace>
            )
          }
        }
      ]
    }

    const getList = async () => {
      try {
        state.loading = true
        const { data } = await appVersionInfoPage({ ...state.pagination, ...state.searchForm })
        state.loading = false
        state.pagination.pageTotal = Number(data.total)
        state.dataList = data.rows || []
      } catch {
        state.loading = false
      }
    }

    const saveForm = ref()
    const onSubmit = () => {
      // console.log('搜索', { ...state.form })
      state.pagination.page = 1
      getList()
    }

    const onSearch = () => {
      saveForm.value?.submit()
    }
    const onBtnReset = () => {
      saveForm.value?.reset()
    }
    onMounted(() => {
      getList()
    })
    return () => (
      <div class="system-menu-container">
        <h2>APP版本控制</h2>
        <SaveForm
          ref={saveForm}
          model={state.searchForm}
          onSubmit={onSubmit}
          onSetModel={(val: any) => (state.searchForm = val)}
        >
          <NFormItem label="客户端" path="platform">
            <NSelect
              v-model:value={state.searchForm.platform}
              placeholder="请选择客户端"
              options={platformArr}
              clearable
            />
          </NFormItem>
          <NFormItem label="状态" path="status">
            <NSelect
              v-model:value={state.searchForm.status}
              placeholder="请选择状态"
              options={statusArr}
              clearable
            />
          </NFormItem>
          {/* platform version */}
          <NFormItem>
            <NSpace>
              <NButton type="primary" onClick={onSearch}>
                搜索
              </NButton>
              <NButton type="default" onClick={onBtnReset}>
                重置
              </NButton>
            </NSpace>
          </NFormItem>
        </SaveForm>

        <div class={['section-container']}>
          <NSpace style={{ paddingBottom: '12px' }}>
            <NButton
              type="primary"
              //v-auth="appVersionInfo/save1598525667141849090"
              onClick={() => {
                state.visiableApp = true
                state.appOperation = 'add'
                state.appData = {}
              }}
            >
              添加APP版本
            </NButton>
          </NSpace>

          <NDataTable
            loading={state.loading}
            columns={columns()}
            data={state.dataList}
          ></NDataTable>
          <Pagination
            v-model:page={state.pagination.page}
            v-model:pageSize={state.pagination.rows}
            v-model:pageTotal={state.pagination.pageTotal}
            onList={getList}
            sync
          ></Pagination>
        </div>
        <NModal
          v-model:show={state.visiableApp}
          preset="dialog"
          showIcon={false}
          title={state.appOperation === 'add' ? '新增APP版本' : '修改APP版本'}
          style={{ width: '500px' }}
        >
          <VersionOperation
            v-if={state.visiableApp}
            type={state.appOperation}
            data={state.appData}
            onClose={() => (state.visiableApp = false)}
            onGetList={getList}
          />
        </NModal>
      </div>
    )
  }
})
