<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="导航名称" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入导航名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="英语名称" prop="englishTitle">
        <el-input
          v-model="queryParams.englishTitle"
          placeholder="请输入英语名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="primary"-->
      <!--          plain-->
      <!--          icon="el-icon-plus"-->
      <!--          size="mini"-->
      <!--          @click="handleAdd"-->
      <!--          v-hasPermi="['sist:navigation:add']"-->
      <!--        >新增-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="navigationList"
      row-key="id"
      default-expand-all
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column label="导航名称" align="left" prop="title"/>
      <el-table-column label="英语名称" align="center" prop="englishTitle"/>
      <el-table-column label="导航类型" align="center" prop="type" :formatter="typeFormat"/>
      <el-table-column label="显示类型" align="center" prop="displayType" :formatter="displayTypeFormat"/>
      <el-table-column label="排序" align="center" prop="sort"/>
      <el-table-column label="展示图片" align="center" prop="link">
        <template slot-scope="scope">
          <img style="width: 300px;" :src="scope.row.link" alt="">
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" width="350" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-show="scope.row.type == '0'"
                     type="text"
                     icon="el-icon-plus"
                     size="mini"
                     @click="handleAdd(scope.row)"
                     v-hasPermi="['sist:navigation:add']"
          >新增下级
          </el-button>
          <el-button v-show="scope.row.type == '1'"
                     size="mini"
                     type="text"
                     icon="el-icon-edit"
                     @click="handleUpdateDetails(scope.row.refId)"
          >修改中文详情
          </el-button>
          <el-button v-show="scope.row.type == '1'"
                     size="mini"
                     type="text"
                     icon="el-icon-edit"
                     @click="handleUpdateDetails(scope.row.enRefId)"
          >修改英文详情
          </el-button>
          <el-button v-show="scope.row.pid != 0"
                     size="mini"
                     type="text"
                     icon="el-icon-edit"
                     @click="handleUpdate(scope.row)"
                     v-hasPermi="['sist:navigation:edit']"
          >修改
          </el-button>
          <el-button v-show="scope.row.pid != 0"
                     size="mini"
                     type="text"
                     icon="el-icon-delete"
                     @click="handleDelete(scope.row)"
                     v-hasPermi="['sist:navigation:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改导航对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="550px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="父级">
          <el-input v-model="form.ptitle" disabled/>
          <!--          <treeselect v-model="form.pid" :options="navigationOptions" :normalizer="normalizer" placeholder="请选择父ID"/>-->
        </el-form-item>
        <el-form-item label="导航名称" prop="title">
          <el-input v-model="form.title" placeholder="请输入导航名称"/>
        </el-form-item>
        <el-form-item label="英语名称" prop="englishTitle">
          <el-input v-model="form.englishTitle" placeholder="英语名称"/>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="form.sort" placeholder="排序"/>
        </el-form-item>
        <el-form-item label="显示网站">
          <el-radio-group v-model="form.displayType">
            <el-radio-button v-for="dict in displayTypeOptions" :key="dict.dictValue"
                             :label="dict.dictValue"
            >{{ dict.dictLabel }}
            </el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-show="anvTypeEdit" label="导航类型" prop="type">
          <!--          <el-input v-model="form.link" placeholder="请输入导航链接"/>-->
          <el-radio-group v-model="form.type" @change="navigationTypeChang(form.type)">
            <el-radio
              v-for="dict in navigationTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{ dict.dictLabel }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="缩略图" prop="isThumbnail" v-show="articleSelShow">
          <el-radio-group v-model="form.isThumbnail">
            <el-radio-button
              v-for="dict in thumbnailOptions"
              :key="dict.key"
              :label="dict.key"
            >{{ dict.label }}
            </el-radio-button>
          </el-radio-group>
        </el-form-item>
        <!--        <el-form-item label="内容类型" prop="refId" v-show="articleSelShow">-->
        <!--          <el-select v-model="form.refId" placeholder="请选择内容类型" clearable :style="{width: '100%'}">-->
        <!--            <el-option-->
        <!--              v-for="dict in articleTypeOptions"-->
        <!--              :key="dict.dictCode"-->
        <!--              :label="dict.dictLabel"-->
        <!--              :value="dict.dictCode"-->
        <!--            />-->
        <!--          </el-select>-->
        <!--        </el-form-item>-->
        <el-form-item label="中文层次数据" prop="refId" v-show="empleeSelShow">
          <el-select v-model="form.refId" placeholder="请选择层次数据" clearable :style="{width: '100%'}">
            <el-option
              v-for="dict in direTypeOptions"
              :key="dict.dictCode"
              :label="dict.dictLabel"
              :value="dict.dictCode"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="英文层次数据" prop="enRefId" v-show="empleeSelShow">
          <el-select v-model="form.enRefId" placeholder="请选择层次数据" clearable :style="{width: '100%'}">
            <el-option
              v-for="dict in direTypeOptions"
              :key="dict.dictCode"
              :label="dict.dictLabel"
              :value="dict.dictCode"
            />
          </el-select>
        </el-form-item>
        <el-form-item v-show="imageShow" label="" prop="link">
          <UploadFile v-model="form.link"
                      file-type="image"
                      :v-public="true"
                      :v-data="false"
          />
          <!--          <el-input v-model="form.link" placeholder="请输入导航链接"/>-->
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="编辑详情" :visible="detailsShow" width="1100px" :close-on-click-modal="false" @close="detailsClose()"
               append-to-body
    >
      <el-form :model="formData" size="medium" label-width="100px">
        <el-row>
          <el-col :span="12">
            <!--            <el-form-item label="详情标题" prop="title">-->
            <el-form-item label="详情标题">
              <el-input v-model="formData.title" placeholder="请输入详情标题" clearable :style="{width: '100%'}">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :offset="1" :span="22">
            <Editor v-model="formData.content" :height="500"/>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24" class="save">
            <el-form-item size="large">
              <el-button type="primary" @click="save">保存</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  listNavigation,
  getNavigation,
  delNavigation,
  addNavigation,
  updateNavigation,
  updateDetails,
  getDetails,
  exportNavigation
} from '@/api/sist/navigation'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import Editor from '@/components/Editor'
import UploadFile from '@/views/utils/uploadFile'
import { getArticleNav } from '../../../api/sist/article'

