<template>
  <div id="index">
    <el-row>
      <el-col :span="24" class="main">
        <el-col :span="24" class="one">
          <el-col :span="24" class="one_1"> 保安资讯信息查询选项 </el-col>
          <el-col :span="24" class="one_2">
            <el-form ref="searchFrom" :model="searchFrom" label-width="140px">
              <el-col :span="24" class="one_2txt">
                <el-col :span="8">
                  <el-form-item label="信息标题">
                    <el-input v-model="searchFrom.title"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="信息来源">
                    <el-input v-model="searchFrom.origin"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="信息简介">
                    <el-input v-model="searchFrom.brief"></el-input>
                  </el-form-item>
                </el-col>
              </el-col>
              <el-col :span="24" class="one_2txt">
                <el-col :span="24" class="btn">
                  <el-button type="primary" size="mini" @click="search()">查询</el-button>
                  <el-button type="primary" size="mini" @click="toAdd()">添加</el-button>
                </el-col>
              </el-col>
            </el-form>
          </el-col>
        </el-col>
        <el-col :span="24" class="two">
          <data-table @query="search" :fields="fields" :opera="opera" :data="list" :total="total" @edit="toEdit"> </data-table>
        </el-col>
      </el-col>
    </el-row>
    <el-dialog title="咨询信息管理" :visible.sync="show" width="50%" :before-close="close">
      <form-1 :form="form" @close="close" @onSubmit="onSubmit"></form-1>
    </el-dialog>
  </div>
</template>

<script>
import form1 from './parts/form-1.vue';
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: baoan_news } = createNamespacedHelpers('baoan_news');
export default {
  name: 'index',
  props: {},
  components: { form1 },
  data: function () {
    return {
      // 查询
      searchFrom: {},
      // 列表
      fields: [
        { label: '信息标题', model: 'title' },
        { label: '信息来源', model: 'origin' },
        { label: '发布时间', model: 'publish_date' },
      ],
      opera: [{ label: '维护', method: 'edit' }],
      list: [],
      total: 0,
      // 添加信息
      show: false,
      form: {},
    };
  },
  created() {
    this.search();
  },
  methods: {
    ...baoan_news(['query', 'create', 'update']),
    async search({ skip = 0, limit = 10, ...info } = {}) {
      const condition = _.cloneDeep(this.searchFrom);
      let res = await this.query({ skip, limit, ...condition, ...info });
      if (this.$checkRes(res)) {
        this.$set(this, `list`, res.data);
        this.$set(this, `total`, res.total);
      }
    },
    // 添加信息
    toAdd() {
      this.show = true;
    },
    // 修改信息
    toEdit({ data }) {
      this.$set(this, `form`, data);
      this.show = true;
    },
    // 提交信息
    async onSubmit({ data }) {
      if (data.id) {
        let res = await this.update(data);
        if (this.$checkRes(res)) {
          this.$message({ type: `success`, message: `操作成功` });
          this.search();
          this.close();
        }
      } else {
        let res = await this.create(data);
        if (this.$checkRes(res)) {
          this.$message({ type: `success`, message: `操作成功` });
          this.search();
          this.close();
        }
      }
    },
    // 关闭
    close() {
      this.show = false;
    },
  },
  computed: {
    ...mapState(['user']),
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
  watch: {
    test: {
      deep: true,
      immediate: true,
      handler(val) {},
    },
  },
};
</script>

<style lang="less" scoped>
.main {
  .one {
    .one_1 {
      height: 40px;
      line-height: 40px;
      border-bottom: 1px solid #ccc;
      margin: 0 0 10px 0;
      font-size: 18px;
      font-weight: bold;
    }
    .one_2 {
      .one_2txt {
        border: 1px solid #ccc;
        /deep/.el-form-item {
          margin: 0;
        }
        /deep/.el-date-editor.el-input,
        .el-date-editor.el-input__inner {
          width: 100%;
        }
        /deep/.el-select {
          width: 100%;
        }
        .btn {
          text-align: right;
          padding: 5px 5px;
        }
      }
    }
  }
}
</style>
