<template>
  <div class="div_main">
    <div class="div_search">
      <el-form :inline="true"
               :model="formData"
               ref="ref_searchform"
               :label-width="'100px'">
        <div>

          <el-form-item label="场所名称"
                        prop="placeOrgName">
            <el-input v-model="formData.placeOrgName"
                      size="small"
                      class="item_width_1"
                      clearable
                      placeholder="请输入九小场所名称">
            </el-input>

          </el-form-item>

          <el-form-item label="首次检查结果"
                        prop="firstResult">
            <el-select class="item_width_1"
                       v-model="formData.firstResult"
                       size="small"
                       placeholder="请选择"
                       clearable>
              <el-option v-for="item in resultList"
                         :key="item.value"
                         :label="item.label"
                         :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="场所区域"
                        prop="gridId">
            <el-select class="item_width_1"
                       v-model="formData.gridId"
                       placeholder="全部"
                       clearable>
              <el-option v-for="item in gridList"
                         :key="item.value"
                         :label="item.label"
                         :value="item.value">
              </el-option>
            </el-select>

          </el-form-item>

        </div>
        <div>

          <el-form-item label="场所类型"
                        prop="ninePlaceVal">
            <el-select class="item_width_1"
                       v-model="formData.ninePlaceVal"
                       size="small"
                       placeholder="请选择"
                       clearable>
              <el-option v-for="item in placeTypeList"
                         :key="item.value"
                         :label="item.label"
                         :value="item.value">
              </el-option>
            </el-select>

          </el-form-item>

          <el-form-item label="最新结果"
                        prop="finalResult">
            <el-select class="item_width_1"
                       v-model="formData.finalResult"
                       size="small"
                       placeholder="请选择"
                       clearable>
              <el-option v-for="item in resultList"
                         :key="item.value"
                         :label="item.label"
                         :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>

          <el-button style="margin-left:30px"
                     size="small"
                     type="primary"
                     @click="handleSearch">查询</el-button>
          <el-button style="margin-left:10px"
                     size="small"
                     type="yellow"
                     @click="resetSearch">重置</el-button>
        </div>
      </el-form>
    </div>
    <div class="div_table">
      <el-button type="green"
                 size="small"
                 @click="handleAdd">新增</el-button>

      <el-table class="table"
                :data="tableData"
                border
                :height="tableHeight"
                v-loading="tableLoading"
                :header-cell-style="{background:'#2195FE',color:'#FFFFFF'}"
                style="width: 100%">
        <el-table-column label="序号"
                         header-align="center"
                         align="center"
                         type="index"
                         width="50"></el-table-column>

        <el-table-column prop="placeOrgName"
                         header-align="center"
                         align="center"
                         label="场所名称"
                         min-width="180">
        </el-table-column>
        <el-table-column prop="ninePlaceName"
                         header-align="center"
                         align="center"
                         label="场所类型"
                         width="180">
        </el-table-column>
        <el-table-column prop="placePatrolTeamName"
                         header-align="center"
                         align="center"
                         label="分队名称"
                         width="180">
        </el-table-column>
        <el-table-column prop="inspectorsNames"
                         header-align="center"
                         align="center"
                         label="检查人员"
                         min-width="180">
        </el-table-column>
        <el-table-column prop="firstTime"
                         header-align="center"
                         align="center"
                         label="首次巡检时间"
                         width="110">
        </el-table-column>
        <el-table-column prop="detailed"
                         header-align="center"
                         align="center"
                         label="隐患明细"
                         min-width="280">
        </el-table-column>
        <el-table-column prop="firstResult"
                         header-align="center"
                         align="center"
                         label="首次检查结果"
                         width="110">
        </el-table-column>
        <el-table-column prop="reviewTime"
                         header-align="center"
                         align="center"
                         label="拟复查时间"
                         min-width="110">
        </el-table-column>
        <el-table-column prop="finalResult"
                         header-align="center"
                         align="center"
                         label="最新结果"
                         width="80">
        </el-table-column>
        <el-table-column prop="finalTime"
                         header-align="center"
                         align="center"
                         label="最新检查时间"
                         min-width="110">
        </el-table-column>

        <el-table-column label="操作"
                         fixed="right"
                         width="180"
                         header-align="center"
                         align="center"
                         class="operate">
          <template slot-scope="scope">
            <el-button type="text"
                       style="color:#1C6AFD;text-decoration: underline;"
                       size="small"
                       @click="handleDetail(scope.row)">查看</el-button>

            <el-button type="text"
                       style="color:#D51010;text-decoration: underline;"
                       size="small"
                       @click="handleDelete(scope.row)">删除</el-button>
            <el-button type="text"
                       style="color:#00A7A9;text-decoration: underline;"
                       size="small"
                       @click="handleReview(scope.row)">复查记录</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div>
        <el-pagination @size-change="handleSizeChange"
                       @current-change="handleCurrentChange"
                       :current-page.sync="pageNo"
                       :page-sizes="[20, 50, 100, 200]"
                       :page-size="pageSize"
                       layout="sizes, prev, pager, next, total"
                       :total="total">
        </el-pagination>
      </div>
    </div>

    <!-- 修改弹出框 -->
    <el-dialog :visible.sync="formShow"
               :close-on-click-modal="false"
               :close-on-press-escape="false"
               :title="formTitle"
               width="850px"
               top="5vh"
               @closed="diaClose">
      <inspect-form ref="ref_form"
                    :gridList="gridList"
                    :resultList="resultList"
                    :placeTypeList="placeTypeList"
                    @dialogCancle="addFormCancle"
                    @dialogOk="addFormOk"></inspect-form>
    </el-dialog>

    <!-- 复查记录弹出框 -->
    <el-dialog :visible.sync="reviewShow"
               :close-on-click-modal="false"
               :close-on-press-escape="false"
               :title="'复查记录'"
               width="80%"
               top="5vh"
               @closed="reviewDiaClose">
      <inspect-review ref="ref_review"
                      :resultList="resultList"
                      @reviewDiaClose="reviewDiaClose"></inspect-review>
    </el-dialog>

  </div>
