<template>
  <el-row>
    <el-row>
      <el-form ref="searchForm" :model="searchForm" label-width="80px">
        <search-white-board :show-more="true" @search="list">
          <div slot="base">
            <el-row style="line-height: 40px; text-align: right;">
              <!-- <el-col :span="2"><label class="search-label">机构</label></el-col>
              <el-col :span="5">
                <select-organ v-model="searchForm.organIds" />
              </el-col> -->
              <el-col :span="2" :offset="1"><label class="search-label">新闻标题</label></el-col>
              <el-col :span="5">
                <el-input v-model.trim="searchForm.title" placeholder="请输入新闻标题" clearable />
              </el-col>
            </el-row>
          </div>

          <div slot="more">
            <el-row style="line-height: 40px; text-align: right;">
              <el-col :span="2"><label class="search-label">是否删除</label></el-col>
              <el-col :span="5" style="text-align: left;">
                <el-switch v-model="searchForm.isDel" active-color="#13ce66" inactive-color="#ff4949" @change="list" />
              </el-col>
            </el-row>
          </div>
        </search-white-board>
      </el-form>
    </el-row>

    <el-row>
      <white-board>
        <el-row>
          <el-col :span="2" :offset="22" style="text-align: right;">
            <el-button icon="el-icon-plus" type="success" plain @click="handleAdd">新增</el-button>
          </el-col>
        </el-row>

        <el-row style="padding: 10px 0px;">
          <el-table :data="tableData" style="width: 100%;" highlight-current-row stripe>
            <el-table-column prop="title" label="新闻标题" show-overflow-tooltip />
            <el-table-column prop="organName" label="机构名称" show-overflow-tooltip />
            <el-table-column prop="author" label="发布机构/人" show-overflow-tooltip />
<!--             <el-table-column prop="images" label="图片">
              <template slot-scope="scope"><img :src="HOME_URL + scope.row.images" width="50"></template>
            </el-table-column> -->
            <el-table-column label="更新时间" width="135">
              <template slot-scope="scope">
                <el-row>{{ scope.row.updateByName }}</el-row>
                <el-row>{{ scope.row.updateAt }}</el-row>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="230" fixed="right">
              <template slot-scope="scope">
                <el-button type="success" plain @click="handleDetail(scope.$index, scope.row)">详情</el-button>
                <el-button type="primary" plain @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                <el-button v-if="!scope.row.isDel" type="danger" plain @click="handleIsDel(scope.$index, scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-row>

        <el-row>
          <el-pagination :current-page="searchForm.page" :page-sizes="[10, 20, 50, 100]" :page-size="50" layout="total, sizes, prev, pager, next, jumper" :total="total" small @size-change="changeSizeChange" @current-change="changeCurrentChange" />
        </el-row>
      </white-board>
    </el-row>

    <el-dialog :visible.sync="dialogVisible" :before-close="cancelDialog">
      <el-divider />
      <label slot="title" style="float: left;">{{ dialogTitle }}</label>

      <el-row>
        <el-form ref="form" :model="form" :rules="formRules" label-suffix="：" label-width="100px">
          <el-form-item label="标题" prop="title">
            <el-input v-model.trim="form.title" placeholder="请输入新闻标题" clearable />
          </el-form-item>
          <el-form-item label="机构名称" prop="organId">
            <select-organ v-model="form.organId" :multiple="false" />
          </el-form-item>
          <el-form-item label="发布机构/人" prop="author">
            <el-input v-model.trim="form.author" placeholder="请输入发布机构/人" clearable />
          </el-form-item>
          <el-form-item label="图片" prop="images" style="text-align: left;">
            <upload-img v-model="form.images" :limit="1" />
          </el-form-item>
          <el-form-item label="内容" prop="content" style="text-align: left;">
            <el-input v-model="form.content" type="textarea" :autosize="{ minRows: 2, maxRows: 6}" placeholder="请输入内容" />
          </el-form-item>
        </el-form>
      </el-row>

      <div slot="footer" class="dialog-footer">
        <el-divider />
        <el-button @click="cancelDialog">取 消</el-button>
        <el-button type="primary" @click="handleSave">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 修改新闻 -->
    <new-edit :id="newsId" :show-bind-drawer.sync="showBindDrawer" :title="dialogTitle" @closeBindDrawer="closeDrawer" />
    <new-detail :id="newsId" :show-bind-drawer-detail.sync="showBindDrawerDetail" @closeBindDrawerDetail="closeDrawerDetail" />

  </el-row>
