<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="600px"
    @close="close"
  >
    <el-form ref="form" label-width="80px" :model="form">
      <el-form-item
        :label="__('审核状态')"
        prop="purchase_order_approval_state"
      >
        <el-select v-model="form.purchase_order_approval_state">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item
        v-if="form.purchase_order_approval_state == 2"
        prop="purchase_verify_reason"
        style="margin-left: -80px; margin-top: 30px"
      >
        <el-input
          v-model="form.purchase_verify_reason"
          :autosize="{ minRows: 20, maxRows: 100 }"
          :placeholder="__('原因')"
          type="textarea"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">{{ __('取消') }}</el-button>
      <el-button type="primary" @click="save">{{ __('确定') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'

  import { doEdit } from '@/api/purchase/order/base'

  export default {
    name: 'PurchaseOrderBaseEdit',
    data() {
      return {
        options: [],
        value: '',
        form: {},
        rules: {},
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {},
    mounted() {
      this.form = {
        unit_name: '',
      }
      ;(this.options = [
        {
          value: 0,
          label: this.__('未审核'),
        },
        {
          value: 1,
          label: this.__('审核通过'),
        },
        {
          value: 2,
          label: this.__('审核失败'),
        },
      ]),
        (this.rules = {
          purchase_order_id: [
            { required: true, trigger: 'blur', message: this.__('订单编号') },
          ],
          purchase_order_title: [
            { required: true, trigger: 'blur', message: this.__('货品名称') },
          ],
          purchase_order_time: [
            { required: true, trigger: 'blur', message: this.__('下单时间') },
          ],
          purchase_order_amount: [
            { required: true, trigger: 'blur', message: this.__('税前费用') },
          ],
          purchase_order_state: [
            { required: true, trigger: 'blur', message: this.__('订单状态') },
          ],
          purchase_order_approval_state: [
            {
              required: true,
              trigger: 'blur',
              message: this.__('订单审核状态'),
            },
          ],
          purchase_order_files: [
            { required: true, trigger: 'blur', message: this.__('附件') },
          ],
          purchase_order_description: [
            { required: true, trigger: 'blur', message: this.__('货品描述') },
          ],
          category_id: [
            { required: true, trigger: 'blur', message: this.__('货品分类') },
          ],
          purchase_order_quantity: [
            { required: true, trigger: 'blur', message: this.__('采购数量') },
          ],
          purchase_order_unit: [
            { required: true, trigger: 'blur', message: this.__('数量单位') },
          ],
          buyer_user_id: [
            { required: true, trigger: 'blur', message: this.__('买家编号') },
          ],
          buyer_user_name: [
            { required: true, trigger: 'blur', message: this.__('买家姓名') },
          ],
          supply_user_id: [
            { required: true, trigger: 'blur', message: this.__('供应商编号') },
          ],
          supply_user_name: [
            { required: true, trigger: 'blur', message: this.__('供应商名称') },
          ],
          purchase_province: [
            {
              required: true,
              trigger: 'blur',
              message: this.__('收货地址省份'),
            },
          ],
          purchase_city: [
            { required: true, trigger: 'blur', message: this.__('市') },
          ],
          purchase_county: [
            { required: true, trigger: 'blur', message: this.__('县区') },
          ],
          purchase_province_id: [
            { required: true, trigger: 'blur', message: this.__('省编号') },
          ],
          purchase_city_id: [
            { required: true, trigger: 'blur', message: this.__('市编号') },
          ],
          purchase_county_id: [
            { required: true, trigger: 'blur', message: this.__('县') },
          ],
          origin_place_id: [
            { required: true, trigger: 'blur', message: this.__('期望产地') },
          ],
          purchase_deadline: [
            { required: true, trigger: 'blur', message: this.__('截止时间') },
          ],
          purchase_payment_time: [
            { required: true, trigger: 'blur', message: this.__('付款时间') },
          ],
          purchase_is_check: [
            { required: true, trigger: 'blur', message: this.__('验收状态') },
          ],
          purchase_is_trusteeship: [
            { required: true, trigger: 'blur', message: this.__('托管状态') },
          ],
          purchase_task_state: [
            { required: true, trigger: 'blur', message: this.__('需求状态') },
          ],
          purchase_mode_id: [
            { required: true, trigger: 'blur', message: this.__('是否担保') },
          ],
          purchase_tender_discount: [
            { required: true, trigger: 'blur', message: this.__('投标折扣') },
          ],
          purchase_tender_endtime: [
            { required: true, trigger: 'blur', message: this.__('截止时间') },
          ],
          purchase_complete_time: [
            {
              required: true,
              trigger: 'blur',
              message: this.__('订单完成时间'),
            },
          ],
          purchase_buyer_evaluation_status: [
            { required: true, trigger: 'blur', message: this.__('对买家评价') },
          ],
          purchase_supply_evaluation_status: [
            { required: true, trigger: 'blur', message: this.__('对店铺评价') },
          ],
          purchase_commission_fee: [
            { required: true, trigger: 'blur', message: this.__('平台佣金') },
          ],
          purchase_product_spec: [
            { required: true, trigger: 'blur', message: this.__('规格') },
          ],
          application_user_ids: [
            { required: true, trigger: 'blur', message: this.__('投标用户') },
          ],

          purchase_product_images: [
            { required: true, trigger: 'blur', message: this.__('货品图片') },
          ],
        })

      this.$forceUpdate()
    },
    methods: {
      __,

      showEdit(row) {
        this.form = {
          purchase_order_id: row.purchase_order_id,
          purchase_order_approval_state: row.purchase_order_approval_state,
          purchase_verify_reason: row.purchase_verify_reason,
        }

        this.dialogFormVisible = true
      },
      close() {
        this.form = this.$options.data().form
        this.$refs['form'].resetFields()
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { msg, status } = await doEdit(this.form)
            if (200 == status) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
    },
  }
</script>
