<template>
  <div class="app-container">
<!--    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">-->
<!--      <el-form-item label="服务类别" prop="serviceType">-->
<!--        <el-select v-model="queryParams.serviceType" placeholder="请选择服务类别" clearable size="small">-->
<!--          <el-option-->
<!--            v-for="dict in dict.type.service_type"-->
<!--            :key="dict.value"-->
<!--            :label="dict.label"-->
<!--            :value="dict.value"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
<!--      &lt;!&ndash;      <el-form-item  label="职工" prop="userId">&ndash;&gt;-->
<!--      &lt;!&ndash;        <el-select v-model="queryParams.userId" filterable placeholder="请选择职工">&ndash;&gt;-->
<!--      &lt;!&ndash;          <el-option v-for="item in workerList" :key="item.id" :value="item.id" :label="item.name">{{ item.name }}</el-option>&ndash;&gt;-->
<!--      &lt;!&ndash;        </el-select>&ndash;&gt;-->
<!--      &lt;!&ndash;      </el-form-item>&ndash;&gt;-->
<!--      <el-form-item label="订单状态" prop="orderStatus">-->
<!--        <el-select v-model="queryParams.orderStatus" placeholder="请选择订单状态" clearable size="small">-->
<!--          <el-option-->
<!--            v-for="dict in dict.type.order_status"-->
<!--            :key="dict.value"-->
<!--            :label="dict.label"-->
<!--            :value="dict.value"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
<!--      &lt;!&ndash;      <el-form-item label="审核状态" prop="examineStatus">&ndash;&gt;-->
<!--      &lt;!&ndash;        <el-select v-model="queryParams.examineStatus" placeholder="请选择审核状态" clearable size="small">&ndash;&gt;-->
<!--      &lt;!&ndash;          <el-option&ndash;&gt;-->
<!--      &lt;!&ndash;            v-for="dict in dict.type.examine_status"&ndash;&gt;-->
<!--      &lt;!&ndash;            :key="dict.value"&ndash;&gt;-->
<!--      &lt;!&ndash;            :label="dict.label"&ndash;&gt;-->
<!--      &lt;!&ndash;            :value="dict.value"&ndash;&gt;-->
<!--      &lt;!&ndash;          />&ndash;&gt;-->
<!--      &lt;!&ndash;        </el-select>&ndash;&gt;-->
<!--      &lt;!&ndash;      </el-form-item>&ndash;&gt;-->
<!--      <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">-->
<!--      &lt;!&ndash;      <el-col :span="1.5">&ndash;&gt;-->
<!--      &lt;!&ndash;        <el-button&ndash;&gt;-->
<!--      &lt;!&ndash;          type="success"&ndash;&gt;-->
<!--      &lt;!&ndash;          plain&ndash;&gt;-->
<!--      &lt;!&ndash;          icon="el-icon-edit"&ndash;&gt;-->
<!--      &lt;!&ndash;          size="mini"&ndash;&gt;-->
<!--      &lt;!&ndash;          :disabled="single"&ndash;&gt;-->
<!--      &lt;!&ndash;          @click="handleUpdate"&ndash;&gt;-->
<!--      &lt;!&ndash;          v-hasPermi="['gas:order:edit']"&ndash;&gt;-->
<!--      &lt;!&ndash;        >审核&ndash;&gt;-->
<!--      &lt;!&ndash;        </el-button>&ndash;&gt;-->
<!--      &lt;!&ndash;      </el-col>&ndash;&gt;-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="submitY"-->
<!--          v-hasPermi="['gas:order:edit']"-->
<!--        >合格-->
<!--        </el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="submitN"-->
<!--          v-hasPermi="['gas:order:edit']"-->
<!--        >不合格-->
<!--        </el-button>-->
<!--      </el-col>-->
<!--    </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="serviceType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.service_type" :value="scope.row.serviceType"/>
        </template>
      </el-table-column>
      <el-table-column label="职工" align="center" prop="userName"/>
      <el-table-column label="房屋" align="center" prop="house"/>
      <el-table-column label="管子类型" align="center" prop="pipeType"/>
      <el-table-column label="管子长度" align="center" prop="pipeLength"/>
      <el-table-column label="时间" align="center" prop="time"/>
      <!--      <el-table-column label="订单状态" align="center" prop="orderStatus">-->
      <!--        <template slot-scope="scope">-->
      <!--          <dict-tag :options="dict.type.order_status" :value="scope.row.orderStatus"/>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column label="审核状态" align="center" prop="examineStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.examine_status" :value="scope.row.examineStatus"/>
        </template>
      </el-table-column>
      <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="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-search"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['gas:order:edit']"
            >查看详情
            </el-button>