</template>

<script>

import inspectForm from './inspectForm'
import inspectReview from './inspectReview'

import { requestPost } from "@/js/dai/request";
import { mapGetters } from 'vuex'
import { Loading } from 'element-ui' // 引入Loading服务

let loading // 加载动画
export default {
  data () {
    return {
      loading: false,
      total: 0,
      pageSize: 10,
      pageNo: 0,
      tableLoading: false,

      agencyId: '',
      gridList: [],//所属网格list--场所区域
      resultList: [
        {
          value: '0',
          label: '合格'
        },
        {
          value: '1',
          label: '不合格'
        }
      ],//是否合格list
      placeTypeList: [],//场所类型list

      formData: {
        placeOrgName: '',//场所名称	
        firstResult: '',//首次检查结果【0:合格 1:不合格】	
        gridId: '',//场所区域【网格Id】	
        ninePlaceVal: '',//场所类型【九小场所Value值】	
        finalResult: '',//最终检查结果【0:合格 1:不合格】	
      },

      tableData: [],

      //form相关
      formShow: false,
      formTitle: '新增巡查',

      reviewShow: false

    }
  },
  components: {
    inspectForm, inspectReview
  },
  async created () {

  },
  async mounted () {
    //获取场所类型
    await this.loadPlaceType()

    const { user } = this.$store.state
    this.agencyId = user.agencyId
    //获取网格下拉框数据
    await this.loadGrid()

    await this.loadTable()
  },

  methods: {
    handleSearch () {
      this.loadTable()
    },

    async loadGrid () {
      const url = "/gov/org/customergrid/gridoption"

      let params = {
        agencyId: this.agencyId
      }

      const { data, code, msg } = await requestPost(url, params)

      if (code === 0) {
        this.gridList = data

      } else {
        this.$message.error(msg)
      }

    },

    //获取场所类型
    async loadPlaceType () {
      const url = "/sys/dict/data/dictlist"

      let params = {
        dictType: 'nine_small_places'
      }

      const { data, code, msg } = await requestPost(url, params)

      if (code === 0) {
        this.placeTypeList = data

      } else {
        this.$message.error(msg)
      }

    },

    async loadTable () {
      this.tableLoading = true

      const url = "/gov/org/placepatrolrecord/getlist"
      // const url = "http://yapi.elinkservice.cn/mock/245/gov/org/placepatrolrecord/getlist"
      let params = {
        pageSize: this.pageSize,
        pageNo: this.pageNo,
        ...this.formData
      }

      const { data, code, msg } = await requestPost(url, params)

      if (code === 0) {
        this.total = data.total
        this.tableData = data.list
        if (this.tableData && this.tableData.length > 0) {
          this.tableData.forEach(item => {
            if (item.firstTime) {
              let firstTimeArray = item.firstTime.split(' ')
              item.firstTime = firstTimeArray[0]
            }

            if (item.reviewTime) {
              let reviewTimeArray = item.reviewTime.split(' ')
              item.reviewTime = reviewTimeArray[0]
            }

            if (item.finalTime) {
              let finalTimeArray = item.finalTime.split(' ')
              item.finalTime = finalTimeArray[0]
            }

            if (item.firstResult) {
              item.firstResult = item.firstResult === '0' ? '合格' : '不合格'
            }
            if (item.finalResult) {
              item.finalResult = item.finalResult === '0' ? '合格' : '不合格'
            }
          });

        }

      } else {
        this.$message.error(msg)
      }
      this.tableLoading = false
    },



    diaClose () {
      this.$refs.ref_form.resetData()
      this.formShow = false
    },

    reviewDiaClose () {
      this.reviewShow = false
    },

    handleDetail (row) {
      this.formTitle = '详情'
      this.formShow = true
      this.$nextTick(() => {
        this.$refs.ref_form.initForm('detail', row.placePatrolRecordId)
      })
    },

    handleAdd () {
      this.formTitle = '新增'
      this.formShow = true
      this.$nextTick(() => {
        this.$refs.ref_form.initForm('add', null)
      })
    },

    handleReview (row) {
      this.reviewShow = true
      this.$nextTick(() => {
        this.$refs.ref_review.initTable(row.placePatrolRecordId)
      })
    },

    addFormCancle () {
      this.formShow = false
    },

    addFormOk () {
      this.formShow = false
      this.loadTable()
    },

    async handleDelete (row) {

      this.$confirm("确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.deleteRecord(row)
        })
        .catch(err => {
          if (err == "cancel") {

          }

        });
    },

    async deleteRecord (row) {
      const url = "/gov/org/placepatrolrecord/del"
      // const url = "http://yapi.elinkservice.cn/mock/245/gov/org/placepatrolrecord/del"

      let params = {
        placePatrolRecordId: row.placePatrolRecordId
      }

      const { data, code, msg } = await requestPost(url, params)

      if (code === 0) {
        this.$message({
          type: "success",
          message: "删除成功"
        });

        this.loadTable()
      } else {
        this.$message.error(msg)
      }
    },

    //重置搜索条件
    resetSearch () {
      this.formData = {
        placeOrgName: '',//场所名称	
        mobile: '',//联系电话	
        scale: '',//场所规模【 0:10人以下 1:10-20人 2:21-40人 3:41-100人 4:100人以上】	
        gridId: '',//场所区域【网格Id】	
        ninePlaceVal: '',//场所类型【九小场所Value值】	
        isPage: true,//是否分页(是:true 否:false) 有这个参数是给新增巡查记录时用的，默认是	
      }
      this.pageSize = 10
      this.pageNo = 0
      this.loadTable()
    },


    handleSizeChange (val) {
      this.pageSize = val
      this.pageNo = 1
      this.loadTable()
    },
    handleCurrentChange (val) {
      this.pageNo = val
      this.loadTable()
    },

    // 开启加载动画
    startLoading () {
      loading = Loading.service({
        lock: true, // 是否锁定
        text: '正在加载……', // 加载中需要显示的文字
        background: 'rgba(0,0,0,.7)' // 背景颜色
      })
    },
    // 结束加载动画
    endLoading () {
      // clearTimeout(timer);
      if (loading) {
        loading.close()
      }
    }
  },
  computed: {
    tableHeight () {

      return (this.clientHeight - 460)

    },
    rowHeight () {
      return (this.clientHeight - 200) + 'px'
    },
    ...mapGetters(['clientHeight'])
  },
  watch: {

  },
  props: {

  }
}
</script>
<style lang="scss" scoped >
@import "@/assets/scss/buttonstyle.scss";

.div_main {
  width: 100%;
}

.div_search {
  background: #ffffff;
  border-radius: 4px;
  padding: 30px 20px 5px;
  box-shadow: 0px 2px 12px 0px rgba(0, 0, 0, 0.1);
}

.item_width_1 {
  width: 260px;
}

.div_table {
  background: #ffffff;
  box-shadow: 0px 2px 12px 0px rgba(0, 0, 0, 0.1);
  border-radius: 4px;
  margin-top: 15px;
  padding: 23px 30px 10px;

  .table {
    margin-top: 20px;
  }
}

.div_btn {
}

.el-row {
  /* margin-bottom: 20px; */
  display: flex;
  flex-wrap: wrap;
  margin-top: 10px;
  margin-right: 50px;
}
</style>
