<template>
  <div class="manage-repair main-wrap">
    <el-form class="search-bar">
      <div class="grid-content">
        <el-input v-model.trim="filters.name" placeholder="报修人" clearable></el-input>
      </div>
      <div class="grid-content">
        <el-select v-model="filters.areaId" filterable clearable placeholder="报修区域">
          <el-option v-for="item in repairAreaOptions" :key="item.id" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
      </div>
      <div class="grid-content">
        <el-select v-model="filters.dealState" filterable clearable placeholder="处理状态">
          <el-option v-for="item in dealStateOptions" :key="item.id" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </div>
      <div class="grid-content">
        <el-select v-model="filters.applyIdentity" filterable clearable placeholder="身份">
          <el-option v-for="item in applyIdentityOptions" :key="item.id" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </div>
      <div class="grid-content">
        <el-date-picker v-model="filters.startDate" type="datetime" placeholder="报修开始时间" clearable>
        </el-date-picker>
      </div>
      <div class="grid-content">
        <el-date-picker v-model="filters.endDate" type="datetime" placeholder="报修结束时间" clearable>
        </el-date-picker>
      </div>
      <el-button type="primary" icon="el-icon-search" @click="searchList">搜索</el-button>
      <el-button type="primary" icon="el-icon-download" @click="exportList">导出</el-button>
    </el-form>
    <div class="tabel-wrap">
      <el-table border v-loading="loading" :data="list" style="width: 100%">
        <el-table-column type="index" label="序号" width="50"></el-table-column>
        <el-table-column prop="name" label="报修人" min-width="120"></el-table-column>
        <el-table-column prop="repairCellphone" label="报修人tel" min-width="120"></el-table-column>
        <el-table-column prop="applyIdentity" label="报修人身份" min-width="100"></el-table-column>
        <el-table-column prop="area" label="报修区域" min-width="120"></el-table-column>
        <el-table-column prop="term" label="报修项目" min-width="120"></el-table-column>
        <el-table-column prop="address" label="报修地址" min-width="120"></el-table-column>
        <el-table-column prop="createDate" :formatter="(c,r,val) => $formatDate(new Date(val),'yyyy-MM-dd')" label="报修时间" min-width="120"></el-table-column>
        <el-table-column prop="applyCellphone" label="申报人电话" min-width="120"></el-table-column>
        <el-table-column prop="dealState" :formatter="getDealStateValue" label="处理状态" min-width="120"></el-table-column>
        <el-table-column fixed="right" label="操作" min-width="80" align="center">
          <template slot-scope="scope">
            <el-button v-if="scope.row.dealState == 1" type="primary" size="mini" class="operationBtnWidth" @click="showEditDialog(1, scope)">处理</el-button>
            <el-button v-else type="primary" size="mini" class="operationBtnWidth" @click="showEditDialog(0, scope)">详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination layout="prev, pager, next" :current-page="pagination.pageNum" :page-size="pagination.pageSize" :total="pagination.total" @current-change="changePage">
      </el-pagination>
    </div>
    <drag-dialog :title="editType ? '处理' : '详情'" :visible.sync="dialogEditVisible" :before-close="resetEditDialog" width="70%">
      <el-form class="editForm" :disabled="loading">
        <el-form-item class="col-line-2" label="报修人" label-width="100px">
          <span>{{selected.name}}</span>
        </el-form-item>
        <el-form-item class="col-line-2" label="报修人tel" label-width="100px">
          <span>{{selected.repairCellphone}}</span>
        </el-form-item>
        <el-form-item class="col-line-2" label="报修区域" label-width="100px">
          <span>{{selected.area}}</span>
        </el-form-item>
        <el-form-item class="col-line-2" label="报修项目" label-width="100px">
          <span>{{selected.term}}</span>
        </el-form-item>
        <el-form-item class="col-line-1" label="报修地址" label-width="100px">
          <span>{{selected.address}}</span>
        </el-form-item>
        <el-form-item class="col-line-1" label="报修时间" label-width="100px">
          <span>{{$formatDate(new Date(selected.createDate),'yyyy-MM-dd')}}</span>
        </el-form-item>
        <el-form-item v-if="selected.uploadImg" class="col-line-1" label="图片" label-width="100px">
          <div class="previewRow">
            <div class="previewItem" v-for="(item, index) in selected.uploadImg.split(',')" :key="index">
              <img :src="item" alt="" @click="showDetail(item)">
            </div>
          </div>
        </el-form-item>
        <el-form-item class="col-line-1" v-if="selected.dealState == 1" label-width="100px">
          <el-radio v-model="selected.dealed" :label="2">已处理</el-radio>
        </el-form-item>
      </el-form>
      <div v-if="editType" slot="footer" class="dialog-footer">
        <el-button @click="resetEditDialog">取 消</el-button>
        <el-button type="primary" @click="dealApplyHandle">确 定</el-button>
      </div>
      <div v-else slot="footer" class="dialog-footer">
        <el-button type="primary" @click="resetEditDialog">确 定</el-button>
      </div>
    </drag-dialog>
    <drag-dialog title="查看图片" :visible.sync="dialogImgVisible" :before-close="resetImgDialog" width="70%">
      <img style="width: 100%;" :src="imgUrl" alt="">
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="resetImgDialog">确 定</el-button>
      </div>
    </drag-dialog>
  </div>
