<template>
  <j-modal
  :title="title"
  :width="width"
  :visible="visible"
  @ok="handleOk"
  @cancel="handleCancel"
  cancelText="关闭">
    <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
      <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
      <a style="margin-left: 24px" @click="onClearSelected">清空</a>
    </div>
    <a-table
      ref="table"
      size="middle"
      :scroll="{x:true}"
      bordered
      rowKey="id"
      :columns="columns"
      :dataSource="dataSource"
      :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange, onSelect: onSelect}"
      class="j-table-force-nowrap"
      :pagination="ipagination"
      @change="handleTableChange">
    </a-table>
  </j-modal>
</template>

<script>
  import { httpAction, getAction } from '@/api/manage'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  
  export default {
    name: 'InspectLineModalAdd',
    mixins:[JeecgListMixin, mixinDevice],
    props: {
      equipmentId: {
        type: String,
        default: ''
      },
      selectData: {
        type: Array,
        default: function(){
          return [] // 使用工厂函数返回默认值
        }
      },
    },
    data () {
      return {
        disableMixinCreated: true,
        title:'',
        width: 1000,
        visible: false,
        type: '',
        /* table选中keys*/
        selectedRowKeys: [],
        /* table选中records*/
        selectionRows: [],
        columns: [
          // {
          //   title: '',
          //   dataIndex: '',
          //   key:'rowIndex',
          //   width:60,
          //   align:"center",
          //   customRender:function (t,r,index) {
          //     return parseInt(index)+1;
          //   }
          // },
          {
            title:'巡检路线编号',
            align:"center",
            dataIndex: 'linecode'
          },
          {
            title:'巡检路线名称',
            align:"center",
            dataIndex: 'linename'
          },
          {
            title:'备注',
            align:"center",
            dataIndex: 'remark'
          },
        ],
        dataSource: [],
        chooseData: [],
        url: {
          list: "/cmmsInspectLine/cmmsInspectLine/listDetails",
          // getList: "/cmmsInspectItem/cmmsInspectItem/getItemByEqId",
          // lineList: "/cmmsInspectLine/cmmsInspectLine/listDetails",
        },
      }
    },
    watch: {
      selectData: {
        handler(newV, oldV){
          console.log(newV, oldV)
          if(newV.length > 0){
            this.selectList()
          }
        },
      }
    },
    methods: {
      add(){
        this.visible = true;
        this.loadData()
      },
      loadData () {
        //  {status: '0'}
        var params = this.getQueryParams();//查询条件
        this.loading = true;
        getAction(this.url.list, params).then((res) => {
          if (res.success) {
            this.dataSource = res.result.records||res.result;
            if(res.result.total)
            {
              this.ipagination.total = res.result.total;
            }else{
              this.ipagination.total = 0;
            }
            // this.selectList()
          }else{
            this.$message.warning(res.message)
          }
        }).finally(() => {
          this.loading = false
        })
      },
      // 将以选中的值重新在列表中选中
      selectList() {
        this.chooseData = this.selectData
        // console.log(this.selectData)
        this.selectedRowKeys = this.selectData.map((res) => {
          return res.id
        })
      },
      close () {
        this.visible = false;
      },
      handleOk () {
        this.$emit('ok', this.chooseData);
        console.log(this.chooseData)
        this.visible = false;
      },
      handleCancel () {
        this.close()
      },
      onSelect(selectedRow, selected){
        // console.log(selectedRow, selected)
        if(selected){
          // 新增
          this.chooseData.push(selectedRow)
        } else {
          // 删除(过滤)
          var index = this.chooseData.findIndex(item => item.id === selectedRow.id)
          this.chooseData.splice(index, 1)
        }
      },
    }
  }
</script>