<template>
  <div class="mod-config">
    <el-form :inline="true" label-position="top" label-width="100px" style="margin-top: 0px;">
      <el-form-item :label="'BU:'">
        <el-select v-model="searchData.buNo" placeholder="请选择"  >
          <el-option label="全部" value=""></el-option>
          <el-option
            v-for = "i in buList"
            :key = "i.buNo"
            :label = "i.buDesc"
            :value = "i.buNo">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="'模版名称:'">
        <el-input v-model="searchData.name"  ></el-input>
      </el-form-item>
      <el-form-item :label="'模版类型:'">
        <el-select filterable v-model="searchData.type" clearable style="width: 140px;">
          <el-option :label="item" :value="item" v-for="(item,index) in types "
                     :key="index"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="' '" >
        <el-button @click="searchTable()" type="primary" style="margin-left: 2px;margin-top: 0px">{{'查询'}}</el-button>
        <el-button @click="addModelOpen()" type="primary" style="margin-left: 2px;margin-top: 0px">{{'新增'}}</el-button>
      </el-form-item>
    </el-form>

    <el-table
      @row-click="changeData"
      highlight-current-row
      :height="height"
      :data="dataList"
      ref="mainTable"
      border
      v-loading="dataListLoading"
      style="width: 100%;">
      <el-table-column
        header-align="center"
        align="center"
        width="100"
        fixed="left"
        label="操作">
        <template slot-scope="scope">
          <a type="text" size="small"  @click="updateModelOpen(scope.row)">编辑</a>
          <a type="text" size="small"  @click="deleteEcssTemplate(scope.row)">删除</a>
        </template>
      </el-table-column>
      <el-table-column
        v-for="(item,index) in columnList1" :key="index"
        :sortable="item.columnSortable"
        :prop="item.columnProp"
        :header-align="item.headerAlign"
        :show-overflow-tooltip="item.showOverflowTooltip"
        :align="item.align"
        :fixed="item.fixed==''?false:item.fixed"
        :min-width="item.columnWidth"
        :label="item.columnLabel">
        <template slot-scope="scope">
          <span v-if="!item.columnHidden"> {{scope.row[item.columnProp]}}</span>
          <span v-if="item.columnImage"><img :src="scope.row[item.columnProp]"
                                             style="width: 100px; height: 80px"/></span>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[20, 50, 100, 1000]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>

    <el-dialog title="维护模版信息" :close-on-click-modal="false" v-drag :visible.sync="addModelFlag" width="500px">
      <el-form  label-position="top"  style="margin-left: 2px;margin-top: -5px;">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item :label="'BU:'">
              <el-select v-model="addModel.buNo" @change="changeBu" placeholder="请选择" :disabled="addModel.addFlag!==0"  style="width: 100%">
                <el-option
                  v-for = "i in buList"
                  :key = "i.buNo"
                  :label = "i.buDesc"
                  :value = "i.buNo">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="''" >
              <template #label>
                <span slot="label" style="" class="big-label">
                  <a href="#" @click="openCustom()">客户</a>
                </span>
              </template>
              <el-input v-model="addModel.customName" disabled ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="'模版类型'" >
              <el-select filterable v-model="addModel.type" placeholder="请选择"  :disabled="addModel.addFlag!==0" style="width:100%;">
                <el-option :label="item" :value="item" v-for="(item,index) in types "
                           :key="index"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="'模版名称'" >
              <el-input v-model="addModel.name"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-show="addModel.type=='发票'">
            <el-form-item :label="''" >
              <el-checkbox v-model="addModel.hsCodeDesc">品名</el-checkbox>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-show="addModel.type=='发票'">
            <el-form-item :label="''" >
              <el-checkbox v-model="addModel.contractFlag">合同</el-checkbox>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="'品名类型'" >
              <el-radio v-model="addModel.hsCodeDescType" label="Y">中文</el-radio>
              <el-radio v-model="addModel.hsCodeDescType" label="N">英文</el-radio>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-show="addModel.type=='箱单'">
            <el-form-item :label="'序号类型'" >
              <el-radio v-model="addModel.itemNo" label="Y">序号</el-radio>
              <el-radio v-model="addModel.itemNo" label="N">栈板号</el-radio>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-show="addModel.type=='箱单'">
            <el-form-item :label="''" >
              <el-checkbox v-model="addModel.upc">UPC</el-checkbox>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-show="addModel.type=='箱单'">
            <el-form-item :label="''" >
              <el-checkbox v-model="addModel.so">SO</el-checkbox>
            </el-form-item>
          </el-col>
