<template>
  <el-dialog
    :title="title"
    :visible.sync="showModal"
    width="1100px"
    append-to-body
    @close="cancel">
    <el-form :model="form" ref="forms" label-position="left" :rules="rules"  label-width="100px">
      <el-divider content-position="left">基本信息</el-divider>
      <el-row :gutter="16">
        <el-col :span="8">
          <el-form-item label="供应商名称">
            <el-input v-model="form.supplierName" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="评价周期" prop="pingjiazhouqi">
            <el-radio-group v-model="form.pingjiazhouqi">
              <el-radio :label="1">季度</el-radio>
              <el-radio :label="2">年度</el-radio>
              <el-radio :label="3">专项</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="评价日期" prop="evaDate">
            <el-date-picker style="width: 100%;" v-model="form.evaDate" value-format="YYYY-MM-DD"></el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-divider content-position="left">生猪质量（总分40分）</el-divider>
      <el-row :gutter="16">
        <el-col :span="18">
          <el-form-item label="健康状况">
            <el-checkbox-group disabled>
              <el-checkbox :label="5">出场无检疫合格证明：扣5分；</el-checkbox>
              <el-checkbox :label="3">宰前异常（咳嗽、腹泻等）：扣3分</el-checkbox>
            </el-checkbox-group>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="扣分" label-width="60px" prop="jiankang">
            <el-input
              type="number"
              v-model.number="form.jiankang"
              @input="handInput($event,0,8,'jiankang')">
              <template #append>范围0-8</template>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="16">
        <el-col :span="18">
          <el-form-item label="品种与规格">
            <el-checkbox-group disabled>
              <el-checkbox :label="5">品种不符订单：扣5分</el-checkbox>
            </el-checkbox-group>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="扣分"  label-width="60px" prop="pinzhong">
            <el-input
              v-model.number="form.pinzhong"
              type="number"
              @input="handInput($event,0,5,'pinzhong')">
              <template #append>范围0-5</template>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="16">
        <el-col :span="18">
          <el-form-item label="药残与合规性">
            <el-checkbox-group disabled>
              <el-checkbox :label="5">缺失药残检测报告：扣5分；</el-checkbox>
              <el-checkbox :label="30">瘦肉精、抗生素等超标：直接判定不合格</el-checkbox>
            </el-checkbox-group>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="扣分" label-width="60px" prop="yaocan">
            <el-input
              v-model.number="form.yaocan"
              type="number"
              @input="handInput($event,0,27,'yaocan')"
            >
              <template #append>范围0-27</template>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="16">
        <el-col :span="6">
          <el-form-item label="总得分" prop="shengzhu">
            <el-input v-model.number="form.shengzhu" disabled type="number"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-divider content-position="left">供应能力（总分30分）</el-divider>
      <el-row :gutter="16">
        <el-col :span="18">
          <el-form-item label="交货及时率">
            <el-checkbox-group disabled>
              <el-checkbox :label="5">出场无检疫合格证明：扣5分；</el-checkbox>
              <el-checkbox :label="3">宰前异常（咳嗽、腹泻等）：扣3分</el-checkbox>
            </el-checkbox-group>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="扣分"  label-width="60px" prop="jaohuo">
            <el-input v-model.number="form.jaohuo" type="number" @input="handInput($event,0,10,'jaohuo')">
              <template #append>范围0-10</template>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="16">
        <el-col :span="18">
          <el-form-item label="供应稳定性">
            <el-checkbox-group disabled>
              <el-checkbox :label="5">季度断供≧2次：扣3分</el-checkbox>
            </el-checkbox-group>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="扣分"  label-width="60px"  prop="gongyingWendaing">
            <el-input v-model.number="form.gongyingWendaing" type="number" @input="handInput($event,0,10,'gongyingWendaing')">
              <template #append>范围0-10</template>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="16">
        <el-col :span="18">
          <el-form-item label="运输管理">
            <el-checkbox-group disabled>
              <el-checkbox :label="5">运输工具消毒记录不全：扣2分；</el-checkbox>
              <el-checkbox :label="3">运输死亡率﹥3％：扣2分</el-checkbox>
            </el-checkbox-group>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="扣分" label-width="60px" prop="yunshu">
            <el-input v-model.number="form.yunshu" type="number" @input="handInput($event,0,10,'yunshu')">
              <template #append>范围0-10</template>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="16">
        <el-col  :span="6">
          <el-form-item label="总得分" prop="gongyingNengli">
            <el-input v-model.number="form.gongyingNengli" type="number" disabled>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-divider content-position="left">服务与配合度（总分20分）</el-divider>
      <el-row :gutter="16">
        <el-col :span="18">
          <el-form-item label="需求响应速度">
            <el-checkbox-group disabled>
              <el-checkbox :label="5">临时需求2小时未响应：扣3分</el-checkbox>
              <el-checkbox :label="3">问题反馈24小时未响应：扣5分</el-checkbox>
            </el-checkbox-group>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="扣分" label-width="60px" prop="xvqiu">
            <el-input v-model.number="form.xvqiu" type="number" @input="handInput($event,0,10,'xvqiu')">
              <template #append>范围0-10</template>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="16">
        <el-col :span="18">
          <el-form-item label="配合度">
            <el-checkbox-group disabled>
              <el-checkbox :label="5">拒绝配合检疫、追溯调查：扣10分</el-checkbox>
            </el-checkbox-group>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="扣分" label-width="60px" prop="peihe">
            <el-input v-model.number="form.peihe" type="number" @input="handInput($event,0,10,'peihe')">
              <template #append>范围0-10</template>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="16">
        <el-col :span="6">
          <el-form-item label="总得分" prop="fuwu">
            <el-input v-model.number="form.fuwu" disabled type="number"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-divider content-position="left">合规与安全（总分10分）</el-divider>
      <el-row :gutter="16">
        <el-col :span="18">
          <el-form-item label="资质完整性">
            <el-checkbox-group disabled>
              <el-checkbox :label="5">缺失养殖许可证、防疫条件合格证等：扣5分；</el-checkbox>
              <el-checkbox :label="3">证件过期未更新：扣3分；</el-checkbox>
            </el-checkbox-group>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="扣分" label-width="60px" prop="zizhi">
            <el-input v-model.number="form.zizhi" type="number" @input="handInput($event,0,10,'zizhi')">
              <template #append>范围0-5</template>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="16">
        <el-col :span="18">
          <el-form-item label="防疫管理">
            <el-checkbox-group disabled>
              <el-checkbox :label="5">未完成口蹄疫、猪瘟等免疫：扣5分；</el-checkbox>
              <el-checkbox :label="3">隐瞒疫情：直接判定不合格；</el-checkbox>
            </el-checkbox-group>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="扣分" label-width="60px" prop="fangyi">
            <el-input v-model.number="form.fangyi" type="number" @input="handInput($event,0,10,'fangyi')">
              <template #append>范围0-5</template>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="16">
        <el-col  :span="6">
          <el-form-item label="总得分" prop="hegui">
            <el-input v-model.number="form.hegui" disabled type="number"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-divider content-position="left">等级评定</el-divider>
      <el-row :gutter="16">
        <el-col :span="6">
          <el-form-item label="总分" prop="zongfen">
            <el-input v-model.number="form.zongfen" disabled type="number"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="等级" prop="dengji">
            <el-select v-model="form.dengji" disabled>
              <el-option v-for="item in djList" :key="item.id" :value="item.id" :label="item.name"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" >
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>
<script>
  import moment from "moment";
  import { required } from "../../../../utils/formRules";
  // import { addSupplierEvaluate, editSupplierEvaluate, getSupplierEvaluateLast } from "../../../../api/app/supplier";

  export default {
    data() {
      return {
        showModal: false,
        form: {
          id: null,
          supplierId: null,
          supplierName: null,
          pingjiazhouqi: null,
          evaDate: null,
          jiankang: 0,
          pinzhong: 0,
          yaocan: 0,
          jaohuo: 0,
          gongyingWendaing: 0,
          yunshu: 0,
          xvqiu: 0,
          peihe: 0,
          zizhi: 0,
          fangyi: 0,
          shengzhu: 40,
          gongyingNengli: 30,
          fuwu: 20,
          hegui: 10,
          zongfen: 100,
          dengji: 1,
        },
        rules: {
          pingjiazhouqi: [required('评价周期不能为空')],
          evaDate: [required('评价日期不能为空')],
          jiankang: [required('健康状况扣分不能为空')],
          pinzhong: [required('品种与规格扣分不能为空')],
          yaocan: [required('药残与合规性扣分不能为空')],
          jaohuo: [required('交货及时率扣分不能为空')],
          gongyingWendaing: [required('供应稳定性扣分不能为空')],
          yunshu: [required('运输管理扣分不能为空')],
          xvqiu: [required('需求响应扣分不能为空')],
          peihe: [required('配合度扣分不能为空')],
          zizhi: [required('资质扣分不能为空')],
          fangyi: [required('防疫管理扣分不能为空')],
        },
        djList: [
          {
            id: 1,
            name: '优秀（≧90分）'
          },
          {
            id: 2,
            name: '良好（80-89分）'
          },
          {
            id: 3,
            name: '合格（70-79分）'
          },
          {
            id: 4,
            name: '不合格（﹤70分）'
          },
        ],
        title: '',
        user: null
      }
    },
    watch: {
      "form.jiankang": {
        handler(newVal) {
          this.form.shengzhu = 40 - Number(newVal) - Number(this.form.pinzhong) - Number(this.form.yaocan)
        },
        deep: true
      },
      "form.pinzhong": {
        handler(newVal) {
          this.form.shengzhu = 40 - Number(newVal) - Number(this.form.jiankang) - Number(this.form.yaocan)
        },
        deep: true
      },
      "form.yaocan": {
        handler(newVal) {
          this.form.shengzhu = 40 - Number(newVal) - Number(this.form.jiankang) - Number(this.form.pinzhong)
        },
        deep: true
      },
      "form.jaohuo": {
        handler(newVal) {
          this.form.gongyingNengli = 30 - Number(newVal) - Number(this.form.gongyingWendaing) - Number(this.form.yunshu)
        },
        deep: true
      },
      "form.gongyingWendaing": {
        handler(newVal) {
          this.form.gongyingNengli = 30 - Number(newVal) - Number(this.form.jaohuo) - Number(this.form.yunshu)
        },
        deep: true
      },
      "form.yunshu": {
        handler(newVal) {
          this.form.gongyingNengli = 30 - Number(newVal) - Number(this.form.jaohuo) - Number(this.form.gongyingWendaing)
        },
        deep: true
      },
      "form.xvqiu": {
        handler(newVal) {
          this.form.fuwu = 20 - Number(newVal) - Number(this.form.peihe)
        },
        deep: true
      },
      "form.peihe": {
        handler(newVal) {
          this.form.fuwu = 20 - Number(newVal) - Number(this.form.xvqiu)
        },
        deep: true
      },
      "form.zizhi": {
        handler(newVal) {
          this.form.hegui = 10 - Number(newVal) - Number(this.form.fangyi)
        },
        deep: true
      },
      "form.fangyi": {
        handler(newVal) {
          this.form.hegui = 10 - Number(newVal) - Number(this.form.zizhi)
        },
        deep: true
      },
      "form.shengzhu": {
        handler(newVal) {
          this.form.zongfen =  Number(newVal) + Number(this.form.gongyingNengli) + Number(this.form.fuwu) + Number(this.form.hegui)
        },
        deep: true
      },
      "form.gongyingNengli": {
        handler(newVal) {
          this.form.zongfen =  Number(newVal) + Number(this.form.shengzhu) + Number(this.form.fuwu) + Number(this.form.hegui)
        },
        deep: true
      },
      "form.fuwu": {
        handler(newVal) {
          this.form.zongfen =  Number(newVal) + Number(this.form.shengzhu) + Number(this.form.gongyingNengli) + Number(this.form.hegui)
        },
        deep: true
      },
      "form.hegui": {
        handler(newVal) {
          this.form.zongfen =  Number(newVal) + Number(this.form.shengzhu) + Number(this.form.gongyingNengli) + Number(this.form.fuwu)
        },
        deep: true
      },
      "form.zongfen": {
        handler(newVal) {
          if(newVal < 70) {
            this.form.dengji = 4
          } else if( newVal >= 70 && newVal <= 79 ) {
            this.form.dengji = 3
          } else if( newVal >= 80 && newVal <=89) {
            this.form.dengji = 2
          } else if(newVal >= 90) {
            this.form.dengji = 1
          }
        },
        deep: true
      }
    },
    methods: {
      async openModal(user) {
        this.resetForms()
        this.form.supplierId = user.supplierId;
        this.form.supplierName = user.supplierName;
        if(user.edit) {
          this.title = '编辑评分'
          getSupplierEvaluateLast(user).then(res => {
            this.form = res.data[0]
          })
        } else {
          this.title = '新增评分'
        }
        this.showModal = true
      },
      resetForms() {
        this.form = {
          id: null,
          supplierId: null,
          supplierName: null,
          pingjiazhouqi: null,
          evaDate: moment().format('YYYY-MM-DD'),
          jiankang: 0,
          pinzhong: 0,
          yaocan: 0,
          jaohuo: 0,
          gongyingWendaing: 0,
          yunshu: 0,
          xvqiu: 0,
          peihe: 0,
          zizhi: 0,
          fangyi: 0,
          shengzhu: 40,
          gongyingNengli: 30,
          fuwu: 20,
          hegui: 10,
          zongfen: 100,
          dengji: 1,
        }
        this.resetForm("forms");
      },
      cancel() {
        this.resetForms()
        this.showModal = false
      },
      handInput(val, min, max, key) {
        if(val !== '' && !isNaN(val)) {
          let numValue = parseInt(val)
          if(numValue < min) {
            this.form[key] = min
          } else if(numValue > max) {
            this.form[key] = max
          }
        }
      },
      submitForm() {
        this.$refs["forms"].validate(valid => {
          if(valid){
            if(this.form.id !== null) {
              editSupplierEvaluate(this.form).then(res => {
                this.$modal.msgSuccess("修改评分成功");
                this.resetForms()
                this.showModal = false
                this.$emit('refresh')
              })
            } else {
              addSupplierEvaluate(this.form).then(res => {
                this.$modal.msgSuccess("新增评分成功");
                this.resetForms()
                this.showModal = false
                this.$emit('refresh')
              })
            }
          }
        })
      },
    }
  }
</script>
<style scoped lang="scss">

</style>
