<template>
  <a-card :bordered="false">
    <!-- 搜索栏 -->
    <search-form :items="formItems" v-model="queryParam" @search="searchQuery">
      <a-space style="float: right" class="btn-group" slot="additional">
        <a-button @click="onAdd" type="primary">
          <img src="@/assets/images/global/add.png" alt="" />
          Add
        </a-button>
        <a-button @click="onEdit" type="primary">
          <img src="@/assets/images/global/edit.png" alt="" />
          Edit
        </a-button>
        <a-button @click="onDel" type="primary">
          <img src="@/assets/images/global/delete.png" alt="" />
          Delete
        </a-button>
      </a-space>
    </search-form>
    <!-- 搜索栏结束 -->
    <!-- 列表 -->
    <div>
      <custom-table
        size="middle"
        rowKey="facilityId"
        :columns="columns"
        :list="dataSource"
        :pagination="ipagination"
        :loading="loading"
        @change="handleTableChange"
        :selectedRowKeys.sync="selectedRowKeys"
        :scroll="{ y: 'calc(100vh - 370px)' }"
      >
        <template slot="index" slot-scope="{ index }">
          {{ index + 1 }}
        </template>
        <template slot="roles" slot-scope="{ text }">
          <span v-for="role of text" :key="role.id">
            {{ role.roleName }}
          </span>
        </template>
      </custom-table>
    </div>
    <!-- 列表结束 -->
    <!-- 新增/编辑 -->
    <custom-modal :title="isAdd ? 'Add' : 'Edit'" v-model="visible" :width="475" :okHandler="submit">
      <a-form-model
        ref="form"
        layout="horizontal"
        :model="model"
        :rules="rules"
        :colon="false"
        :labelCol="{ style: { width: '105px' } }"
        :wrapperCol="{ style: { width: '300px' } }"
      >
        <a-form-model-item label="Facility Id" prop="facilityId">
          <a-input v-model="model.facilityId" type="number"></a-input>
        </a-form-model-item>
        <a-form-model-item label="Facility Name" prop="facilityName">
          <a-input v-model="model.facilityName"></a-input>
        </a-form-model-item>
        <a-form-model-item label="Type" prop="type">
          <a-input v-model="model.type"></a-input>
        </a-form-model-item>
        <a-form-model-item label="Location" prop="location">
          <a-input v-model="model.location"></a-input>
        </a-form-model-item>
        <a-form-model-item label="Longitude" prop="longitude">
          <a-input v-model="model.longitude"></a-input>
        </a-form-model-item>
        <a-form-model-item label="Latitude" prop="latitude">
          <a-input v-model="model.latitude"></a-input>
        </a-form-model-item>
        <a-form-model-item label="Status" prop="status">
          <j-dict-select-tag v-model="model.status" dictCode="NUCLEARFACILITY_STATUS"></j-dict-select-tag>
        </a-form-model-item>
      </a-form-model>
    </custom-modal>
    <!-- 新增/编辑 结束 -->
  </a-card>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import FormMixin from '@/mixins/FormMixin'
import { cloneDeep } from 'lodash'
import { getAction } from '../../api/manage'

const columns = [
  {
    title: 'NO',
    align: 'left',
    width: 100,
    scopedSlots: {
      customRender: 'index'
    },
    customHeaderCell: () => {
      return {
        style: {
          'padding-left': '60px !important'
        }
      }
    },
    customCell: () => {
      return {
        style: {
          'padding-left': '60px !important'
        }
      }
    }
  },
  {
    title: 'FACILITY ID',
    align: 'left',
    dataIndex: 'facilityId',
    width: 100
  },
  {
    title: 'FACILITY NAME',
    align: 'left',
    width: 100,
    dataIndex: 'facilityName'
  },
  {
    title: 'TYPE',
    align: 'left',
    width: 100,
    dataIndex: 'type'
  },
  {
    title: 'LOCATION',
    align: 'left',
    width: 100,
    dataIndex: 'location'
  },
  {
    title: 'LONGITUDE',
    align: 'left',
    width: 100,
    dataIndex: 'longitude'
  },
  {
    title: 'LATITUDE',
    align: 'left',
    width: 100,
    dataIndex: 'latitude'
  },
  {
    title: 'STATUS',
    align: 'left',
    width: 100,
    dataIndex: 'status'
  }
]

export default {
  mixins: [JeecgListMixin, FormMixin],
  data() {
    this.columns = columns
    return {
      queryParam: {},
      rules: {
        facilityId: [{ required: true, message: 'Please Enter Facility Id' }],
        facilityName: [{ required: true, message: 'Please Enter Facility Name' }]
      },
      url: {
        list: '/gardsNuclearfacility/findPage',
        delete: '/gardsNuclearfacility/deleteById',
        add: '/gardsNuclearfacility/create',
        edit: '/gardsNuclearfacility/update'
      },
      typeList: [],
      locationList: []
    }
  },
  created() {
    this.getTypeList()
    this.getLocationList()
  },
  methods: {
    async getTypeList() {
      try {
        const res = await getAction('/gardsNuclearfacility/findType')
        this.typeList = res.filter(item => item).map(item => ({ label: item, value: item }))
      } catch (error) {
        console.error(error)
      }
    },

    async getLocationList() {
      try {
        const res = await getAction('/gardsNuclearfacility/findLocation')
        this.locationList = res.filter(item => item).map(item => ({ label: item, value: item }))
      } catch (error) {
        console.error(error)
      }
    },

    filterOption(input, option) {
      return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
    },

    onAdd() {
      this.isAdd = true
      this.model = {}
      this.visible = true
    },
    onEdit() {
      if (this.selectedRowKeys && this.selectedRowKeys.length) {
        this.isAdd = false
        this.visible = true
        const find = this.dataSource.find(item => item.facilityId === this.selectedRowKeys[0])
        this.model = cloneDeep(find)
      } else {
        this.$message.warn('Please Select An Item To Edit')
      }
    },
    onDel() {
      if (this.selectedRowKeys && this.selectedRowKeys.length) {
        this.$confirm({
          title: 'Do You Want To Delete This Item?',
          okText: 'OK',
          cancelText: 'Cancel',
          onOk: () => {
            this.handleDelete(this.selectedRowKeys[0])
          }
        })
      } else {
        this.$message.warn('Please Select An Item To Delete')
      }
    }
  },
  computed: {
    formItems() {
      return [
        {
          label: 'Name',
          type: 'a-input',
          name: 'facilityName',
          props: {
            allowClear: true,
            style: {
              width: '261px'
            }
          },
          style: {
            width: 'auto'
          }
        },
        {
          label: 'Type',
          type: 'custom-select',
          name: 'type',
          props: {
            options: this.typeList,
            allowClear: true,
            showSearch: true,
            filterOption: this.filterOption,
            style: {
              width: '261px'
            }
          },
          style: {
            width: 'auto'
          }
        },
        {
          label: 'Location',
          type: 'custom-select',
          name: 'location',
          props: {
            options: this.locationList,
            allowClear: true,
            showSearch: true,
            filterOption: this.filterOption,
            style: {
              width: '261px'
            }
          },
          style: {
            width: 'auto'
          }
        },
        {
          label: 'Status',
          type: 'j-dict-select-tag',
          name: 'status',
          props: {
            getPopupContainer: () => {
              return document.body
            },
            dictCode: 'NUCLEARFACILITY_STATUS',
            style: {
              width: '261px'
            }
          },
          style: {
            width: 'auto'
          }
        }
      ]
    }
  }
}
</script>

<style lang="less" scoped>
.btn-group {
  img {
    margin-right: 12px;
    height: 18px;
  }
}
</style>