<!--          <el-col :span="12" v-show="addModel.type=='箱单'">
            <el-form-item :label="''" >
              <el-checkbox v-model="addModel.boxChange">显示箱数零头</el-checkbox>
            </el-form-item>
          </el-col>-->
          <el-col :span="12" v-show="addModel.type=='箱单' || addModel.type=='发票'">
            <el-form-item :label="''" >
              <el-checkbox v-model="addModel.goodsLabel">货物明细</el-checkbox>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-show="addModel.type=='箱单' || addModel.type=='发票'">
            <el-form-item :label="''" >
              <el-checkbox v-model="addModel.fscWeight">是否维护纯FSC纸重量损耗</el-checkbox>
            </el-form-item>
          </el-col>

          <el-col :span="12" v-show="addModel.type=='箱单'">
            <el-form-item :label="'Shipping Mark'" >
              <el-input v-model="addModel.shippingMark"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-show="addModel.type=='箱单'">
            <el-form-item :label="'栈板重量参数'" >
              <el-input  v-model="addModel.palletWeight"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24" v-show="addModel.type=='箱单' || addModel.type=='发票'">
            <el-form-item :label="'制造地'" >
              <el-input  v-model="addModel.origin"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-show="addModel.type=='箱单' || addModel.type=='发票'">
            <el-form-item :label="'RFID Base Material'" >
              <el-checkbox v-model="addModel.material"></el-checkbox>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-show="addModel.type=='箱单' || addModel.type=='发票'">
            <el-form-item :label="'HS Code'" >
              <el-input :disabled="!addModel.material" v-model="addModel.hsCode"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-show="addModel.type=='箱单' || addModel.type=='发票'">
            <el-form-item :label="'Non-reusable plastic packaging'" >
              <el-checkbox v-model="addModel.packaging"></el-checkbox>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-show="addModel.type=='箱单' || addModel.type=='发票'">
            <el-form-item :label="'plastic packaging'" >
              <el-input :disabled="!addModel.packaging" v-model="addModel.kgs"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="12" v-show="addModel.type=='出口货物委托书'">
            <el-form-item :label="'贸易方式'" >
              <el-input v-model="addModel.salesMethod"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-show="addModel.type=='出口货物委托书'">
            <el-form-item :label="'币制'" >
              <el-input  v-model="addModel.currency"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-show="addModel.type=='出口货物委托书'">
            <el-form-item :label="'货物产地'" >
              <el-input  v-model="addModel.madeArea"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-show="addModel.type=='出口货物委托书'">
            <el-form-item :label="'发货港'" >
              <el-input v-model="addModel.sendPort"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-show="addModel.type=='出口货物委托书'">
            <el-form-item :label="'唛头'" >
              <el-input v-model="addModel.voyage"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-show="addModel.type=='出口货物委托书'">
            <el-form-item :label="'提/送货日期'" >
              <el-input v-model="addModel.deliveryGoodsDate"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-show="addModel.type=='出口货物委托书'">
            <el-form-item :label="'船期'" >
              <el-input v-model="addModel.shippingDate"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24" v-show="addModel.type=='出口货物委托书'">
            <el-form-item :label="'发货人'" >
              <el-input type="textarea" :rows="4" v-model="addModel.shipper"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-footer :style="addModel.type=='出口货物委托书'?'height:40px;margin-top: 80px;text-align:center':'height:40px;margin-top: 10px;text-align:center'">
        <el-button type="primary" @click="saveEcssTemplate()">保存</el-button>
        <el-button type="primary" @click="addModelFlag=false">关闭</el-button>
      </el-footer>
    </el-dialog>
    <el-dialog title="客户" @close="closeCustomDialog" @open="openCustomDialog" :visible.sync="customFlag" width="559px" v-drag>
      <el-form inline="inline" label-position="top" :model="customData" style="margin-left: 7px;margin-top: -5px;">
        <el-form-item label="客户名称">
          <el-input v-model="customData.ccusname" clearable style="width: 200px"></el-input>
        </el-form-item>
        <el-form-item label=" ">
          <el-button type="primary" style="padding: 3px 12px" @click="searchCustomList()">查询</el-button>
        </el-form-item>
      </el-form>
      <el-table
        :height="400"
        :data="customList"
        stripe
        highlight-current-row
        border
        @row-dblclick="customRowDblclick"
        style="width: 100%;">
        <el-table-column
          prop="ccusname"
          header-align="left"
          align="left"
          label="客户名称">
        </el-table-column>
      </el-table>
      <el-footer style="height:40px;margin-top: 10px;text-align:center">
        <el-button @click="customFlag = false">关闭</el-button>
      </el-footer>
    </el-dialog>
    <!--列表的组件-->
    <Chooselist ref="baseList" @getBaseData="getBaseData"></Chooselist>
  </div>
