<template>
  <div id="declareElementsModal">
    <a-modal
      title="报关要素"
      v-model="declareElementsModVis"
      :confirmLoading="confirmLoading"
      @cancel="handleCancel"
      width="80%"
      style="top:330px;left:100px;"
    >
      <!-- 主表信息 回显  -->
      <a-card :bordered="false">
        <div class="table-page-search-wrapper">
          <a-form-model layout="inline" ref="form" :model="declareElements">
            <a-row :gutter="24">
              <a-col :md="6" :sm="8" style="display: none;">
                <a-form-model-item label="ID" prop="id">
                  {{ declareElements.id }}
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="8" style="display: none;">
                <a-form-model-item label="账套" prop="acSetNo">
                  {{ declareElements.acSetNo }}
                </a-form-model-item>
              </a-col>

            <a-col :md="6" :sm="8" style="display: none;">
                <a-form-model-item label="款号" prop="itemNumber">
                  {{ declareElements.itemNumber }}
                </a-form-model-item>
              </a-col>
                <a-col :md="6" :sm="8" style="display: none;">
                <a-form-model-item label="存货名称" prop="inventoryName">
                  {{ declareElements.inventoryName }}
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="8" style="display: none;">
                <a-form-model-item label="发运明细子表ID" prop="syShippingids">
                  {{ declareElements.itemIds }}
                </a-form-model-item>
              </a-col>
            <a-col :md="6" :sm="8">
                <a-form-model-item label="报关品名" prop="declarationName" :rules="[{ required: true, message: '请输入报关品名!' }]">
                  <a-input placeholder="请输入报关品名" v-model="declareElements.declarationName"></a-input>
                </a-form-model-item>
             </a-col>

              <!-- <a-col :md="6" :sm="8">
                <a-form-model-item label="报关品名" prop="declarationName">
                  {{ declareElements.declarationName }}
                </a-form-model-item>
              </a-col> -->
             <a-col :md="6" :sm="8">
                <a-form-model-item label="HSCODE" prop="hsCode" :rules="[{ required: true, message: '请输入HSCODE!' }]">
                  <a-input placeholder="请输入HSCODE" v-model="declareElements.hsCode"></a-input>
                </a-form-model-item>
             </a-col>

              <!-- <a-col :md="6" :sm="8">
                <a-form-model-item label="HSCODE" prop="hsCode">
                  {{ declareElements.hsCode }}
                </a-form-model-item>
              </a-col> -->
                <a-col :md="6" :sm="8">
                <a-form-model-item label="英文品名" prop="englishProductName" :rules="[{ required: true, message: '请输入英文品名!' }]">
                  <a-input placeholder="请输入英文品名" v-model="declareElements.englishProductName"  @blur="convertUppercase(declareElements.englishProductName)"></a-input>
                </a-form-model-item>
             </a-col>

              <!-- <a-col :md="6" :sm="8">
                <a-form-model-item label="英文品名" prop="englishProductName">
                  {{ declareElements.englishProductName }}
                </a-form-model-item>
              </a-col> -->
            </a-row>
          </a-form-model>
        </div>
      </a-card>

      <!-- 増行 子表 -->
      <a-card :bordered="false" style="margin:10px 0">
        <a-button type="primary" @click="handleAddColumn" icon="plus" style="margin:10px 0;">
          増行
        </a-button>
        <!-- 子表 -->
        <a-spin :spinning="confirmLoading">
          <a-form-model ref="formRef">
            <a-table
              v-if="declareElementsData"
              bordered
              rowKey="id"
              :columns="declareElementsColumns"
              :data-source="declareElementsData"
              :loading="loading"
              :pagination="false"
            >
              <!-- 类型 下拉选择 -->
              <template slot="typeValueSlot" slot-scope="text, record, index">
                <a-form-model-item prop="typeValue">
                  <j-dict-select-tag v-model="record.typeValue" placeholder="请选择类型" style="text-align:left;"
                    dictCode="sys_dict_item,item_text,item_text,dict_id='1536626597689970689'" :disabled="pushState">
                  </j-dict-select-tag>
                </a-form-model-item>
              </template>

              <!-- 描述 输入框-->
              <template slot="describeText" slot-scope="text, record, index">
                <a-form-model-item prop="describeText">
                  <a-input style="width:100%" type="text" v-model="record.describeText" :disabled="pushState"/>
                </a-form-model-item>
              </template>

              <template slot="state" slot-scope="text, record, index">
                <a-form-model-item prop="state">
                  <a-input style="width:100%" type="text" v-model="state" />
                </a-form-model-item>
              </template>

              <!-- 操作 -->
              <span slot="operationSlot" slot-scope="text, record, index"  >
                <a-popconfirm title="确定删除吗?" ok-text="是" cancel-text="否" @confirm="handleDelete(index)" v-if="!pushState">
                  <a href="javascript:void(0);"  style="color:red;">删除</a>
                </a-popconfirm>
                  <a   style="color:red;" v-if="pushState" @click="handleNotDelete">删除</a>          
              </span>
            </a-table>
          </a-form-model>
        </a-spin>
      </a-card>

      <!-- 页面底部保存取消 -->
      <div
        :style="{
          position: 'absolute',
          right: 0,
          bottom: 0,
          width: '100%',
          borderTop: '1px solid #e9e9e9',
          padding: '10px 16px',
          background: '#fff',
          textAlign: 'right',
          zIndex: 1
        }"
      >
        <a-popconfirm title="确定放弃？" @confirm="handleCancel" okText="确定" cancelText="取消">
          <a-button :style="{ marginRight: '8px' }">返回</a-button>
        </a-popconfirm>
        <a-button type="primary" @click="addSave">
          保存
        </a-button>
      </div>
    </a-modal>
  </div>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import JEllipsis from '@/components/jeecg/JEllipsis'
