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

    <el-button
      icon="el-icon-back"
      size="mini"
      style="margin-bottom: 15px"
      @click="$router.go(-1)"
    >
      返回
    </el-button>

    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="监护人员" prop="guarder">
        <el-input
          v-model="queryParams.guarder"
          placeholder="请输入监护人员"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="施工人员" prop="builder">
        <el-input
          v-model="queryParams.builder"
          placeholder="请输入施工人员"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </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:inspect: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:inspect: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:inspect: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:inspect:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="inspectList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="施工情况" align="center" prop="describe" :show-overflow-tooltip="true"/>
      <!--<el-table-column label="施工图片" align="center" prop="imgurl">
        <template slot-scope="scope">
          <el-image v-if="scope.row.imgurl" :src="scope.row.imgurl" :preview-src-list="[scope.row.imgurl]" :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="guarder" />
      <el-table-column label="施工人员" align="center" prop="builder" />
      <el-table-column label="监护时间" align="center" prop="createTime" />
      <el-table-column label="备注" align="center" prop="remark">
        <template slot-scope="scope">
          <span v-if="scope.row.remark">{{ scope.row.remark }}</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
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="handleDetail(scope.row)"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:inspect:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:inspect: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="open" width="800px" append-to-body destroy-on-close :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="施工情况" prop="describe">
          <el-input type="textarea" v-model="form.describe" placeholder="请输入施工情况" />
        </el-form-item>

        <el-row>
          <el-col :span="12">
            <el-form-item label="监护人员" prop="guarder">
              <el-input v-model="form.guarder" placeholder="请输入监护人员" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="施工人员" prop="builder">
              <el-input v-model="form.builder" placeholder="请输入施工人员" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="监护时间" prop="createTime">
              <el-date-picker clearable size="small"
                              v-model="form.createTime"
                              type="datetime"
                              value-format="yyyy-MM-dd HH:mm:ss"
                              style="width: 100%"
                              placeholder="选择监护时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>

        <el-row style="margin: 14px 0px 14px 23px;">
          <div class="picture-label">
            <span style="color: red">*</span>
            施工图片
          </div>
          <MultipleImageUpload
            v-model="form.imgurl"
            ref="imgurl"
          />
        </el-row>
      </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>


    <!-- 详情 -->
    <el-dialog :title="title" :visible.sync="detailOpen" width="800px" append-to-body destroy-on-close :close-on-click-modal="false">
      <el-form ref="form" :model="form" label-width="100px">
        <el-form-item label="施工情况">
          <span>{{ form.describe }}</span>
        </el-form-item>

        <el-row>
          <el-col :span="12">
            <el-form-item label="监护人员">
              <span>{{ form.guarder }}</span>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="施工人员">
              <span>{{ form.builder }}</span>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="监护时间" prop="createTime">
              <span>{{ form.createTime }}</span>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="备注">
          <span v-if="form.remark">{{ form.remark }}</span>
          <span v-else>-</span>
        </el-form-item>

        <el-row style="margin: 14px 0px 14px 23px;">
          <div class="picture-label">施工图片</div>
          <div v-if="form.imgurl">
            <el-image
              v-for="pic in form.imgurl.split(',')"
              :src="pic"
              :preview-src-list="[pic]"
              :z-index="9999"
              class="picture"
            >
            </el-image>
          </div>
          <div v-else>-</div>
        </el-row>

      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { listInspect, getInspect, delInspect, addInspect, updateInspect, exportInspect } from "@/api/thirdbuild/inspect";
import MultipleImageUpload from "@/components/MultipleImageUpload";
export default {
  name: "Inspect",
  components: {
    MultipleImageUpload
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 第三方施工执行表格数据
      inspectList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        thirdBuildId: null,
        describe: null,
        imgurl: null,
        guarder: null,
        builder: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        describe: [
          { required: true, message: "施工情况不能为空", trigger: "blur" }
        ],
        imgurl: [
          { required: true, message: "请选择施工图片", trigger: "blur" }
        ],
        guarder: [
          { required: true, message: "监护人员不能为空", trigger: "blur" }
        ],
        builder: [
          { required: true, message: "施工人员不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "请选择监护时间", trigger: "change" }
        ]
      },
      detailOpen: false
    };
  },
  created() {
    this.queryParams.thirdBuildId = this.$route.query.thirdBuildId;
    this.getList();
  },
  methods: {
    /** 查询第三方施工执行列表 */
    getList() {
      this.loading = true;
      listInspect(this.queryParams).then(response => {
        this.inspectList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        thirdBuildId: null,
        describe: null,
        imgurl: null,
        guarder: null,
        builder: null,
        remark: 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.reset();
      this.open = true;
      this.title = "添加第三方施工执行";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInspect(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改第三方施工执行";
        this.$nextTick(() => {
          if(this.form.imgurl){
            let temp = 1;
            this.$refs.imgurl.fileList = this.form.imgurl.split(",").map(item => { return{ url:item,uid: new Date().getTime() + temp++ }});
          }
        })
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if(!this.form.imgurl) return this.$message.error("施工图片不能为空");
          if (this.form.id != null) {
            updateInspect(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.thirdBuildId = this.$route.query.thirdBuildId;
            addInspect(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 delInspect(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有第三方施工执行数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportInspect(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },
    handleDetail(row){
      this.reset();
      getInspect(row.id).then(response => {
        this.form = response.data;
        this.detailOpen = true;
        this.title = "第三方施工执行详情";
      });
    }
  }
};
</script>
<style scoped lang="scss">
  .picture-label{
    font-size: 14px;
    color: rgb(96, 98, 102);
    font-weight: 700;
    margin: 8px 0px 8px 0px;
  }
  .picture{
    width: 130px;
    height: 130px;
    margin: 2px
  }
</style>
