<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="500px">
        <el-form ref="form" :model="form" :rules="rules" size="mini" label-width="80px">
          <el-form-item label="编码" prop="sales_code">
            <el-input v-model="form.sales_code" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="名称" prop="sales_name">
            <el-input v-model="form.sales_name" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="区域" prop="area">
            <el-select
              v-model="form.area"
              clearable
              filterable
              size="mini"
              class="filter-item"
              style="width: 370px;"
            >
              <el-option
                v-for="item in areaList"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="是否启用" prop="is_active">
            <el-radio v-model="form.is_active" label="0">否</el-radio>
            <el-radio v-model="form.is_active" label="1">是</el-radio>
          </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="sales_code" label="编码" />
        <el-table-column prop="sales_name" label="名称" />
        <el-table-column prop="area" label="区域" width="135" />
        <el-table-column prop="is_active" label="启用 ">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.is_active"
              active-color="#409EFF"
              inactive-color="#F56C6C"
              active-value="1"
              inactive-value="0"
              @change="changeEnabled(scope.row, scope.row.is_active)"
            />
          </template>
        </el-table-column>
        <el-table-column v-permission="['admin','mdPbMeasureunit:edit','mdPbMeasureunit:del']" fixed="right" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudSales from '@/views/wms/basedata/master/sales/sales'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
const defaultForm = { id: null, sales_code: null, sales_name: null, area: null, is_active: null }
export default {
  dicts: ['is_used'],
  name: 'Sales',
  // eslint-disable-next-line vue/no-unused-components
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '业务员',
      url: 'api/sales',
      optShow: {
        add: true,
        reset: true
      },
      idField: 'id',
      sort: 'sales_code,desc',
      crudMethod: { ...crudSales }})
  },
  data() {
    return {
      permission: {
      },
      areaList: [
        {
          'value': '华东区域',
          'text': '华东区域'
        },
        {
          'value': '华南区域',
          'text': '华南区域'
        },
        {
          'value': '华中区域',
          'text': '华中区域'
        },
        {
          'value': '中心区域',
          'text': '中心区域'
        }
      ],
      rules: {
        sales_code: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        sales_name: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        is_active: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
      }}
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    // 改变状态
    changeEnabled(data, val) {
      this.$confirm('此操作将 "' + this.dict.label.is_used[val] + '" ' + data.sales_code + ', 是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        crudSales.edit(data).then(res => {
          this.crud.notify(this.dict.label.is_used[val] + '成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
        }).catch(() => {
          if (data.is_active === '0') {
            data.is_active = '1'
            return
          }
          if (data.is_active === '1') {
            data.is_active = '0'
          }
        })
      }).catch(() => {
        if (data.is_active === '0') {
          data.is_active = '1'
          return
        }
        if (data.is_active === '1') {
          data.is_active = '0'
        }
      })
    },
    handleChange(value) {
      console.log(value)
    }
  }
}
</script>

<style scoped>

</style>
