<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryParams" v-show="showSearch" :inline="true">
      <el-form-item label="问题标题" prop="problemTitle">
        <el-input v-model="queryParams.problemTitle" placeholder="请输入问题标题" size="small" maxlength="30"></el-input>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker v-model="daterangeCreateTime" size="small" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请输入状态" clearable size="small" style="width: 240px">
          <el-option label="上架" :value="true" />
          <el-option label="下架" :value="false" />
        </el-select>
      </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="goNew({},'新增')">新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="list" row-key="id" :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <el-table-column type="index" width="55" label="序号" align="center">
        <template slot-scope="scope">
          <div>{{(queryParams.pageNum-1)*queryParams.pageSize+scope.$index+1}}</div>
        </template>
      </el-table-column>
      <el-table-column label="问题标题" prop="problemTitle" width="400" align="center" />
      <el-table-column label="排序号" prop="sort" align="center" />
      <el-table-column label="上架状态" align="center" prop="status">
        <template slot-scope="{row}">
          <el-tag type="success" v-if="row.status">上架</el-tag>
          <el-tag type="danger" v-if="!row.status">下架</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" prop="createTime" align="center">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="goNew(scope.row,'编辑')">编辑</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
          <el-button size="mini" type="text" @click="setStatus(scope.row)">{{scope.row.status?'下架':'上架'}}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <el-drawer :title="title+'常见问题'" :visible.sync="drawer" size="40%" :before-close="handleClose">
      <el-form class="form" ref="form" :rules="rules" :model="form" label-width="130px">
        <el-form-item label="问题标题:" prop="problemTitle">
          <el-input v-model="form.problemTitle" placeholder="请输入问题标题" maxlength="30"></el-input>
        </el-form-item>
        <el-form-item label="序号:" prop="sort">
          <el-input-number v-model="form.sort" :min="1" label="描述文字"></el-input-number>
        </el-form-item>
        <el-form-item label="答复内容:" prop="replyContent">
          <editor v-model="form.replyContent" :min-height="192" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :loading="btnLoading" @click="onSubmit">确定</el-button>
          <el-button @click="handleClose()">取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
import { problemAdd, problemEdit, problemPage, problemInfo, problemDel, problemStatus } from "@/api/operating.js"
export default {
  data () {
    return {
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 遮罩层
      loading: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        status: undefined,
        problemTitle: undefined
      },
      list: [],
      form: {
        status: true,
        sort: 1
      },
      daterangeCreateTime: [],
      drawer: false,
      rules: {
        problemTitle: [
          { required: true, message: '请输入问题标题', trigger: 'change' },
        ],
        sort: [
          { required: true, message: '请输入序号', trigger: 'change' },
        ],
        replyContent: [
          { required: true, message: '请输入答复内容', trigger: 'change' },
        ],
      },
      btnLoading: false,
      associatedAppListShow: false,
      title: ""
    }
  },
  created () {
    this.getList()
  },
  methods: {
    goNew (row = {}, title) {
      this.drawer = true;
      this.title = title;
      this.form = {
        status: true,
        sort: 1
      };
      if (row.id) {
        this.getDetails(row.id)
      }
    },
    goModelList () {
      this.$router.push({
        name: "modelList",
        query: {
        }
      })
    },
    getDetails (id) {
      problemInfo(id).then(res => {
        this.form = res.data
      })
    },
    handleClose () {
      this.drawer = false
    },
    onSubmit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.btnLoading = true;
          let apiFun = this.form.id ? problemEdit : problemAdd;
          apiFun(this.form).then(res => {
            this.$message.success(this.form.id ? '编辑成功！' : '新增成功！');
            this.getList()
            this.handleClose();
          }).finally(e => {
            this.btnLoading = false
          })
        }
      });
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.daterangeCreateTime = [];
      this.resetForm("queryParams");
      this.handleQuery();
    },
    // 获取列表
    getList () {
      this.loading = true;
      this.queryParams["startTime"] = undefined;
      this.queryParams["endTime"] = undefined;
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams["startTime"] = this.daterangeCreateTime[0];
        this.queryParams["endTime"] = this.daterangeCreateTime[1];
      }
      problemPage(this.queryParams).then(response => {
        this.list = response.rows;
        this.total = response.total;
        this.loading = false;
      }
      );
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      this.$modal.confirm('是否确认删除？').then(function () {
        return problemDel(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    setStatus (row) {
      let status = row.projectCategoryStatus ? '下架' : '上架'
      this.$modal.confirm(`是否确认${status}？`).then(function () {
        return problemStatus(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess(status + "成功");
      }).catch(() => { });
    }
  }
}
</script>

<style lang="scss" scoped>
.flex {
  display: flex;
}
.ml20 {
  margin-left: 20px;
}
::v-deep .el-drawer__body {
  padding-right: 10%;
}
</style>