<template>
  <div class="app-container">
    <div class="filter-container">
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item>
          <el-button
            icon="el-icon-search"
            size="small"
            type="primary"
            @click="AddTolerance"
          >添加容差</el-button>
        </el-form-item>
      </el-form>

      <el-table :data="sData">
        <el-table-column label="重量类型" prop="UseCustomerWeight" width="120">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.UseCustomerWeight" type="success">客户重量</el-tag>
            <el-tag v-if="!scope.row.UseCustomerWeight">导入重量</el-tag>
          </template>
        </el-table-column>

        <el-table-column label="开始重量" prop="StartWeight" />
        <el-table-column label="结束重量" prop="EndWeight" />
        <el-table-column label="容差" prop="Tolerance" />
        <el-table-column label="客户名称" prop="CustomerName" />
        <el-table-column label="是否有临时容差">
          <template slot-scope="scope">
            <el-tag v-if="isTempTolerance(scope.row)==true" type="success">是</el-tag>
            <el-tag v-else type="danger">无</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否启用" prop="IsEnable" width="120">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.IsEnable" type="success">启用</el-tag>
            <el-tag v-if="!scope.row.IsEnable" type="danger">停用</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="300px">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="EditTolerance(scope.row)">
              编辑
            </el-button>
            <el-button type="danger" size="mini" @click="deleteTolerance(scope.row)">
              删除
            </el-button>
            <el-button
              type="warning"
              size="mini"
              @click="openEditTempTolerance(scope.row)"
            >
              临时调整容差
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-dialog title="添加容差" :visible.sync="dialogAddTolerance">
        <el-form :model="createDto" label-width="80px">
          <el-form-item label="客户名称">
            <el-select v-model="createDto.CustomerId" placeholder="请选择" filterable>
              <el-option
                v-for="item in Customers"
                :key="item.Id"
                :label="item.CompanyName"
                :value="item.Id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="开始重量">
            <el-input v-model="createDto.StartWeight" autocomplete="off" />
          </el-form-item>
          <el-form-item label="结束重量">
            <el-input v-model="createDto.EndWeight" autocomplete="off" />
          </el-form-item>
          <el-form-item label="容差">
            <el-input v-model="createDto.Tolerance" autocomplete="off" />
          </el-form-item>

          <el-switch
            v-model="createDto.UseCustomerWeight"
            active-text="使用客户重量"
            inactive-text="使用导入重量"
            active-value="True"
            inactive-value="False"
          />
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogAddTolerance = false">取 消</el-button>
          <el-button type="primary" @click="postAddTolerance">确 定</el-button>
        </div>
      </el-dialog>

      <el-dialog title="编辑容差" :visible.sync="dialogEditTolerance">
        <el-form :model="editDto" label-width="80px">
          <el-form-item label="客户名称">
            <el-select v-model="editDto.CustomerId" placeholder="请选择" filterable>
              <el-option
                v-for="item in Customers"
                :key="item.Id"
                :label="item.CompanyName"
                :value="item.Id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="开始重量">
            <el-input v-model="editDto.StartWeight" autocomplete="off" />
          </el-form-item>
          <el-form-item label="结束重量">
            <el-input v-model="editDto.EndWeight" autocomplete="off" />
          </el-form-item>
          <el-form-item label="容差">
            <el-input v-model="editDto.Tolerance" autocomplete="off" />
          </el-form-item>

          <el-form-item label="类型">
            <el-switch
              v-model="editDto.UseCustomerWeight"
              active-text="使用客户重量"
              inactive-text="使用导入重量"
            />
          </el-form-item>
          <el-form-item label="是否启用">
            <el-switch
              v-model="editDto.IsEnable"
              active-text="启用"
              inactive-text="停用"
            />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogEditTolerance = false">取 消</el-button>
          <el-button type="primary" @click="postEditTolerance">确 定</el-button>
        </div>
      </el-dialog>

      <el-dialog title="确定删除容差" :visible.sync="dialogDeleteTolerance">
        是否确定删除容差
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogDeleteTolerance = false">取 消</el-button>
          <el-button type="primary" @click="postDeleteTolerance">确 定</el-button>
        </div>
      </el-dialog>

      <el-dialog
        title="临时调整容差范围"
        :visible.sync="showEditTempTolerance"
        width="640px"
      >
        <div class="margin-bottom-lg">
          <el-alert
            title="临时调整容差范围，仅当天有效"
            type="warning"
            :closable="false"
          />
        </div>
        <div>
          <el-form>
            <el-form-item label="客户">
              <span>{{ toleranceCustomerName }}</span>
            </el-form-item>
            <el-form-item label="容差范围">
              <el-input-number v-model="tempTolerance" />
            </el-form-item>
          </el-form>
        </div>
        <div slot="footer" class="dialog-footer">
          <el-button @click="closeTempTolerance">取 消</el-button>
          <el-button type="primary" @click="editTempTolerance">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { _getCustomers, _getChannel } from '@/api/baseInfo'