</template>

<script>
  import Chooselist from '@/views/modules/common/Chooselist_eam'
  import {} from "@/api/sysLanguage.js"
  import {
    searchEcssTemplateData,
    saveEcssTemplateData,
    deleteEcssTemplate,
    searchCustomList
  }from "@/api/ecss/ecss.js"
  import {getBuList}from  '@/api/factory/site.js'
  export default {
    name: "null",
    components:{
      Chooselist,
    },
    data() {
      return {
        types: [ '发票', '箱单', '出口货物委托书'],
        pageIndex: 1,
        pageSize: 100,
        totalPage: 0,
        height: 200,
        buList:[],
        dataList:[],
        dataList2:[],
        dataListLoading: false,
        searchData: {
          page: 1,
          limit: 100,
          buNo:'',
          type:'',
          name:'',
          username:this.$store.state.user.name,
        },
        buttons:{
          search:'查询',
        },
        addModelFlag:false,
        addModel:{
          addFlag:0,
          site:'',
          buNo:'',
          customName:'',
          name:'',
          type:'',
          upc:'',
          so:'',
          fscWeight:'',
          origin:'',
          packaging:'',
          kgs:'',
          shippingMark:'',
          hsCode:'',
          palletWeight:'',
          material:'',
          hsCodeDesc:'',
          contractFlag:'',
          hsCodeDescType:'',
          goodsLabel:'',
          remark:'',
          boxChange:'',
          salesMethod :'EXW',
          currency:'USD',
          madeArea:'Shanghai, China',
          sendPort:'Shanghai, China',
          deliveryGoodsDate:'',
          voyage:'',
          shippingDate:'',
          shipper:'Checkpoint Commercial (Shanghai) Co., Ltd.   保点贸易（上海）有限公司\t\t\t\t\t\t\n' +
            'Room 1411, No. 31, Lane 2419, Hunan Road, Pudong New Area, Shanghai\t\t\t\t\t\t\n' +
            '电话/Tel: (86-21)38112888   传真/Fax: (86-21)38112990\t\t\t\t\t\t\n' +
            '上海市浦东新区沪南路2419弄31号1411室\t\t\t\t\t\t\n'
        },
        addDisabledFlag:true,
        activeName:'attribute',
        columnList1: [
          {
            userId: this.$store.state.user.name,
            functionId: 801007,
            serialNumber: '801007Table1BuDesc',
            tableId: "801007Table1",
            tableName: "EcssTemplate基础信息",
            columnProp: "buDesc",
            headerAlign: "center",
            align: "center",
            columnLabel: "BU",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 80
          },
          {
            userId: this.$store.state.user.name,
            functionId: 801003,
            serialNumber: '801007Table1EcssTemplateNo',
            tableId: "801007Table1",
            tableName: "EcssTemplate基础信息",
            columnProp: "customName",
            headerAlign: "center",
            align: "center",
            columnLabel: "客户名称",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 200
          },
          {
            userId: this.$store.state.user.name,
            functionId: 801003,
            serialNumber: '801007Table1EcssTemplateNo',
            tableId: "801007Table1",
            tableName: "EcssTemplate基础信息",
            columnProp: "name",
            headerAlign: "center",
            align: "center",
            columnLabel: "模版名称",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 160
          },
          {
            userId: this.$store.state.user.name,
            functionId: 801003,
            serialNumber: '801007Table1EcssTemplateType',
            tableId: "801007Table1",
            tableName: "EcssTemplate基础信息",
            columnProp: "type",
            headerAlign: "center",
            align: "center",
            columnLabel: "模版类型",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 100
          },
          {
            userId: this.$store.state.user.name,
            functionId: 801003,
            serialNumber: '801007Table1CreateBy',
            tableId: "801007Table1",
            tableName: "EcssTemplate基础信息",
            columnProp: "createBy",
            headerAlign: "center",
            align: "center",
            columnLabel: "创建人",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 100
          },
          {
            userId: this.$store.state.user.name,
            functionId: 801003,
            serialNumber: '801007Table1CreateDate',
            tableId: "801007Table1",
            tableName: "EcssTemplate基础信息",
            columnProp: "createDate",
            headerAlign: "center",
            align: "center",
            columnLabel: "创建日期",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 140
          },
          {
            userId: this.$store.state.user.name,
            functionId: 801003,
            serialNumber: '801007Table1UpdateBy',
            tableId: "801007Table1",
            tableName: "EcssTemplate基础信息",
            columnProp: "updateBy",
            headerAlign: "center",
            align: "left",
            columnLabel: "修改人",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 100
          },
          {
            userId: this.$store.state.user.name,
            functionId: 801003,
            serialNumber: '801007Table1UpdateDate',
            tableId: "801007Table1",
            tableName: "EcssTemplate基础信息",
            columnProp: "updateDate",
            headerAlign: "center",
            align: "center",
            columnLabel: "修改日期",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 100
          },
          {
            userId: this.$store.state.user.name,
            functionId: 801003,
            serialNumber: '801007Table1Remark',
            tableId: "801007Table1",
            tableName: "EcssTemplate基础信息",
            columnProp: "remark",
            headerAlign: "center",
            align: "left",
            columnLabel: "备注",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 300
          },
        ],
        currentRow:{},
        customFlag:false,
        customData:{},
        customList:[],
      }
    },
    mounted() {
      this.$nextTick(() => {
        this.height = (window.innerHeight - 200);
      })
    },
    methods: {
      // 获取基础数据列表S
      getBaseList(val, type) {
        this.tagNo = val
        this.$nextTick(() => {
          let strVal = ''
          if (val === 33) {
            strVal = this.addModel.codeNo
          }
          this.$refs.baseList.init(val, strVal)
        })
      },

      /* 列表方法的回调 */
      getBaseData(val) {
        if (this.tagNo === 33) {
          this.addModel.codeNo = val.Code_No
        }
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.searchTable()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.searchTable()
      },
      searchTable(){
        this.searchData.limit = this.pageSize
        this.searchData.page = this.pageIndex
        searchEcssTemplateData(this.searchData).then(({data}) => {
          //区分请求成功和失败的状况
          if (data && data.code == 0) {
            this.dataList = data.page.list
            this.pageIndex = data.page.currPage
            this.pageSize = data.page.pageSize
            this.totalPage = data.page.totalCount
            if(this.dataList.length>0){
              this.$refs.mainTable.setCurrentRow(this.dataList[0]);
              this.changeData(this.dataList[0])
            }else {
              this.changeData(null)
            }
          } else {
            this.dataList = [];
          }
        });
      },
      changeData(row){
        this.currentRow = JSON.parse(JSON.stringify(row));
        this.headerData=row;
        this.refreshCurrentTabTable ();
      },
      refreshCurrentTabTable(){
        if(this.currentRow===''||this.currentRow===null){
          this.currentRow={site:'',hsCode:'',buNo:'',recordType:'',codeNo:''}
        }
      },
      tabClick (tab, event) {
        // 刷新列表数据
        this.refreshCurrentTabTable()
      },

      getBu () {
        let tempData = {
          username: this.$store.state.user.name,
        }
        getBuList(tempData).then(({data}) => {
          if (data.code === 0) {
            this.buList = data.row2
          }
        })
      },
      addModelOpen(){
        this.addDisabledFlag=true
        this.addModel={
          addFlag:0,
          site:'',
          buNo:'',
          name:'',
          type:'',
          upc:'',
          so:'',
          origin:'',
          packaging:'',
          fscWeight:'',
          kgs:'',
          shippingMark:'',
          hsCode:'',
          palletWeight:'',
          material:'',
          hsCodeDesc:'',
          contractFlag:'',
          hsCodeDescType:'',
          goodsLabel:true,
          remark:'',
          boxChange:'',
          salesMethod :'EXW',
          currency:'USD',
          madeArea:'Shanghai, China',
          sendPort:'Shanghai, China',
          deliveryGoodsDate:'',
          voyage:'',
          shippingDate:'',
          shipper:'Checkpoint Commercial (Shanghai) Co., Ltd.   保点贸易（上海）有限公司\t\t\t\t\t\t\n' +
            'Room 1411, No. 31, Lane 2419, Hunan Road, Pudong New Area, Shanghai\t\t\t\t\t\t\n' +
            '电话/Tel: (86-21)38112888   传真/Fax: (86-21)38112990\t\t\t\t\t\t\n' +
            '上海市浦东新区沪南路2419弄31号1411室\t\t\t\t\t\t\n'
        }

        this.addModelFlag=true
      },
      updateModelOpen(row){
        this.addDisabledFlag=false
        this.addModel={
          addFlag:1,
          site:row.site,
          buNo:row.buNo,
          name:row.name,
          nameNative:row.name,
          type:row.type,
          upc:row.upc,
          so:row.so,
          origin:row.origin,
          packaging:row.packaging,
          itemNo:row.itemNo,
          fscWeight:row.fscWeight,
          kgs:row.kgs,
          shippingMark:row.shippingMark,
          hsCode:row.hsCode,
          palletWeight:row.palletWeight,
          material:row.material,
          hsCodeDesc:row.hsCodeDesc,
          contractFlag:row.contractFlag,
          hsCodeDescType:row.hsCodeDescType,
          goodsLabel:row.goodsLabel,
          remark:row.remark,
          boxChange:row.boxChange,
          customName: row.customName,
          salesMethod :row.salesMethod,
          currency:row.currency,
          madeArea:row.madeArea,
          sendPort:row.sendPort,
          deliveryGoodsDate:row.deliveryGoodsDate,
          voyage:row.voyage,
          shippingDate:row.shippingDate,
          shipper:row.shipper
        }
        this.addModelFlag=true
      },
      deleteEcssTemplate(row){
        this.$confirm('确认删除？', '提示').then(() => {
          deleteEcssTemplate(row).then(({data}) => {
            if (data && data.code === 0) {
              this.searchTable()
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {}
              })
            } else {
              this.$alert(data.msg, '错误', {
                confirmButtonText: '确定'
              })
            }
          })
        })
      },
      saveEcssTemplate(){
        if(this.addModel.buNo==null||this.addModel.buNo===''){
          this.$alert('请选择BU！', '错误', {
            confirmButtonText: '确定'
          })
          return false
        }
        if(this.addModel.type==null||this.addModel.type===''){
          this.$alert('请选择模版类型！', '错误', {
            confirmButtonText: '确定'
          })
          return false
        }
        if(this.addModel.name==null||this.addModel.name===''){
          this.$alert('请输入模版名称！', '错误', {
            confirmButtonText: '确定'
          })
          return false
        }
        saveEcssTemplateData(this.addModel).then(({data}) => {
          if (data && data.code === 0) {
            this.searchTable()
            this.addModelFlag = false
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {}
            })
          } else {
            this.$alert(data.msg, '错误', {
              confirmButtonText: '确定'
            })
          }
        })
      },
      openCustom () {
        this.customFlag = true;
      },

      openCustomDialog () {
        //请求
        this.searchCustomList();
      },
      closeCustomDialog () {
        this.customList = []
        this.customFlag = false
      },
      searchCustomList () {
        searchCustomList(this.customData).then(({data}) => {
          if (data && data.code === 0){
            this.customList = data.rows;
          }
        })
      },
      customRowDblclick (row) {
        this.addModel.customName=row.ccusname
        this.customFlag = false
      },
      changeBu(){
        this.addModel.kgs=this.addModel.buNo==='03-RFID'?'4.5':this.addModel.buNo==='01-Label'?'2.5':''
      }
    },
    activated() {
      this.searchTable()
    },
    created() {
      this.getBu ()
    }
  }
</script>

<style scoped>

</style>
