<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="7" :md="8" :sm="24">
            <a-form-item label="印章类别">
              <j-input placeholder="请输入印章类别" v-model="queryParam.sealType"></j-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="印章名称">
              <j-input placeholder="请输入印章名称" v-model="queryParam.sealName"></j-input>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="印章材质">
                <j-input placeholder="请输入印章材质" v-model="queryParam.sealMaterial"></j-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="印章尺寸">
                <j-input placeholder="请输入印章尺寸" v-model="queryParam.sealSize"></j-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="印章状态">
                <a-select placeholder="请选择印章状态" v-model="queryParam.sealStatus" >
                  <a-select-option value="正在使用">正在使用</a-select-option>
                  <a-select-option value="已销毁">已销毁</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="印章保管人">
                <j-input placeholder="请输入印章保管人" v-model="queryParam.sealCustodian"></j-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="保管部门">
                <j-input placeholder="请输入保管部门" v-model="queryParam.custodianDept"></j-input>
              </a-form-item>
            </a-col>
          </template>
          <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>-->
<!--      <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"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{fixed:true,selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"

        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无此图片</span>
          <img v-else :src="getImgView(text)" 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="uploadFile(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-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>


        <span slot="status" slot-scope="text, record">
          <a @click="changeStatus(record)">{{record.sealStatus}}</a>
        </span>

      </a-table>
    </div>
    <a-modal
      :visible='visible'
      :width="100"
      cancelText='关闭'
      title='更改状态'
      @ok="ok"
      @cancel="cancel">
      <a-form :form="form">
        <a-form-item label="印章状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select placeholder="请选择印章状态" v-decorator="[ 'sealStatus']" >
            <a-select-option value="正在使用">正在使用</a-select-option>
            <a-select-option value="已销毁">已销毁</a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-modal>
    <sealArchives-modal ref="modalForm" @ok="modalFormOk"></sealArchives-modal>
  </a-card>
</template>

<script>

  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import SealArchivesModal from './modules/SealArchivesModal'
  import JDictSelectTag from '@/components/dict/JDictSelectTag.vue'
  import { httpAction } from '@api/manage'
  import pick from 'lodash.pick'
  import JInput from '@/components/jeecg/JInput.vue'

  export default {
    name: "SealArchivesList",
    mixins:[JeecgListMixin],
    components: {
      JDictSelectTag,
      JInput,
      SealArchivesModal
    },
    data () {
      return {
        form: this.$form.createForm(this),
        visible: false,
        description: '印章档案管理页面',
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'印章类别',
            align:"center",
            dataIndex: 'sealType'
          },
          {
            title:'印章名称',
            align:"center",
            dataIndex: 'sealName'
          },
          {
            title:'印章材质',
            align:"center",
            dataIndex: 'sealMaterial'
          },
          {
            title:'印章尺寸',
            align:"center",
            dataIndex: 'sealSize'
          },
          {
            title:'印章状态',
            align:"center",
            scopedSlots: { customRender: 'status' },
            dataIndex: 'sealStatus'
          },
          {
            title:'印章保管人',
            align:"center",
            dataIndex: 'sealCustodian'
          },
          {
            title:'保管部门',
            align:"center",
            dataIndex: 'custodianDept',
            customRender: (text, record, index) => {
            if(text!==''&&text&&text!=='null'){
              return text
            }else{
              var cc=''
              return cc
            }
          }, },
          {
            title:'刻章日期',
            align:"center",
            dataIndex: 'engravingDate'
          },
          {
            title:'销毁日期',
            align:"center",
            dataIndex: 'destroyDate',
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title:'上缴日期',
            align:"center",
            dataIndex: 'turnDate',
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title:'附件',
            align:"center",
            dataIndex: 'file',
            scopedSlots: {customRender: 'fileSlot'},
          },
          // {
          //   title: '操作',
          //   dataIndex: 'action',
          //   align:"center",
          //   scopedSlots: { customRender: 'action' }
          // }
        ],
        url: {
          list: "/sealArchives/sealArchives/list",
          delete: "/sealArchives/sealArchives/delete",
          deleteBatch: "/sealArchives/sealArchives/deleteBatch",
          exportXlsUrl: "/sealArchives/sealArchives/exportXls",
          importExcelUrl: "sealArchives/sealArchives/importExcel",
          edit: "/sealArchives/sealArchives/edit",
        },
        dictOptions:{},
      }
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      }
    },
    methods: {
      initDictConfig(){
      },
      changeStatus(record){
        console.log(record);
        this.form.resetFields();
        this.visible = true;
        this.model = Object.assign({}, record);
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'sealStatus'))
        })
      },
      ok(){
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            httpurl+=this.url.edit;
            method = 'put';
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              this.visible = false;
              this.loadData()
            })
          }
        })
      },
      cancel(){
        this.visible = false;
      },
      /* 文件下载 */
      uploadFile(text){
        console.log(text)
        if(!text){
          this.$message.warning("未知的文件")
          return;
        }
        var strArr = text;
        if(text.indexOf(",")>0){
          strArr = text.split(',');
          console.log("strArr",strArr)
          text = text.substring(0,text.indexOf(","))
          var count=0;
          for(var i =0;i<strArr.length;i++){
            console.log(strArr[i])
            var hiddenIFrameID = 'hiddenDownloader' + count++;
            var iframe = document.createElement('iframe');
            iframe.id = hiddenIFrameID;
            iframe.style.display = 'none';
            document.body.appendChild(iframe);
            iframe.src = strArr[i];//直接下载，不会弹出新的页面
          }
        }else {
          window.open(window._CONFIG['staticDomainURL']+ "/"+text);
        }
      },
    }
  }
</script>
<style scoped lang="less">
  @import '~@assets/less/common.less';
  /deep/ .table-page-search-wrapper .ant-form-inline .ant-form-item>.ant-form-item-label{
    width: 26% ;
  }
</style>