<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <el-input
        v-model="query.search"
        clearable
        style="width: 300px"
        size="mini"
        placeholder="输入供应商编码或名称"
        prefix-icon="el-icon-search"
        class="filter-item"
      />
      <rrOperation />
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="1200px"
      >
        <el-form ref="form" :model="form" :rules="rules" size="mini" label-width="130px">
          <el-row :gutter="20">
            <el-col :span="8">
              <el-form-item label="供应商编码" prop="supp_code">
                <el-input v-model.trim="form.supp_code" style="width: 200px;" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="供应商名称 " prop="supp_name">
                <el-input v-model.trim="form.supp_name" style="width: 200px;" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="法人代表">
                <el-input v-model.trim="form.jurid_name" style="width: 200px;" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="8">
              <el-form-item label="税务登记号">
                <el-input v-model.trim="form.tax_no" style="width: 200px;" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="工商注册号">
                <el-input v-model.trim="form.register_no" style="width: 200px;" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="经营许可证号">
                <el-input v-model.trim="form.manage_lice_no" style="width: 200px;" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="8">
              <el-form-item label="营业执照">
                <el-input v-model.trim="form.busi_char_name" style="width: 200px;" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="条码打印起始值">
                <el-input v-model.trim="form.barcode_print" style="width: 200px;" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="邮政编码">
                <el-input v-model.trim="form.zip_code" style="width: 200px;" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="8">
              <el-form-item label="公司电话">
                <el-input v-model.trim="form.corp_tele_no" style="width: 200px;" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="公司地址">
                <el-input v-model.trim="form.corp_address" style="width: 200px;" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="是否启用" prop="is_used">
                <el-radio v-model="form.is_used" label="0">否</el-radio>
                <el-radio v-model="form.is_used" label="1">是</el-radio>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="8">
              <el-form-item label="外部标识">
                <el-input v-model.trim="form.ext_id" style="width: 200px;" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="资金计划履约天数">
                <el-input v-model.trim="form.honour_days" style="width: 200px;" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="基础分类" prop="class_id">
                <treeselect
                  v-model="form.class_id"
                  :load-options="loadClass"
                  :options="classes"
                  style="width: 200px;"
                  placeholder="请选择"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="备注">
            <el-input v-model.trim="form.remark" style="width: 600px;" type="textarea" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="mini"
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column prop="supp_code" label="供应商编码" />
        <el-table-column prop="supp_name" label="供应商名称 " />
        <el-table-column prop="corp_address" label="公司地址" />
        <el-table-column prop="corp_tele_no" label="公司电话" />
        <el-table-column prop="jurid_name" label="法人代表" />
        <el-table-column prop="update_optname" label="修改者" />
        <el-table-column prop="update_time" label="修改时间" width="150" />
        <el-table-column label="启用" align="center" prop="is_used">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.is_used"
              active-color="#409EFF"
              inactive-color="#F56C6C"
              active-value="1"
              inactive-value="0"
              @change="changeEnabled(scope.row, scope.row.is_used)"
            />
          </template>
        </el-table-column>
        <el-table-column
          v-permission="['admin','Supplierbase:edit','Supplierbase:del']"
          label="操作"
          width="150px"
          lign="center"
        >
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudSupplierbase from '@/api/wms/basedata/master/supplierbase'
import CRUD, { crud, form, header, presenter } from '@crud/crud'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import rrOperation from '@crud/RR.operation'
import crudClassstandard from '@/api/wms/basedata/master/classstandard'
import Treeselect, { LOAD_CHILDREN_OPTIONS } from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

const defaultForm = {
  supp_id: null,
  supp_code: null,
  supp_name: null,
  jurid_name: null,
  tax_no: null,
  register_no: null,
  manage_lice_no: null,
  busi_char_name: null,
  area_id: null,
  zip_code: null,
  corp_tele_no: null,
  corp_address: null,
  create_id: null,
  create_name: null,
  create_time: null,
  update_optid: null,
  update_optname: null,
  update_time: null,
  is_used_time: null,
  is_used: '1',
  is_delete: null,
  ext_id: null,
  barcode_print: null,
  class_code: null,
  remark: null,
  honour_days: null
}
export default {
  name: 'Supplierbase',
  components: { pagination, crudOperation, rrOperation, udOperation, Treeselect },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  // 数据字典
  dicts: ['is_used'],
  cruds() {
    return CRUD({
      title: '供应商',
      url: 'api/supplierbase',
      optShow: {
        add: true,
        reset: true
      },
      idField: 'supp_id',
      sort: 'supp_id,desc',
      crudMethod: { ...crudSupplierbase }
    })
  },
  data() {
    return {
      permission: {},
      classes: [],
      rules: {
        supp_code: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        supp_name: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        create_id: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        create_name: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        create_time: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        is_used: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        is_delete: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        class_id: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    [CRUD.HOOK.afterToCU](crud, form) {
      if (form.material_type_id != null) {
        this.getSubTypes(form.material_type_id)
      } else {
        this.getClass()
      }
    },
    getSubTypes(id) {
      crudClassstandard.getClassSuperior(id).then(res => {
        const date = res.content
        this.buildClass(date)
        this.classes = date
      })
    },
    getClass() {
      crudClassstandard.getClass({ enabled: true, 'base_data_type': '04' }).then(res => {
        this.classes = res.content.map(function(obj) {
          if (obj.hasChildren) {
            obj.children = null
          }
          return obj
        })
      })
    },
    // 获取弹窗内部门数据
    loadClass({ action, parentNode, callback }) {
      if (action === LOAD_CHILDREN_OPTIONS) {
        crudClassstandard.getClass({ pid: parentNode.id }).then(res => {
          parentNode.children = res.content.map(function(obj) {
            if (obj.hasChildren) {
              obj.children = null
            }
            return obj
          })
          setTimeout(() => {
            callback()
          }, 100)
        })
      }
    },
    // 改变状态
    changeEnabled(data, val) {
      this.$confirm('此操作将 "' + this.dict.label.is_used[val] + '" ' + data.supp_code + ', 是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        crudSupplierbase.edit(data).then(res => {
          this.crud.notify(this.dict.label.is_used[val] + '成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
        }).catch(() => {
          if (data.is_used === '0') {
            data.is_used = '1'
            return
          }
          if (data.is_used === '1') {
            data.is_used = '0'
          }
        })
      }).catch(() => {
        if (data.is_used === '0') {
          data.is_used = '1'
          return
        }
        if (data.is_used === '1') {
          data.is_used = '0'
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
