<template>
  <div class="app-container calendar-list-container">

    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input clearable class="filter-item" style="width: 200px;" placeholder="请输入问题关键字" v-model="listQuery.content"></el-input>
      <el-select v-model="listQuery.deptUnid" :disabled="!canGetAll" clearable placeholder="请选择部门" style="top: -4px;width: 200px;">
        <el-option label="全部" value="">
        </el-option>

        <el-option v-for="item in deptList" :label="item.deptName" :value="item.deptUnid">
        </el-option>
      </el-select>

      <el-date-picker
        v-model="listQuery.startDate"
        type="date"
        value-format="yyyy-MM-dd"
        placeholder="选择起始日期"
        style="top: -4px;width: 200px;"
      >
      </el-date-picker>
      ~
      <el-date-picker
        v-model="listQuery.endDate"
        type="date"
        value-format="yyyy-MM-dd"
        placeholder="选择起始日期"
        style="top: -4px;width: 200px;"
      >
      </el-date-picker>

      <el-select v-model="listQuery.type" clearable placeholder="请选择类型" style="top: -4px;width: 200px;">
        <el-option label="投诉" value="1">
        </el-option>
        <el-option label="咨询" value="2">
        </el-option>
      </el-select>
      <el-select v-model="listQuery.isAnswer" clearable placeholder="请选择回复状态" style="top: -4px;width: 200px;">
        <el-option label="未回复" value="0">
        </el-option>
        <el-option label="已回复" value="1">
        </el-option>
      </el-select>
      <el-button class="filter-item" type="primary" v-waves icon="el-icon-search" @click="handleFilter">查找</el-button>
      <el-button class="filter-item" type="warning" v-waves icon="el-icon-refresh" @click="clearQuery">重置</el-button>
      <el-button class="filter-item" type="success" v-waves icon="el-icon-refresh" @click="getList">刷新</el-button>

    </div>

    <!-- 查询结果 -->
    <el-table size="small" :data="list" v-loading="listLoading" element-loading-text="正在查询中。。。" border fit highlight-current-row>


      <!--<el-table-column align="center" width="100px" label="BannerID" prop="id" sortable>
      </el-table-column>-->
      <el-table-column type="index" label="序号" header-align="center" align="center">
      </el-table-column>

      <el-table-column align="center" min-width="100px" label="事项名称" prop="serviceName" :show-overflow-tooltip='true'>
      </el-table-column>

      <el-table-column align="center" min-width="100px" label="类型" >
        <template slot-scope="scope">
            <p v-if="scope.row.type ==1">投诉</p>
            <p v-else-if="scope.row.type ==2">咨询</p>
        </template>
      </el-table-column>

      <el-table-column align="center" min-width="100px" label="标题" prop="title" :show-overflow-tooltip='true'>
      </el-table-column>

      <el-table-column align="center" min-width="100px" label="内容" prop="content" :show-overflow-tooltip='true'>
      </el-table-column>

      <el-table-column align="center" min-width="200px" label="回复" prop="answer" :show-overflow-tooltip='true'>
      </el-table-column>

      <el-table-column align="center" min-width="80px" label="创建人" prop="consultantName">
      </el-table-column>
      <el-table-column align="center" min-width="90px" label="联系方式" prop="consultantPhone">
      </el-table-column>

      <el-table-column align="center" min-width="100px" label="创建时间" prop="addTime">
      </el-table-column>

      <el-table-column align="center" label="操作" width="210px" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="scope.row.isAnswer == 0" type="primary" size="mini" @click="handleUpdate(scope.row)">回复</el-button>

          <el-button type="danger" size="mini"  @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="listQuery.page"
        :page-sizes="[10,20,30,50]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>

    <!-- 添加或修改对话框 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" width="800px">
      <el-form :rules="rules" ref="dataForm" :model="dataForm" status-icon label-position="left" label-width="100px" style='width: 700px; margin-left:50px;'>
        <el-form-item label="标题" prop="title">
          <!--<span>{{dataForm.questions}}</span>-->
          <el-tag style="max-width: 600px; height: auto; white-space: normal;" >{{dataForm.title}}</el-tag>
        </el-form-item>
        <el-form-item label="问题" prop="content">
          <!--<span>{{dataForm.questions}}</span>-->
          <el-tag style="max-width: 600px; height: auto; white-space: normal;" >{{dataForm.content}}</el-tag>
        </el-form-item>

        <el-form-item style="width: 700px;" label="回复" prop="answer">
          <el-input
            type="textarea"
            :rows="5"
            placeholder="请输入内容"
            v-model="dataForm.answer">
          </el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="updateData">确定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<style>
  .demo-table-expand {
    font-size: 0;
  }
  .demo-table-expand label {
    width: 200px;
    color: #99a9bf;
  }
  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
  }
  .el-tooltip__popper{
    max-width:70%
  }