</template>

<script>
import UploadImg from '@/components/common/upload/upload-img'
import { SelectOrgan } from '@/components/organ'
import { NewsEdit, NewsDetail } from '@/components/News'
import newsApi from '@/api/info/newsApi'

// todo 补一个详情页面效果
export default {
  name: 'News',
  components: {
    'upload-img': UploadImg,
    'select-organ': SelectOrgan,
    'new-edit': NewsEdit,
    'new-detail': NewsDetail
  },
  data() {
    return {
      searchForm: {
        title: '',
        organIds: [],
        isDel: false,
        page: 1,
        size: 50
      },
      showBindDrawer: false,
      newsId: '',
      total: 0,
      tableData: [],
      dialogTitle: '',
      showBindDrawerDetail: false,
      dialogVisible: false,
      form: {
        id: '',
        title: '',
        images: '',
        content: '',
        author: '',
        organId: ''
      },
      formRules: {
        title: [{ required: true, message: '请输入标题', trigger: 'blur' }],
        author: [{ required: true, message: '请输入作者', trigger: 'blur' }],
        content: [{ required: true, message: '请输入内容', trigger: 'blur' }],
        organId: [{ required: true, message: '请选择机构', trigger: 'blur' }]
      }
    }
  },
  created(){
    console.log("created")
    console.log(this.searchForm)
  },
  mounted() {
    this.list()
    console.log("mounted")
    console.log(this.searchForm)
  },
  methods: {
    cancelDialog() {
      this.dialogVisible = false
      this.form = {
        id: '',
        title: '',
        author: '',
        images: '',
        content: ''
      }
      this.$refs['form'].resetFields()
    },
    handleAdd() {
      this.dialogTitle = '新闻新增'
      this.showBindDrawer = true
    },
    handleEdit(index, row) {
      this.form = JSON.parse(JSON.stringify(row))
      this.dialogTitle = '新闻编辑'
      this.newsId = row.id
      this.showBindDrawer = true
    },
    handleDetail(index, row) {
      this.form = JSON.parse(JSON.stringify(row))
      this.newsId = row.id
      this.showBindDrawerDetail = true
    },
    closeDrawerDetail() {
      this.showBindDrawerDetail = false
      this.list()
    },
    closeDrawer() {
      this.showBindDrawer = false
      this.list()
    },
    handleIsDel(index, row) {
      newsApi.handleIsDel(row.id).then(data => {
        this.list()
      })
    },
    handleSave() {
      this.$refs['form'].validate((valid) => {
        if (!valid) {
          return
        }
        newsApi.saveOrUpdate(this.form).then(data => {
          this.list()

          this.dialogVisible = false
          this.$refs['form'].resetFields()
        })
      })
    },
    list() {
      this.tableData = []
      newsApi.page(this.searchForm).then(data => {
        this.total = data.total
        this.tableData = data.data
      })
    },
    changeSizeChange(val) {
      this.searchForm.size = val
      this.list()
    },
    changeCurrentChange(val) {
      this.searchForm.page = val
      this.list()
    }
  }
}
</script>

<style scoped>
  .distpicker-address-wrapper select{
    padding: 0px;
    height: 28px;
    color: #606266;
    font-size: 0.5rem;line-height: 0.3
  }
</style>