import moment from 'moment'
import { syDeclarationElementsAdd } from '@api/document/shipmentList'

export default {
  name: 'DeclareElementsModal', // 报关要素
  mixins: [JeecgListMixin],
  components: { JEllipsis, moment },

  data() {
    let ellipsis = (v, l = 20) => <j-ellipsis value={v} length={l} /> // 省略

    return {
      loading: false, // 表格加载
      pushState: false, //是否推送
      // 子表表头
      declareElementsColumns: [
        {
          title: '序号',
          width: 30,
          dataIndex: 'index',
          customRender: (text, record, index) => `${index + 1}`,
          className: 'replacecolor'
        },
        {
          title: '类型',
          dataIndex: 'typeValue',
          width: 250,
          scopedSlots: { customRender: 'typeValueSlot' },
          // customRender: t => ellipsis(t),
        },
        {
          title: '描述',
          dataIndex: 'describeText',
          width: 200,
          scopedSlots: { customRender: 'describeText' },

          className: 'replacecolor'
        },

        {
          title: '操作',
          dataIndex: 'operation',
          scopedSlots: { customRender: 'operationSlot' },
          width: 30,
          className: 'replacecolor'
        }
      ],
      declareElements: {}, // 主表信息
      declareElementsData: [], // 子表信息
      confirmLoading: false,
      declareElementsModVis: false
    }
  },
  // 接收父组件 方法
  props: {},

  created() {  },
  methods: {
    //英文品名
    convertUppercase(val){
      this.declareElements.englishProductName = val.toUpperCase()
    },
    // 増行
    handleAddColumn() {
      console.log('増行')
      const addrow = {
        index: '',
        typeValue: '',
        describeText: '',
        operation: ''
      }
      this.declareElementsData.push(addrow)
    },
    // 报关要素 保存
    addSave() {
    //表单验证
    this.$refs.form.validate(valid => {
      if (valid) { 
      var type = [],
          sta = true 
      this.declareElementsData.map(item => {
        type.push(item.typeValue)
       if(item.typeValue === '' ){
         sta = false
        } 
      })
      var typeSet = [...new Set(type)]
      //类型/描述为空时
      if(!sta){
        this.$message.error("类型不可为空！");
      } else if(typeSet.length !== type.length){  //类名重复时
        this.$message.error("类型不能重复选择！");
      } else {
        var newObj = {} // 新增主表
        newObj.id = this.declareElements.id
        newObj.declarationName = this.declareElements.declarationName //报关品名
        newObj.hsCode = this.declareElements.hsCode 
        newObj.englishProductName = this.declareElements.englishProductName //英文品名
        newObj.acSetNo = this.declareElements.acSetNo //账套
        newObj.inventoryName = this.declareElements.inventoryName //物料名称
        newObj.itemNumber = this.declareElements.itemNumber //款号
        newObj.syShippingids = this.declareElements.itemIds//发运明细子表ID
        // 新增子表
        newObj.syDeclarationElementsItemList = this.declareElementsData
        // newDeclareElement.documentNo = this.addShipDet.documentNo // 单据号
        syDeclarationElementsAdd(newObj).then(res => {
        if (res.success) {
          this.$message.success('新增成功')
          this.$emit('table')
          this.close()
        }else {
          this.$message.error(res.message)
        }
        })
       this.close()
       // 刷新
      }
      
      }
       })
    
    },
    handletypeValue(e) {
      console.log('类型下拉菜单click', e)
    },
    // 操作 删除
    handleDelete(index) {
      console.log('index', index)
      console.log('删除报关要素数据', index)
      if(this.declareElementsData.length === 1){
        this.$message.error("明细至少有一行！");
      }else {
        return this.declareElementsData.splice(index, 1)
      }
    },

    //已推送的情况下删除
    handleNotDelete(){
       this.$message.error("不可删除！");
    },

    close() {
      this.$emit('close-declare')
      this.declareElementsModVis = false
      this.declareElements = {}
      this.declareElementsData = []
      // console.log('清空')
    },
    handleCancel() {
      this.close()
    },
    handleTableChange() {
      console.log('??这是啥')
    }
  },

  computed: {}
}
</script>
<style lang="less" scoped>
@import '~@assets/less/common.less';
@import '~@assets/less/overwriter.less';
/deep/ .ant-table-thead > tr > th {
  text-align: center;
  // font-weight: 700;
}
/deep/ .ant-table-tbody {
  text-align: center;
}
// th.replacecolor {
// background-color:  #ccc;
// }
// 对话框里的card样式
/deep/ .ant-modal-content {
  background-color: #f0f2f5;
}
/deep/ .ant-modal-body {
  padding: 10px;
}
</style>
