<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <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="person">
        <el-input
          v-model="queryParams.person"
          placeholder="请输入检察人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属企业" prop="enterpriseId">
        <el-select v-model="queryParams.enterpriseId" placeholder="请选择企业" clearable size="small">
          <el-option
            v-for="item in enterprises"
            :key="item.enterpriseId"
            :label="item.enterpriseName"
            :value="item.enterpriseId"/>
        </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:supervise: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:supervise: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:supervise: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:supervise:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="superviseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="内容" align="center" prop="content" />
      <el-table-column label="检查结果" align="center" prop="resultContent" />
      <el-table-column label="检察人" align="center" prop="person" />
      <el-table-column label="检查地址" align="center" prop="address" />
      <!--<el-table-column label="所属企业" align="center" prop="enterpriseId" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            <!--size="mini"-->
            <!--type="text"-->
            <!--icon="el-icon-document"-->
            <!--@click="handledetail(scope.row)"-->
          <!--&gt;详情</el-button>-->

          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</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="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>

        <el-row>
            <el-col :span="12">
              <el-form-item label="所属企业" prop="enterpriseId">
                <el-select v-model="form.enterpriseId" placeholder="请选择所属单位名称" style="width: 100%" >
                  <el-option
                    v-for="item in enterprises"
                    :key="item.enterpriseId"
                    :label="item.enterpriseName"
                    :value="item.enterpriseId"/>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="12" >
              <el-form-item label="检察人" prop="person">
                <el-input v-model="form.person" placeholder="请输入检察人" />
              </el-form-item>
            </el-col>
        </el-row>

        <el-row>
          <el-col :span="25">
            <el-form-item label="经纬度坐标" prop="longitude">
              <el-col :span="9">
                <el-input v-model="form.longitude" placeholder="请输入经度" />
              </el-col>
              <el-col :span="9" style="margin-left: 13px">
                <el-input v-model="form.latitude" placeholder="请输入纬度"/>
              </el-col>
              <el-col :span="4" style="margin-left: 30px">
                <el-button type="primary" plain @click="MapdialogFun">选择经纬度</el-button>
              </el-col>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入检查地址" />
        </el-form-item>
        <el-form-item label="检查内容">
          <el-input v-model="form.content" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="检查结果">
          <el-input v-model="form.resultContent" type="textarea" placeholder="请输入内容" />
        </el-form-item>

        <el-row>
          <el-col :span="23">
            <el-form-item label="现场照片">
              <FileUpload
                listType="picture"
                @resFun="getFileInfo"
                @remove="listRemove"
                :fileArr="fileList"
              >
              </FileUpload>
            </el-form-item>
          </el-col>
        </el-row>

        <!--<el-form-item label="现场图片" prop="pictrueUrl">-->
          <!--<el-input v-model="form.pictrueUrl" type="textarea" placeholder="请输入内容" />-->
        <!--</el-form-item>-->
        <el-row>
          <el-col :span="24">
            <el-form-item label="视频上传"  prop="videoUrl">
              <VideoUpload
                listType="picture"
                v-if="open"
                @resFun="getFileInfoVideo"
                @remove="listRemoveVideo"
                :fileArr="fileListVideo"
                :fileSize="20"
                :fileType="['mp4']"
              />
              <el-input v-show="false" disabled v-model="form.vedioUrl"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <!--<el-form-item label="现场视频" prop="vedioUrl">-->
          <!--<el-input v-model="form.vedioUrl" placeholder="请输入现场视频" />-->
        <!--</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>


    <GetPos
      :dialogVisible.sync="dialogTableVisibles"
      device=""
      :devicePos="devicePos"
      @close="dialogcancelFun"
      @getPath="getPath"
    />
  </div>
</template>

<script>
  import GetPos from '@/components/GetPos';
