<!-- 主营价格 供应商价格维护 -->
<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 {
  GetFacPricePageList,
  FacPriceAdd,
  FacPriceChange,
  FacPriceDelete,
  GetFactorySelectItem
} from '@/api/baseData'
import MyTable from '_c/my-table/my-table'
import MyForm from '@/view/base-manage/provider-maintenance/my-form'
import MyModalForm from '_c/my-modal-form/material-table-modal'
export default {
  name: 'material_table',
  components: {
    MyTable,
    MyForm,
    MyModalForm
  },
  props: {
    id: {
      type: String,
      default: '',
      require: true
    }
  },
  watch: {
    id () {
      this.getList()
    }
  },
  data () {
    return {
      factorySelect: [],
      show: false,
      updShow: false,
      data: [],
      deleteId: '',
      formType: '',
      formData: {},
      ruleValidate: {
        name123: [
          { required: true, message: '仓库名称不能为空', trigger: 'blur' }
        ]
      },
      formConfig: [
        // {
        //   type: 'select',
        //   key: 'facId',
        //   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: 150
        },
        {
          title: '物料名称',
          key: 'materialName',
          minWidth: 150
        },
        {
          title: '门幅',
          key: 'width',
          minWidth: 80
        },
        {
          title: '克重',
          key: 'unitWeight',
          minWidth: 80
        },
        {
          title: '单价',
          key: 'price',
          minWidth: 80
        },
        {
          title: '操作',
          key: 'action',
          align: 'center',
          width: 150,
          render: (h, params) => {
            return h('div', [
              h(
                'Button',
                {
                  props: {
                    type: 'primary',
                    size: 'small'
                  },
                  style: {
                    marginRight: '.5vw'
                  },
                  on: {
                    click: () => {
                      this.formData = { ...params.row }
                      this.formType = 'edit'
                      this.updShow = true
                    }
                  }
                },
                '调价'
              ),
              h(
                'Button',
                {
                  props: {
                    type: 'error',
                    size: 'small'
                  },
                  style: {},
                  on: {
                    click: () => {
                      this.$MyModal
                        .show({ text: '是否删除？' })
                        .$on('confirm', () => {
                          FacPriceDelete({
                            id: params.row.id
                          }).then(res => {
                            if (res.code === 0) {
                              this.$Message.info(res.msg)
                              this.getList()
                            }
                          })
                        })
                    }
                  }
                },
                '删除'
              )
            ])
          }
        }
      ],
      tableFilter: [
        {
          name: 'Input',
          value: 'key',
          placeholder: '请输入关键字'
        },
        {
          name: 'Button',
          type: 'primary',
          text: '查询',
          e: 'search'
        },
        {
          name: 'Button',
          type: 'primary',
          text: '添加',
          align: 'right',
          e: 'add'
        }
      ],
      params: {
        pageIndex: 1,
        pageSize: 20,
        total: 0
      }
    }
  },
  methods: {
    cfm (type, formData) {
      if (type === 'add') {
        FacPriceAdd({
          ...formData,
          facId: this.id,
          materialCode: formData.code
        }).then(res => {
          if (res.code === 0) {
            this.$Message.info(res.msg)
            this.show = false
            this.getList()
          }
        })
      } else if (type === 'edit') {
        FacPriceChange({
          ...formData,
          facId: this.id,
          factoryPriceId: formData.id
        }).then(res => {
          if (res.code === 0) {
            this.$Message.info(res.msg)
            this.show = false
            this.getList()
          }
        })
      }
    },
    // 检索条件事件处理
    eventHandle (option) {
      switch (option._evnet) {
        case 'search':
          this.params.key = option.key
          this.getList()
          break
        case 'add':
          this.formType = 'add'
          this.show = true
          break
        case 'back':
          this.$router.go(-1)
          break
      }
    },
    changePage (pageIndex) {
      this.params.pageIndex = pageIndex
      this.getList()
    },
    getList () {
      GetFacPricePageList({
        ...this.params,
        factoryId: this.id
      }).then(res => {
        if (res.code === 0) {
          this.data = res.result.list
          this.params.total = res.result.totalCount
        }
      })
    },
    getFactorySelectItem () {
      GetFactorySelectItem().then(res => {
        if (res.code === 0) {
          // this.factorySelect = res.result
          this.formConfig[0].list = res.result
        }
      })
    }
  },
  mounted () {
    this.getList()
    this.getFactorySelectItem()
  }
}
</script>

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