<template>
  <div class="container">
    <div class="head">
      <div>卫星介绍</div>
      <el-button type="primary" @click="addItem">添加卫星</el-button>
    </div>
    <div class="app-container calendar-list-container">
      <div class="filter-container">
        <el-form :inline="true" :model="searchForm" class="demo-form-inline">
          <el-form-item label="所有状态">
            <el-select v-model="searchForm.status">
              <el-option v-for="(item, index) in bannerStatus" :key="index" :label="item" :value="~~index"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="卫星名称">
            <el-input v-model="searchForm.title" placeholder="请输入卫星名称"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" plain @click="filterData">搜索</el-button>
            <el-button type="primary" plain @click="clearFilterData">清除搜索</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-table :data="list" v-loading.body="listLoading" border fit highlight-current-row style="width: 100%">
        <el-table-column align="center" type="index" label="序号" width="65"></el-table-column>
        <el-table-column align="center" label="卫星名称" prop="name"></el-table-column>
        <el-table-column align="center" label="缩略图">
          <template slot-scope="scope">
            <img class="cover" :src="scope.row.coverImg" />
          </template>
        </el-table-column>
        <el-table-column align="center" label="分辨率" prop="resolution"></el-table-column>
        <el-table-column align="center" label="排序" prop="rank"></el-table-column>
        <el-table-column align="center" label="状态">
          <template slot-scope="scope">
            {{ getStatus(scope.row.status) }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="250">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="edit(scope.row)">编辑</el-button>
            <el-button :type="scope.row.status === 1 ? 'info': 'warning'" size="mini" @click="changeParams(scope.row, 'status')">{{scope.row.status === 1 ? '下架': '上架'}}</el-button>
            <el-button type="danger" size="mini" @click="changeParams(scope.row, 'isDel')">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="page-foot">
        <el-pagination
          background
          layout="prev, pager, next"
          :page-size="listQuery.limit"
          :total="total" @current-change="currentChange">
        </el-pagination>
      </div>


      <el-dialog
        top="10%"
        :close-on-click-modal="false"
        :title="dialogTitle"
        :visible.sync="dialogVisible">
        <el-form
          ref="ruleForm"
          size="small"
          label-position="right"
          :model="editForm"
          :rules="editFormRules"
          label-width="100px"
          class="demo-ruleForm">
          <el-form-item v-for="(item, index) in editFormInfo" :key="index" :label="item.label" :prop="index">
            <div v-if="item.type && item.type == 'radio'">
              <el-radio-group v-model="editForm[index]">
                <el-radio v-for="(item, index) in item.radioArr" :key="~~index" :label="~~index">{{item}}</el-radio>
              </el-radio-group>
            </div>
            <div v-else>
              <el-input v-model="editForm[index]" :placeholder="`请输入${item.label}`"></el-input>
            </div>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm()">确定</el-button>
            <el-button type="info" @click="resetForm()">取消</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>

    </div>

  </div>
</template>

<script>
import { satelliteIntroduction } from "@/utils/formDatas.js"
import { page, update } from "@/api/website/satelliteIntroduction"

export default {
  name: "satelliteIntroduction",
  data() {
    return {
      data: null,//原数据
      list: null,//赋值数据 | 过滤数据
      total: null,
      listLoading: true,//请求加载状态
      //请求参数。页码页数等
      listQuery: {
        page: 1,
        limit: 20
      },
      //搜索表单
      searchForm: {
        status: 0,
        title: ""
      },
      //添加banner与修改banner的form对象
      editForm: satelliteIntroduction.init(),//提交的表单参数
      editFormInfo: satelliteIntroduction.formInfo,//表单项。
      editFormRules: satelliteIntroduction.rules,//表单规则
      //是否启用，1、启用，2、禁用
      bannerStatus: {
        0: '全部',
        1: '启用',
        2: '禁用',
      },
      dialogVisible: false,
      dialogTitle: "新增卫星"
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    //清除搜索
    filterData() {
      if (this.searchForm.status === 0 && this.searchForm.title === '') {
        this.list = this.data;
        return ;
      }
      let temp = this.data.filter(v => this.searchForm.status === 0 ? true: !!(this.searchForm.status === v.status))
      if (!!this.searchForm.title) {
        temp = temp.filter(v => v.title.includes(this.searchForm.title))
      }

      this.list = temp
    },
    //清除搜索
    clearFilterData() {
      this.searchForm = {
        status: 0,
        title: ""
      }
      this.list = this.data;
    },
    //切换页码
    currentChange(val) {
      this.listQuery.page = val;
      this.getList();
    },
    //编辑
    edit(row) {
      this.dialogTitle = "编辑卫星";
      this.dialogVisible = true;
      this.editForm = row
    },
    //校验表单
    submitForm() {
      let _this = this;
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          _this.updateFunc();
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    //更新方法
    updateFunc() {
      update(this.editForm).then(res => {
        if (res.status ==  200) {
          this.dialogVisible = false;
          this.getList();
        } else {
          this.$message.error(res.message);
        }
      })
    },
    //取消表单
    resetForm() {
      this.$refs.ruleForm.resetFields();
      this.dialogVisible = false;
    },
    //新增banner
    addItem() {
      // this.dialogTitle = "新增卫星";
      // this.dialogVisible = true;
      // this.editForm = satelliteIntroduction.init();
      this.$emit('change', 'edit')
    },
    //获取状态
    getStatus(val) {
      return this.bannerStatus[val]
    },
    //上下架、删除更新
    changeParams(row, key) {
      this.editForm = row;
      if (key == 'isDel') {
        this.editForm[key] = row[key] == 0 ? 1 : 0;
      } else {
        this.editForm[key] = row[key] == 1 ? 2 : 1;
      }

      //更新专题
      this.updateFunc();
    },
    //获取banner列表
    getList() {
      this.listLoading = true;
      page(this.listQuery).then(res => {
        this.list = res.data.data
        this.data = res.data.data
        this.total = res.data.totalCount;
        this.listLoading = false;
      }).catch(err => {
        this.listLoading = false;
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 20px;
  width: 100%;
  border-bottom: 1px solid #e6e6e6;
}
.cover {
  max-width: 200px;
  height: auto;
}
.page-foot {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}
</style>
