<template>
    <div>
        <Form :model="formItem" inline>
            <FormItem>
                <Select style="width: 200px" v-model="formItem.select" placeholder="请选择模块">
                    <Option v-for="(item,index) in menuList" :key="index" :value="index">
                        {{item.center_code}}({{item.center_key}})
                    </Option>
                </Select>
            </FormItem>
            <FormItem>
                <Button :loading="loading" type="primary" @click="searchAction">搜索</Button>
            </FormItem>
            <FormItem>
                <Button type="success" @click="syncAction">一键同步</Button>
            </FormItem>
        </Form>
        <Divider/>
        <div>
            <Table border :columns="columns" :data="dataList" v-if="show" :loading="loading">
                <template slot-scope="{ row }" slot="action">
                    <Button type="primary" size="small" style="margin-right: 5px" @click="synchronization(row)">
                        同步
                    </Button>
                </template>
            </Table>
        </div>
    </div>

</template>
<script>

import { getCenterList, sysisSync, updateSysFun } from '../../../api/system/sys-sync-list'

export default {
  name: 'WlSyncList',
  data () {
    return {
      columns: [
        {
          title: '接口标识',
          key: 'id'
        },
        {
          title: '接口名称',
          key: 'funName'
        },
        {
          title: '状态',
          key: 'statusCodeCN'
        },
        {
          title: '描述',
          key: 'desc'
        },
        {
          title: '操作',
          slot: 'action',
          width: 150,
          align: 'center'
        }
      ],
      formItem: {
        select: ''
      },
      menuList: [],
      dataList: [],
      show: false,
      loading: false,
      syncLoading: false
    }
  },
  methods: {
    synchronization (row) {
      let arr = []
      arr.push(row)
      this.updateSysFunAction({ list: arr })
    },
    updateSysFunAction (val) {
      updateSysFun(val).then(ret => {
        if (ret.data.errcode === 0) {
          this.$Modal.remove()
          this.$Message.success('成功' + ret.data.data.sucess + '条，失败' + ret.data.data.fail + '条')
          this.searchAction()
        } else {
          this.$Modal.remove()
          this.$Message.error(ret.data.errmsg)
        }
      })
    },
    init () {
      getCenterList().then(ret => {
        if (ret.data.errcode === 0) {
          this.menuList = ret.data.data
        } else {
          this.$Message.error(ret.data.errmsg)
        }
      })
    },
    searchAction () {
      if (this.formItem.select === '') {
        this.$Message.info('请选择模块！')
      } else {
        const data = {
          center_code: this.menuList[this.formItem.select].center_code,
          center_key: this.menuList[this.formItem.select].center_key
        }
        this.loading = true
        sysisSync(data).then(ret => {
          if (ret.data.errcode === 0) {
            this.dataList = ret.data.data
            this.show = true
            this.loading = false
          } else {
            this.$Message.error(ret.data.errmsg)
            this.loading = false
          }
        })
      }
    },
    syncAction () {
      this.$Modal.confirm({
        title: '确认同步所有接口么？',
        content: '数据量大时会很慢，耐心等待，请勿关闭',
        loading: true,
        onOk: () => {
          this.updateSysFunAction({ list: this.dataList })
        }
      })
    }
  },
  mounted () {
    this.init()
  }

}
</script>
<style scoped>
</style>