import { listSupervise, getSupervise, delSupervise, addSupervise, updateSupervise, exportSupervise } from "@/api/system/supervise";
import Editor from '@/components/Editor';
  import FileUpload from '@/components/FileSuperviseUpload';
  import { enterpriseLists } from "@/api/regulation/info";
  import VideoUpload from "@/components/VideoUpload";

export default {
  name: "Supervise",
  components: {
    Editor,
    GetPos,
    FileUpload,
    VideoUpload
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 监督检查表格数据
      superviseList: [],
      // 弹出层标题
      title: "",
      /**--------------地图使用数据---------------*/
      dialogTableVisibles: false,
      devicePos: [],
      fileList:[],
      pictureUrlList:[],
      enterprises: [],

      fileListVideo:[],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        person: null,
        enterpriseId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: "请输入标题", trigger: "blur" },
        ],
        enterpriseId: [
          { required: true, message: "请选择企业", trigger: "blur" },
        ],
        person: [
          { required: true, message: "请输入监察人", trigger: "blur" },
        ],
        content: [
          { required: true, message: "请输入检查内容", trigger: "blur" },
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getEnterpriseLists();
  },
  methods: {
    //所属单位
    getEnterpriseLists(){
      const param = {};
      // this.judgeOperateType(param);
      enterpriseLists(param).then(response => {
        if (response.rows.length>1){
        this.enterprises = response.rows;
      }else {
        this.form.beyondEnterpriseId=response.rows[0]
        this.enterprises = response.rows;
      }

    });
    },
    //山传视频
    getFileInfoVideo(res){
      this.fileListVideo.push(res.url)
    },
    listRemoveVideo(index){
      this.fileListVideo.splice(index,1);
    },
    //上传图片
    getFileInfo(res){
      //this.form.dealPlan = res.fileName;
      this.pictureUrlList.push(res.url)
      this.form.pictrueUrl=this.pictureUrlList.join(",");
      this.form.burl=res.burl;
      this.fileList.push({
        name: res.fileName,
        url: uploadfile,
        burl:res.burl,
      });
    },
    listRemove(e) {
      this.form.pictrueUrl = "";
      this.fileList.delete(e.uid)
      // this.fileList = [];
    },
    //
    getPath(res){
      //确认选择经纬度
      this.form.longitude = res[0];
      this.form.latitude = res[1];
    },
    /**
     * 地图关闭方法
     */
    dialogcancelFun() {
      this.dialogTableVisibles = false;
    },
    /**
     * 经纬度坐标方法
     */
    MapdialogFun() {
      this.dialogTableVisibles = true;
    },
    /** 查询监督检查列表 */
    getList() {
      this.loading = true;
      listSupervise(this.queryParams).then(response => {
        this.superviseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        content: null,
        resultContent: null,
        person: null,
        address: null,
        longitude: null,
        latitude: null,
        enterpriseId: null,
        pictrueUrl: null,
        vedioUrl: null,
        createTime: 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.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.fileListVideo =[];
      this.form.pictrueUrl ="";
      this.fileList = [];
      this.pictureUrlList =[];
      this.form.vedioUrl= "";
      this.reset();
      this.open = true;
      this.title = "添加监督检查";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.fileListVideo =[];
      this.fileList = [];
      this.reset();
      const id = row.id || this.ids
      getSupervise(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改监督检查";
        if(this.form.vedioUrl){
          this.fileListVideo = this.form.vedioUrl.split(',');
        }

        if (this.form.pictrueUrl){
          for(var i = 0; i < this.form.pictrueUrl.split(',').length; i++){
            this.fileList.push({
              name: '照片',
              url: this.form.pictrueUrl.split(',')[i],
            });
          }
        }
        //this.form.vedioUrl = this.fileListVideo.join(",");
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.vedioUrl = this.fileListVideo.join(",");
          if (this.form.id != null) {
            updateSupervise(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSupervise(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除监督检查编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delSupervise(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有监督检查数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportSupervise(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
