<template>
  <a-card :bordered="false">
    <div class="equipmentManagement">
      <!-- 搜索框 -->
      <div class="table-page-search-wrapper">
        <a-form layout="inline" @keyup.enter.native="$refs.table.refresh(true)">
          <a-row :gutter="15">
            <a-col :md="5" :sm="24">
              <a-form-item label="设备编码">
                <a-input allowClear v-model.trim="queryParam.deviceCode" placeholder="请输入设备编码" />
              </a-form-item>
            </a-col>
            <a-col :md="5" :sm="24">
              <a-form-item label="客户名称">
                <storeList ref="storeList" @change="custChange"></storeList>
              </a-form-item>
            </a-col>
            <a-col :md="5" :sm="24">
              <a-form-item label="所属省份">
                <a-select id="equipmentManagement-addrProvince" allowClear v-model="queryParam.provinceSn" placeholder="请选择省">
                  <a-select-option v-for="item in addrProvinceList" :value="item.areaSn" :key="item.areaSn + 'a'">{{ item.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="5" :sm="24">
              <a-form-item label="启用状态">
                <v-select
                  v-model="queryParam.enabledFlag"
                  ref="auditStatus"
                  code="ENABLE_FLAG"
                  placeholder="请选择启用状态"
                  allowClear></v-select>
              </a-form-item>
            </a-col>
            <a-col :md="4" :sm="24">
              <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="equipmentManagement-refresh">查询</a-button>
              <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="equipmentManagement-reset">重置</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <div class="table-operator">
        <a-button type="primary" class="button-error" @click="openModal = true">添加设备</a-button>
      </div>
      <!-- 列表 -->
      <s-table
        class="sTable"
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns"
        :data="loadData"
        bordered>
        <!-- 地区 -->
        <template slot="addressInfo" slot-scope="text, record">
          <span v-if="record.dealer">{{ record.dealer.provinceName }}{{ record.dealer.cityName }}</span>
        </template>
        <!--是否启用 -->
        <template slot="isEnabled" slot-scope="text, record">
          <a-switch checked-children="是" un-checked-children="否" :checked="record.isChecked" @change="handleChecked(record)" />
        </template>
        <!-- 操作 -->
        <template slot="action" slot-scope="text, record">
          <a-button size="small" type="link" class="button-primary" @click="handleEdit(record)">编辑</a-button>
          <a-button size="small" type="link" class="button-primary" @click="handleDel(record)">删除</a-button>
        </template>
      </s-table>
    </div>
    <!-- 添加设备 -->
    <addEquipmentModal ref="equipmentBox" :openModal="openModal" :itemId="itemId" @confirm="$refs.table.refresh(true)" @close="openModal=false" />
  </a-card>
</template>

<script>
import storeList from '@/views/common/storeList.vue'
import { STable, VSelect } from '@/components'
import addEquipmentModal from './addEquipmentModal'
import { handDeviceList, handDeviceDel, handDeviceEdit } from '@/api/handDevice.js'
import { getAreaCgj } from '@/api/data'
export default {
  name: 'EquipmentManagement',
  components: { STable, VSelect, storeList, addEquipmentModal },
  data () {
    return {
      advanced: false, // 高级搜索 展开/关闭
      disabled: false, //  查询、重置按钮是否可操作
      queryParam: {
        dealerSn: undefined,
        deviceCode: '',
        deviceName: '',
        enabledFlag: undefined,
        provinceSn: undefined
      },
      addrProvinceList: [],
      openModal: false,
      itemId: '',
      columns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center', fixed: 'left' },
        { title: '设备编码', dataIndex: 'deviceCode', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '设备名称', dataIndex: 'deviceName', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '地区', scopedSlots: { customRender: 'addressInfo' }, align: 'center' },
        { title: '经销商', dataIndex: 'dealer.dealerName', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '是否启用', scopedSlots: { customRender: 'isEnabled' }, align: 'center' },
        { title: '添加时间', dataIndex: 'createDate', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '操作', scopedSlots: { customRender: 'action' }, align: 'center' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        return handDeviceList(Object.assign(parameter, this.queryParam)).then(res => {
          const data = res.data
          const no = (data.pageNo - 1) * data.pageSize
          for (var i = 0; i < data.list.length; i++) {
            data.list[i].no = no + i + 1
            data.list[i].isChecked = data.list[i].enabledFlag == 1
          }
          this.disabled = false
          return data
        })
      }
    }
  },
  mounted () {
    this.getCityList()
  },
  methods: {
    // 获取所属省份
    getCityList () {
      getAreaCgj({ type: '2' }).then(res => {
        if (res.status == 200) {
          this.addrProvinceList = res.data || []
        } else {
          this.addrProvinceList = []
        }
      })
    },
    // 修改启用状态
    handleChecked (row) {
      const _this = this
      handDeviceEdit({
        id: row.id,
        enabledFlag: row.isChecked ? '0' : '1',
        dealerSn: row.dealerSn,
        deviceCode: row.deviceCode,
        deviceName: row.deviceName
      }).then(res => {
        if (res.status == 200) {
          _this.$message.success(res.message)
          _this.resetSearchForm()
        }
      })
    },
    // 选择客户
    custChange (val) {
      this.queryParam.dealerSn = val.key
    },
    handleEdit (row) {
      this.itemId = row.id
      this.$nextTick(() => {
        this.openModal = true
        this.$refs.equipmentBox.pageInit()
      })
    },
    handleDel (row) {
      const _this = this
      this.$warning({
        title: '提示',
        content: '删除该设备后，所有绑定数据将被清除，并且不能继续操作。',
        okText: '删除',
        onOk () {
          handDeviceDel({ id: row.id }).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.resetSearchForm()
            }
          })
        },
        onCancel () {}
      })
    },
    //  时间  change
    dateChange (date) {
      this.queryParam.beginDate = date[0]
      this.queryParam.endDate = date[1]
    },
    // 重置
    resetSearchForm () {
      this.queryParam.dealerSn = undefined
      this.queryParam.deviceCode = ''
      this.queryParam.deviceName = ''
      this.queryParam.enabledFlag = undefined
      this.queryParam.provinceSn = undefined
      this.$refs.storeList.resetForm()
      this.$refs.table.refresh(true)
    }
  }
}
</script>
<style lang="less">
  .equipmentManagement{
    .sTable{
      margin-top: 10px;
    }
  }
</style>