export default {
  name: 'Navigation',
  components: {
    Treeselect,
    Editor,
    UploadFile
  },
  data() {
    return {
      formData: {
        title: '',
        content: ''
      },
      detailsShow: false,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      articleSelShow: false,
      empleeSelShow: false,
      imageShow: false,
      // 导航表格数据
      navigationTypeOptions: [],
      thumbnailOptions: [
        {
          key: 0,
          label: '无'
        },
        {
          key: 1,
          label: '有'
        }
      ],
      // 导航树选项
      navigationList: [],
      articleTypeOptions: [],
      sistArticleTypeOption: [],
      labArticleTypeOption: [],
      direTypeOptions: [],
      sistDireTypeOptions: [],
      labDireTypeOptions: [],
      displayTypeOptions: [
        {
          'dictSort': 1,
          'dictLabel': '同时',
          'dictValue': '1'
        },
        {
          'dictSort': 2,
          'dictLabel': '英文',
          'dictValue': '2'
        },
        {
          'dictSort': 3,
          'dictLabel': '中文',
          'dictValue': '3'
        },
        {
          'dictSort': 4,
          'dictLabel': '隐藏',
          'dictValue': '4'
        }
      ],
      navigationType: [],
      navList: [],
      anvTypeEdit: true,
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 1: 正常  0：软删除字典
      // 查询参数
      queryParams: {
        title: null,
        englishTitle: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: '导航名称不能为空', trigger: 'blur' }
        ],
        englishTitle: [
          { required: true, message: '英语名称不能为空', trigger: 'blur' }
        ]
      },
      detailsRules: {
        // title: [
        //   {required: true, message: "导航名称不能为空", trigger: "blur"}
        // ],
      }
    }
  },
  created() {
    this.getList()
    this.getDicts('navigation_type').then(res => {
      this.navigationTypeOptions = res.data
    })
    // this.getDicts('display_type').then(res => {
    //   this.displayTypeOptions = res.data
    // })
    this.getDicts('dire_type').then(res => {
      let direList = res.data
      for (let dire of direList) {
        if (dire.attribute2 === 1) {
          this.sistDireTypeOptions.push(dire)
        } else if (dire.attribute2 === 2) {
          this.labDireTypeOptions.push(dire)
        }
      }
    })
    this.getDicts('article_type').then(res => {
      let articleList = res.data
      for (let article of articleList) {
        if (article.attribute2 === 1) {
          this.sistArticleTypeOption.push(article)
        } else if (article.attribute2 === 2) {
          this.labArticleTypeOption.push(article)
        }
      }
    })
  },
  methods: {
    /** 查询导航列表 */
    getList() {
      this.loading = true
      listNavigation(this.queryParams).then(response => {
        this.navList = response.data
        this.navigationList = this.handleTree(response.data, 'id', 'pid')
        this.loading = false
      })
    },
    /** 转换导航数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children
      }
      return {
        id: node.id,
        label: node.title,
        children: node.children
      }
    },
    typeFormat(row, column) {
      return this.selectDictLabel(this.navigationTypeOptions, row.type)
    },
    displayTypeFormat(row, column) {
      return this.selectDictLabel(this.displayTypeOptions, row.displayType)
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      listNavigation(this.queryParams).then(response => {
        this.navigationOptions = []
        const data = { id: 0, title: '顶级节点', children: [] }
        data.children = this.handleTree(response.data, 'id', 'pid')
        this.navigationOptions.push(data)
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    detailsClose() {
      this.detailsShow = false
    },
    // 表单重置
    reset() {
      this.articleSelShow = false
      this.imageShow = false
      this.empleeSelShow = false
      this.form = {
        id: null,
        pid: null,
        title: null,
        link: null,
        sort: 0,
        refId: null,
        enRefId: null,
        displayType: '1'
      }
      this.anvTypeEdit = true
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset()
      // this.getTreeselect();
      this.open = true
      this.form.ptitle = row.title
      this.form.pid = row.id
      this.form.sitetype = row.sitetype
      this.form.type = '0'
      this.title = '添加下级导航'
    },
    handleUpdateDetails(id) {
      this.detailsShow = true
      this.formData = {
        id: id,
        title: '',
        content: ''
      }
      getDetails(id).then(res => {
        this.formData = res.data
      })
    },
    save() {
      updateDetails(this.formData).then(res => {
        this.msgSuccess('修改成功')
        this.detailsShow = false
      })
    },
    //导航类型变化
    navigationTypeChang(type) {
      // if (type != "3"){
      //   this.form.refId = null
      // }
      this.articleTypeOptions = []
      this.direTypeOptions = []
      if (type == 2) {
        this.articleSelShow = true
        this.empleeSelShow = false
        if (this.form.sitetype == 1) {
          this.articleTypeOptions = this.sistArticleTypeOption
        } else if (this.form.sitetype == 2) {
          this.articleTypeOptions = this.labArticleTypeOption
        }
      } else if (type == 3) {
        this.articleSelShow = false
        this.empleeSelShow = true
        if (this.form.sitetype == 1) {
          this.direTypeOptions = this.sistDireTypeOptions
        } else if (this.form.sitetype == 2) {
          this.direTypeOptions = this.labDireTypeOptions
        }
      } else {
        this.articleSelShow = false
        this.empleeSelShow = false
      }

      if (this.form.type == 0) {
        this.imageShow = false
      } else {
        this.imageShow = true
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      if (row.sisttype == 1) {
        this.articleTypeOptions = this.sistArticleTypeOption
      } else if (row.sisttype == 2) {
        this.articleTypeOptions = this.labArticleTypeOption
      }
      getNavigation(row.id).then(response => {
        this.form = response.data
        console.log(this.form, 'this.form')
        if (this.form.isThumbnail == null) {
          this.form.isThumbnail = 0
        }
        console.log(response.data, 'data')
        if (this.form.pid == 0) {
          this.form.ptitle = '顶级节点'
        } else {
          for (let nav of this.navList) {
            if (nav.id == this.form.pid) {
              this.form.ptitle = nav.title
            }
            if (nav.pid == this.form.id) {
              this.anvTypeEdit = false
            }
          }
          console.log(this.navigationList, 'fgfgfgfg')
        }
        console.log(this.form, 'from')
        if (this.form.type == 0) {
          this.imageShow = false
        } else {
          this.imageShow = true
        }
        this.open = true
        this.title = '修改导航'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateNavigation(this.form).then(response => {
              this.open = false
              this.msgSuccess('修改成功')
              this.getList()
            })
          } else {
            addNavigation(this.form).then(response => {
              this.open = false
              this.msgSuccess('新增成功')
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除导航编号为"' + row.id + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delNavigation(row.id)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      })
    }
  }
}
</script>
<style scoped lang="scss">
.save {
  //text-align: center;
  margin-top: 50px;
}
</style>
