<template>
  <view class="content">
    <view class="card">
      <u-cell-group :border="false">
        <u-cell>
          <view slot="title" class="title">申辩人：</view>
          <view slot="label" class="mt-10">
            <u--image :showLoading="true" :src="$store.state.filePath + form.inspectedSiteuserSignImg"
                      width="100%" height="80px" mode="scaleToFill"
                      @click="previewImage($store.state.filePath + form.inspectedSiteuserSignImg)"></u--image>
          </view>
        </u-cell>
        <u-cell>
          <view slot="title" class="title">申辩时间：</view>
          <view slot="value">{{ form.inspectedSiteuserSignTime }}</view>
        </u-cell>
        <u-cell>
          <view slot="title" class="title">申辩说明：</view>
          <view slot="value">{{ form.inspectedExplain }}</view>
        </u-cell>
        <u-cell v-if="form.inspectedExplainFilename">
          <view slot="title" class="title">申辩附件：</view>
          <view slot="label" class="mt-10 flex-between">
            <text class="u-line-1">{{ form.inspectedExplainFilename }}</text>
            <view class="ml-10">
              <u-button type="primary" text="下载" size="mini" @click="downloadAppendix"></u-button>
            </view>
          </view>
        </u-cell>
        <u-cell>
          <view slot="title" class="title required">申辩是否成立：</view>
          <view slot="value">
            <u-radio-group v-model="form.inspectionStatus" placement="row">
              <u-radio label="是" name="-2"></u-radio>
              <u-radio label="否" name="3" class="ml-10"></u-radio>
            </u-radio-group>
          </view>
        </u-cell>
        <template v-if="form.inspectionStatus === '-2'">
          <u-cell>
            <view slot="title" class="title flex-between">
              确认人签字：
              <view>
                <u-button type="primary" text="手写添加" size="mini"
                          @click="signShow = true"></u-button>
                <sign v-if="signShow" :signShow.sync="signShow" @confirm="signConfirm"></sign>
              </view>
            </view>
            <view slot="label" class="mt-10">
            </view>
          </u-cell>
          <u-cell v-if="form.inspectedSiteUserSignImg">
            <view slot="label" class="mt-10 flex-1" v-show="form.inspectedSiteUserSignImg">
              <u--image v-show="form.inspectedSiteUserSignImg" :showLoading="true" :src="form.inspectedSiteUserSignImg"
                        width="100%" height="80px" mode="scaleToFill"
                        @click="previewImage(form.inspectedSiteUserSignImg)"></u--image>

            </view>
          </u-cell>
        </template>
        <template v-if="form.inspectionStatus === '3'">
          <u-cell>
            <view slot="title" class="title required">不成立理由：</view>
            <view slot="label" class="mt-10">
              <u--textarea v-model="form.inspectedExplainRefuse" placeholder="请输入不成立理由"></u--textarea>
            </view>
          </u-cell>
          <u-cell>
            <view slot="title" class="title flex-between">
              确认人签字：
              <view>
                <u-button type="primary" text="手写添加" size="mini"
                          @click="signShow = true"></u-button>
                <sign v-if="signShow" :signShow.sync="signShow" @confirm="signConfirm"></sign>
              </view>
            </view>
            <view slot="label" class="mt-10">
            </view>
          </u-cell>
          <view>
            <u--image v-show="form.inspectedSiteUserSignImg" :showLoading="true" :src="form.inspectedSiteUserSignImg"
                      width="100%" height="80px" mode="scaleToFill"
                      @click="previewImage(form.inspectedSiteUserSignImg)"></u--image>
          </view>
        </template>
        <u-cell :border="false">
          <view slot="label" class="flex-between">
            <u-button type="primary" text="提交" @click="$u.debounce(fnSubmit, 1000,true)"></u-button>
          </view>
        </u-cell>
      </u-cell-group>
    </view>
  </view>
</template>

<script>
import sign from "../../../components/sign/sign";
import { setSafetyenvironmentalExplain } from "@/api";

export default {
  components: {
    sign,
  },
  data() {
    return {
      inspectionId: '',
      form: {},
      signShow: false
    }
  },
  onLoad(event) {
    this.inspectionId = event.inspectionId;
    this.form.inspectedSiteuserSignTime = event.inspectedSiteuserSignTime;
    this.form.inspectedExplain = event.inspectedExplain;
    this.form.inspectedExplainFilename = event.inspectedExplainFilename;
    this.form.inspectedExplainFilepath = event.inspectedExplainFilepath;
    this.form.inspectionSource = event.inspectionSource;
    this.form.inspectedSiteuserSignImg = event.inspectedSiteuserSignImg;
  },
  computed: {
    userInfo() {
      return this.$store.getters.getUserInfo
    }
  },
  methods: {
    previewImage(current) {
      uni.previewImage({
        urls: [current],
        current
      });
    },
    downloadAppendix() {
      uni.downloadFile({
        url: this.$store.state.filePath + this.form.inspectedExplainFilepath,
        success: (res) => {
          uni.openDocument({
            filePath: res.tempFilePath,
          });
        },
      });
    },
    signConfirm({path}) {
      this.form.inspectedSiteUserSignImg = path
    },
    async fnSubmit() {
      if (!this.form.inspectionStatus) {
        uni.showToast({
          title: '请选择申辩是否成立',
          icon: "none"
        })
        return;
      }
      if (this.form.inspectionStatus === '3') {
        if (!this.form.inspectedExplainRefuse) {
          uni.showToast({
            title: '请填写不成立理由',
            icon: "none"
          })
          return;
        }
        if (!this.form.inspectedSiteUserSignImg) {
          uni.showToast({
            title: '请签字',
            icon: "none"
          })
          return;
        }
        this.form.loginUserId = this.userInfo.USER_ID;
        this.form.loginUserName = this.userInfo.USERNAME;
        await setSafetyenvironmentalExplain({
          filePath: this.form.inspectedSiteUserSignImg,
          name: 'FFILE',
          formData: this.form
        })
        uni.showToast({
          title: '申辩成功',
        })
        uni.$u.route({
          type: 'navigateBack'
        })
      } else if (this.form.inspectionStatus === '-2') {
        uni.$u.route({
          url: '/pages/safety-environmental-inspection/initiate/detail',
          params: {
            INSPECTION_ID: this.INSPECTION_ID,
            type: 'edit',
            vector: 'list'
          }
        })
      }
    }
  }
}
</script>

<style scoped>
.title {
  min-width: 150rpx;
}

.flex-1 {
  flex: 1;
}
</style>