</style>

<script>
  import { list, updateData, deleteData,getDeptList } from '@/api/consultant'
  import waves from '@/directive/waves' // 水波纹指令
  import BackToTop from '@/components/BackToTop'
  import Tinymce from '@/components/Tinymce'

export default {
  name: 'questions',
  components: { BackToTop, Tinymce },
  directives: {
    waves
  },
  data() {
    return {
      list: undefined,
      total: undefined,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10,
        content: undefined,
        isAnswer: undefined,
        deptUnid:undefined,
        type:undefined,
        startDate:undefined,
        endDate:undefined
      },
      dataForm: {
        id: undefined,
        title: undefined,
        content: undefined,
        answer: undefined
      },
      canGetAll:true,
      dialogFormVisible: false,
      dialogStatus: '',
      deptList:[],
      textMap: {
        update: '回复'
      },
      rules: {
        answer: [{ required: true, message: '回复不能为空', trigger: 'blur' }]
      },
      downloadLoading: false,
     pickerOptions: {
       shortcuts: [{
         text: '最近一周',
         onClick(picker) {
           const end = new Date();
           const start = new Date();
           start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
           picker.$emit('pick', [start, end]);
         }
       }, {
         text: '最近一个月',
         onClick(picker) {
           const end = new Date();
           const start = new Date();
           start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
           picker.$emit('pick', [start, end]);
         }
       }, {
         text: '最近三个月',
         onClick(picker) {
           const end = new Date();
           const start = new Date();
           start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
           picker.$emit('pick', [start, end]);
         }
       }]
     }
    }
  },
  created() {
    this.getDept();
  },
  methods: {
    getDept(){
      getDeptList().then(res =>{
          if(res.data.errno==0){
            this.deptList = res.data.data;
            this.getList()
          }
      })
    },
    getList() {
      this.listLoading = true
      list(this.listQuery).then(response => {
        this.list = response.data.data.items
        this.total = response.data.data.total
        this.listQuery.deptUnid = response.data.data.deptUnid
        this.canGetAll = response.data.data.canGetAll
        this.listLoading = false
      }).catch(() => {
        this.list = []
        this.total = 0
        this.listLoading = false
      })
    },

    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },

    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },

    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },

    handleUpdate(row) {
      this.dataForm.id =  row.id
      this.dataForm.title= row.title,
      this.dataForm.content=row.content,
      this.dataForm.answer=row.answer
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },

    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          updateData(this.dataForm).then(() => {
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '更新成功',
              type: 'success',
              duration: 2000
            })
            this.getList()
          })
        }
      })
    },

    handleDelete(row) {

      this.$confirm('确认删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let delData={
          id :row.id
        };
        deleteData(delData).then(response => {
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 2000
          })
          const index = this.list.indexOf(row)
          this.list.splice(index, 1)
        })
      }).catch(() => {

      })

    },
    /*清除查询条件*/
    clearQuery(){
        this.listQuery.content='';
        this.listQuery.isAnswer='';
        this.listQuery.page=1;
        this.getList();
    }
  },

}
</script>
<style>
  .ad-avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .ad-avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .ad-avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .ad-avatar {
    display: block;
  }
</style>
