<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="行政区" prop="district" >
        <el-select v-model="queryParams.district" placeholder="请选择行政区">
          <el-option
            v-for="dict in dict.type.district"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item  label="所属小区" prop="areaId">
        <el-select v-model="queryParams.areaId" filterable clearable placeholder="请选择所属小区"  @change="getBuilding(queryParams.areaId),resetqueryform()">
          <el-option v-for="item in areaList" :key="item.id" :value="item.id" :label="item.name">{{ item.name }}</el-option>
        </el-select>
      </el-form-item>
      <el-form-item  label="所属楼宇" prop="buildingId">
        <el-select v-model="queryParams.buildingId" filterable clearable placeholder="请选择楼宇" @change="getUnit(queryParams.buildingId),resetqueryunitform()">
          <el-option v-for="item in buildingList" :key="item.id" :value="item.id" :label="item.name">{{ item.name }}</el-option>
        </el-select>
      </el-form-item>
      <el-form-item  label="所属单元" prop="unitId">
        <el-select v-model="queryParams.unitId" filterable clearable placeholder="请选择单元" @change="getHouse(queryParams.unitId),resetqueryhouseform()">
          <el-option v-for="item in unitList" :key="item.id" :value="item.id" :label="item.name">{{ item.name }}</el-option>
        </el-select>
      </el-form-item>
      <el-form-item  label="房屋" prop="houseId">
        <el-select v-model="queryParams.houseId" filterable clearable placeholder="请选择房屋">
          <el-option v-for="item in houseList" :key="item.id" :value="item.id" :label="item.name">{{ item.name }}</el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="职工" prop="userId">
        <el-select v-model="queryParams.userId" filterable placeholder="请选择职工">
          <el-option v-for="item in workerList" :key="item.id" :value="item.id" :label="item.name">{{
              item.name
            }}
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="开始时间" prop="starTime">
        <el-date-picker clearable size="small"
                        v-model="queryParams.starTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker clearable size="small"
                        v-model="queryParams.endTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="工号" prop="jobNum">
        <el-input
          v-model="queryParams.jobNum"
          :maxlength="4"
          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="['gas:order:add']"-->
        <!--&gt;新增-->
        <!--</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="['gas:order:remove']"-->
        <!--&gt;删除</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="['gas:anjian:getListAnJianExport']"
        >导出</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          :loading="exportLoading"-->
<!--          @click="backup"-->
<!--          v-hasPermi="['gas:order:export']"-->
<!--        >备份</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="职工" align="center" prop="userName"/>
      <el-table-column label="房屋" align="center" prop="house"/>
      <el-table-column label="创建时间" align="center" prop="time" />
      <el-table-column label="修改时间" align="center" prop="updateTime" />
      <el-table-column label="照片" align="center" prop="photoList">
        <template slot-scope="scope">
          <el-image
            v-if="scope.row.photoList.length> 0"
            style="width: 100px; height: 100px"
            :src="scope.row.photoList[0]"
            :preview-src-list="scope.row.photoList">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="是否合格" align="center" prop="whether"/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <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-view"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['gas:order:edit']"
          >详情
          </el-button>
          <!--<el-button-->
            <!--size="mini"-->
            <!--type="text"-->
            <!--icon="el-icon-edit"-->
            <!--@click="handleUpdate(scope.row)"-->
            <!--v-hasPermi="['gas:order:edit']"-->
          <!--&gt;修改-->
          <!--</el-button>-->
          <!--<el-button-->
            <!--size="mini"-->
            <!--type="text"-->
            <!--icon="el-icon-delete"-->
            <!--@click="handleDelete(scope.row)"-->
            <!--v-hasPermi="['gas:order:remove']"-->
          <!--&gt;删除</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="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="照片" prop="photoList">
          <!--<el-form-item label="照片">-->
            <el-image
                v-if="form.photoList && form.photoList.length> 0 && (form.photoList[0].substring(form.photoList[0].lastIndexOf('.')+1)!='mp4')"
                style="width: 100px; height: 100px"
                :src="form.photoList[0]"
                :preview-src-list="form.photoList"
            >
            </el-image>
            <video
                v-if="form.photoList && form.photoList.length> 0 && (form.photoList[0].substring(form.photoList[0].lastIndexOf('.')+1)=='mp4')"
                style="width: 100px; height: 100px"
                :src="form.photoList[0]"
            >
            </video>
            <!--<imageUpload v-model="form.photo" :disabled="true"/>-->
          </el-form-item>
          <!--<imageUpload v-model="form.photoList"/>-->
        <!--</el-form-item>-->
        <el-form-item label="职工" prop="userId">
          <el-select v-model="form.userId" filterable placeholder="请选择职工" disabled>
            <el-option v-for="item in workerList" :key="item.id" :value="item.id" :label="item.name">{{
                item.name
              }}
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属小区" prop="areaId">
          <el-select v-model="form.areaId" filterable placeholder="请选择小区"
                     @change="getBuilding(form.areaId),resetform()" disabled>
            <el-option v-for="item in areaList" :key="item.id" :value="item.id" :label="item.name">{{
                item.name
              }}
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属楼宇" prop="buildingId">
          <el-select v-model="form.buildingId" filterable placeholder="请选择楼宇"
                     @change="getUnit(form.buildingId),resetunitform()" disabled>
            <el-option v-for="item in buildingList" :key="item.id" :value="item.id" :label="item.name">{{
                item.name
              }}
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属单元" prop="unitId">
          <el-select v-model="form.unitId" filterable placeholder="请选择单元"
                     @change="getHouse(form.unitId),resethouseform()" disabled>
            <el-option v-for="item in unitList" :key="item.id" :value="item.id" :label="item.name">{{
                item.name
              }}
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="房屋" prop="houseId">
          <el-select v-model="form.houseId" filterable placeholder="请选择房屋" disabled>
            <el-option v-for="item in houseList" :key="item.id" :value="item.id" :label="item.name">{{item.name}}
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否合格" prop="whether">
          <el-select v-model="form.whether" filterable placeholder="请选择" disabled>
            <el-option v-for="item in whetherList" :value="item.value" :label="item.label">{{item.label}}</el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" disabled/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <!--<el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>-->
        <!--<el-button @click="cancel">取 消</el-button>-->
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listOrder,
  getOrderForStatus,
  getListAnJian,
  getHouse,
  backup,
  getUnit,
  getArea,
  getBuilding,
  getOrder,
  getWorker,
  getPipeType,
  getValveType,
  getPipeLength,
  delOrder,
  addOrder,
  submitY,
  submitN,
  updateOrder
} from "@/api/gas/order";