import { MessageBox, Message } from 'element-ui'
import {
  _GetTolerance,
  _AddTolerance,
  _EditTolerance,
  _DeleteTolerance,
  _setTempTolerance
} from '@/api/toleranceConfig'

export default {
  name: 'ToleranceConfig',
  data() {
    return {
      showEditTempTolerance: false,
      tempTolerance: 0,
      tempToleranceCustomerId: null,
      toleranceCustomerName: null,
      sData: [],
      createDto: {
        StartWeight: 1,
        EndWeight: 1000,
        Tolerance: 50
      },
      dialogAddTolerance: false,
      editDto: {
        StartWeight: 1,
        EndWeight: 1000,
        Tolerance: 50,
        Id: 0,
        CustomerId: 0,
        UseCustomerWeight: 0,
        IsEnable: 0
      },
      Customers: [{ CompanyName: '澳鹏', Id: 7 }],
      dialogEditTolerance: false,
      deleteDto: {},
      dialogDeleteTolerance: false
    }
  },
  created() {
    _getCustomers().then((response) => {
      this.Customers = response
      this.search()
    })
  },
  methods: {
    isTempTolerance(item) {
      if (item.TempToleranceExpired) {
        const expired = new Date(item.TempToleranceExpired)
        return expired > new Date()
      } return false
    },
    closeTempTolerance() {
      this.tempToleranceCustomerId = null
      this.toleranceCustomerName = null
      this.showEditTempTolerance = false
      this.tempTolerance = null
    },
    openEditTempTolerance(item) {
      this.tempToleranceCustomerId = item.CustomerId
      this.toleranceCustomerName = item.CustomerName

      if (item.TempToleranceExpired) {
        const expired = new Date(item.TempToleranceExpired)
        if (expired > new Date()) {
          this.tempTolerance = item.TempTolerance
        }
      }
      this.showEditTempTolerance = true
    },
    editTempTolerance() {
      const customerId = this.tempToleranceCustomerId
      const tolerance = this.tempTolerance
      const data = { customerId, tolerance }
      _setTempTolerance(data).then((res) => {
        console.log(res)
        if (res.Success === true) {
          this.closeTempTolerance()
          this.$message({ type: 'success', message: '设置成功' })
        } else {
          const msg = res.Message || '设置失败'
          this.$message({ type: 'error', message: msg })
        }
      })
    },
    search() {
      _GetTolerance().then((response) => {
        for (var i = 0; i < response.length; i++) {
          for (var j = 0; j < this.Customers.length; j++) {
            if (this.Customers[j].Id == response[i].CustomerId) {
              response[i].CustomerName = this.Customers[j].CompanyName
            }
          }
        }
        this.sData = response
      })
    },
    postDeleteTolerance() {
      _DeleteTolerance(this.deleteDto).then((response) => {
        this.dialogDeleteTolerance = false
        this.search()
      })
    },
    deleteTolerance(row) {
      this.deleteDto = row
      this.dialogDeleteTolerance = true
    },
    AddTolerance() {
      this.dialogAddTolerance = true
    },
    checkTolerance(dto) {
      if (dto.StartWeight <= 0) {
        Message({
          message: '开始重量不能小于等0',
          type: 'error',
          duration: 5 * 1000
        })
        return false
      }
      if (dto.EndWeight <= 0) {
        Message({
          message: '结束重量不能小于等0',
          type: 'error',
          duration: 5 * 1000
        })
        return false
      }
      if (dto.Tolerance <= 0) {
        Message({
          message: '容差不能小于等0',
          type: 'error',
          duration: 5 * 1000
        })
        return false
      }

      if (dto.StartWeight > dto.EndWeight) {
        Message({
          message: '开始重量不能大于结束重量',
          type: 'error',
          duration: 5 * 1000
        })
        return false
      }
      return true
    },
    postAddTolerance() {
      if (!this.checkTolerance(this.createDto)) {
        return
      }
      _AddTolerance(this.createDto).then((response) => {
        this.dialogAddTolerance = false
        this.search()
      })
    },
    EditTolerance(row) {
      debugger
      this.editDto.Id = row.Id
      this.editDto.StartWeight = row.StartWeight
      this.editDto.EndWeight = row.EndWeight
      this.editDto.Tolerance = row.Tolerance
      this.editDto.CustomerId = row.CustomerId
      this.editDto.UseCustomerWeight = row.UseCustomerWeight
      this.editDto.IsEnable = row.IsEnable
      this.dialogEditTolerance = true
    },
    postEditTolerance() {
      if (!this.checkTolerance(this.editDto)) {
        return
      }

      _EditTolerance(this.editDto).then((response) => {
        this.dialogEditTolerance = false
        this.search()
      })
    }
  }
}
</script>
