import util from '@src/common/util.js'
import TreeSelect from '@src/components/TreeSelect.vue'
import siteSortDialog from './SiteSortDialog.vue'
import siteExportTypeDialog from './SiteExportTypeDialog.vue'
import linkCheck from './LinkCheck.vue'
import privOwnerTabs from './privOwnerTabs.vue'
import templateSelectDialog from '../templateSelectDialog.vue'
import LogoPath from './LogoPath.vue'
import CatalogImportButton from './CatalogImportButton.vue'
import CatalogExportStructureButton from './CatalogExportStructureButton.vue'

export default {
  components: {
    'c-export-structure-button': CatalogExportStructureButton,
    'c-import-button': CatalogImportButton,
    'tree-select': TreeSelect,
    siteExportTypeDialog: siteExportTypeDialog,
    siteSortDialog: siteSortDialog,
    linkCheckDialog: linkCheck,
    privOwnerTabsDialog: privOwnerTabs,
    templateSelectDialog: templateSelectDialog,
    'logo-path': LogoPath
  },
  props: {
    siteID: {
      type: Number,
      default () {
        return 0
      }
    }
  },
  data () {
    return {
      langs: [],
      resourceDialogProps: {}, // 图片选择资源弹框参数
      templateIndex: null, // 首页模板索引
      template: {}, // 首页模板
      templateSelectModal: false, // 模板选择
      saveSiteLoading: false, // 保存按钮
      exportSiteModal: false, // 导出站点
      linkCheckModal: false, // 链接及文件检查
      privOwnerTabsModal: false, // 分配权限
      sortSiteModal: false, // 排序
      siteInfoRules: {
        name: [
          { required: true, message: '不能为空', trugger: 'blur,change' },
          {
            pattern: /^.{1,100}$/,
            message: '字符串长度不能超过100',
            trigger: 'blur,change'
          }
        ],
        path: [
          { required: true, message: '不能为空', trigger: 'blur,change' },
          {
            pattern: /^[-._\w]+$/,
            message: '只允许大小写字母、数字、英文句号、下划线及英文破折线',
            trigger: 'blur,change'
          },
          {
            pattern: /^.{1,100}$/,
            message: '字符串长度不能超过100',
            trigger: 'blur,change'
          }
        ],
        URL: [
          { required: true, message: 'URL不能为空', trugger: 'blur,change' },
          {
            pattern: /^.{1,100}$/,
            message: '字符串长度不能超过100',
            trigger: 'blur,change'
          }
        ]
      },
      config: {
        showSiteSEOConfig: 'Y'
      }, // 需读取系统配置项
      branchTree: [], // 机构树下拉框数据
      treeItemsOptions: {
        children: 'children',
        label: 'name',
        key: 'branchInnerCode'
      }, // 所属机构树形菜单下拉框
      templateTableSearchParam: {
        siteID: '',
        type: 'SiteIndex',
        value: '',
        platformID: '',
        name: '',
        orderFlag: ''
      }, // 模板表格查询参数
      siteInfo: {}, // 基本信息tabs数据
      taskID: 0,
      progressModal: false,
      progressTitle: '',
      taskMode: '',
      path: '',
      isShowModal: false,
      isAppointTime: false,
      dateInterval: [],
      submitLoading: false
    }
  },
  methods: {
    editTemplate (item) {
      if (item) {
        let fileName = item.indexTemplate
        let platformID = item.platformID
        window.open(`./templateEditor.html?ID=&fileName=${fileName}&platformID=${platformID}&siteID=${this.siteID}`)
      }
    },

    /** 链接及文件检查 */
    linkCheckLog () {
      this.linkCheckModal = true
    },
    /** 分配权限 */
    assignPriv () {
      if (!this.siteID) {
        this.$alert('请先选择一个站点', '提示', {
          type: 'info'
        })
        return
      }
      this.privOwnerTabsModal = true
    },
    /** 预览/浏览站点 预览preview、浏览browse */
    previewSite () {
      if (!this.siteID) {
        this.$alert('请先选择一个站点', '提示', {
          type: 'info'
        })
        return
      }
      window.open(`preview.html?id=${this.siteID}&type=site`)
    },
    browseSite () {
      if (!this.siteID) {
        this.$alert('请先选择一个站点', '提示', {
          type: 'info'
        })
        return
      }
      window.open(this.siteInfo.URL)
    },
    /** 选择首页模板文件 */
    browseTemplate (data, index) {
      this.template = {
        type: 'SiteIndex',
        value: data.indexTemplate,
        platformID: data.platformID
      }
      this.templateIndex = index
      this.templateSelectModal = true
    },
    /** 选中模板 */
    templateCallback (val) {
      for (let i = 0; i < this.siteInfo.indexTemplates.length; i++) {
        if (i === this.templateIndex) {
          this.siteInfo.indexTemplates[i].indexTemplate = val
        }
      }
    },
    /** 保存站点信息 */
    async saveSite () {
      try {
        await util.validateForm(this.$refs['siteInfoForm'])
      } catch (e) {
        util.showErrorMessageBox(e)
        return
      }
      this.saveSiteLoading = true
      let res = await axios.put(`/ui/sites/${this.siteInfo.ID}`, this.siteInfo)
      util.showResponseMessage(res.data)
      this.saveSiteLoading = false
      if (res.data.status === 1) {
        this.$emit('callback')
      }
    },
    /** 排序 */
    sortSiteClick () {
      if (!this.siteID) {
        this.$alert('请先选择一个站点！', '提示', {
          type: 'info'
        })
        return
      }
      this.sortSiteModal = true
    },
    updateSiteTree () {
      this.$emit('callback')
    },
    /** 发布首页 */
    async publishIndexClick () {
      if (!this.siteID) {
        this.$alert('请先选择一个站点！', '提示', {
          type: 'info'
        })
        return
      }
      try {
        await this.$confirm('本次生成的静态页面将覆盖原有页面，确认生成所有静态页面吗？', '发布首页', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        let res = await axios.put(`/ui/sites/${this.siteID}/publishindex`)
        util.showResponseMessage(res.data)
        if (res.data.status === 1) {
          await util.showProgress(res.data.taskID, '正在发布首页 . . .')
        }
      } catch (e) {}
    },
    /** 导出站点 */
    exportSite () {
      if (!this.siteID) {
        this.$alert('请先选择一个站点！', '提示', {
          type: 'info'
        })
        return
      }
      this.exportSiteModal = true
    },
    /** 导出站点回调 */
    async exportSiteCallback (taskID, path) {
      try {
        await util.showProgress(taskID, '正在导出站点数据 . . .')
        window.location.href = `ui/site/export?path=${path}&siteID=` + this.siteID
      } catch (e) {}
    },
    async publish (status) {
      this.submitLoading = false
      this.isShowModal = true
      this.publishStatus = status
    },
    /** 发布全站 status ：状态 */
    async confirm () {
      if (!this.siteID) {
        this.$alert('请选择一个站点！', '提示', {
          type: 'info'
        })
        return
      }
      try {
        this.submitLoading = true
        let res = await axios.put(`/ui/sites/${this.siteID}/publish`, {
          publishStatus: this.publishStatus,
          startTime: this.dateInterval && this.isAppointTime ? Date.parse(this.dateInterval[0]) : null,
          endTime: this.dateInterval && this.isAppointTime ? Date.parse(this.dateInterval[1]) : null
        })
        this.submitLoading = false
        if (res.data.status === 1) {
          this.isShowModal = false
          await util.showProgress(res.data.taskID, '正在发布 . . .')
        } else {
          util.showResponseMessage(res.data)
        }
      } catch (e) {}
    },
    /** 确认上传导入站点文件 */
    async siteImportCallback (taskID) {
      try {
        await util.showProgress(taskID, '正在导入 . . .')
        this.$emit('callback')
      } catch (e) {}
    },
    /** 复制模板 */
    async copyTemplateFile () {
      if (!this.siteID) {
        this.$alert('请先选择一个站点！', '提示', {
          type: 'info'
        })
        return
      }
      let res = await axios.put(`/ui/sites/${this.siteID}/copytemplatefile`)
      util.showResponseMessage(res.data)
    },
    /** 加载站点详情数据 */
    async init () {
      this.langs = []
      let getLang = await axios.get('/ui/application/lang')
      for (let i in getLang.data.data) {
        this.langs.push({ key: i, value: getLang.data.data[i] })
      }
      let getBranchTree = await axios.get('/ui/branches')
      this.branchTree = getBranchTree.data.data
      let res = await axios.get(`/ui/sites/${this.siteID}`)
      if (res.data.status === 1) {
        this.siteInfo = res.data.data
        this.resourceDialogProps = {
          inputType: 'radio',
          siteID: this.siteID,
          dataType: 'Site',
          dataID: this.siteID,
          catalogID: 0,
          resourceType: 'image',
          imageType: 'logo',
          imageWidth: 120,
          imageHeight: 120,
          resourceID: this.siteInfo.resourceID
        }
      }
    },
    dialog (msg) {
      this.$confirm(msg, '系统提示', {
        confirmButtonText: '确定',
        showCancelButton: false,
        type: 'warning'
      })
    }
  },
  created () {}
}
