<template>
  <div class="app-container">
    <div class="filter-container">
      <el-select v-model="getdataListParm.parammaps.pastureName" style="width: 140px;" placeholder="牧场" class="filter-item" @change="changePastureName">
        <el-option v-for="item in findAllPasture" :key="item.id" :label="item.name" :value="item.name" />
      </el-select>
      <el-select v-model="getdataListParm.parammaps.departName" style="width: 140px;" clearable placeholder="部门" class="filter-item">
        <el-option v-for="item in findAllDepart" :key="item.id" :label="item.name" :value="item.name" />
      </el-select>
      <el-input v-model="getdataListParm.parammaps.formNunber" placeholder="表编号" style="width: 140px;" class="filter-item" />
      <el-input v-model="getdataListParm.parammaps.formName" placeholder="表名称" style="width: 140px;" class="filter-item" />
      <el-select v-model="getdataListParm.parammaps.formType" clearable placeholder="表类型" class="filter-item">
        <el-option v-for="item in getDictByName" :key="item.id" :label="item.value" :value="item.value" />
      </el-select>
      <el-select v-model="getdataListParm.parammaps.useType" clearable placeholder="表结构" class="filter-item">
        <el-option v-for="item in useTypes" :key="item.id" :label="item.name" :value="item.name" />
      </el-select>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="form_search">搜索</el-button>
      <div>
        <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="form_add">新增</el-button>
        <el-upload style="display: inline-block;" :headers="headers" :data="uploadData" :action="uploadExcelUrl" :show-file-list="false" :before-upload="beforeImportExcel" :on-success="handleImportExcelSuccess">
          <el-button v-waves class="filter-item" type="warning" icon="el-icon-upload2">导入</el-button>
        </el-upload>
      </div>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      element-loading-text="给我一点时间"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      :row-style="rowStyle"
      :cell-style="cellStyle"
      class="elTable"
      @sort-change="tableSort1"
      :max-height="myHeight"
    >
      <el-table-column label="序号" align="center" type="index" width="50px">
        <template slot-scope="scope">
          <span>{{ scope.$index + (pageNum-1) * pageSize + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="牧场" min-width="110px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.pastureName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="部门" min-width="110px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.departName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="表名" min-width="110px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.formName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="表编号" align="center" width="120">
        <template slot-scope="scope">
          <span>{{ scope.row.formNumber }}</span>
        </template>
      </el-table-column>
      <el-table-column label="表结构" align="center" width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.useType }}</span>
        </template>
      </el-table-column>
      <el-table-column label="表类型" width="100px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.formType }}</span>
        </template>
      </el-table-column>
      <el-table-column label="具体位置" min-width="110px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.location }}</span>
        </template>
      </el-table-column>
      <el-table-column label="单价" sortable prop="price" min-width="90px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.price }}</span>
        </template>
      </el-table-column>
      <el-table-column label="负责人" min-width="110px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.employeName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否统计" min-width="80px" header-align="center" align="center">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.isInfo" active-color="#13ce66" inactive-color="#ff4949" :active-value="1" :inactive-value="0" @change="handleEnableChange(scope.$index, scope.row)" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="250px" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="form_see(row)">查看</el-button>
          <el-button type="success" size="mini" @click="form_edit(row)">编辑</el-button>
          <el-button type="danger" size="mini" @click="form_delete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>=0" :total="total" :page.sync="getdataListParm.offset" :limit.sync="getdataListParm.pagecount" @pagination="get_table_data" />
    <!-- 弹出层新增or修改 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" :close-on-click-modal="false" width="80%">
      <div class="app-add">
        <el-form ref="createTemp" :rules="rules" :model="createTemp" label-position="right" label-width="100px" style="width: 90%;margin:0 auto">
          <el-row>
            <el-col :span="8">
              <el-form-item label="牧场：" prop="pastureId">
                <el-select v-model="createTemp.pastureId" placeholder="牧场" class="filter-item" style="width:100%" @change="changePasture">
                  <el-option v-for="item in findAllPasture" :key="item.id" :label="item.name" :value="item.id" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="部门：" prop="departmentId">
                <el-select v-model="createTemp.departmentId" placeholder="部门" class="filter-item" style="width:100%" @change="changeDepart">
                  <el-option v-for="item in createDepartList" :key="item.id" :label="item.name" :value="item.id" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="表结构：" prop="useType">
                <el-select v-model="createTemp.useType" placeholder="表结构" class="filter-item" style="width:100%" :disabled="dialogStatus==='update'">
                  <el-option v-for="item in useTypes" :key="item.id" :label="item.name" :value="item.name" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="表类型：" prop="formType">
                <el-select v-model="createTemp.formType" placeholder="表类型" class="filter-item" style="width:100%" :disabled="dialogStatus==='update'">
                  <el-option v-for="item in getDictByName" :key="item.id" :label="item.value" :value="item.value" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="表名称：" prop="formName">
                <el-input ref="formName" v-model="createTemp.formName" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="表编号：" prop="formNumber">
                <el-input ref="formNumber" v-model="createTemp.formNumber" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="单价：" prop="price">
                <el-input ref="price" v-model="createTemp.price" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="倍率：" prop="Multiple">
                <el-input ref="Multiple" v-model="createTemp.Multiple"  disabled/>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="当前值：" prop="startAmount">
                <el-input ref="startAmount" v-model="createTemp.startAmount" :disabled="dialogStatus==='update'" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="具体位置：" prop="location">
                <el-input ref="location" v-model="createTemp.location" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="负责人：" prop="employeId">
                <el-select v-model="createTemp.employeId" placeholder="负责人" class="filter-item" style="width:100%">
                  <el-option v-for="item in findAllEmploye" :key="item.id" :label="item.name" :value="item.id" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="录入时间：" prop="inputDatetime">
                <el-date-picker v-model="createTemp.inputDatetime" type="date" style="width:100%;top:-3px;" format="yyyy-MM-dd" value-format="yyyy-MM-dd" :clearable="false" />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- <el-row>
            <el-col :span="8">
              <el-form-item label="iot编号：" prop="iotNumber">
                <el-input ref="iotNumber" v-model="createTemp.iotNumber" type="number" />
              </el-form-item>
            </el-col>
          </el-row> -->
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="dialogStatus==='create'" ref="createb" type="success" @click="add_dialog_save_again()">确认新增</el-button>
        <el-button type="primary" @click="dialogStatus==='create'?add_dialog_save():edit_dialog_save()">确认</el-button>
        <el-button @click="dialogFormVisible = false;get_table_data()">关闭</el-button>
      </div>
    </el-dialog>
    <!-- 卡片 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisibleSee" :close-on-click-modal="false" width="80%">
      <div class="app-see">
        <el-tabs v-model="activeName">
          <el-tab-pane label="基本信息" name="first">
            <el-form ref="seeTemp" :rules="rules" :model="seeTemp" label-position="right" label-width="100px" style="width: 90%;margin:0 auto">
              <el-row>
                <el-col :span="8">
                  <el-form-item label="牧场：" prop="pastureName">
                    <el-input ref="pastureName" v-model="seeTemp.pastureName" disabled />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="部门：" prop="departName">
                    <el-input ref="departName" v-model="seeTemp.departName" disabled />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="表结构：" prop="useType">
                    <el-input ref="useType" v-model="seeTemp.useType" disabled />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="8">
                  <el-form-item label="表类型：" prop="formType">
                    <el-input ref="formType" v-model="seeTemp.formType" disabled />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="表名称：" prop="formName">
                    <el-input ref="formName" v-model="seeTemp.formName" disabled />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="表编号：" prop="formNumber">
                    <el-input ref="formNumber" v-model="seeTemp.formNumber" disabled />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="8">
                  <el-form-item label="单价：" prop="price">
                    <el-input ref="price" v-model="seeTemp.price" disabled />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="倍率：" prop="Multiple">
                    <el-input ref="Multiple" v-model="seeTemp.Multiple" disabled />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="当前值：" prop="endAmount">
                    <el-input ref="endAmount" v-model="seeTemp.endAmount" disabled />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="8">
                  <el-form-item label="具体位置：" prop="location">
                    <el-input ref="location" v-model="seeTemp.location" disabled />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="负责人：" prop="employeId">
                    <el-input ref="employeName" v-model="seeTemp.employeName" disabled />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="上次录入时间：" prop="endDate">
                    <el-input ref="endDate" v-model="seeTemp.endDate" disabled />
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="用量记录" name="second">
            <el-table
              :key="tableKey"
              v-loading="listLoading"
              element-loading-text="给我一点时间"
              :data="listSee"
              border
              fit
              highlight-current-row
              style="width: 100%;"
              :row-style="rowStyle"
              :cell-style="cellStyle"
              class="elTable"
              @sort-change="tableSort2"
            >
              <el-table-column label="序号" align="center" type="index" width="50px">
                <template slot-scope="scope">
                  <span>{{ scope.$index + (pageNumSee-1) * pageSizeSee + 1 }}</span>
                </template>
              </el-table-column>
              <el-table-column sortable prop="lastAmount" label="上次值" min-width="110px" align="center">
                <template slot-scope="scope">
                  <span>{{ scope.row.lastAmount }}</span>
                </template>
              </el-table-column>
              <el-table-column sortable prop="endAmount" label="本次值" min-width="110px" align="center">
                <template slot-scope="scope">
                  <span>{{ scope.row.endAmount }}</span>
                </template>
              </el-table-column>
              <el-table-column sortable prop="waterConsumption" label="用量" min-width="110px" align="center">
                <template slot-scope="scope">
                  <span>{{ scope.row.waterConsumption }}</span>
                </template>
              </el-table-column>
              <el-table-column sortable prop="price" label="单价" align="center" width="150">
                <template slot-scope="scope">
                  <span>{{ scope.row.price }}</span>
                </template>
              </el-table-column>
              <el-table-column sortable prop="sumPrice" label="总价" align="center" width="150">
                <template slot-scope="scope">
                  <span>{{ scope.row.sumPrice }}</span>
                </template>
              </el-table-column>
              <el-table-column label="记录人" width="150px" align="center">
                <template slot-scope="scope">
                  <span>{{ scope.row.empname }}</span>
                </template>
              </el-table-column>
              <el-table-column label="记录日期" min-width="110px" align="center" sortable prop="date" />
            </el-table>
            <pagination v-show="total>=0" :total="totalSee" :page.sync="getSeeListParm.offset" :limit.sync="getSeeListParm.pagecount" @pagination="getSeeList" />
          </el-tab-pane>
        </el-tabs>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisibleSee = false">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// 引入