export default {
  name: "Order",
  dicts: ['district'],
  data() {
    return {
      whetherList:[
        {
          value: '是',
          label: '是',
        },
        {
          value: '否',
          label: '否',
        }
      ],
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      //职工下拉
      workerList: [],
      pipeLengthList: [],
      pipeTypeList: [],
      valveTypeList: [],
      areaList: [],
      buildingList: [],
      unitList: [],
      houseList: [],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工程管理表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open1: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        serviceType: undefined,
        starTime: undefined,
        endTime: undefined,
        areaId: undefined,
        buildingId: undefined,
        unitId: undefined,
        houseId: undefined,
        userId: undefined,
        orderStatus: undefined,
        examineStatus: undefined,
        jobNum: undefined,
      },
      // 表单参数
      form: {
        serviceType: '004',
        orderStatus:'002',
        examineStatus:'001'
      },
      // 表单校验
      rules: {
        photoList: [
          {required: true, message: "请上传照片", trigger: "change"}
        ],
        userId: [
          {required: true, message: "职工不能为空", trigger: "blur"}
        ],
        houseId: [
          {required: true, message: "房屋不能为空", trigger: "change"}

        ],
      }
    };
  },
  created() {
    this.getList();
    this.getWorker();
    this.getArea();
  },
  methods: {
    backup() {
      backup().then(response => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.$download.excel('/gas/order/getListAnJianExport', this.queryParams);
    },
    /** 查询工程管理列表 */
    getList() {
      this.loading = true;
      getListAnJian(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /**职工下拉框*/
    getWorker() {
      getWorker().then(response => {
        this.workerList = response.data;
      });
    },

    /**小区下拉框*/
    getArea() {
      getArea().then(response => {
        this.areaList = response.data;
      });
    },
    /**楼宇下拉框*/
    getBuilding(areaId) {
      this.form.areaName =  this.areaList.filter(item => item.id == areaId )[0].name
      getBuilding(areaId).then(response => {
        this.buildingList = response.data;
        this.getUnit(this.form.buildingId);
      });
    },
    getUnit(buildingId) {
      this.form.buildingName =  this.buildingList.filter(item => item.id == buildingId )[0].name
      getUnit(buildingId).then(response => {
        this.unitList = response.data;
        this.getHouse(this.form.unitId);
      });
    },
    getHouse(unitId) {
      this.form.unitName =  this.unitList.filter(item => item.id == unitId )[0].name
      getHouse(unitId).then(response => {
        this.houseList = response.data;
      });
    },
    /**
     * 联级清空
     */
    resetform() {
      this.form.buildingId = undefined;
      this.form.unitId = undefined;
      this.form.houseId = undefined;
    },
    resetunitform() {
      this.form.unitId = undefined;
      this.form.houseId = undefined;
    },
    resethouseform() {
      this.form.houseId = undefined;
    },
    resetqueryform(){
      this.queryParams.buildingId = undefined;
      this.queryParams.unitId = undefined;
      this.queryParams.houseId = undefined;
    },
    resetqueryunitform(){
      this.queryParams.unitId = undefined;
      this.queryParams.houseId = undefined;
    },
    resetqueryhouseform(){
      this.queryParams.houseId = undefined;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        serviceType: '004',
        photoList: [],
        userId: undefined,
        unitId: undefined,
        buildingId: undefined,
        areaId: undefined,
        houseId: undefined,
        remark: undefined,
        delFlag: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        orderStatus:'002',
        examineStatus:'001',
        whether:undefined
      };
      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 = "添加";
      this.getWorker();
      this.getArea();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getWorker();
      this.getArea();
      const id = row.id || this.ids
      getOrder(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        // this.title = "修改";
        this.title = "详情";
        this.getBuilding(this.form.areaId);
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            if (typeof this.form.photoList==='string'){
              this.form.photoList = this.form.photoList.split(',')
            }
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
              this.getWorker();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            this.form.photoList = this.form.photoList.split(',')
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              this.getWorker();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除工程管理编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delOrder(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.getWorker();
        this.$modal.msgSuccess("删除成功");
      }).finally(() => {
        this.loading = false;
      });
    },

  }
};
</script>