</template>

<script>
import listMixin from '@/mixin/listPage.js';
import { mapGetters, mapActions } from 'vuex';
import store from './store.js';
import { dealApply, exportApplyList } from '@/api/management/repair.js';

export default {
  beforeRouteEnter(to, from, next) {
    store.install();
    next();
  },
  mixins: [listMixin],
  data() {
    return {
      filters: {
        name: '',
        dealState: '',
        startDate: '',
        endDate: '',
      },
      dealStateOptions: [
        {
          label: '处理中',
          value: 1,
        },
        {
          label: '已处理',
          value: 2,
        },
      ],
      applyIdentityOptions: [
        {
          label: '学生',
          value: '学生',
        },
        {
          label: '老师',
          value: '老师',
        },
      ],
      dialogImgVisible: false,
      imgUrl: '',
    };
  },
  created() {
    this.fetchRepairAreaOptions();
  },
  computed: {
    ...mapGetters(['loading', 'repairAreaOptions']),
    ...mapGetters('management/repair', ['list', 'pagination']),
  },
  methods: {
    ...mapActions(['fetchRepairAreaOptions']),
    ...mapActions('management/repair', ['fetchList']),
    initSelected() {
      this.selected = {
        name: '',
        repairCellphone: '',
        area: '',
        term: '',
        address: '',
        dealed: '',
        uploadImg: '',
      };
    },
    getDealStateValue(c, r, val) {
      let item = this.dealStateOptions.find(item => item.value == val);
      return item ? item.label : val;
    },
    exportList() {
      const filters = this.getFilters();
      exportApplyList(filters);
    },
    showEditDialog(type, data) {
      this.editType = type;
      this.selected = {
        id: data.row.id,
        name: data.row.name,
        repairCellphone: data.row.repairCellphone,
        area: data.row.area,
        term: data.row.term,
        address: data.row.address,
        createDate: data.row.createDate,
        dealState: data.row.dealState,
        uploadImg: data.row.uploadImg,
      };
      this.dialogEditVisible = true;
    },
    validateSelect() {
      if (!this.selected.dealed) {
        this.$message.error('请勾选已处理！');
        return;
      }
      return true;
    },
    dealApplyHandle() {
      if (!this.validateSelect()) {
        return;
      }
      const entity = {
        id: this.selected.id,
        dealState: this.selected.dealed,
      };
      dealApply(entity)
        .then(res => {
          this.fetchList();
          this.$message.success(res.msg);
          this.resetEditDialog();
        })
        .catch(err => {
          console.log(err);
          this.$message.error(err.msg || '处理失败！');
        });
    },
    resetImgDialog() {
      this.imgUrl = '';
      this.dialogImgVisible = false;
    },
    showDetail(imgUrl) {
      this.imgUrl = imgUrl;
      this.dialogImgVisible = true;
    },
  },
};
</script>

<style lang="scss">
.previewRow {
  display: flex;
  .previewItem {
    width: 170px;
    height: 96px;
    margin-right: 20px;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .previewItem:last-child {
    margin-right: 0;
  }
}
</style>
