<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="personInCharge">
            <el-input v-model="formData.personInCharge"
                      size="small"
                      class="item_width_1"
                      clearable
                      placeholder="请输入负责人姓名">
            </el-input>

          </el-form-item>

          <el-form-item label="联系电话"
                        prop="mobile">
            <el-input v-model="formData.mobile"
                      size="small"
                      class="item_width_1"
                      clearable
                      placeholder="请输入电话">
            </el-input>

          </el-form-item>
        </div>
        <div>
          <el-form-item label="负责区域"
                        prop="gridId">
            <el-select class="item_width_1"
                       v-model="formData.gridId"
                       size="small"
                       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>

          <el-form-item label="场所类型"
                        prop="ninePlaceVal">
            <el-select class="item_width_1"
                       size="small"
                       v-model="formData.ninePlaceVal"
                       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-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="teamName"
                         header-align="center"
                         align="center"
                         label="分队名称"
                         min-width="180">
        </el-table-column>
        <el-table-column prop="personInCharge"
                         header-align="center"
                         align="center"
                         label="负责人"
                         width="180">
        </el-table-column>
        <el-table-column prop="mobile"
                         header-align="center"
                         align="center"
                         label="联系电话"
                         width="110">
        </el-table-column>
        <el-table-column prop="gridNames"
                         header-align="center"
                         align="center"
                         label="负责区域"
                         min-width="280">
        </el-table-column>
        <el-table-column prop="ninePlaceNames"
                         header-align="center"
                         align="center"
                         label="负责场所类型"
                         min-width="180">
        </el-table-column>
        <el-table-column prop="plan"
                         header-align="center"
                         align="center"
                         label="监察计划"
                         min-width="180">
        </el-table-column>
        <el-table-column prop="time"
                         header-align="center"
                         align="center"
                         label="创建时间"
                         min-width="120">
        </el-table-column>

        <el-table-column label="操作"
                         fixed="right"
                         width="140"
                         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:#00A7A9;text-decoration: underline;"
                       size="small"
                       @click="handleEdit(scope.row)">修改</el-button>

            <el-button type="text"
                       style="color:#D51010;text-decoration: underline;"
                       size="small"
                       @click="handleDelete(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">
      <team-form ref="ref_form"
                 :gridList="gridList"
                 :placeTypeList="placeTypeList"
                 @dialogCancle="addFormCancle"
                 @dialogOk="addFormOk"></team-form>
    </el-dialog>

  </div>
</template>

<script>

import teamForm from './teamForm'

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--场所区域
      scaleList: [],//规模list
      placeTypeList: [],//场所类型list

      formData: {
        personInCharge: '',//负责人		
        mobile: '',//联系电话	
        gridId: '',//负责区域【网格Id】	
        ninePlaceVal: '',//场所类型【九小场所Value值】	
        isPage: true,//是否分页(是:true 否:false) 有这个参数是给新增巡查记录时用的，默认是	
      },

      tableData: [],

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

    }
  },
  components: {
    teamForm
  },
  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/placepatrolteam/getlist"
      // const url = "http://yapi.elinkservice.cn/mock/245/gov/org/placeorg/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

        this.tableData.forEach(item => {
          let timeArray = item.time.split(' ')
          item.time = timeArray[0]
        });
      } else {
        this.$message.error(msg)
      }
      this.tableLoading = false
    },



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

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

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

    handleEdit (row) {
      this.formTitle = '修改'
      this.formShow = true
      this.$nextTick(() => {
        this.$refs.ref_form.initForm('edit', row.teamId, this.agencyId)
      })
    },

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

    },

    async handleDelete (row) {

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

          }

        });
    },

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

      let params = {
        teamId: row.teamId
      }

      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 = {
        personInCharge: '',//负责人		
        mobile: '',//联系电话	
        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>