import { GetDataByName, GetDataByNames, PostDataByName, failproccess } from '@/api/common'
import waves from '@/directive/waves' // waves directive
import { parseTime, sortChange } from '@/utils/index.js'
// eslint-disable-next-line no-unused-vars
import { validateEMail } from '@/utils/validate.js'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import { MessageBox } from 'element-ui'
import { getToken } from '@/utils/auth'
import Cookies from 'js-cookie'
export default {
  name: 'Measure',
  components: { Pagination },
  directives: { waves },
  data() {
    return {

      // 校验规则
      // -----------------------------
      myHeight:document.documentElement.clientHeight - 85- 200,
      rules: {
        useType: [{ required: true, message: '必填', trigger: 'blur' }],
        formType: [{ required: true, message: '必填', trigger: 'blur' }],
        formName: [{ required: true, message: '必填', trigger: 'blur' }],
        formNumber: [{ required: true, message: '必填', trigger: 'blur' }]
      },
      rowStyle: { maxHeight: 50 + 'px', height: 45 + 'px' },
      cellStyle: { padding: 0 + 'px' },
      findAllPasture: [],
      findAllDepart: [],
      findAllEmploye: [],
      getDictByName: [], createDepartList: [], edit: 0,
      useTypes: [{ id: 0, name: '1级表' }, { id: 1, name: '2级表' }, { id: 2, name: '3级表' }],
      requestParams: [
        { name: 'findAllPasture', offset: 0, pagecount: 0, returntype: 'Map', parammaps: { 'id': Cookies.get('pastureid') }},
        { name: 'findAllEmploye', offset: 0, pagecount: 0, parammaps: { 'pastureId': Cookies.get('pastureid') }},
        { name: 'getDictByName', offset: 0, pagecount: 0, params: ['表类型'] }
      ],
      getDepartParam: {
        name: 'findAllDepart', offset: 0, pagecount: 0, parammaps: { 'pastureId': Cookies.get('pastureid') }
      },
      dialogFormVisible: false,
      dialogFormVisibleSee: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '新增',
        see: '查看详情'
      },
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: false,
      getdataListParm: {
        name: 'getMeasureList',
        page: 1,
        offset: 1,
        pagecount: 10,
        returntype: 'Map',
        parammaps: {
          pastureName: Cookies.get('pasturename'),
          departName: '',
          formNunber: ''
        }
      },
      createTemp: {
        departmentId: this.$store.state.user.departmentid,
        departmentName: '',
        location: '',
        startAmount: '',
        formName: '',
        formType: '',
        useType: '',
        price: '',
        employeId: this.$store.state.user.employeid,
        pastureId: this.$store.state.user.pastureid,
        formNumber: '',
        inputDatetime: parseTime(new Date(), '{y}-{m}-{d}'),
        Multiple: '1',
        iotNumber:''
      },
      requestParam: {},
      activeName: 'first',
      seeTemp: {},
      listSee: [],
      totalSee: 0,
      getSeeListParm: {
        name: 'getAllMeasureTypeList',
        page: 1,
        offset: 1,
        pagecount: 10,
        returntype: 'Map',
        parammaps: {}
      }
    }
  },
  computed: {
    // 设置请求头
    headers() {
      return {
        // 设置token
        token: getToken()
      }
    },
    uploadData() {
      return {
        name: 'importMeasureNew',
        importParams: '牧场,表名称,表编号,表结构,表类型,部门,位置,最后一次抄表值,单价,管理人,倍率',
        sheetname: 'SheetJS'
      }
    },
    // 设置上传地址
    uploadExcelUrl() {
      // process.env.VUE_APP_BASE_API是服务器的路径，也是axios的基本路径
      return process.env.VUE_APP_BASE_API + 'authdata/ImportExcel'
    }
  },

  created() {
    this.get_select_list()
    this.get_table_data()
  },

  methods: {
    tableSort1(column) {
      sortChange(column, this.list)
    },
    tableSort2(column) {
      sortChange(column, this.listSee)
    },
    get_select_list() {
      GetDataByNames(this.requestParams).then(response => {
        this.findAllPasture = response.data.findAllPasture.list
        this.findAllEmploye = response.data.findAllEmploye.list
        this.getDictByName = response.data.getDictByName.list
        this.getDepartDownList()
      })
    },
    getDepartDownList() {
      GetDataByName(this.getDepartParam).then(response => {
        this.findAllDepart = response.data.list
      })
    },
    changePastureName(item) {
      this.getDepartParam.parammaps.pastureId = this.findAllPasture.find(obj => obj.name == item).id
      this.getdataListParm.parammaps.departName = ''
      this.getDepartDownList()
    },
    getCreateDepartDownList() {
      GetDataByName(this.getDepartParam).then(response => {
        this.createDepartList = response.data.list
        if (this.edit == 1) {
          if (this.createDepartList.find(obj => obj.id == Cookies.get('departmentid'))) {
            this.createTemp.departmentId = parseInt(Cookies.get('departmentid'))
            this.createTemp.departmentName = this.createDepartList.find(obj => obj.id == Cookies.get('departmentid')).name
          } else {
            this.createTemp.departmentId = response.data.list[0].id
            this.createTemp.departmentName = response.data.list[0].name
          }
          console.log(this.createTemp.departmentName, 'this.createTemp.departmentName')
        }
      })
    },
    changePasture(item) {
      this.getDepartParam.parammaps.pastureId = item
      this.edit = 1
      this.getCreateDepartDownList()
    },
    changeDepart(item) {
      this.createTemp.departmentName = this.createDepartList.find(obj => obj.id == item).name
    },
    get_table_data() {
      this.listLoading = true
      GetDataByName(this.getdataListParm).then(response => {
        if (response.data.list !== null) {
          this.list = response.data.list
          this.pageNum = response.data.pageNum
          this.pageSize = response.data.pageSize
          // Just to simulate the time of the request
        } else {
          this.list = []
        }
        this.total = response.data.total
        setTimeout(() => {
          this.listLoading = false
        }, 100)
      })
    },

    form_search() {
      this.listLoading = true
      this.getdataListParm.offset = 1
      this.get_table_data()
    },
    handleEnableChange(index, row) {
      this.requestParam.name = 'updateMeasureInfo'
      this.requestParam.parammaps['isInfo'] = row.isInfo
      this.requestParam.parammaps['id'] = row.id
      PostDataByName(this.requestParam).then(() => {
        this.$notify({
          title: '成功',
          message: '变更成功',
          type: 'success',
          duration: 2000
        })
      })
    },
    resetcreateTemp() {
      this.createTemp = {
        departmentId: parseInt(Cookies.get('departmentid')),
        departmentName: '',
        location: '',
        startAmount: '',
        formName: '',
        formType: '',
        useType: '',
        price: '',
        employeId: parseInt(Cookies.get('employeid')),
        pastureId: parseInt(Cookies.get('pastureid')),
        formNumber: '',
        inputDatetime: parseTime(new Date(), '{y}-{m}-{d}'),
        Multiple: '1',
        iotNumber:''
      }
    },
    form_add() {
      this.resetcreateTemp()
      this.edit = 1
      this.getDepartParam.parammaps.pastureId = this.createTemp.pastureId
      this.getCreateDepartDownList()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['createTemp'].clearValidate()
      })
    },
    add_dialog_save() {
      this.$refs['createTemp'].validate(valid => {
        if (valid) {
          this.requestParam.name = 'insertMeasure'
          this.requestParam.parammaps = this.createTemp
          if (this.createTemp.price == '') { this.createTemp.price = 0 }
          if (this.createTemp.Multiple == '') { this.createTemp.Multiple = 1 }
          if (this.createTemp.startAmount == '') { this.createTemp.startAmount = 0 }
          PostDataByName(this.requestParam).then(response => {
            if (response.msg !== 'fail') {
              this.get_table_data()
              this.dialogFormVisible = false
              this.$notify({
                title: '成功',
                message: '新增成功',
                type: 'success',
                duration: 2000
              })
            } else {
              failproccess(response, this.$notify)
            }
          })
        }
      })
    },
    add_dialog_save_again() {
      this.$refs['createTemp'].validate(valid => {
        if (valid) {
          this.requestParam.name = 'insertMeasure'
          this.requestParam.parammaps = this.createTemp
          if (this.createTemp.price == '') { this.createTemp.price = 0 }
          if (this.createTemp.Multiple == '') { this.createTemp.Multiple = 1 }
          if (this.createTemp.startAmount == '') { this.createTemp.startAmount = 0 }
          PostDataByName(this.requestParam).then(response => {
            if (response.msg !== 'fail') {
              this.resetcreateTemp()
              this.$notify({
                title: '成功',
                message: '新增成功',
                type: 'success',
                duration: 2000
              })
            } else {
              failproccess(response, this.$notify)
            }
          })
        }
      })
    },

    form_edit(row) {
      this.edit = 0
      this.getDepartParam.parammaps.pastureId = row.pastureId
      this.getCreateDepartDownList()
      this.createTemp = Object.assign({}, row) // copy obj
      this.createTemp.departmentId = parseInt(row.departmentId)
      this.createTemp.inputDatetime = row.endDate
      this.createTemp.startAmount = row.endAmount
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['createTemp'].clearValidate()
      })
    },
    edit_dialog_save() {
      this.$refs['createTemp'].validate(valid => {
        if (valid) {
          this.requestParam.name = 'updateMeasure'
          this.requestParam.parammaps = this.createTemp
          PostDataByName(this.requestParam).then(response => {
            if (response.msg !== 'fail') {
              this.get_table_data()
              this.dialogFormVisible = false
              this.$notify({
                title: '成功',
                message: '修改成功',
                type: 'success',
                duration: 2000
              })
            } else {
              failproccess(response, this.$notify)
            }
          })
        }
      })
    },
    form_delete(row) {
      MessageBox.confirm('设备名称：' + row.formName, '确认删除？', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.requestParam.name = 'deleteMeasure'
          this.requestParam.parammaps = {}
          this.requestParam.parammaps['id'] = row.id
          PostDataByName(this.requestParam).then(() => {
            this.get_table_data()
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '删除成功',
              type: 'success',
              duration: 2000
            })
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    form_see(row) {
      this.seeTemp = Object.assign({}, row) // copy obj
      this.dialogStatus = 'see'
      this.dialogFormVisibleSee = true
      this.getSeeList()
    },
    getSeeList() {
      this.listLoading = true
      this.getSeeListParm.parammaps.id = this.seeTemp.id
      GetDataByName(this.getSeeListParm).then(response => {
        if (response.data.list !== null) {
          this.listSee = response.data.list
          this.pageNumSee = response.data.pageNum
          this.pageSizeSee = response.data.pageSize
        } else {
          this.listSee = []
        }
        this.totalSee = response.data.total
        setTimeout(() => {
          this.listLoading = false
        }, 100)
      })
    },
    beforeImportExcel(file) {
      const isLt2M = file.size / 1024 / 1024 < 2
      if (!isLt2M) {
        this.$message.error('上传文件大小不能超过 2MB!')
      }
      return isLt2M
    },
    handleImportExcelSuccess(res, file) {
      this.get_table_data()
      if (res.msg === 'ok') {
        this.$message({
          title: '成功',
          message: '导入成功:' + res.data.success + '条!',
          type: 'success',
          duration: 2000
        })
        if (res.data.err_count > 0) {
          this.$notify({
            title: '失败',
            message: '导入失败:' + res.data.err_count + '条!',
            type: 'danger',
            duration: 2000
          })
           import('@/vendor/Export2Excel').then(excel => {
             const list1 = res.data.result
             const tHeader = [
               '牧场', '表名称', '表编号', '表结构', '表类型', '部门', '位置', '最后一次抄表值', '单价', '管理人', '倍率', '错误信息'
             ]
             const filterVal = [
               '牧场', '表名称', '表编号', '表结构', '表类型', '部门', '位置', '最后一次抄表值', '单价', '管理人', '倍率', 'error_msg'
             ]
             const data1 = this.formatJson(filterVal, list1)
             excel.export_json_to_excel({
               header: tHeader,
               data: data1,
               filename: '计量设备管理',
               autoWidth: true,
               bookType: 'xlsx'
             })
           })
        }
      } else {
        this.$notify({
          title: '失败',
          message: '上传失败',
          type: 'danger',
          duration: 2000
        })
      }
    },

    formatJson(filterVal, jsonData) {
      return jsonData.map(v =>
        filterVal.map(j => {
          if (j === 'timestamp') {
            return parseTime(v[j])
          } else {
            return v[j]
          }
        })
      )
    }
  }
}
</script>