<!--          <p v-if="scope.row.orderStatus ==='001'">-->
<!--            <el-button-->
<!--              size="mini"-->
<!--              type="text"-->
<!--              icon="el-icon-edit"-->
<!--              @click="handleUpdate(scope.row)"-->
<!--              v-hasPermi="['gas:order:edit']"-->
<!--            >审核-->
<!--            </el-button>-->
<!--          </p>-->
<!--          <p v-if="scope.row.orderStatus ==='002'">-->
<!--            <el-button-->
<!--              size="mini"-->
<!--              type="text"-->
<!--              icon="el-icon-edit"-->
<!--              @click="handleUpdate(scope.row)"-->
<!--              v-hasPermi="['gas:order:edit']"-->
<!--            >复核-->
<!--            </el-button>-->
<!--          </p>-->
        </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="serviceType">
          <el-select disabled v-model="form.serviceType" placeholder="请选择服务类别">
            <el-option
              v-for="dict in dict.type.service_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="照片">
          <imageUpload v-model="form.photoList" :disabled="true"/>
        </el-form-item>
        <el-form-item label="职工" prop="userId">
          <el-select disabled v-model="form.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="areaId">
          <el-select disabled v-model="form.areaId" filterable placeholder="请选择小区"
                     @change="getBuilding(form.areaId),resetform()">
            <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 disabled v-model="form.buildingId" filterable placeholder="请选择楼宇"
                     @change="getUnit(form.buildingId),resetunitform()">
            <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 disabled v-model="form.unitId" filterable placeholder="请选择单元"
                     @change="getHouse(form.unitId),resethouseform()">
            <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 disabled v-model="form.houseId" filterable 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="pipeType">
          <el-radio-group disabled v-model="form.pipeType">
            <el-radio
              v-for="item in pipeTypeList"
              :key="item.pipeType"
              :label="item.pipeType"
            >{{ item.pipeType }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="管子长度" prop="pipeLength">
          <el-radio-group disabled v-model="form.pipeLength">
            <el-radio
              v-for="item in pipeLengthList"
              :key="item.pipeLength"
              :label="item.pipeLength"
            >{{ item.pipeLength }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input disabled v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
<!--        <el-button :loading="buttonLoading" type="primary" @click="submitForm('001')">合格</el-button>-->
<!--        <el-button :loading="buttonLoading" type="primary" @click="submitForm('002')">不合格</el-button>-->
        <el-button @click="cancel">取 消</el-button>

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

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

export default {
  name: "Order",
  dicts: ['service_type', 'order_status', 'examine_status'],
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      //职工下拉
      workerList: [],
      pipeLengthList: [],
      pipeTypeList: [],
      areaList: [],
      buildingList: [],
      unitList: [],
      houseList: [],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工程管理表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        serviceType: undefined,
        userId: undefined,
        orderStatus: undefined,
        examineStatus: undefined,
        areaId: undefined,
        foremanId: undefined,
        workerId: undefined,
        value: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        serviceType: [
          {required: true, message: "服务类别不能为空", trigger: "change"}
        ],
        userId: [
          {required: true, message: "职工id不能为空", trigger: "blur"}
        ],
        houseId: [
          {required: true, message: "房屋不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.queryParams.areaId = this.$route.query.areaId
    this.queryParams.foremanId = this.$route.query.foremanId
    this.queryParams.workerId = this.$route.query.workerId
    this.queryParams.value = this.$route.query.value
    this.getList();
    this.getWorker();
    this.getPipeLength();
    this.getPipeType();

  },
  methods: {
    /** 查询工程管理列表 */
    getList() {
      this.loading = true;
      getListByStatus(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /**工长下拉框*/
    getWorker() {
      getWorker().then(response => {
        this.workerList = response.data;
      });
    },
    //管子长度
    getPipeLength() {
      getPipeLength().then(response => {
        this.pipeLengthList = response.data;
      });
    },
    //管子长度
    getPipeType() {
      getPipeType().then(response => {
        this.pipeTypeList = response.data;
      });
    },
    /**小区下拉框*/
    getArea() {
      getArea().then(response => {
        this.areaList = response.data;
      });
    },
    /**楼宇下拉框*/
    getBuilding(areaId) {
      getBuilding(areaId).then(response => {
        this.buildingList = response.data;
      });
    },
    getUnit(buildingId) {
      getUnit(buildingId).then(response => {
        this.unitList = response.data;
      });
    },
    getHouse(unitId) {
      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;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        serviceType: undefined,
        picUrl: undefined,
        userId: undefined,
        unitId: undefined,
        buildingId: undefined,
        areaId: undefined,
        houseId: undefined,
        pipeLength: undefined,
        pipeType: undefined,
        remark: undefined,
        delFlag: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: 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.getPipeLength();
      this.getPipeType();
      this.getArea();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      this.getWorker();
      this.getPipeLength();
      this.getPipeType();
      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.getBuilding(this.form.areaId);
        this.getUnit(this.form.buildingId);
        this.getHouse(this.form.unitId);
      });
    },
    /** 提交按钮 */
    submitForm(status) {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          this.form.examineStatus = status;
          this.form.orderStatus = "002";
          updateOrder(this.form).then(response => {
            this.$modal.msgSuccess("审核成功");
          }).finally(() => {
            this.buttonLoading = false;
          });
        }
      });
      this.getList();
      this.handleUpdate(this.orderList[0]);
    },
    /** 删除按钮操作 */
    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;
      });
    },
    submitY(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm("确定审核？").then(() => {
        this.loading = true;
        return submitY(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.getWorker();
        this.$modal.msgSuccess("审核成功");
      }).finally(() => {
        this.loading = false;
      });
    },
    submitN(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm("确定审核？").then(() => {
        this.loading = true;
        return submitN(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.getWorker();
        this.$modal.msgSuccess("审核成功");
      }).finally(() => {
        this.loading = false;
      });
    },
  }
};
</script>
