<!-- 供应商价格维护 -->
<template>
  <div class="container">
    <my-table
      :data="data"
      :columns="columns"
      :table-filter="tableFilter"
      :table-page="params"
      @event-handle="eventHandle"
      @on-change="changePage"
    ></my-table>
    <my-modal-form
      v-model="show"
      title="添加供应商面料"
      :config="formConfig"
      :rule-validate="ruleValidate"
      :data="formData"
      :type="formType"
      @confirm="cfm"
    ></my-modal-form>
    <!--    <my-modal-form v-model="updShow" title="价格调整单" :config="updFormConfig" :rule-validate="ruleValidate" :data="formData" :type="formType" @confirm="cfm"></my-modal-form>-->
    <my-form v-model="updShow" :data="formData"></my-form>
  </div>
</template>

<script>

import MyTable from '_c/my-table/my-table'
import MyModalForm from '_c/my-modal-form/my-modal-form'
import MyForm from './my-form'
import { exportExcel } from '@/libs/util'
export default {
  name: 'provider_price_maintenance',
  components: {
    MyTable,
    MyModalForm,
    MyForm
  },
  data () {
    return {
      factorySelect: [],
      show: false,
      updShow: false,
      data: [],
      deleteId: '',
      formType: '',
      formData: {},
      ruleValidate: {
        name123: [
          { required: true, message: '仓库名称不能为空', trigger: 'blur' }
        ]
      },
      formConfig: [
        {
          type: 'select',
          key: 'supplierId',
          label: '供应商',
          list: []
        },
        {
          type: 'materialBtn',
          label: '选择物料',
          multiple: true
        },
        {
          type: 'input',
          label: '物料编码',
          key: 'code',
          disabled: true,
          span: 12
        },
        {
          type: 'input',
          label: '物料名称',
          key: 'name',
          disabled: true,
          span: 12
        },
        {
          type: 'input',
          label: '物料规格',
          key: 'spec',
          disabled: true
        },
        {
          type: 'input',
          label: '门幅',
          key: 'width',
          disabled: true,
          span: 12
        },
        {
          type: 'input',
          label: '克重',
          key: 'unitWeight',
          disabled: true,
          span: 12
        },
        {
          type: 'input',
          label: '采购周期',
          key: 'safetyWarnDay',
          disabled: true,
          span: 12
        },
        {
          type: 'input',
          label: '采购单价',
          key: 'price',
          placeholder: '请输入采购单价',
          span: 12
        }
      ],
      updFormConfig: [
        {
          type: 'input',
          label: '申请人',
          key: 'applyUser',
          disabled: true,
          span: 12
        },
        {
          type: 'input',
          label: '申请部门',
          key: 'applyDepartment',
          disabled: true,
          span: 12
        },
        {
          type: 'input',
          label: '申请时间',
          key: 'applyDateTime',
          disabled: true,
          span: 12
        },
        {
          type: 'select',
          key: 'facId',
          label: '供应商',
          list: [],
          disabled: true
        },
        {
          type: 'input',
          label: '物料编码',
          key: 'code',
          disabled: true,
          span: 12
        },
        {
          type: 'input',
          label: '物料名称',
          key: 'name',
          disabled: true,
          span: 12
        },
        {
          type: 'input',
          label: '原价格',
          key: 'price',
          placeholder: '请输入采购单价',
          disabled: true,
          span: 12
        },
        {
          type: 'input',
          label: '新价格',
          key: 'newPrice',
          placeholder: '请输入采购单价',
          span: 12
        },
        {
          type: 'input',
          comType: 'textarea',
          label: '调整原因',
          key: 'remark',
          placeholder: '请输入调整原因'
        }
      ],
      columns: [
        {
          title: '序号',
          type: 'index',
          width: 60,
          align: 'center'
        },
        {
          title: '物料编码',
          key: 'materialCode',
          minWidth: 200
        },
        {
          title: '物料名称',
          key: 'materialName',
          minWidth: 200
        },
        {
          title: '门幅',
          key: 'width',
          minWidth: 100
        },
        {
          title: '克重',
          key: 'unitWeight',
          minWidth: 100
        },
        {
          title: '供应商',
          key: 'supplierName',
          minWidth: 200
        },
        {
          title: '单价',
          key: 'price',
          minWidth: 150
        },
        {
          title: '采购周期',
          key: 'safetyWarnDay',
          minWidth: 150
        },
        {
          title: '操作',
          key: 'action',
          align: 'center',
          width: '150px',
          fixed: 'right',
          render: (h, params) => {
            return h('div', [
              h(
                'Icon',
                {
                  props: {
                    custom: 'iconfont icon-edit',
                    size: '20',
                    color: '#087BE1'
                  },
                  style: {
                    marginRight: '20px'
                  },
                  on: {
                    click: () => {
                      this.formData = { ...params.row }
                      this.updShow = true
                    }
                  }
                },
                '调价'
              ),
              h(
                'Icon',
                {
                  props: {
                    custom: 'iconfont icon-delete',
                    size: '20',
                    color: 'red'
                  },
                  on: {
                    click: () => {
                      this.$MyModal
                        .show({ text: '是否删除？' })
                        .$on('confirm', () => {
                          this.$post('/api/storage-restructure/supplierPrice/delete',{
                            id:params.row.id
                          }).then(res=>{
                            if(res.code == 200){
                              this.$Message.info(res.msg)
                              this.getList()
                            }
                          })
                        })
                    }
                  }
                },
                '删除'
              )
            ])
          }
        }
      ],
      tableFilter: [
        {
          name: 'Input',
          value: 'keyword',
          placeholder: '请输入关键字'
        },
        {
          name: 'Button',
          type: 'primary',
          text: '查询',
          e: 'search'
        },
        {
          name: 'Button',
          type: 'primary',
          text: '导出Excel',
          e: 'export'
        },
        {
          name: 'Button',
          type: 'primary',
          text: '返回上级',
          align: 'right',
          e: 'back'
        },
        {
          name: 'Button',
          type: 'primary',
          text: '添加',
          align: 'right',
          e: 'add'
        }
      ],
      params: {
        pageNum: 1,
        pageSize: 20,
        total: 0
      }
    }
  },
  methods: {
    cfm (type, formData) {
      console.log(formData)
      if (type === 'add') {
        this.$post('/api/storage-restructure/supplierPrice/add',{
          supplierId:formData.supplierId,
          materialId:formData.id,
          price:formData.price
        }).then(res=>{
          if(res.code == 200){
            this.show = false
            this.$Message.info(res.msg)
            this.getList()
          }
        })
        
      } else if (type === 'edit') {
        this.$post('/api/storage-restructure/supplierPrice/updateFlowCreate',{
          id:formData.factoryPriceId,
          remarks:formData.remark,
          price:formData.price
        }).then(res=>{
          if(res.code == 200){
            this.updShow = false
            this.$Message.info(res.msg)
            this.getList()
          }
        })
        
      }
    },
    // 检索条件事件处理
    eventHandle (option) {
      switch (option._evnet) {
        case 'search':
          this.params.pageNum = 1
          this.params.key = option.key
          this.getList()
          break
        case 'add':
          this.formType = 'add'
          this.show = true
          break
        case 'back':
          this.$router.go(-1)
          break
        case 'export':
          this.$post('/api/storage-restructure/supplierPrice/page',{
            pageNum:1,
            pageSize:500,
          }).then(res=>{
            if(res.code == 200){
              let data = res.data.records
              if (data.length < 1) return this.$Message.error('数据为空！')
              exportExcel(this.columns, data, '价格维护')
            }
          })
          
          break
      }
    },
    changePage (pageNum) {
      this.params.pageNum = pageNum
      this.getList()
    },
    getList () {
      const v = this
      v.$post('/api/storage-restructure/supplierPrice/page',this.params).then(res=>{
        
        this.data = res.data.records
        this.params.total = res.data.pages

        console.log(res)
      })
      // GetFacPricePageList(this.params).then(res => {
      //   if (res.code === 0) {
      //     this.data = res.result.list
      //     this.params.total = res.result.totalCount
      //   }
      // })
    },
    getFactorySelectItem () {
      this.$post('/api/storage-restructure/supplier/page',{
        pageNum: 1,
        pageSize: 500,
      }).then(res=>{
        
        this.formConfig[0].list = res.data.records.map(item => {
          return {
            ...item,
            label: item.supplierName,
            value:item.id
          }
        })
        console.log(res)
      })
      
      // GetFactorySelectItem().then(res => {
      //   if (res.code === 0) {
      //     // this.factorySelect = res.result
      //     this.formConfig[0].list = res.result.map(item => {
      //       return {
      //         ...item,
      //         label: item.text
      //       }
      //     })
      //   }
      // })
    }
  },
  mounted () {
    this.getList()
    this.getFactorySelectItem()
  }
}
</script>

<style lang="less" scoped>
.container {
  height: 100%;
}
</style>
