<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="11" :md="12" :sm="24">
            <a-form-item label="点检时间">
              <a-range-picker date-format="YYYY-MM-DD" v-model="queryParam.date" @change="onChangeDate" />
              <!-- <j-date :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择开始时间" class="query-group-cust" v-model="queryParam.spotcheckdate_begin"></j-date>
              <span class="query-group-split-cust"></span>
              <j-date :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择结束时间" class="query-group-cust" v-model="queryParam.spotcheckdate_end"></j-date> -->
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="点检结果">
              <j-dict-select-tag placeholder="请选择点检结果" v-model="queryParam.result" dictCode="spotcheck_result"/>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <!-- <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a> -->
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <!-- <a-button type="primary" icon="download" @click="handleExportXls('设备点检')">导出</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload> -->
      <!-- 高级查询区域 -->
      <!-- <j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query> -->
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <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"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text,record">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" :preview="record.id" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>

          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>

    <!-- <cmms-spotcheck-modal ref="modalForm" @ok="modalFormOk"></cmms-spotcheck-modal> -->
    <!-- 第二版 -->
    <!-- <spotcheck-modal ref="modalForm" @ok="modalFormOk" :dataList="dataList"></spotcheck-modal> -->
    <!-- 第三版 -->
    <spotcheck-modal ref="modalForm" @ok="modalFormOk"></spotcheck-modal>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import {filterMultiDictText} from '@/components/dict/JDictSelectUtil'
  import SpotcheckModal from './modules/SpotcheckModal.vue'
  import { getAction } from '@/api/manage'

  export default {
    name: 'SpotcheckList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
        SpotcheckModal
    },
    props: {
      selectData: {
        type: Object,
        default: function(){
            return {} // 使用工厂函数返回默认值
        }
      },
    },
    data () {
      return {
        description: '设备点检管理页面',
        // 查询参数
        queryParam: {},
        // 表头
        columns: [
          {
            title: '序号',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'点检内容编号',
            align:"center",
            dataIndex: 'contentcode'
          },
          {
            title:'点检内容名称',
            align:"center",
            dataIndex: 'contentname'
          },
          {
            title:'设备',
            align:"center",
            dataIndex: 'equipmentid_dictText'
          },
          // {
          //   title:'所属点检内容ID',
          //   align:"center",
          //   dataIndex: 'spotcheckcontid_dictText'
          // },
          {
            title:'点检结果',
            align:"center",
            dataIndex: 'result_dictText'
          },
          {
            title:'点检人',
            align:"center",
            dataIndex: 'createBy_dictText'
          },
          {
            title:'点检时间',
            align:"center",
            dataIndex: 'spotcheckdate'
          },
          {
            title:'点检描述',
            align:"center",
            dataIndex: 'description'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/cmmsSpotcheck/cmmsSpotcheck/list",
          delete: "/cmmsSpotcheck/cmmsSpotcheck/delete",
          deleteBatch: "/cmmsSpotcheck/cmmsSpotcheck/deleteBatch",
          exportXlsUrl: "/cmmsSpotcheck/cmmsSpotcheck/exportXls",
          importExcelUrl: "cmmsSpotcheck/cmmsSpotcheck/importExcel",

        },
        dictOptions:{},
        superFieldList:[],
        // 弹框中所有数据
        dataList: {
          cmmsSpotcheckTitemList: [],
          files: []
        }
      }
    },
    created() {
    this.getSuperFieldList();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      // 时间处理
      onChangeDate(date, dateString){
        console.log(date, dateString);
        this.queryParam.spotcheckdate_begin= dateString[0]
        this.queryParam.spotcheckdate_end= dateString[1]
      },
      loadData(arg) {
        if(!this.url.list){
          this.$message.error("请设置url.list属性!")
          return
        }
        //加载数据 若传入参数1则加载第一页的内容
        if (arg === 1) {
          this.ipagination.current = 1;
        }
        var params = this.getQueryParams();//查询条件
        this.loading = true;
        getAction(this.url.list, params).then((res) => {
          if (res.success) {
            //update-begin---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
            this.dataSource = res.result.records||res.result;
            if(res.result.total)
            {
              this.ipagination.total = res.result.total;
            }else{
              this.ipagination.total = 0;
            }
            //update-end---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
          }else{
            this.$message.warning(res.message)
          }
        }).finally(() => {
          this.loading = false
        })
      },
      // 处理父组件传过来的数据
      getDataList(){
        // console.log(111,this.selectData)
        this.queryParam.equipmentid = this.selectData.id
        this.loadData()
      },
      // 新增
      handleAdd: function () {
        // 第一版
        // if (this.selectData.id == undefined) {
        //   this.$message.warning("请先选择设备");
        // } else {
        //   getAction(`/cmmsSpotcheck/cmmsSpotcheck/getInfoByEquipmentid/${this.selectData.id}`).then((res) => {
        //     console.log("000",res)
        //     this.dataList = res.result
        //     console.log(1111,this.dataList)
        //     this.$refs.modalForm.add();
        //     this.$refs.modalForm.title = "新增";
        //     this.$refs.modalForm.disableSubmit = false;
        //   })
        // }
        // 第二版
        // if (this.selectData.id == undefined) {
        //   this.dataList = []
        //   this.$refs.modalForm.add();
        //   this.$refs.modalForm.title = "新增";
        //   this.$refs.modalForm.disableSubmit = false;
        // } else {
        //   getAction(`/cmmsSpotcheck/cmmsSpotcheck/getInfoByEquipmentid/${this.selectData.id}`).then((res) => {
        //     this.dataList = res.result
        //     this.$refs.modalForm.add();
        //     this.$refs.modalForm.title = "新增";
        //     this.$refs.modalForm.disableSubmit = false;
        //   })
        // }
        // 第三版
        this.$refs.modalForm.add();
        this.$refs.modalForm.title = "新增";
        this.$refs.modalForm.disableSubmit = false;
      },
      handleEdit: function (record) {
        this.$refs.modalForm.edit(record);
        this.$refs.modalForm.title = "编辑";
        this.$refs.modalForm.disableSubmit = false;
      },
      initDictConfig(){
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'string',value:'contentcode',text:'所属点检内容编号',dictCode:''})
        fieldList.push({type:'string',value:'contentname',text:'所属点检内容名称',dictCode:''})
        fieldList.push({type:'sel_search',value:'equipmentid',text:'设备ID',dictTable:"tpm_equipment", dictText:'equipmentname', dictCode:'id'})
        fieldList.push({type:'sel_search',value:'spotcheckcontid',text:'所属点检内容ID',dictTable:"cmms_spotcheck_content", dictText:'contentname', dictCode:'id'})
        fieldList.push({type:'datetime',value:'spotcheckdate',text:'点检时间'})
        fieldList.push({type:'string',value:'result',text:'点检结果',dictCode:'spotcheck_result'})
        fieldList.push({type:'string',value:'description',text:'点检描述',dictCode:''})
        this.superFieldList = fieldList
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>