<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="督导类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择督导类型" clearable size="small">
          <el-option
            v-for="dict in typeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="督导标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入督导标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </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="handleAdd"
          v-hasPermi="['system:supervision:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:supervision:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:supervision:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['system:supervision:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="supervisionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="督导类型" align="center" prop="type" :formatter="typeFormat" />
      <el-table-column label="督导标题" align="center" prop="title" />
      <el-table-column label="督导内容" align="center" prop="content" />
      <el-table-column label="反馈状态" align="center" prop="status">
        <template slot-scope="scope">
          <span v-if="scope.row.status==1" style="color: #ff4949" >未反馈</span>
          <span v-if="scope.row.status==2" @click="openfeed(scope.row)" style="color: rgb(48, 180, 107);cursor: pointer">已反馈</span>
          <span v-if="scope.row.status==0">不需反馈</span>
        </template>
      </el-table-column>
      <el-table-column label="图片" align="center" prop="imageUrl" >
        <template slot-scope="scope">
          <el-image v-if="scope.row.imageUrl" :src="scope.row.imageUrl" :preview-src-list="[scope.row.imageUrl]" :z-index="9999" style="width: 30px;height: 30px;"></el-image>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="文件" align="center" prop="fileUrl" >
        <template slot-scope="scope">
          <span
            class="dbtn"
            @click="checkFile(scope.row.fileUrl)"
            v-if="scope.row.fileUrl != null && scope.row.fileUrl!=''"
          >
            <i class="el-icon el-icon-view"></i>查看/下载
          </span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status==2"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="openfeed(scope.row)"
          >反馈信息</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:supervision:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:supervision:remove']"
          >删除</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-dialog :title="title" :visible.sync="open2" width="1200px" append-to-body>
      <Feedback :supervisionId="supervisionId" />
    </el-dialog>
    <!-- 添加或修改工作督导对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="督导类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择督导类型">
            <el-option
              v-for="dict in typeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="督导标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入督导标题" />
        </el-form-item>
        <el-form-item label="督导内容" prop="content">
          <el-input v-model="form.content" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="是否反馈">
          <el-radio-group v-model="form.status">
            <el-radio
              label="0"
            >否</el-radio>
            <el-radio
              label="1"
            >是</el-radio>
            <!--<el-radio-->
              <!--v-for="dict in statusOptions"-->
              <!--:key="dict.dictValue"-->
              <!--:label="parseInt(dict.dictValue)"-->
            <!--&gt;{{dict.dictLabel}}</el-radio>-->
          </el-radio-group>
        </el-form-item>
        <el-form-item label="图片" prop="imageUrl">
          <imageUpload v-model="form.imageUrl"/>
          <!--<el-input v-model="form.imageUrl" type="textarea" placeholder="请输入内容" />-->
        </el-form-item>
        <el-form-item label="文件" prop="fileUrl">
          <FileUpload
            listType="picture"
            @resFun="getFileInfo"
            @remove="listRemove"
            :fileArr="fileList"
          />
          <el-input v-show="false" disabled v-model="form.fileUrl"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSupervision, getSupervision, delSupervision, addSupervision, updateSupervision, exportSupervision } from "@/api/supervision/supervision";
import FileUpload from '@/components/FileUpload';
import ImageUpload from '@/components/ImageUpload';
import Feedback from "./feedback/index"
let uploadfile = require("@/assets/uploadfile.png");
export default {
  name: "Supervision",
  components: {
    FileUpload,ImageUpload,Feedback
  },
  data() {
    return {
      supervisionId:null,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工作督导表格数据
      supervisionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open2:false,
      // 督导类型字典
      typeOptions: [],
      // 状态字典
      statusOptions: [],
      fileList:[],
      fileList2:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: null,
        title: null,
        status: null,
      },
      // 表单参数
      form: {
        status:0
      },
      // 表单校验
      rules: {
        type: [
          { required: true, message: "督导类型不能为空", trigger: "change" }
        ],
        title: [
          { required: true, message: "督导标题不能为空", trigger: "blur" }
        ],
        content: [
          { required: true, message: "督导内容不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("t_supervision_type").then(response => {
      this.typeOptions = response.data;
    });
    this.getDicts("t_supervision_status").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    /** 查询工作督导列表 */
    getList() {
      this.loading = true;
      listSupervision(this.queryParams).then(response => {
        this.supervisionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 督导类型字典翻译
    typeFormat(row, column) {
      return this.selectDictLabel(this.typeOptions, row.type);
    },
    // 状态字典翻译
    status1Format(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    //上传
    getFileInfo(res){
      //this.form.dealPlan = res.fileName;
      this.form.fileUrl = res.url;
      this.fileList.push({
        name: res.fileName,
        url: uploadfile,
      });
    },
    listRemove(e) {
      this.form.fileUrl = "";
      this.fileList = [];
    },


    checkFile(url) {
      window.open(url,'_blank');
    },

    //点击下载
    downloadFile() {
      var item = this.fileurl;
      var title = item.split("/")[item.split("/").length-1];
      this.download(item,title);
    },
    download (src, fileName) {
      let x = new XMLHttpRequest();
      x.open("GET", src, true);
      x.responseType = 'blob';
      x.onload = function(e) {
        let url = window.URL.createObjectURL(x.response)
        let a = document.createElement('a');
        a.href = url
        a.download = fileName
        a.click()
      }
      x.send();
    },
    // 取消按钮
    cancel() {
      this.fileList = [];
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        supervisionId: null,
        type: null,
        title: null,
        content: null,
        status: "0",
        imageUrl: null,
        fileUrl: null,
        createTime: null,
        createBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.supervisionId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    openfeed(row){
      this.title = "反馈";
      this.supervisionId = row.supervisionId;
      this.open2 = true;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加工作督导";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const supervisionId = row.supervisionId || this.ids
      getSupervision(supervisionId).then(response => {
        this.form = response.data;
        this.fileList.push({
          name: "督导文件",
          url: uploadfile,
        });
        this.open = true;
        this.title = "修改工作督导";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.supervisionId != null) {
            updateSupervision(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSupervision(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const supervisionIds = row.supervisionId || this.ids;
      this.$confirm('是否确认删除工作督导编号为"' + supervisionIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delSupervision(supervisionIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有工作督导数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportSupervision(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
<style scoped lang="scss">
  ::v-deep .el-select{
    width: 100%;
  }
  .dbtn {
      display: inline-block;
      line-height: normal;
      padding-left: 2px;
      padding-right: 2px;
      cursor: pointer;
      border-radius: 3px;
      border-style: solid;
      border-width: 0;
      color: rgb(48, 180, 107);
  }
  </style>
